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:
if wasBackgrounded {
wasBackgrounded = false
Task {
await viewModel.fetchNewestStatuses()
}
viewModel.refreshTimeline()
}
case .background:
wasBackgrounded = true

View file

@ -157,11 +157,19 @@ extension TimelineViewModel {
extension TimelineViewModel: StatusesFetcher {
func pullToRefresh() async {
timelineTask?.cancel()
if !timeline.supportNewestPagination {
await reset()
}
await fetchNewestStatuses()
}
func refreshTimeline() {
timelineTask?.cancel()
timelineTask = Task {
await fetchNewestStatuses()
}
}
func fetchNewestStatuses() async {
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.
// 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)
}
}