Better caching

This commit is contained in:
Thomas Ricouard 2023-02-01 13:41:28 +01:00
parent ed80895fcd
commit 43d975a27b

View file

@ -73,7 +73,7 @@ class TimelineViewModel: ObservableObject {
pendingStatusesObserver.pendingStatuses.insert(event.status.id, at: 0) pendingStatusesObserver.pendingStatuses.insert(event.status.id, at: 0)
statuses.insert(event.status, at: 0) statuses.insert(event.status, at: 0)
Task { Task {
await cache(statuses: statuses) await cacheHome()
} }
withAnimation { withAnimation {
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage) statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)
@ -82,7 +82,7 @@ class TimelineViewModel: ObservableObject {
withAnimation { withAnimation {
statuses.removeAll(where: { $0.id == event.status }) statuses.removeAll(where: { $0.id == event.status })
Task { Task {
await cache(statuses: statuses) await cacheHome()
} }
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage) statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)
} }
@ -90,7 +90,7 @@ class TimelineViewModel: ObservableObject {
if let originalIndex = statuses.firstIndex(where: { $0.id == event.status.id }) { if let originalIndex = statuses.firstIndex(where: { $0.id == event.status.id }) {
statuses[originalIndex] = event.status statuses[originalIndex] = event.status
Task { Task {
await cache(statuses: statuses) await cacheHome()
} }
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage) statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)
} }
@ -101,8 +101,8 @@ class TimelineViewModel: ObservableObject {
// MARK: - Cache // MARK: - Cache
extension TimelineViewModel { extension TimelineViewModel {
private func cache(statuses: [Status]) async { private func cacheHome() async {
if let client { if let client, timeline == .home {
await cache.set(statuses: statuses, client: client) await cache.set(statuses: statuses, client: client)
} }
} }
@ -152,9 +152,7 @@ extension TimelineViewModel: StatusesFetcher {
maxId: nil, maxId: nil,
minId: nil, minId: nil,
offset: statuses.count)) offset: statuses.count))
if timeline == .home { await cacheHome()
await cache(statuses: statuses)
}
withAnimation { withAnimation {
statusesState = .display(statuses: statuses, nextPageState: statuses.count < 20 ? .none : .hasNextPage) statusesState = .display(statuses: statuses, nextPageState: statuses.count < 20 ? .none : .hasNextPage)
} }
@ -184,9 +182,7 @@ extension TimelineViewModel: StatusesFetcher {
statuses.insert(contentsOf: newStatuses, at: 0) statuses.insert(contentsOf: newStatuses, at: 0)
// Cache statuses for home timeline. // Cache statuses for home timeline.
if timeline == .home { await cacheHome()
await cache(statuses: statuses)
}
// If pending statuses are not enabled, we simply load status on the top regardless of the current position. // If pending statuses are not enabled, we simply load status on the top regardless of the current position.
if !pendingStatusesEnabled { if !pendingStatusesEnabled {