damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit b79d3610162cd4a0dbed22f7df8963ef487f45dd
parent 4889c0a7d9a7475129170452e388c99ceb691571
Author: William Casarin <jb55@jb55.com>
Date:   Tue,  2 May 2023 07:33:54 -0700

Preload profile pictures while scrolling

Changelog-Added: Preload profile pictures while scrolling

Diffstat:
Mdamus/Components/ImageCarousel.swift | 3+++
Mdamus/Util/EventCache.swift | 26++++++++++++++++++++++----
2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/damus/Components/ImageCarousel.swift b/damus/Components/ImageCarousel.swift @@ -132,6 +132,9 @@ struct ImageCarousel: View { .imageFill(for: geo.size, max: maxHeight, fill: fillHeight) { fill in state.previews.cache_image_meta(evid: evid, image_fill: fill) // blur hash can be discarded when we have the url + // NOTE: this is the wrong place for this... we need to remove + // it when the image is loaded in memory. This may happen + // earlier than this (by the preloader, etc) DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { state.events.lookup_img_metadata(url: url)?.state = .not_needed } diff --git a/damus/Util/EventCache.swift b/damus/Util/EventCache.swift @@ -337,11 +337,32 @@ func get_preload_plan(cache: EventData, ev: NostrEvent, our_keypair: Keypair, se return PreloadPlan(data: cache, event: ev, load_artifacts: load_artifacts, load_translations: load_translations, load_preview: load_preview) } +func preload_image(url: URL) { + if ImageCache.default.isCached(forKey: url.absoluteString) { + print("Preloaded image \(url.absoluteString) found in cache") + // looks like we already have it cached. no download needed + return + } + + print("Preloading image \(url.absoluteString)") + + KingfisherManager.shared.retrieveImage(with: ImageResource(downloadURL: url)) { val in + print("Preloaded image \(url.absoluteString)") + } +} + func preload_event(plan: PreloadPlan, profiles: Profiles, our_keypair: Keypair, settings: UserSettingsStore) async { var artifacts: NoteArtifacts? = plan.data.artifacts.artifacts print("Preloading event \(plan.event.content)") + // preload pfp + if let profile = profiles.lookup(id: plan.event.pubkey), + let picture = profile.picture, + let url = URL(string: picture) { + preload_image(url: url) + } + if artifacts == nil && plan.load_artifacts { let arts = render_note_content(ev: plan.event, profiles: profiles, privkey: our_keypair.privkey) artifacts = arts @@ -352,10 +373,7 @@ func preload_event(plan: PreloadPlan, profiles: Profiles, our_keypair: Keypair, } for url in arts.images { - print("Preloading image \(url.absoluteString)") - KingfisherManager.shared.retrieveImage(with: ImageResource(downloadURL: url)) { val in - print("Finished preloading image \(url.absoluteString)") - } + preload_image(url: url) } }