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
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

View File

@ -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 = []
}
}
}
}

View File

@ -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 = []
}
}
}
}

View File

@ -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 = []
}
}
}
}

View File

@ -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 = []
}
}
}
}