From 4bc26725837f6c2ec6cef6edb2f684d1bd97331c Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Mon, 27 Feb 2023 18:41:51 +0100 Subject: [PATCH] Timeline: Fix trending refreshing when navigating back --- .../Timeline/Sources/Timeline/TimelineView.swift | 2 +- .../Sources/Timeline/TimelineViewModel.swift | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Packages/Timeline/Sources/Timeline/TimelineView.swift b/Packages/Timeline/Sources/Timeline/TimelineView.swift index 461192ed..5d8c27ab 100644 --- a/Packages/Timeline/Sources/Timeline/TimelineView.swift +++ b/Packages/Timeline/Sources/Timeline/TimelineView.swift @@ -115,7 +115,7 @@ public struct TimelineView: View { } .refreshable { HapticManager.shared.fireHaptic(of: .dataRefresh(intensity: 0.3)) - await viewModel.fetchNewestStatuses() + await viewModel.pullToRefresh() HapticManager.shared.fireHaptic(of: .dataRefresh(intensity: 0.7)) } .onChange(of: watcher.latestEvent?.id) { _ in diff --git a/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift b/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift index f64e18a9..e1e0b837 100644 --- a/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift +++ b/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift @@ -156,13 +156,19 @@ extension TimelineViewModel { // MARK: - StatusesFetcher extension TimelineViewModel: StatusesFetcher { + func pullToRefresh() async { + if !timeline.supportNewestPagination { + await reset() + } + await fetchNewestStatuses() + } + func fetchNewestStatuses() async { guard let client else { return } do { - if await datasource.isEmpty || !timeline.supportNewestPagination { - await reset() + if await datasource.isEmpty { try await fetchFirstPage(client: client) - } else if let latest = await datasource.get().first { + } else if let latest = await datasource.get().first, timeline.supportNewestPagination { try await fetchNewPagesFrom(latestStatus: latest, client: client) } } catch {