pfps: load profile pics in the background

So we don't get annoying popping artifacts when scrolling

Changelog-Fixed: Profile pics are now loaded in the background
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2022-08-06 13:34:04 -07:00
parent 97bca010f6
commit 03748a2b02
8 changed files with 133 additions and 75 deletions

View File

@@ -79,7 +79,7 @@ class FollowersModel: ObservableObject {
if ev.known_kind == .contacts {
handle_contact_event(ev)
} else if ev.known_kind == .metadata {
process_metadata_event(profiles: self.damus_state.profiles, ev: ev)
process_metadata_event(image_cache: damus_state.image_cache, profiles: damus_state.profiles, ev: ev)
}
case .notice(let msg):

View File

@@ -60,7 +60,7 @@ class FollowingModel {
switch nev {
case .event(_, let ev):
if ev.kind == 0 {
process_metadata_event(profiles: damus_state.profiles, ev: ev)
process_metadata_event(image_cache: damus_state.image_cache, profiles: damus_state.profiles, ev: ev)
}
case .notice(let msg):
print("followingmodel notice: \(msg)")

View File

@@ -6,6 +6,7 @@
//
import Foundation
import UIKit
struct NewEventsBits {
let bits: Int
@@ -300,7 +301,7 @@ class HomeModel: ObservableObject {
}
func handle_metadata_event(_ ev: NostrEvent) {
process_metadata_event(profiles: damus_state.profiles, ev: ev)
process_metadata_event(image_cache: damus_state.image_cache, profiles: damus_state.profiles, ev: ev)
}
func get_last_event_of_kind(relay_id: String, kind: Int) -> NostrEvent? {
@@ -489,7 +490,7 @@ func print_filters(relay_id: String?, filters groups: [[NostrFilter]]) {
print("-----")
}
func process_metadata_event(profiles: Profiles, ev: NostrEvent) {
func process_metadata_event(image_cache: ImageCache, profiles: Profiles, ev: NostrEvent) {
guard let profile: Profile = decode_data(Data(ev.content.utf8)) else {
return
}
@@ -503,7 +504,19 @@ func process_metadata_event(profiles: Profiles, ev: NostrEvent) {
let tprof = TimestampedProfile(profile: profile, timestamp: ev.created_at)
profiles.add(id: ev.pubkey, profile: tprof)
// load pfps asap
let picture = tprof.profile.picture ?? "https://robohash.org/\(ev.pubkey)"
if let url = URL(string: picture) {
Task<UIImage?, Never>.init(priority: .background) {
let res = await load_image(cache: image_cache, from: url)
DispatchQueue.main.async {
notify(.profile_updated, ProfileUpdate(pubkey: ev.pubkey, profile: profile))
}
return res
}
}
notify(.profile_updated, ProfileUpdate(pubkey: ev.pubkey, profile: profile))
}

View File

@@ -14,15 +14,13 @@ class SearchHomeModel: ObservableObject {
@Published var loading: Bool = false
var seen_pubkey: Set<String> = Set()
let profiles: Profiles
let pool: RelayPool
let damus_state: DamusState
let base_subid = UUID().description
let profiles_subid = UUID().description
let limit: UInt32 = 250
init(pool: RelayPool, profiles: Profiles) {
self.pool = pool
self.profiles = profiles
init(damus_state: DamusState) {
self.damus_state = damus_state
}
func get_base_filter() -> NostrFilter {
@@ -34,21 +32,21 @@ class SearchHomeModel: ObservableObject {
func subscribe() {
loading = true
pool.subscribe(sub_id: base_subid, filters: [get_base_filter()], handler: handle_event)
damus_state.pool.subscribe(sub_id: base_subid, filters: [get_base_filter()], handler: handle_event)
}
func unsubscribe() {
loading = false
pool.unsubscribe(sub_id: base_subid)
damus_state.pool.unsubscribe(sub_id: base_subid)
}
func load_profiles(relay_id: String) {
var filter = NostrFilter.filter_profiles
let authors = find_profiles_to_fetch(profiles: profiles, events: events)
let authors = find_profiles_to_fetch(profiles: damus_state.profiles, events: events)
filter.authors = authors
if !authors.isEmpty {
pool.subscribe(sub_id: profiles_subid, filters: [filter], handler: handle_event)
damus_state.pool.subscribe(sub_id: profiles_subid, filters: [filter], handler: handle_event)
}
}
@@ -71,7 +69,7 @@ class SearchHomeModel: ObservableObject {
$0.created_at > $1.created_at
}
} else if ev.known_kind == .metadata {
process_metadata_event(profiles: self.profiles, ev: ev)
process_metadata_event(image_cache: damus_state.image_cache, profiles: damus_state.profiles, ev: ev)
}
case .notice(let msg):
print("search home notice: \(msg)")
@@ -81,7 +79,7 @@ class SearchHomeModel: ObservableObject {
if sub_id == self.base_subid {
load_profiles(relay_id: relay_id)
} else if sub_id == self.profiles_subid {
pool.unsubscribe(sub_id: self.profiles_subid)
damus_state.pool.unsubscribe(sub_id: self.profiles_subid)
}
break