Remove popToRoot

This commit is contained in:
Thomas Ricouard 2024-07-09 15:53:59 +02:00
parent 3ec5c8c676
commit 7c2e8d6e80
10 changed files with 18 additions and 93 deletions

View file

@ -133,7 +133,7 @@ extension View {
StatusEditHistoryView(statusId: status) StatusEditHistoryView(statusId: status)
.withEnvironments() .withEnvironments()
case .settings: case .settings:
SettingsTabs(popToRootTab: .constant(.settings), isModal: true) SettingsTabs(isModal: true)
.withEnvironments() .withEnvironments()
.preferredColorScheme(Theme.shared.selectedScheme == .dark ? .dark : .light) .preferredColorScheme(Theme.shared.selectedScheme == .dark ? .dark : .light)
case .accountPushNotficationsSettings: case .accountPushNotficationsSettings:

View file

@ -24,7 +24,6 @@ struct AppView: View {
@Binding var selectedTab: AppTab @Binding var selectedTab: AppTab
@Binding var appRouterPath: RouterPath @Binding var appRouterPath: RouterPath
@State var popToRootTab: AppTab = .other
@State var iosTabs = iOSTabs.shared @State var iosTabs = iOSTabs.shared
@State var sidebarTabs = SidebarTabs.shared @State var sidebarTabs = SidebarTabs.shared
@ -64,21 +63,14 @@ struct AppView: View {
#endif #endif
return return
} }
if newTab == selectedTab {
/// Stupid hack to trigger onChange binding in tab views.
popToRootTab = .other
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
popToRootTab = selectedTab
}
}
HapticManager.shared.fireHaptic(.tabSelection) HapticManager.shared.fireHaptic(.tabSelection)
SoundEffectManager.shared.playSound(.tabSelection) SoundEffectManager.shared.playSound(.tabSelection)
selectedTab = newTab selectedTab = newTab
})) { })) {
ForEach(availableTabs) { tab in ForEach(availableTabs) { tab in
tab.makeContentView(selectedTab: $selectedTab, popToRootTab: $popToRootTab) tab.makeContentView(selectedTab: $selectedTab)
.tabItem { .tabItem {
if userPreferences.showiPhoneTabLabel { if userPreferences.showiPhoneTabLabel {
tab.label tab.label
@ -108,14 +100,13 @@ struct AppView: View {
#if !os(visionOS) #if !os(visionOS)
var sidebarView: some View { var sidebarView: some View {
SideBarView(selectedTab: $selectedTab, SideBarView(selectedTab: $selectedTab,
popToRootTab: $popToRootTab,
tabs: availableTabs) tabs: availableTabs)
{ {
HStack(spacing: 0) { HStack(spacing: 0) {
TabView(selection: $selectedTab) { TabView(selection: $selectedTab) {
ForEach(availableTabs) { tab in ForEach(availableTabs) { tab in
Tab(value: tab) { Tab(value: tab) {
tab.makeContentView(selectedTab: $selectedTab, popToRootTab: $popToRootTab) tab.makeContentView(selectedTab: $selectedTab)
} label: { } label: {
tab.label tab.label
} }
@ -142,8 +133,8 @@ struct AppView: View {
#endif #endif
var notificationsSecondaryColumn: some View { var notificationsSecondaryColumn: some View {
NotificationsTab(selectedTab: .constant(.notifications), NotificationsTab(selectedTab: .constant(.notifications)
popToRootTab: $popToRootTab, lockedType: nil) , lockedType: nil)
.environment(\.isSecondaryColumn, true) .environment(\.isSecondaryColumn, true)
.frame(maxWidth: .secondaryColumnWidth) .frame(maxWidth: .secondaryColumnWidth)
.id(appAccountsManager.currentAccount.id) .id(appAccountsManager.currentAccount.id)

View file

@ -19,7 +19,6 @@ struct SideBarView<Content: View>: View {
@Environment(RouterPath.self) private var routerPath @Environment(RouterPath.self) private var routerPath
@Binding var selectedTab: AppTab @Binding var selectedTab: AppTab
@Binding var popToRootTab: AppTab
var tabs: [AppTab] var tabs: [AppTab]
@ViewBuilder var content: () -> Content @ViewBuilder var content: () -> Content
@ -143,13 +142,6 @@ struct SideBarView<Content: View>: View {
Button { Button {
// ensure keyboard is always dismissed when selecting a tab // ensure keyboard is always dismissed when selecting a tab
hideKeyboard() hideKeyboard()
if tab == selectedTab {
popToRootTab = .other
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
popToRootTab = tab
}
}
selectedTab = tab selectedTab = tab
SoundEffectManager.shared.playSound(.tabSelection) SoundEffectManager.shared.playSound(.tabSelection)
if tab == .notifications { if tab == .notifications {

View file

@ -14,7 +14,6 @@ struct ExploreTab: View {
@Environment(Client.self) private var client @Environment(Client.self) private var client
@State private var routerPath = RouterPath() @State private var routerPath = RouterPath()
@State private var scrollToTopSignal: Int = 0 @State private var scrollToTopSignal: Int = 0
@Binding var popToRootTab: AppTab
var body: some View { var body: some View {
NavigationStack(path: $routerPath.path) { NavigationStack(path: $routerPath.path) {
@ -28,15 +27,6 @@ struct ExploreTab: View {
} }
.withSafariRouter() .withSafariRouter()
.environment(routerPath) .environment(routerPath)
.onChange(of: $popToRootTab.wrappedValue) { _, newValue in
if newValue == .explore {
if routerPath.path.isEmpty {
scrollToTopSignal += 1
} else {
routerPath.path = []
}
}
}
.onChange(of: client.id) { .onChange(of: client.id) {
routerPath.path = [] routerPath.path = []
} }

View file

@ -16,7 +16,6 @@ struct MessagesTab: View {
@Environment(AppAccountsManager.self) private var appAccount @Environment(AppAccountsManager.self) private var appAccount
@State private var routerPath = RouterPath() @State private var routerPath = RouterPath()
@State private var scrollToTopSignal: Int = 0 @State private var scrollToTopSignal: Int = 0
@Binding var popToRootTab: AppTab
var body: some View { var body: some View {
NavigationStack(path: $routerPath.path) { NavigationStack(path: $routerPath.path) {
@ -29,15 +28,6 @@ struct MessagesTab: View {
.toolbarBackground(theme.primaryBackgroundColor.opacity(0.30), for: .navigationBar) .toolbarBackground(theme.primaryBackgroundColor.opacity(0.30), for: .navigationBar)
.id(client.id) .id(client.id)
} }
.onChange(of: $popToRootTab.wrappedValue) { _, newValue in
if newValue == .messages {
if routerPath.path.isEmpty {
scrollToTopSignal += 1
} else {
routerPath.path = []
}
}
}
.onChange(of: client.id) { .onChange(of: client.id) {
routerPath.path = [] routerPath.path = []
} }

View file

@ -23,7 +23,6 @@ struct NotificationsTab: View {
@State private var scrollToTopSignal: Int = 0 @State private var scrollToTopSignal: Int = 0
@Binding var selectedTab: AppTab @Binding var selectedTab: AppTab
@Binding var popToRootTab: AppTab
let lockedType: Models.Notification.NotificationType? let lockedType: Models.Notification.NotificationType?
@ -51,15 +50,6 @@ struct NotificationsTab: View {
} }
.withSafariRouter() .withSafariRouter()
.environment(routerPath) .environment(routerPath)
.onChange(of: $popToRootTab.wrappedValue) { _, newValue in
if newValue == .notifications {
if routerPath.path.isEmpty {
scrollToTopSignal += 1
} else {
routerPath.path = []
}
}
}
.onChange(of: selectedTab) { _, _ in .onChange(of: selectedTab) { _, _ in
clearNotifications() clearNotifications()
} }

View file

@ -15,7 +15,6 @@ struct ProfileTab: View {
@Environment(CurrentAccount.self) private var currentAccount @Environment(CurrentAccount.self) private var currentAccount
@State private var routerPath = RouterPath() @State private var routerPath = RouterPath()
@State private var scrollToTopSignal: Int = 0 @State private var scrollToTopSignal: Int = 0
@Binding var popToRootTab: AppTab
var body: some View { var body: some View {
NavigationStack(path: $routerPath.path) { NavigationStack(path: $routerPath.path) {
@ -31,15 +30,6 @@ struct ProfileTab: View {
.allowsHitTesting(false) .allowsHitTesting(false)
} }
} }
.onChange(of: $popToRootTab.wrappedValue) { _, newValue in
if newValue == .profile {
if routerPath.path.isEmpty {
scrollToTopSignal += 1
} else {
routerPath.path = []
}
}
}
.onChange(of: client.id) { .onChange(of: client.id) {
routerPath.path = [] routerPath.path = []
} }

View file

@ -28,8 +28,6 @@ struct SettingsTabs: View {
@State private var cachedRemoved = false @State private var cachedRemoved = false
@State private var timelineCache = TimelineCache() @State private var timelineCache = TimelineCache()
@Binding var popToRootTab: AppTab
let isModal: Bool let isModal: Bool
@State private var startingPoint: SettingsStartingPoint? = nil @State private var startingPoint: SettingsStartingPoint? = nil
@ -103,11 +101,6 @@ struct SettingsTabs: View {
} }
.withSafariRouter() .withSafariRouter()
.environment(routerPath) .environment(routerPath)
.onChange(of: $popToRootTab.wrappedValue) { _, newValue in
if newValue == .notifications {
routerPath.path = []
}
}
} }
private var accountsSection: some View { private var accountsSection: some View {

View file

@ -31,28 +31,28 @@ enum AppTab: Int, Identifiable, Hashable, CaseIterable, Codable {
} }
@ViewBuilder @ViewBuilder
func makeContentView(selectedTab: Binding<AppTab>, popToRootTab: Binding<AppTab>) -> some View { func makeContentView(selectedTab: Binding<AppTab>) -> some View {
switch self { switch self {
case .timeline: case .timeline:
TimelineTab(popToRootTab: popToRootTab) TimelineTab()
case .trending: case .trending:
TimelineTab(popToRootTab: popToRootTab, timeline: .trending) TimelineTab(timeline: .trending)
case .local: case .local:
TimelineTab(popToRootTab: popToRootTab, timeline: .local) TimelineTab(timeline: .local)
case .federated: case .federated:
TimelineTab(popToRootTab: popToRootTab, timeline: .federated) TimelineTab(timeline: .federated)
case .notifications: case .notifications:
NotificationsTab(selectedTab: selectedTab, popToRootTab: popToRootTab, lockedType: nil) NotificationsTab(selectedTab: selectedTab, lockedType: nil)
case .mentions: case .mentions:
NotificationsTab(selectedTab: selectedTab, popToRootTab: popToRootTab, lockedType: .mention) NotificationsTab(selectedTab: selectedTab, lockedType: .mention)
case .explore: case .explore:
ExploreTab(popToRootTab: popToRootTab) ExploreTab()
case .messages: case .messages:
MessagesTab(popToRootTab: popToRootTab) MessagesTab()
case .settings: case .settings:
SettingsTabs(popToRootTab: popToRootTab, isModal: false) SettingsTabs(isModal: false)
case .profile: case .profile:
ProfileTab(popToRootTab: popToRootTab) ProfileTab()
case .bookmarks: case .bookmarks:
NavigationTab { NavigationTab {
AccountStatusesListView(mode: .bookmarks) AccountStatusesListView(mode: .bookmarks)

View file

@ -18,7 +18,6 @@ struct TimelineTab: View {
@Environment(UserPreferences.self) private var preferences @Environment(UserPreferences.self) private var preferences
@Environment(Client.self) private var client @Environment(Client.self) private var client
@State private var routerPath = RouterPath() @State private var routerPath = RouterPath()
@Binding var popToRootTab: AppTab
@State private var didAppear: Bool = false @State private var didAppear: Bool = false
@State private var timeline: TimelineFilter = .home @State private var timeline: TimelineFilter = .home
@ -33,9 +32,8 @@ struct TimelineTab: View {
private let canFilterTimeline: Bool private let canFilterTimeline: Bool
init(popToRootTab: Binding<AppTab>, timeline: TimelineFilter? = nil) { init(timeline: TimelineFilter? = nil) {
canFilterTimeline = timeline == nil canFilterTimeline = timeline == nil
_popToRootTab = popToRootTab
_timeline = .init(initialValue: timeline ?? .home) _timeline = .init(initialValue: timeline ?? .home)
} }
@ -77,15 +75,6 @@ struct TimelineTab: View {
.onChange(of: currentAccount.account?.id) { .onChange(of: currentAccount.account?.id) {
resetTimelineFilter() resetTimelineFilter()
} }
.onChange(of: $popToRootTab.wrappedValue) { _, newValue in
if newValue == .timeline {
if routerPath.path.isEmpty {
scrollToTopSignal += 1
} else {
routerPath.path = []
}
}
}
.onChange(of: client.id) { .onChange(of: client.id) {
routerPath.path = [] routerPath.path = []
} }