Don't update the timeline is the tab is not visible

This commit is contained in:
Thomas Ricouard 2023-02-04 16:54:03 +01:00
parent 8c7efb7df5
commit 427452db30
2 changed files with 12 additions and 1 deletions

View file

@ -79,6 +79,7 @@ public struct TimelineView: View {
.navigationTitle(timeline.localizedTitle())
.navigationBarTitleDisplayMode(.inline)
.onAppear {
viewModel.isTimelineVisible = true
if viewModel.client == nil {
viewModel.client = client
viewModel.timeline = timeline
@ -88,6 +89,9 @@ public struct TimelineView: View {
}
}
}
.onDisappear {
viewModel.isTimelineVisible = false
}
.refreshable {
HapticManager.shared.impact(intensity: 0.3)
await viewModel.fetchStatuses()

View file

@ -27,6 +27,7 @@ class TimelineViewModel: ObservableObject {
}
private var canStreamEvents: Bool = true
var isTimelineVisible: Bool = false
let pendingStatusesObserver: PendingStatusesObserver = .init()
@ -86,6 +87,7 @@ class TimelineViewModel: ObservableObject {
func handleEvent(event: any StreamEvent, currentAccount _: CurrentAccount) {
if let event = event as? StreamEventUpdate,
canStreamEvents,
isTimelineVisible,
pendingStatusesEnabled,
!statuses.contains(where: { $0.id == event.status.id })
{
@ -212,12 +214,17 @@ extension TimelineViewModel: StatusesFetcher {
ReblogCache.shared.removeDuplicateReblogs(&newStatuses)
// If no new statuses, resume streaming and exit.
guard !newStatuses.isEmpty else {
canStreamEvents = true
return
}
// If the timeline is not visible, we don't update it as it would mess up the user position.
guard isTimelineVisible else {
canStreamEvents = true
return
}
// Keep track of the top most status, so we can scroll back to it after view update.
let topStatusId = statuses.first?.id