Better safeguard around timeline refresh

This commit is contained in:
Thomas Ricouard 2023-04-09 15:11:02 +02:00
parent b48e921699
commit 9897307c79
2 changed files with 13 additions and 4 deletions

View file

@ -164,9 +164,7 @@ public struct TimelineView: View {
case .active: case .active:
if wasBackgrounded { if wasBackgrounded {
wasBackgrounded = false wasBackgrounded = false
Task { viewModel.refreshTimeline()
await viewModel.fetchNewestStatuses()
}
} }
case .background: case .background:
wasBackgrounded = true wasBackgrounded = true

View file

@ -157,11 +157,19 @@ extension TimelineViewModel {
extension TimelineViewModel: StatusesFetcher { extension TimelineViewModel: StatusesFetcher {
func pullToRefresh() async { func pullToRefresh() async {
timelineTask?.cancel()
if !timeline.supportNewestPagination { if !timeline.supportNewestPagination {
await reset() await reset()
} }
await fetchNewestStatuses() await fetchNewestStatuses()
} }
func refreshTimeline() {
timelineTask?.cancel()
timelineTask = Task {
await fetchNewestStatuses()
}
}
func fetchNewestStatuses() async { func fetchNewestStatuses() async {
guard let client else { return } guard let client else { return }
@ -304,7 +312,10 @@ extension TimelineViewModel: StatusesFetcher {
// We trigger a new fetch so we can get the next new statuses if any. // We trigger a new fetch so we can get the next new statuses if any.
// If none, it'll stop there. // If none, it'll stop there.
if !Task.isCancelled, let latest = await datasource.get().first { // Only do that in the context of the home timeline as other don't worth catching up that much.
if timeline == .home,
!Task.isCancelled,
let latest = await datasource.get().first {
try await fetchNewPagesFrom(latestStatus: latest, client: client) try await fetchNewPagesFrom(latestStatus: latest, client: client)
} }
} }