Skip to content

Commit

Permalink
relays: fix crash in new RelayPicView
Browse files Browse the repository at this point in the history
  • Loading branch information
jb55 committed Sep 11, 2023
1 parent e3ccf95 commit 6a88ca2
Showing 1 changed file with 44 additions and 31 deletions.
75 changes: 44 additions & 31 deletions damus/Views/Relays/RelayPicView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 6a88ca2

Please sign in to comment.