Force the timeline to stay at the bottom when first loading

This commit is contained in:
Stefan Ceriu
2022-11-18 14:45:07 +02:00
committed by Stefan Ceriu
parent 3e1fde6442
commit ef8d230328
2 changed files with 26 additions and 16 deletions
@@ -82,20 +82,12 @@ struct TimelineItemList: View {
}
}
}
.onChange(of: pinnedItem) { item in
guard let item else {
return
}
if item.animated {
withAnimation(Animation.elementDefault) {
proxy.scrollTo(item.id, anchor: item.anchor)
}
} else {
proxy.scrollTo(item.id, anchor: item.anchor)
}
pinnedItem = nil
.onChange(of: timelineItems) { _ in
// Run this multiple times on first appearance to fix offset issues
updatePinnedOffset(scrollViewProxy: proxy)
}
.onChange(of: pinnedItem) { _ in
updatePinnedOffset(scrollViewProxy: proxy)
}
}
.scrollDismissesKeyboard(.immediately)
@@ -148,8 +140,6 @@ struct TimelineItemList: View {
let pinnedItem = PinnedItem(id: currentFirstItem.id, anchor: .top, animated: false)
timelineItems = context.viewState.items
self.pinnedItem = pinnedItem
return
}
// Otherwise just update the items
@@ -198,6 +188,25 @@ struct TimelineItemList: View {
return selectedItemId == item.id ? 1.0 : 0.5
}
private func updatePinnedOffset(scrollViewProxy proxy: ScrollViewProxy) {
guard let item = pinnedItem else {
return
}
if item.animated {
withAnimation(.elementDefault) {
proxy.scrollTo(item.id, anchor: item.anchor)
}
} else {
proxy.scrollTo(item.id, anchor: item.anchor)
}
// Need to delay this for a bit to fix offset problems when loading the initial page
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
pinnedItem = nil
}
}
private var isRunningPreviews: Bool {
#if DEBUG
return ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"