From b79d3610162cd4a0dbed22f7df8963ef487f45dd Mon Sep 17 00:00:00 2001 From: William Casarin Date: Tue, 2 May 2023 07:33:54 -0700 Subject: [PATCH] Preload profile pictures while scrolling Changelog-Added: Preload profile pictures while scrolling --- damus/Components/ImageCarousel.swift | 3 +++ damus/Util/EventCache.swift | 26 ++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/damus/Components/ImageCarousel.swift b/damus/Components/ImageCarousel.swift index 88d587d7..caff10b8 100644 --- 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 index 4c35b234..3766c7c0 100644 --- 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) } }