1
0
mirror of git://jb55.com/damus synced 2024-09-19 19:46:51 +00:00

Preload profile pictures while scrolling

Changelog-Added: Preload profile pictures while scrolling
This commit is contained in:
William Casarin 2023-05-02 07:33:54 -07:00
parent 4889c0a7d9
commit b79d361016
2 changed files with 25 additions and 4 deletions

View File

@ -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
}

View File

@ -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)
}
}