damus

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

NIP05Badge.swift (3321B)


      1 //
      2 //  NIP05Badge.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-01-11.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct NIP05Badge: View {
     11     let nip05: NIP05
     12     let pubkey: Pubkey
     13     let contacts: Contacts
     14     let show_domain: Bool
     15     let profiles: Profiles
     16 
     17     @Environment(\.openURL) var openURL
     18     
     19     init(nip05: NIP05, pubkey: Pubkey, contacts: Contacts, show_domain: Bool, profiles: Profiles) {
     20         self.nip05 = nip05
     21         self.pubkey = pubkey
     22         self.contacts = contacts
     23         self.show_domain = show_domain
     24         self.profiles = profiles
     25     }
     26     
     27     var nip05_color: Bool {
     28        return use_nip05_color(pubkey: pubkey, contacts: contacts)
     29     }
     30     
     31     var Seal: some View {
     32         Group {
     33             if nip05_color {
     34                 LINEAR_GRADIENT
     35                     .mask(Image("verified.fill")
     36                         .resizable()
     37                     ).frame(width: 18, height: 18)
     38             } else if show_domain {
     39                 Image("verified")
     40                     .resizable()
     41                     .frame(width: 18, height: 18)
     42                     .nip05_colorized(gradient: nip05_color)
     43             }
     44         }
     45     }
     46 
     47     var username_matches_nip05: Bool {
     48         guard let name = profiles.lookup(id: pubkey)?.map({ p in p?.name }).value
     49         else {
     50             return false
     51         }
     52 
     53         return name.lowercased() == nip05.username.lowercased()
     54     }
     55 
     56     var nip05_string: String {
     57         if nip05.username == "_" || username_matches_nip05 {
     58             return nip05.host
     59         } else {
     60             return "\(nip05.username)@\(nip05.host)"
     61         }
     62     }
     63 
     64     var body: some View {
     65         HStack(spacing: 2) {
     66             Seal
     67 
     68             if show_domain {
     69                 Text(nip05_string)
     70                     .nip05_colorized(gradient: nip05_color)
     71                     .onTapGesture {
     72                         if let nip5url = nip05.siteUrl {
     73                             openURL(nip5url)
     74                         }
     75                     }
     76             }
     77         }
     78 
     79     }
     80 }
     81 
     82 extension View {
     83     func nip05_colorized(gradient: Bool) -> some View {
     84         if gradient {
     85             return AnyView(self.foregroundStyle(LINEAR_GRADIENT))
     86         } else {
     87             return AnyView(self.foregroundColor(.gray))
     88         }
     89         
     90     }
     91 }
     92 
     93 func use_nip05_color(pubkey: Pubkey, contacts: Contacts) -> Bool {
     94     return contacts.is_friend_or_self(pubkey) ? true : false
     95 }
     96 
     97 struct NIP05Badge_Previews: PreviewProvider {
     98     static var previews: some View {
     99         let test_state = test_damus_state
    100         VStack {
    101             NIP05Badge(nip05: NIP05(username: "jb55", host: "jb55.com"), pubkey: test_state.pubkey, contacts: test_state.contacts, show_domain: true, profiles: test_state.profiles)
    102 
    103             NIP05Badge(nip05: NIP05(username: "_", host: "jb55.com"), pubkey: test_state.pubkey, contacts: test_state.contacts, show_domain: true, profiles: test_state.profiles)
    104 
    105             NIP05Badge(nip05: NIP05(username: "jb55", host: "jb55.com"), pubkey: test_state.pubkey, contacts: test_state.contacts, show_domain: true, profiles: test_state.profiles)
    106 
    107             NIP05Badge(nip05: NIP05(username: "jb55", host: "jb55.com"), pubkey: test_state.pubkey, contacts: Contacts(our_pubkey: test_pubkey), show_domain: true, profiles: test_state.profiles)
    108         }
    109     }
    110 }
    111