damus

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

commit 6a88ca27772d967bcb5ec5589f363bfa9e6b3450
parent e3ccf95780a3ae4b1e6980a7d8bbc53edfdfbc10
Author: William Casarin <jb55@jb55.com>
Date:   Mon, 11 Sep 2023 14:00:10 -0700

relays: fix crash in new RelayPicView

Diffstat:
Mdamus/Views/Relays/RelayPicView.swift | 75++++++++++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 44 insertions(+), 31 deletions(-)

diff --git a/damus/Views/Relays/RelayPicView.swift b/damus/Views/Relays/RelayPicView.swift @@ -9,43 +9,55 @@ import SwiftUI import Kingfisher import TLDExtract +struct FailedRelayImage: View { + let url: URL? + + var body: some View { + let abbrv = String(url?.hostname?.first?.uppercased() ?? "R") + Text("\(abbrv)") + .font(.system(size: 40, weight: .bold)) + } +} + struct InnerRelayPicView: View { let url: URL? let size: CGFloat let highlight: Highlight let disable_animation: Bool @State var failedImage: Bool = false - - var Placeholder: some View { - RoundedRectangle(cornerRadius: 15) - .frame(width: size, height: size) - .foregroundColor(DamusColors.adaptableGrey) - .overlay(RoundedRectangle(cornerRadius: 15).stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight))) - .padding(2) + + func Placeholder(url: URL?) -> some View { + ZStack { + RoundedRectangle(cornerRadius: 15) + .frame(width: size, height: size) + .foregroundColor(DamusColors.adaptableGrey) + .overlay(RoundedRectangle(cornerRadius: 15).stroke(highlight_color(highlight), lineWidth: pfp_line_width(highlight))) + .padding(2) + + FailedRelayImage(url: url) + } } var body: some View { ZStack { Color(uiColor: .secondarySystemBackground) - KFAnimatedImage(url) - .imageContext(.pfp, disable_animation: disable_animation) - .onFailure({ result in - failedImage = true - }) - .cancelOnDisappear(true) - .configure { view in - view.framePreloadCount = 3 - } - .placeholder { _ in - Placeholder - } - .scaledToFit() - - if failedImage { - let abbrv = String(url?.hostname?.first?.uppercased() ?? "R") - Text("\(abbrv)") - .font(.system(size: 40, weight: .bold)) + if let url { + KFAnimatedImage(url) + .imageContext(.pfp, disable_animation: disable_animation) + .onFailure { _ in + failedImage = true + } + .cancelOnDisappear(true) + .configure { view in + view.framePreloadCount = 3 + } + .placeholder { _ in + Placeholder(url: url) + } + .scaledToFit() + } else { + FailedRelayImage(url: nil) } } .frame(width: size, height: size) @@ -68,23 +80,24 @@ struct RelayPicView: View { self.highlight = highlight self.disable_animation = disable_animation } + + var relay_url: URL? { + get_relay_url(relay: relay, icon: icon) + } var body: some View { - InnerRelayPicView(url: get_relay_url(relay: relay, icon: icon), size: size, highlight: highlight, disable_animation: disable_animation) + InnerRelayPicView(url: relay_url, size: size, highlight: highlight, disable_animation: disable_animation) } } -func get_relay_url(relay: String, icon: String?) -> URL { +func get_relay_url(relay: String, icon: String?) -> URL? { let extractor = TLDExtract() var favicon = relay + "/favicon.ico" if let parseRelay: TLDResult = extractor.parse(relay) { favicon = "https://" + (parseRelay.rootDomain ?? relay) + "/favicon.ico" } let pic = icon ?? favicon - if let url = URL(string: pic) { - return url - } - return URL(string: "")! + return URL(string: pic) } struct RelayPicView_Previews: PreviewProvider {