ProfileName.swift (4727B)
1 // 2 // ProfileName.swift 3 // damus 4 // 5 // Created by William Casarin on 2022-04-16. 6 // 7 8 import SwiftUI 9 10 enum FriendType { 11 case friend 12 case fof 13 14 var priority: Int { 15 switch self { 16 case .friend: return 2 17 case .fof: return 1 18 } 19 } 20 } 21 22 func get_friend_type(contacts: Contacts, pubkey: Pubkey) -> FriendType? { 23 if contacts.is_friend_or_self(pubkey) { 24 return .friend 25 } 26 27 if contacts.is_friend_of_friend(pubkey) { 28 return .fof 29 } 30 31 return nil 32 } 33 34 struct ProfileName: View { 35 let damus_state: DamusState 36 let pubkey: Pubkey 37 let prefix: String 38 39 let show_nip5_domain: Bool 40 private let supporterBadgeStyle: SupporterBadge.Style 41 42 @State var display_name: DisplayName? 43 @State var nip05: NIP05? 44 @State var donation: Int? 45 @State var purple_account: DamusPurple.Account? 46 47 init(pubkey: Pubkey, prefix: String = "", damus: DamusState, show_nip5_domain: Bool = true, supporterBadgeStyle: SupporterBadge.Style = .compact) { 48 self.pubkey = pubkey 49 self.prefix = prefix 50 self.damus_state = damus 51 self.show_nip5_domain = show_nip5_domain 52 self.supporterBadgeStyle = supporterBadgeStyle 53 self.purple_account = nil 54 } 55 56 var friend_type: FriendType? { 57 return get_friend_type(contacts: damus_state.contacts, pubkey: self.pubkey) 58 } 59 60 @MainActor 61 var current_nip05: NIP05? { 62 nip05 ?? damus_state.profiles.is_validated(pubkey) 63 } 64 65 func current_display_name(profile: Profile?) -> DisplayName { 66 return display_name ?? Profile.displayName(profile: profile, pubkey: pubkey) 67 } 68 69 func name_choice(profile: Profile?) -> String { 70 return prefix == "@" ? current_display_name(profile: profile).username.truncate(maxLength: 50) : current_display_name(profile: profile).displayName.truncate(maxLength: 50) 71 } 72 73 func onlyzapper(profile: Profile?) -> Bool { 74 guard let profile else { 75 return false 76 } 77 78 return profile.reactions == false 79 } 80 81 func supporter(profile: Profile?) -> Int? { 82 guard let profile, 83 let donation = profile.damus_donation, 84 donation > 0 85 else { 86 return nil 87 } 88 89 return donation 90 } 91 92 var body: some View { 93 let profile_txn = damus_state.profiles.lookup(id: pubkey) 94 let profile = profile_txn?.unsafeUnownedValue 95 96 HStack(spacing: 2) { 97 Text(verbatim: "\(prefix)\(name_choice(profile: profile))") 98 .font(.body) 99 .fontWeight(prefix == "@" ? .none : .bold) 100 101 if let nip05 = current_nip05 { 102 NIP05Badge(nip05: nip05, pubkey: pubkey, contacts: damus_state.contacts, show_domain: show_nip5_domain, profiles: damus_state.profiles) 103 } 104 105 if let friend = friend_type, current_nip05 == nil { 106 FriendIcon(friend: friend) 107 } 108 109 if onlyzapper(profile: profile) { 110 Image("zap-hashtag") 111 .frame(width: 14, height: 14) 112 } 113 114 SupporterBadge(percent: supporter(profile: profile), purple_account: self.purple_account, style: supporterBadgeStyle) 115 116 117 } 118 .task { 119 if damus_state.purple.enable_purple { 120 self.purple_account = try? await damus_state.purple.get_maybe_cached_account(pubkey: pubkey) 121 } 122 } 123 .onReceive(handle_notify(.profile_updated)) { update in 124 if update.pubkey != pubkey { 125 return 126 } 127 128 var profile: Profile! 129 var profile_txn: NdbTxn<Profile?>! 130 131 switch update { 132 case .remote(let pubkey): 133 profile_txn = damus_state.profiles.lookup(id: pubkey) 134 guard let prof = profile_txn.unsafeUnownedValue else { return } 135 profile = prof 136 case .manual(_, let prof): 137 profile = prof 138 } 139 140 let display_name = Profile.displayName(profile: profile, pubkey: pubkey) 141 if self.display_name != display_name { 142 self.display_name = display_name 143 } 144 145 let nip05 = damus_state.profiles.is_validated(pubkey) 146 if nip05 != self.nip05 { 147 self.nip05 = nip05 148 } 149 150 if donation != profile.damus_donation { 151 donation = profile.damus_donation 152 } 153 } 154 } 155 } 156 157 struct ProfileName_Previews: PreviewProvider { 158 static var previews: some View { 159 ProfileName(pubkey: test_damus_state.pubkey, damus: test_damus_state) 160 } 161 }