Skip to content

feat: optionally open cached topics instantly#389

Open
MufanQiu wants to merge 1 commit into
BugenZhao:mainfrom
MufanQiu:feat/cache-first-topic-details
Open

feat: optionally open cached topics instantly#389
MufanQiu wants to merge 1 commit into
BugenZhao:mainfrom
MufanQiu:feat/cache-first-topic-details

Conversation

@MufanQiu

Copy link
Copy Markdown

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_cache request flag that returns the cache
without hitting the network. This change wires those together on the Swift side:

  • PagingDataSource.injectCachedResponse(_:page:) populates items from an
    already-fetched response without a network round-trip.
  • TopicDetailsView orchestrates two stages on appear: first a local_cache
    read 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.
  • Only applies to a plain first-page browse (the case that has a cache key);
    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

  • Built and ran on the iOS Simulator and a physical device.
  • Verified: with the setting on, re-opening a read topic shows instantly and
    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.

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.
Copilot AI review requested due to automatic review settings June 30, 2026 21:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 TopicDetailsView to (optionally) load page 1 from local_cache on 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants