From 43d975a27b0e772703fd1a2a60e6c593918a3867 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Wed, 1 Feb 2023 13:41:28 +0100 Subject: [PATCH] Better caching --- .../Sources/Timeline/TimelineViewModel.swift | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift b/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift index 46860cea..70a1ce94 100644 --- a/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift +++ b/Packages/Timeline/Sources/Timeline/TimelineViewModel.swift @@ -73,7 +73,7 @@ class TimelineViewModel: ObservableObject { pendingStatusesObserver.pendingStatuses.insert(event.status.id, at: 0) statuses.insert(event.status, at: 0) Task { - await cache(statuses: statuses) + await cacheHome() } withAnimation { statusesState = .display(statuses: statuses, nextPageState: .hasNextPage) @@ -82,7 +82,7 @@ class TimelineViewModel: ObservableObject { withAnimation { statuses.removeAll(where: { $0.id == event.status }) Task { - await cache(statuses: statuses) + await cacheHome() } statusesState = .display(statuses: statuses, nextPageState: .hasNextPage) } @@ -90,7 +90,7 @@ class TimelineViewModel: ObservableObject { if let originalIndex = statuses.firstIndex(where: { $0.id == event.status.id }) { statuses[originalIndex] = event.status Task { - await cache(statuses: statuses) + await cacheHome() } statusesState = .display(statuses: statuses, nextPageState: .hasNextPage) } @@ -101,8 +101,8 @@ class TimelineViewModel: ObservableObject { // MARK: - Cache extension TimelineViewModel { - private func cache(statuses: [Status]) async { - if let client { + private func cacheHome() async { + if let client, timeline == .home { await cache.set(statuses: statuses, client: client) } } @@ -152,9 +152,7 @@ extension TimelineViewModel: StatusesFetcher { maxId: nil, minId: nil, offset: statuses.count)) - if timeline == .home { - await cache(statuses: statuses) - } + await cacheHome() withAnimation { statusesState = .display(statuses: statuses, nextPageState: statuses.count < 20 ? .none : .hasNextPage) } @@ -184,9 +182,7 @@ extension TimelineViewModel: StatusesFetcher { statuses.insert(contentsOf: newStatuses, at: 0) // Cache statuses for home timeline. - if timeline == .home { - await cache(statuses: statuses) - } + await cacheHome() // If pending statuses are not enabled, we simply load status on the top regardless of the current position. if !pendingStatusesEnabled {