damus

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

EventProfileName.swift (4235B)


      1 //
      2 //  EventProfileName.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-03-14.
      6 //
      7 
      8 import SwiftUI
      9 
     10 /// Profile Name used when displaying an event in the timeline
     11 @MainActor
     12 struct EventProfileName: View {
     13     var damus_state: DamusState
     14     let pubkey: Pubkey
     15 
     16     @State var display_name: DisplayName?
     17     @State var nip05: NIP05?
     18     @State var donation: Int?
     19     @State var purple_account: DamusPurple.Account?
     20 
     21     let size: EventViewKind
     22     
     23     init(pubkey: Pubkey, damus: DamusState, size: EventViewKind = .normal) {
     24         self.damus_state = damus
     25         self.pubkey = pubkey
     26         self.size = size
     27         let donation = damus.ndb.lookup_profile(pubkey)?.map({ p in p?.profile?.damus_donation }).value
     28         self._donation = State(wrappedValue: donation)
     29         self.purple_account = nil
     30     }
     31     
     32     var friend_type: FriendType? {
     33         return get_friend_type(contacts: damus_state.contacts, pubkey: self.pubkey)
     34     }
     35     
     36     var current_nip05: NIP05? {
     37         nip05 ?? damus_state.profiles.is_validated(pubkey)
     38     }
     39     
     40     func current_display_name(_ profile: Profile?) -> DisplayName {
     41         return display_name ?? Profile.displayName(profile: profile, pubkey: pubkey)
     42     }
     43     
     44     func onlyzapper(_ profile: Profile?) -> Bool {
     45         guard let profile else {
     46             return false
     47         }
     48         
     49         return profile.reactions == false
     50     }
     51     
     52     func supporter_percentage() -> Int? {
     53         guard let donation, donation > 0
     54         else {
     55             return nil
     56         }
     57         
     58         return donation
     59     }
     60 
     61     var body: some View {
     62         let profile_txn = damus_state.profiles.lookup(id: pubkey)
     63         let profile = profile_txn?.unsafeUnownedValue
     64         HStack(spacing: 2) {
     65             switch current_display_name(profile) {
     66             case .one(let one):
     67                 Text(one)
     68                     .font(.body.weight(.bold))
     69                 
     70             case .both(username: let username, displayName: let displayName):
     71                     HStack(spacing: 6) {
     72                         Text(verbatim: displayName)
     73                             .font(.body.weight(.bold))
     74                         
     75                         Text(verbatim: "@\(username)")
     76                             .foregroundColor(.gray)
     77                             .font(eventviewsize_to_font(size, font_size: damus_state.settings.font_size))
     78                     }
     79             }
     80             
     81             /*
     82             if let nip05 = current_nip05 {
     83                 NIP05Badge(nip05: nip05, pubkey: pubkey, contacts: damus_state.contacts, show_domain: false, clickable: false)
     84             }
     85              */
     86             
     87              
     88             if let frend = friend_type {
     89                 FriendIcon(friend: frend)
     90             }
     91             
     92             if onlyzapper(profile) {
     93                 Image("zap-hashtag")
     94                     .frame(width: 14, height: 14)
     95             }
     96             
     97             SupporterBadge(percent: self.supporter_percentage(), purple_account: self.purple_account, style: .compact)
     98         }
     99         .onReceive(handle_notify(.profile_updated)) { update in
    100             if update.pubkey != pubkey {
    101                 return
    102             }
    103 
    104             let profile_txn = damus_state.profiles.lookup(id: update.pubkey)
    105             guard let profile = profile_txn?.unsafeUnownedValue else { return }
    106 
    107             let display_name = Profile.displayName(profile: profile, pubkey: pubkey)
    108             if display_name != self.display_name {
    109                 self.display_name = display_name
    110             }
    111 
    112             let nip05 = damus_state.profiles.is_validated(pubkey)
    113 
    114             if self.nip05 != nip05 {
    115                 self.nip05 = nip05
    116             }
    117 
    118             if self.donation != profile.damus_donation {
    119                 donation = profile.damus_donation
    120             }
    121         }
    122         .task {
    123             if damus_state.purple.enable_purple {
    124                 self.purple_account = try? await damus_state.purple.get_maybe_cached_account(pubkey: pubkey)
    125             }
    126         }
    127     }
    128 }
    129 
    130 
    131 struct EventProfileName_Previews: PreviewProvider {
    132     static var previews: some View {
    133         EventProfileName(pubkey: test_note.pubkey, damus: test_damus_state)
    134     }
    135 }