ProxyView.swift (3395B)
1 // 2 // ProxyView.swift 3 // damus 4 // 5 // Created by eric on 2/3/24. 6 // 7 8 import SwiftUI 9 10 struct ProxyTag { 11 let id: String 12 let protocolName: String 13 14 init(id: String, protocolName: String) { 15 self.id = id 16 self.protocolName = protocolName 17 } 18 } 19 20 struct ProxyView: View { 21 let event: NostrEvent 22 23 @Environment(\.openURL) var openURL 24 25 var body: some View { 26 Group { 27 if let proxy = event_proxy(ev: event) { 28 VStack(alignment: .leading) { 29 Button( 30 action: { 31 if let url = URL(string: proxy.id) { 32 openURL(url) 33 } 34 }, 35 label: { 36 HStack { 37 let protocolLogo = get_protocol_image(protocolName: proxy.protocolName) 38 if protocolLogo.isEmpty { 39 Text(proxy.protocolName) 40 .font(.caption) 41 } else { 42 Image(protocolLogo) 43 .resizable() 44 .scaledToFit() 45 .frame(width: proxy.protocolName == "activitypub" ? 75 : 20, height: proxy.protocolName == "activitypub" ? 20 : 25) 46 } 47 } 48 } 49 ) 50 .buttonStyle(NeutralButtonStyle(padding: EdgeInsets(top: 2, leading: 5, bottom: 2, trailing: 5), cornerRadius: 20)) 51 } 52 } else { 53 EmptyView() 54 } 55 } 56 } 57 } 58 59 func get_protocol_image(protocolName: String) -> String { 60 switch protocolName { 61 case "activitypub": return "activityPub" 62 case "rss": return "rss" 63 case "atproto": return "atproto" 64 case "web": return "globe" 65 default: 66 return "" 67 } 68 } 69 70 func event_proxy(ev: NostrEvent) -> ProxyTag? { 71 var proxyParts = [String]() 72 for tag in ev.tags { 73 if tag.count == 3 && tag[0].matches_str("proxy") { 74 proxyParts = tag.strings() 75 guard proxyParts.count == 3 else { 76 return nil 77 } 78 return ProxyTag(id: proxyParts[1], protocolName: proxyParts[2]) 79 } 80 } 81 return nil 82 } 83 84 85 struct ProxyView_Previews: PreviewProvider { 86 static var previews: some View { 87 let activityPubEv = NostrEvent(content: "", keypair: test_keypair, kind: 1, tags: [["proxy", "", "activitypub"]])! 88 let atProtoEv = NostrEvent(content: "", keypair: test_keypair, kind: 1, tags: [["proxy", "", "atproto"]])! 89 let rssEv = NostrEvent(content: "", keypair: test_keypair, kind: 1, tags: [["proxy", "", "rss"]])! 90 let webEv = NostrEvent(content: "", keypair: test_keypair, kind: 1, tags: [["proxy", "", "web"]])! 91 let unsupportedEv = NostrEvent(content: "", keypair: test_keypair, kind: 1, tags: [["proxy", "", "unsupported"]])! 92 VStack(alignment: .center, spacing: 10) { 93 ProxyView(event: activityPubEv) 94 ProxyView(event: rssEv) 95 ProxyView(event: atProtoEv) 96 ProxyView(event: webEv) 97 ProxyView(event: unsupportedEv) 98 } 99 } 100 }