diff --git a/damus/Views/Relays/RelayPicView.swift b/damus/Views/Relays/RelayPicView.swift index 40c51af8d..f28a0620c 100644 --- 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 {