Skip to content

Commit

Permalink
Restore ScrollToTop on timeline tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimillian committed Sep 14, 2024
1 parent 594fb3e commit 122e57d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
11 changes: 11 additions & 0 deletions IceCubesApp/App/Main/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct AppView: View {

@State var iosTabs = iOSTabs.shared
@State var sidebarTabs = SidebarTabs.shared
@State var selectedTabScrollToTop: Int = -1

var body: some View {
#if os(visionOS)
Expand Down Expand Up @@ -75,6 +76,7 @@ struct AppView: View {
}
.id(appAccountsManager.currentClient.id)
.withSheetDestinations(sheetDestinations: $appRouterPath.presentedSheet)
.environment(\.selectedTabScrollToTop, selectedTabScrollToTop)
}

private func updateTab(with newTab: AppTab) {
Expand All @@ -90,6 +92,15 @@ struct AppView: View {
HapticManager.shared.fireHaptic(.tabSelection)
SoundEffectManager.shared.playSound(.tabSelection)

if selectedTab == newTab {
selectedTabScrollToTop = newTab.rawValue
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
selectedTabScrollToTop = -1
}
} else {
selectedTabScrollToTop = -1
}

selectedTab = newTab
}

Expand Down
1 change: 1 addition & 0 deletions Packages/Env/Sources/Env/CustomEnvValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ extension EnvironmentValues {
@Entry public var isStatusFocused: Bool = false
@Entry public var isHomeTimeline: Bool = false
@Entry public var indentationLevel: UInt = 0
@Entry public var selectedTabScrollToTop: Int = -1
}
59 changes: 35 additions & 24 deletions Packages/Timeline/Sources/Timeline/View/TimelineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import SwiftUIIntrospect
@MainActor
public struct TimelineView: View {
@Environment(\.scenePhase) private var scenePhase
@Environment(\.selectedTabScrollToTop) private var selectedTabScrollToTop

@Environment(Theme.self) private var theme
@Environment(CurrentAccount.self) private var account
@Environment(StreamWatcher.self) private var watcher
Expand Down Expand Up @@ -45,33 +47,42 @@ public struct TimelineView: View {

public var body: some View {
ZStack(alignment: .top) {
List {
scrollToTopView
TimelineTagGroupheaderView(group: $selectedTagGroup, timeline: $timeline)
TimelineTagHeaderView(tag: $viewModel.tag)
switch viewModel.timeline {
case .remoteLocal:
StatusesListView(fetcher: viewModel, client: client, routerPath: routerPath, isRemote: true)
default:
StatusesListView(fetcher: viewModel, client: client, routerPath: routerPath)
.environment(\.isHomeTimeline, timeline == .home)
ScrollViewReader { proxy in
List {
scrollToTopView
TimelineTagGroupheaderView(group: $selectedTagGroup, timeline: $timeline)
TimelineTagHeaderView(tag: $viewModel.tag)
switch viewModel.timeline {
case .remoteLocal:
StatusesListView(fetcher: viewModel, client: client, routerPath: routerPath, isRemote: true)
default:
StatusesListView(fetcher: viewModel, client: client, routerPath: routerPath)
.environment(\.isHomeTimeline, timeline == .home)
}
}
}
.id(client.id)
.environment(\.defaultMinListRowHeight, 1)
.listStyle(.plain)
#if !os(visionOS)
.scrollContentBackground(.hidden)
.background(theme.primaryBackgroundColor)
#endif
.introspect(.list, on: .iOS(.v17, .v18)) { (collectionView: UICollectionView) in
DispatchQueue.main.async {
self.collectionView = collectionView
.id(client.id)
.environment(\.defaultMinListRowHeight, 1)
.listStyle(.plain)
.onChange(of: selectedTabScrollToTop) { _, newValue in
if newValue == 0, routerPath.path.isEmpty {
withAnimation {
proxy.scrollTo(ScrollToView.Constants.scrollToTop, anchor: .top)
}
}
prefetcher.viewModel = viewModel
collectionView.isPrefetchingEnabled = true
collectionView.prefetchDataSource = prefetcher
}
#if !os(visionOS)
.scrollContentBackground(.hidden)
.background(theme.primaryBackgroundColor)
#endif
.introspect(.list, on: .iOS(.v17, .v18)) { (collectionView: UICollectionView) in
DispatchQueue.main.async {
self.collectionView = collectionView
}
prefetcher.viewModel = viewModel
collectionView.isPrefetchingEnabled = true
collectionView.prefetchDataSource = prefetcher
}
}
if viewModel.timeline.supportNewestPagination {
TimelineUnreadStatusesView(observer: viewModel.pendingStatusesObserver)
}
Expand Down

0 comments on commit 122e57d

Please sign in to comment.