diff --git a/IceCubesApp/App/IceCubesApp.swift b/IceCubesApp/App/IceCubesApp.swift index 31c4e403..a355f3dc 100644 --- a/IceCubesApp/App/IceCubesApp.swift +++ b/IceCubesApp/App/IceCubesApp.swift @@ -6,37 +6,59 @@ import Env @main struct IceCubesApp: App { + enum Tab: Int { + case timeline, notifications, explore, account, settings, other + } + public static let defaultServer = "mastodon.social" @StateObject private var appAccountsManager = AppAccountsManager() @StateObject private var currentAccount = CurrentAccount() @StateObject private var quickLook = QuickLook() + @State private var selectedTab: Tab = .timeline + @State private var popToRootTab: Tab = .other var body: some Scene { WindowGroup { - TabView { - TimelineTab() + TabView(selection: .init(get: { + selectedTab + }, set: { newTab in + if newTab == selectedTab { + /// Stupid hack to trigger onChange binding in tab views. + popToRootTab = .other + DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { + popToRootTab = selectedTab + } + } + selectedTab = newTab + })) { + TimelineTab(popToRootTab: $popToRootTab) .tabItem { Label("Timeline", systemImage: "rectangle.on.rectangle") } + .tag(Tab.timeline) if appAccountsManager.currentClient.isAuth { - NotificationsTab() + NotificationsTab(popToRootTab: $popToRootTab) .tabItem { Label("Notifications", systemImage: "bell") } - ExploreTab() + .tag(Tab.notifications) + ExploreTab(popToRootTab: $popToRootTab) .tabItem { Label("Explore", systemImage: "magnifyingglass") } - AccountTab() + .tag(Tab.explore) + AccountTab(popToRootTab: $popToRootTab) .tabItem { Label("Profile", systemImage: "person.circle") } + .tag(Tab.account) } SettingsTabs() .tabItem { Label("Settings", systemImage: "gear") } + .tag(Tab.settings) } .tint(.brand) .onChange(of: appAccountsManager.currentClient) { newClient in diff --git a/IceCubesApp/App/Tabs/AccountTab.swift b/IceCubesApp/App/Tabs/AccountTab.swift index 7f3a7452..018e23c6 100644 --- a/IceCubesApp/App/Tabs/AccountTab.swift +++ b/IceCubesApp/App/Tabs/AccountTab.swift @@ -8,6 +8,7 @@ import Shimmer struct AccountTab: View { @EnvironmentObject private var currentAccount: CurrentAccount @StateObject private var routeurPath = RouterPath() + @Binding var popToRootTab: IceCubesApp.Tab var body: some View { NavigationStack(path: $routeurPath.path) { @@ -22,5 +23,10 @@ struct AccountTab: View { } } .environmentObject(routeurPath) + .onChange(of: $popToRootTab.wrappedValue) { popToRootTab in + if popToRootTab == .account { + routeurPath.path = [] + } + } } } diff --git a/IceCubesApp/App/Tabs/ExploreTab.swift b/IceCubesApp/App/Tabs/ExploreTab.swift index 70604d9a..8fd7e56b 100644 --- a/IceCubesApp/App/Tabs/ExploreTab.swift +++ b/IceCubesApp/App/Tabs/ExploreTab.swift @@ -6,6 +6,7 @@ import Explore struct ExploreTab: View { @StateObject private var routeurPath = RouterPath() + @Binding var popToRootTab: IceCubesApp.Tab var body: some View { NavigationStack(path: $routeurPath.path) { @@ -14,5 +15,10 @@ struct ExploreTab: View { .withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet) } .environmentObject(routeurPath) + .onChange(of: $popToRootTab.wrappedValue) { popToRootTab in + if popToRootTab == .explore { + routeurPath.path = [] + } + } } } diff --git a/IceCubesApp/App/Tabs/NotificationTab.swift b/IceCubesApp/App/Tabs/NotificationTab.swift index 36a56e73..52b089d2 100644 --- a/IceCubesApp/App/Tabs/NotificationTab.swift +++ b/IceCubesApp/App/Tabs/NotificationTab.swift @@ -6,6 +6,7 @@ import Notifications struct NotificationsTab: View { @StateObject private var routeurPath = RouterPath() + @Binding var popToRootTab: IceCubesApp.Tab var body: some View { NavigationStack(path: $routeurPath.path) { @@ -14,5 +15,10 @@ struct NotificationsTab: View { .withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet) } .environmentObject(routeurPath) + .onChange(of: $popToRootTab.wrappedValue) { popToRootTab in + if popToRootTab == .notifications { + routeurPath.path = [] + } + } } } diff --git a/IceCubesApp/App/Tabs/TimelineTab.swift b/IceCubesApp/App/Tabs/TimelineTab.swift index e1e62ccb..a518da21 100644 --- a/IceCubesApp/App/Tabs/TimelineTab.swift +++ b/IceCubesApp/App/Tabs/TimelineTab.swift @@ -2,10 +2,12 @@ import SwiftUI import Timeline import Env import Network +import Combine struct TimelineTab: View { @EnvironmentObject private var client: Client @StateObject private var routeurPath = RouterPath() + @Binding var popToRootTab: IceCubesApp.Tab var body: some View { NavigationStack(path: $routeurPath.path) { @@ -25,5 +27,10 @@ struct TimelineTab: View { } } .environmentObject(routeurPath) + .onChange(of: $popToRootTab.wrappedValue) { popToRootTab in + if popToRootTab == .timeline { + routeurPath.path = [] + } + } } }