diff --git a/iosApp/Source/App/AppState.swift b/iosApp/Source/App/AppState.swift index 0155351f..3794b988 100644 --- a/iosApp/Source/App/AppState.swift +++ b/iosApp/Source/App/AppState.swift @@ -21,6 +21,8 @@ class AppState: ObservableObject { @Published var path = NavigationPath() + @Published var sizeClass: UserInterfaceSizeClass? + init() { path.append(CompactViewRoute.feed) } diff --git a/iosApp/Source/App/ContentView.swift b/iosApp/Source/App/ContentView.swift index bde93ad2..2dce9565 100644 --- a/iosApp/Source/App/ContentView.swift +++ b/iosApp/Source/App/ContentView.swift @@ -10,9 +10,18 @@ struct ContentView: View { @EnvironmentObject var browserSelector: BrowserSelector + @Environment(\.scenePhase) + private var scenePhase: ScenePhase + + @Environment(\.horizontalSizeClass) + private var horizontalSizeClass: UserInterfaceSizeClass? + @StateObject var homeViewModel = KotlinDependencies.shared.getHomeViewModel() + @State + private var isAppInBackground: Bool = false + var body: some View { ZStack { HomeContainer() @@ -26,6 +35,26 @@ struct ContentView: View { Snackbar(messageQueue: $appState.snackbarQueue) } } + .onAppear { + if appState.sizeClass == nil { + appState.sizeClass = horizontalSizeClass + } + } + .onChange(of: self.horizontalSizeClass) { newSizeClass in + if !isAppInBackground && newSizeClass != appState.sizeClass { + appState.sizeClass = newSizeClass + } + } + .onChange(of: scenePhase) { newScenePhase in + switch newScenePhase { + case.active: + isAppInBackground = false + case .background: + isAppInBackground = true + default: + break + } + } } } @@ -34,9 +63,6 @@ private struct HomeContainer: View { @EnvironmentObject var appState: AppState - @Environment(\.horizontalSizeClass) - private var horizontalSizeClass - @StateObject var homeViewModel = KotlinDependencies.shared.getHomeViewModel() @@ -44,7 +70,7 @@ private struct HomeContainer: View { private var selectedDrawerItem: DrawerItem? = DrawerItem.Timeline() var body: some View { - if horizontalSizeClass == .compact { + if appState.sizeClass == .compact { CompactView(selectedDrawerItem: $selectedDrawerItem, homeViewModel: homeViewModel) } else { RegularView(selectedDrawerItem: $selectedDrawerItem, homeViewModel: homeViewModel) diff --git a/iosApp/Source/Home/HomeScreen.swift b/iosApp/Source/Home/HomeScreen.swift index 58da7600..394f2d0f 100644 --- a/iosApp/Source/Home/HomeScreen.swift +++ b/iosApp/Source/Home/HomeScreen.swift @@ -150,9 +150,6 @@ struct HomeContent: View { @EnvironmentObject private var appState: AppState - @Environment(\.horizontalSizeClass) - private var horizontalSizeClass - @Environment(\.dismiss) private var dismiss @@ -200,15 +197,15 @@ struct HomeContent: View { .onChange(of: toggleListScroll) { _ in proxy.scrollTo(feedState.first?.id) } - .if(horizontalSizeClass == .compact) { view in + .if(appState.sizeClass == .compact) { view in view .navigationBarBackButtonHidden(true) } .navigationBarTitleDisplayMode(.inline) .toolbar { - ToolbarItem(placement: .navigationBarLeading) { + ToolbarItem(id: UUID().uuidString, placement: .navigationBarLeading, showsByDefault: true) { HStack { - if horizontalSizeClass == .compact { + if appState.sizeClass == .compact { Button( action: { self.dismiss() @@ -231,7 +228,7 @@ struct HomeContent: View { } } - ToolbarItem(placement: .primaryAction) { + ToolbarItem(id: UUID().uuidString, placement: .primaryAction, showsByDefault: true) { Menu { Button( action: {