Skip to content

Commit

Permalink
Fix scroll position when opening a link on iPad
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Oct 29, 2023
1 parent 0d8d9b2 commit b97cbb9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
2 changes: 2 additions & 0 deletions iosApp/Source/App/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class AppState: ObservableObject {
@Published
var path = NavigationPath()

@Published var sizeClass: UserInterfaceSizeClass?

init() {
path.append(CompactViewRoute.feed)
}
Expand Down
34 changes: 30 additions & 4 deletions iosApp/Source/App/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
}
}
}

Expand All @@ -34,17 +63,14 @@ private struct HomeContainer: View {
@EnvironmentObject
var appState: AppState

@Environment(\.horizontalSizeClass)
private var horizontalSizeClass

@StateObject
var homeViewModel = KotlinDependencies.shared.getHomeViewModel()

@State
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)
Expand Down
11 changes: 4 additions & 7 deletions iosApp/Source/Home/HomeScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ struct HomeContent: View {
@EnvironmentObject
private var appState: AppState

@Environment(\.horizontalSizeClass)
private var horizontalSizeClass

@Environment(\.dismiss)
private var dismiss

Expand Down Expand Up @@ -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()
Expand All @@ -231,7 +228,7 @@ struct HomeContent: View {
}
}

ToolbarItem(placement: .primaryAction) {
ToolbarItem(id: UUID().uuidString, placement: .primaryAction, showsByDefault: true) {
Menu {
Button(
action: {
Expand Down

0 comments on commit b97cbb9

Please sign in to comment.