damus

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

commit 5a32a384c4bceb1057d193a94a40b73cbd010f34
parent 35ae69740aa96a7d6dba3f21f66c4ab44f65ff35
Author: OlegAba <mail@olegaba.com>
Date:   Mon,  9 Jan 2023 10:24:24 -0500

Add fallback image url to profile pic view

Closes: #294
Changlog-Changed: Add fallback image url to profile pic view

Diffstat:
Mdamus/Views/EditMetadataView.swift | 2+-
Mdamus/Views/ProfilePicView.swift | 46++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/damus/Views/EditMetadataView.swift b/damus/Views/EditMetadataView.swift @@ -103,7 +103,7 @@ struct EditMetadataView: View { VStack(alignment: .leading) { HStack { Spacer() - InnerProfilePicView(url: URL(string: picture), pubkey: damus_state.pubkey, size: PPM_SIZE, highlight: .none) + InnerProfilePicView(url: URL(string: picture), fallbackUrl: nil, pubkey: damus_state.pubkey, size: PPM_SIZE, highlight: .none) Spacer() } Form { diff --git a/damus/Views/ProfilePicView.swift b/damus/Views/ProfilePicView.swift @@ -34,9 +34,12 @@ func pfp_line_width(_ h: Highlight) -> CGFloat { struct InnerProfilePicView: View { let url: URL? + let fallbackUrl: URL? let pubkey: String let size: CGFloat let highlight: Highlight + + @State private var refreshID = UUID().uuidString var PlaceholderColor: Color { return id_to_color(pubkey) @@ -53,11 +56,11 @@ struct InnerProfilePicView: View { var body: some View { ZStack { Color(uiColor: .systemBackground) - + KFAnimatedImage(url) .callbackQueue(.dispatch(.global(qos: .background))) .processingQueue(.dispatch(.global(qos: .background))) - .appendProcessor(LargeImageProcessor()) + .appendProcessor(LargeImageProcessor.shared) .configure { view in view.framePreloadCount = 1 } @@ -67,10 +70,43 @@ struct InnerProfilePicView: View { .scaleFactor(UIScreen.main.scale) .loadDiskFileSynchronously() .fade(duration: 0.1) + .onFailure { _ in + setFallbackImage() + } } .frame(width: size, height: size) .clipShape(Circle()) .overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight))) + .id(refreshID) + } + + func refreshView() -> Void { + refreshID = UUID().uuidString + } + + func setFallbackImage() -> Void { + + guard let url = url, let fallbackUrl = fallbackUrl else { return } + + KingfisherManager.shared.downloader.downloadImage(with: fallbackUrl) { result in + + func fallbackImage() -> UIImage { + switch result { + case .success(let imageLoadingResult): + return imageLoadingResult.image + case .failure(let error): + print(error) + return UIImage() + } + } + + // Kingfisher ID format for caching when using a custom processor + let processorIdentifier = "|>" + LargeImageProcessor.shared.identifier + + KingfisherManager.shared.cache.store(fallbackImage(), forKey: url.absoluteString, processorIdentifier: processorIdentifier) { _ in + refreshView() + } + } } } @@ -91,7 +127,7 @@ struct ProfilePicView: View { } var body: some View { - InnerProfilePicView(url: get_profile_url(picture: picture, pubkey: pubkey, profiles: profiles), pubkey: pubkey, size: size, highlight: highlight) + InnerProfilePicView(url: get_profile_url(picture: picture, pubkey: pubkey, profiles: profiles), fallbackUrl: URL(string: robohash(pubkey)), pubkey: pubkey, size: size, highlight: highlight) .onReceive(handle_notify(.profile_updated)) { notif in let updated = notif.object as! ProfileUpdate @@ -108,7 +144,9 @@ struct ProfilePicView: View { struct LargeImageProcessor: ImageProcessor { - let identifier: String = "com.damus.largeimageprocessor" + static let shared = LargeImageProcessor() + + let identifier = "com.damus.largeimageprocessor" let maxSize: Int = 1000000 let downsampleSize = CGSize(width: 200, height: 200)