feat: optionally open cached topics instantly#389
Open
MufanQiu wants to merge 1 commit into
Open
Conversation
Add an opt-in 'Open Cached Topics Instantly' setting (off by default) under Reading. When enabled, opening a topic reads the on-disk cache first and shows it immediately (no network wait), then silently refreshes the cache in the background so the next visit is up to date. Pull-to-refresh still fetches live content on demand. - PagingDataSource gains `injectCachedResponse` to populate items from an already-fetched response without a network round-trip. - TopicDetailsView orchestrates the two stages: a local-cache read (fast, no network on the Rust side) shows content instantly; on a cache miss it falls back to the normal load. Only applies to a plain first-page browse (the case that has a cache key); jump-to-floor / only-post / author-only are excluded. - Reuses the existing `local_cache` service flag; no proto or Rust changes.
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an opt-in “cache-first” topic details loading path in the SwiftUI layer so previously-read topics can render immediately from the on-disk Rust cache, while still refreshing the cache silently in the background for future opens.
Changes:
- Add a new Reading preference toggle to enable “Open Cached Topics Instantly” (off by default).
- Add
PagingDataSource.injectCachedResponse(_:page:)to populate UI from an already-fetched response without triggering a network load. - Update
TopicDetailsViewto (optionally) load page 1 fromlocal_cacheon initial appear, then fire a background refresh whose response is intentionally discarded.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| app/Shared/Views/TopicDetailsView.swift | Adds cache-first orchestration on initial appear and background cache refresh logic. |
| app/Shared/Views/PreferencesView.swift | Adds the new “Open Cached Topics Instantly” toggle under Reading. |
| app/Shared/Storage/PreferencesStorage.swift | Persists the new preference via @AppStorage. |
| app/Shared/Models/PagingDataSource.swift | Adds a helper to inject cached responses into the paging model. |
| app/Shared/Localization/zh-Hans.lproj/Localizable.strings | Adds Chinese localization for the new toggle label. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+858
to
+862
| func onInitialAppear() { | ||
| guard cacheFirstApplicable else { | ||
| dataSource.initialLoad() | ||
| return | ||
| } |
Comment on lines
+872
to
+875
| logicCallAsync(.topicDetails(cacheRequest), errorToastModel: nil) { (cached: TopicDetailsResponse) in | ||
| // `injectCachedResponse` sets `latestResponse`, which drives | ||
| // `updateTopicOnNewResponse` via the existing `.onChange` in `body`. | ||
| let shown = dataSource.injectCachedResponse(cached, page: 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Make opening a previously-read topic feel instant. Currently every time you
open a topic the details are re-fetched over the network before anything shows,
so even topics you just read have a visible delay. This adds an opt-in
"Open Cached Topics Instantly" setting (off by default, under Reading) that
shows the on-disk cache immediately, then silently refreshes the cache in the
background for next time.
How
The Rust service already writes the topic-details cache on every successful
load and already supports a
local_cacherequest flag that returns the cachewithout hitting the network. This change wires those together on the Swift side:
PagingDataSource.injectCachedResponse(_:page:)populates items from analready-fetched response without a network round-trip.
TopicDetailsVieworchestrates two stages on appear: first alocal_cacheread shows content instantly; then it fires a normal background load whose
only effect is refreshing the cache (the response is discarded so the current
view isn't replaced — no scroll jumps or flicker). On a cache miss it falls
back to the normal load, so first-time opens behave exactly as before.
jump-to-floor / only-post / author-only / local mode are excluded.
Pull-to-refresh still fetches live content on demand. No proto or Rust changes.
Testing
the cache is refreshed in the background for the next visit; first-time opens
and pull-to-refresh are unchanged; jump-to-floor/only-post paths unaffected.