Add fallback image url to profile pic view
Closes: #294 Changlog-Changed: Add fallback image url to profile pic view
This commit is contained in:
@@ -103,7 +103,7 @@ struct EditMetadataView: View {
|
|||||||
VStack(alignment: .leading) {
|
VStack(alignment: .leading) {
|
||||||
HStack {
|
HStack {
|
||||||
Spacer()
|
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()
|
Spacer()
|
||||||
}
|
}
|
||||||
Form {
|
Form {
|
||||||
|
|||||||
@@ -34,9 +34,12 @@ func pfp_line_width(_ h: Highlight) -> CGFloat {
|
|||||||
|
|
||||||
struct InnerProfilePicView: View {
|
struct InnerProfilePicView: View {
|
||||||
let url: URL?
|
let url: URL?
|
||||||
|
let fallbackUrl: URL?
|
||||||
let pubkey: String
|
let pubkey: String
|
||||||
let size: CGFloat
|
let size: CGFloat
|
||||||
let highlight: Highlight
|
let highlight: Highlight
|
||||||
|
|
||||||
|
@State private var refreshID = UUID().uuidString
|
||||||
|
|
||||||
var PlaceholderColor: Color {
|
var PlaceholderColor: Color {
|
||||||
return id_to_color(pubkey)
|
return id_to_color(pubkey)
|
||||||
@@ -53,11 +56,11 @@ struct InnerProfilePicView: View {
|
|||||||
var body: some View {
|
var body: some View {
|
||||||
ZStack {
|
ZStack {
|
||||||
Color(uiColor: .systemBackground)
|
Color(uiColor: .systemBackground)
|
||||||
|
|
||||||
KFAnimatedImage(url)
|
KFAnimatedImage(url)
|
||||||
.callbackQueue(.dispatch(.global(qos: .background)))
|
.callbackQueue(.dispatch(.global(qos: .background)))
|
||||||
.processingQueue(.dispatch(.global(qos: .background)))
|
.processingQueue(.dispatch(.global(qos: .background)))
|
||||||
.appendProcessor(LargeImageProcessor())
|
.appendProcessor(LargeImageProcessor.shared)
|
||||||
.configure { view in
|
.configure { view in
|
||||||
view.framePreloadCount = 1
|
view.framePreloadCount = 1
|
||||||
}
|
}
|
||||||
@@ -67,10 +70,43 @@ struct InnerProfilePicView: View {
|
|||||||
.scaleFactor(UIScreen.main.scale)
|
.scaleFactor(UIScreen.main.scale)
|
||||||
.loadDiskFileSynchronously()
|
.loadDiskFileSynchronously()
|
||||||
.fade(duration: 0.1)
|
.fade(duration: 0.1)
|
||||||
|
.onFailure { _ in
|
||||||
|
setFallbackImage()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.frame(width: size, height: size)
|
.frame(width: size, height: size)
|
||||||
.clipShape(Circle())
|
.clipShape(Circle())
|
||||||
.overlay(Circle().stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight)))
|
.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 {
|
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
|
.onReceive(handle_notify(.profile_updated)) { notif in
|
||||||
let updated = notif.object as! ProfileUpdate
|
let updated = notif.object as! ProfileUpdate
|
||||||
|
|
||||||
@@ -108,7 +144,9 @@ struct ProfilePicView: View {
|
|||||||
|
|
||||||
struct LargeImageProcessor: ImageProcessor {
|
struct LargeImageProcessor: ImageProcessor {
|
||||||
|
|
||||||
let identifier: String = "com.damus.largeimageprocessor"
|
static let shared = LargeImageProcessor()
|
||||||
|
|
||||||
|
let identifier = "com.damus.largeimageprocessor"
|
||||||
let maxSize: Int = 1000000
|
let maxSize: Int = 1000000
|
||||||
let downsampleSize = CGSize(width: 200, height: 200)
|
let downsampleSize = CGSize(width: 200, height: 200)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user