pop to root on tabbar tap

This commit is contained in:
Thomas Ricouard 2022-12-24 11:50:05 +01:00
parent 2c39af280e
commit bbab8981f3
5 changed files with 52 additions and 5 deletions

View file

@ -6,37 +6,59 @@ import Env
@main @main
struct IceCubesApp: App { struct IceCubesApp: App {
enum Tab: Int {
case timeline, notifications, explore, account, settings, other
}
public static let defaultServer = "mastodon.social" public static let defaultServer = "mastodon.social"
@StateObject private var appAccountsManager = AppAccountsManager() @StateObject private var appAccountsManager = AppAccountsManager()
@StateObject private var currentAccount = CurrentAccount() @StateObject private var currentAccount = CurrentAccount()
@StateObject private var quickLook = QuickLook() @StateObject private var quickLook = QuickLook()
@State private var selectedTab: Tab = .timeline
@State private var popToRootTab: Tab = .other
var body: some Scene { var body: some Scene {
WindowGroup { WindowGroup {
TabView { TabView(selection: .init(get: {
TimelineTab() 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 { .tabItem {
Label("Timeline", systemImage: "rectangle.on.rectangle") Label("Timeline", systemImage: "rectangle.on.rectangle")
} }
.tag(Tab.timeline)
if appAccountsManager.currentClient.isAuth { if appAccountsManager.currentClient.isAuth {
NotificationsTab() NotificationsTab(popToRootTab: $popToRootTab)
.tabItem { .tabItem {
Label("Notifications", systemImage: "bell") Label("Notifications", systemImage: "bell")
} }
ExploreTab() .tag(Tab.notifications)
ExploreTab(popToRootTab: $popToRootTab)
.tabItem { .tabItem {
Label("Explore", systemImage: "magnifyingglass") Label("Explore", systemImage: "magnifyingglass")
} }
AccountTab() .tag(Tab.explore)
AccountTab(popToRootTab: $popToRootTab)
.tabItem { .tabItem {
Label("Profile", systemImage: "person.circle") Label("Profile", systemImage: "person.circle")
} }
.tag(Tab.account)
} }
SettingsTabs() SettingsTabs()
.tabItem { .tabItem {
Label("Settings", systemImage: "gear") Label("Settings", systemImage: "gear")
} }
.tag(Tab.settings)
} }
.tint(.brand) .tint(.brand)
.onChange(of: appAccountsManager.currentClient) { newClient in .onChange(of: appAccountsManager.currentClient) { newClient in

View file

@ -8,6 +8,7 @@ import Shimmer
struct AccountTab: View { struct AccountTab: View {
@EnvironmentObject private var currentAccount: CurrentAccount @EnvironmentObject private var currentAccount: CurrentAccount
@StateObject private var routeurPath = RouterPath() @StateObject private var routeurPath = RouterPath()
@Binding var popToRootTab: IceCubesApp.Tab
var body: some View { var body: some View {
NavigationStack(path: $routeurPath.path) { NavigationStack(path: $routeurPath.path) {
@ -22,5 +23,10 @@ struct AccountTab: View {
} }
} }
.environmentObject(routeurPath) .environmentObject(routeurPath)
.onChange(of: $popToRootTab.wrappedValue) { popToRootTab in
if popToRootTab == .account {
routeurPath.path = []
}
}
} }
} }

View file

@ -6,6 +6,7 @@ import Explore
struct ExploreTab: View { struct ExploreTab: View {
@StateObject private var routeurPath = RouterPath() @StateObject private var routeurPath = RouterPath()
@Binding var popToRootTab: IceCubesApp.Tab
var body: some View { var body: some View {
NavigationStack(path: $routeurPath.path) { NavigationStack(path: $routeurPath.path) {
@ -14,5 +15,10 @@ struct ExploreTab: View {
.withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet) .withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet)
} }
.environmentObject(routeurPath) .environmentObject(routeurPath)
.onChange(of: $popToRootTab.wrappedValue) { popToRootTab in
if popToRootTab == .explore {
routeurPath.path = []
}
}
} }
} }

View file

@ -6,6 +6,7 @@ import Notifications
struct NotificationsTab: View { struct NotificationsTab: View {
@StateObject private var routeurPath = RouterPath() @StateObject private var routeurPath = RouterPath()
@Binding var popToRootTab: IceCubesApp.Tab
var body: some View { var body: some View {
NavigationStack(path: $routeurPath.path) { NavigationStack(path: $routeurPath.path) {
@ -14,5 +15,10 @@ struct NotificationsTab: View {
.withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet) .withSheetDestinations(sheetDestinations: $routeurPath.presentedSheet)
} }
.environmentObject(routeurPath) .environmentObject(routeurPath)
.onChange(of: $popToRootTab.wrappedValue) { popToRootTab in
if popToRootTab == .notifications {
routeurPath.path = []
}
}
} }
} }

View file

@ -2,10 +2,12 @@ import SwiftUI
import Timeline import Timeline
import Env import Env
import Network import Network
import Combine
struct TimelineTab: View { struct TimelineTab: View {
@EnvironmentObject private var client: Client @EnvironmentObject private var client: Client
@StateObject private var routeurPath = RouterPath() @StateObject private var routeurPath = RouterPath()
@Binding var popToRootTab: IceCubesApp.Tab
var body: some View { var body: some View {
NavigationStack(path: $routeurPath.path) { NavigationStack(path: $routeurPath.path) {
@ -25,5 +27,10 @@ struct TimelineTab: View {
} }
} }
.environmentObject(routeurPath) .environmentObject(routeurPath)
.onChange(of: $popToRootTab.wrappedValue) { popToRootTab in
if popToRootTab == .timeline {
routeurPath.path = []
}
}
} }
} }