From 2eb15b48d4c23913ebb56b08ae298333b39efede Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Wed, 3 Jan 2024 14:59:28 +0100 Subject: [PATCH] Don't cache non filterable timeline --- Packages/Env/Sources/Env/StreamWatcher.swift | 3 ++- .../Sources/Timeline/View/TimelineView.swift | 1 + .../Sources/Timeline/View/TimelineViewModel.swift | 12 +++++++++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Packages/Env/Sources/Env/StreamWatcher.swift b/Packages/Env/Sources/Env/StreamWatcher.swift index 2f4be81a..9f77fb6a 100644 --- a/Packages/Env/Sources/Env/StreamWatcher.swift +++ b/Packages/Env/Sources/Env/StreamWatcher.swift @@ -17,7 +17,8 @@ import Observation private var retryDelay: Int = 10 public enum Stream: String { - case publicTimeline = "public" + case federated = "public" + case local case user case direct } diff --git a/Packages/Timeline/Sources/Timeline/View/TimelineView.swift b/Packages/Timeline/Sources/Timeline/View/TimelineView.swift index cfd66714..90085528 100644 --- a/Packages/Timeline/Sources/Timeline/View/TimelineView.swift +++ b/Packages/Timeline/Sources/Timeline/View/TimelineView.swift @@ -114,6 +114,7 @@ public struct TimelineView: View { } .navigationBarTitleDisplayMode(.inline) .onAppear { + viewModel.canFilterTimeline = canFilterTimeline viewModel.isTimelineVisible = true if viewModel.client == nil { diff --git a/Packages/Timeline/Sources/Timeline/View/TimelineViewModel.swift b/Packages/Timeline/Sources/Timeline/View/TimelineViewModel.swift index 51486aad..71006aa4 100644 --- a/Packages/Timeline/Sources/Timeline/View/TimelineViewModel.swift +++ b/Packages/Timeline/Sources/Timeline/View/TimelineViewModel.swift @@ -48,11 +48,17 @@ import SwiftUI // Internal source of truth for a timeline. private(set) var datasource = TimelineDatasource() private let cache = TimelineCache() + private var isCacheEnabled: Bool { + canFilterTimeline && timeline.supportNewestPagination + } @ObservationIgnored private var visibileStatuses: [Status] = [] private var canStreamEvents: Bool = true + + @ObservationIgnored + var canFilterTimeline: Bool = true var client: Client? { didSet { @@ -152,20 +158,20 @@ import SwiftUI extension TimelineViewModel { private func cache() async { - if let client, timeline.supportNewestPagination { + if let client, isCacheEnabled { await cache.set(statuses: datasource.get(), client: client.id, filter: timeline.id) } } private func getCachedStatuses() async -> [Status]? { - if let client { + if let client, isCacheEnabled { return await cache.getStatuses(for: client.id, filter: timeline.id) } return nil } private func clearCache(filter: TimelineFilter) async { - if let client { + if let client, isCacheEnabled { await cache.clearCache(for: client.id, filter: filter.id) await cache.setLatestSeenStatuses([], for: client, filter: filter.id) }