Skip to content

Keep avatars and commit images on the phone for a day - #27

Merged
simonhamp merged 1 commit into
mainfrom
feat/image-cache
Oct 19, 2026
Merged

simonhamp merged 1 commit into
mainfrom
feat/image-cache

Conversation

@simonhamp

Copy link
Copy Markdown
Member

Images were fetched again every time they scrolled into view. On iOS, mobile-ui draws a remote URL with SwiftUI's AsyncImage, which keeps nothing once the image leaves the screen, and the server lifetimes are short: GitHub avatars say 5 minutes, commit images an hour. So scrolling the Log meant spinners and repeat requests.

This keeps them on the phone for a day, in the app rather than in mobile-ui.

How it works

  • ImageCache::src() gives an image element the downloaded file for a URL while it's under 24 hours old, and the URL otherwise. mobile-ui draws a device file through its own in-memory cache, with no network.
  • When an answer from devnull arrives, in Client::get() and after a background refresh, ImageCache::prefetch() finds the avatars, commit images and link preview images in it that aren't on the phone yet. It hands them to DownloadImages, an AsyncTask on NativePHP's background thread.
  • DownloadImages fetches six at a time and only keeps responses that are images. It writes each file whole before renaming it into place, and clears out files past the cache's age, so the folder holds about a day's worth.
  • Nothing downloads while a screen is drawn. An image that isn't down yet loads from its URL as before, and comes from the phone next time.
  • DEVNULL_IMAGE_CACHE_HOURS sets the age (24 by default). 0 turns it off, which phpunit.xml does so existing tests don't dispatch downloads.

Worth checking

  • A new avatar, or an image changed behind the same URL, can show the old picture for up to a day. Commit image URLs never change for an image, so in practice it's profile pictures.
  • The files live in storage/app/image-cache.

Testing

852 tests pass and Pint is clean. New tests cover:

  • finding image URLs in answers,
  • drawing from the device while an image is fresh and from the URL once it's stale,
  • queueing only missing images,
  • downloads keeping only images and pruning old files,
  • a Log refresh that brings its avatars down, after which the Log draws them from the device,
  • a foreground load queueing its images.

Not yet tried on a phone.

🤖 Generated with Claude Code

NativePHP's image element streams a remote URL every time it scrolls into
view, but draws a file on the device from memory. So when an answer from
devnull arrives, the avatars, commit images and link preview images in it
that aren't on the phone yet are downloaded on a background thread, and
the views draw each one from its file until it's a day old.

Downloads start where answers arrive, in Client::get() and after a
background refresh, never while a screen is drawn, so an image that isn't
down yet loads from its URL as before. DownloadImages only keeps responses
that are images, writes each file whole before it can be drawn, and clears
out anything past the cache's age. DEVNULL_IMAGE_CACHE_HOURS changes the
day, and 0 turns it off, which phpunit.xml does so other tests don't
dispatch downloads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@simonhamp
simonhamp merged commit 8fbe287 into main Oct 19, 2026
1 check passed
@simonhamp
simonhamp deleted the feat/image-cache branch October 21, 2026 16:24
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.

1 participant