ProfileName.swift (3351B)
1 // 2 // ProfileName.swift 3 // damus 4 // 5 // Created by William Casarin on 2022-04-16. 6 // 7 8 import SwiftUI 9 10 func get_friend_icon(contacts: Contacts, pubkey: String, show_confirmed: Bool) -> String? { 11 if !show_confirmed { 12 return nil 13 } 14 15 if contacts.is_friend_or_self(pubkey) { 16 return "person.fill.checkmark" 17 } 18 19 if contacts.is_friend_of_friend(pubkey) { 20 return "person.fill.and.arrow.left.and.arrow.right" 21 } 22 23 return nil 24 } 25 26 struct ProfileName: View { 27 let damus_state: DamusState 28 let pubkey: String 29 let profile: Profile? 30 let prefix: String 31 32 let show_friend_confirmed: Bool 33 let show_nip5_domain: Bool 34 35 @State var display_name: DisplayName? 36 @State var nip05: NIP05? 37 38 init(pubkey: String, profile: Profile?, damus: DamusState, show_friend_confirmed: Bool, show_nip5_domain: Bool = true) { 39 self.pubkey = pubkey 40 self.profile = profile 41 self.prefix = "" 42 self.show_friend_confirmed = show_friend_confirmed 43 self.show_nip5_domain = show_nip5_domain 44 self.damus_state = damus 45 } 46 47 init(pubkey: String, profile: Profile?, prefix: String, damus: DamusState, show_friend_confirmed: Bool, show_nip5_domain: Bool = true) { 48 self.pubkey = pubkey 49 self.profile = profile 50 self.prefix = prefix 51 self.damus_state = damus 52 self.show_friend_confirmed = show_friend_confirmed 53 self.show_nip5_domain = show_nip5_domain 54 } 55 56 var friend_icon: String? { 57 return get_friend_icon(contacts: damus_state.contacts, pubkey: pubkey, show_confirmed: show_friend_confirmed) 58 } 59 60 var current_nip05: NIP05? { 61 nip05 ?? damus_state.profiles.is_validated(pubkey) 62 } 63 64 var nip05_color: Color { 65 return get_nip05_color(pubkey: pubkey, contacts: damus_state.contacts) 66 } 67 68 var current_display_name: DisplayName { 69 return display_name ?? Profile.displayName(profile: profile, pubkey: pubkey) 70 } 71 72 var name_choice: String { 73 return prefix == "@" ? current_display_name.username : current_display_name.display_name 74 } 75 76 var body: some View { 77 HStack(spacing: 2) { 78 Text(verbatim: "\(prefix)\(name_choice)") 79 .font(.body) 80 .fontWeight(prefix == "@" ? .none : .bold) 81 if let nip05 = current_nip05 { 82 NIP05Badge(nip05: nip05, pubkey: pubkey, contacts: damus_state.contacts, show_domain: show_nip5_domain, clickable: true) 83 } 84 if let friend = friend_icon, current_nip05 == nil { 85 Image(systemName: friend) 86 .foregroundColor(.gray) 87 } 88 } 89 .onReceive(handle_notify(.profile_updated)) { notif in 90 let update = notif.object as! ProfileUpdate 91 if update.pubkey != pubkey { 92 return 93 } 94 display_name = Profile.displayName(profile: update.profile, pubkey: pubkey) 95 nip05 = damus_state.profiles.is_validated(pubkey) 96 } 97 } 98 } 99 100 struct ProfileName_Previews: PreviewProvider { 101 static var previews: some View { 102 ProfileName(pubkey: 103 test_damus_state().pubkey, profile: make_test_profile(), damus: test_damus_state(), show_friend_confirmed: true) 104 } 105 }