damus

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

ZapButtonView.swift (3351B)


      1 //
      2 //  ZapButtonView.swift
      3 //  damus
      4 //
      5 //  Created by Daniel D’Aquino on 2023-10-20.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct ZapButtonView<Content: View>: View {
     11     typealias ContentViewFunction = (_ reactions_enabled: Bool, _ lud16: String?, _ lnurl: String?) -> Content
     12     typealias ActionFunction = () -> Void
     13     
     14     let pubkey: Pubkey
     15     @ViewBuilder let label: ContentViewFunction
     16     let action: ActionFunction?
     17     
     18     let reactions_enabled: Bool
     19     let lud16: String?
     20     let lnurl: String?
     21     
     22     init(pubkey: Pubkey, reactions_enabled: Bool, lud16: String?, lnurl: String?, action: ActionFunction? = nil, @ViewBuilder label: @escaping ContentViewFunction) {
     23         self.pubkey = pubkey
     24         self.label = label
     25         self.action = action
     26         self.reactions_enabled = reactions_enabled
     27         self.lud16 = lud16
     28         self.lnurl = lnurl
     29     }
     30     
     31     init(damus_state: DamusState, pubkey: Pubkey, action: ActionFunction? = nil, @ViewBuilder label: @escaping ContentViewFunction) {
     32         self.pubkey = pubkey
     33         self.label = label
     34         self.action = action
     35         
     36         let profile_txn = damus_state.profiles.lookup_with_timestamp(pubkey)
     37         let record = profile_txn.unsafeUnownedValue
     38         self.reactions_enabled = record?.profile?.reactions ?? true
     39         self.lud16 = record?.profile?.lud06
     40         self.lnurl = record?.lnurl
     41     }
     42     
     43     init(unownedProfileRecord: ProfileRecord?, profileModel: ProfileModel, action: ActionFunction? = nil, @ViewBuilder label: @escaping ContentViewFunction) {
     44         self.pubkey = profileModel.pubkey
     45         self.label = label
     46         self.action = action
     47         
     48         self.reactions_enabled = unownedProfileRecord?.profile?.reactions ?? true
     49         self.lud16 = unownedProfileRecord?.profile?.lud16
     50         self.lnurl = unownedProfileRecord?.lnurl
     51     }
     52     
     53     var body: some View {
     54         Button(
     55             action: {
     56                 if let lnurl {
     57                     present_sheet(.zap(target: .profile(self.pubkey), lnurl: lnurl))
     58                 }
     59                 action?()
     60             },
     61             label: {
     62                 self.label(self.reactions_enabled, self.lud16, self.lnurl)
     63             }
     64         )
     65         .contextMenu {
     66             if self.reactions_enabled == false {
     67                 Text("OnlyZaps Enabled", comment: "Non-tappable text in context menu that shows up when the zap button on profile is long pressed to indicate that the user has enabled OnlyZaps, meaning that they would like to be only zapped and not accept reactions to their notes.")
     68             }
     69 
     70             if let lud16 {
     71                 Button {
     72                     UIPasteboard.general.string = lud16
     73                 } label: {
     74                     Label(lud16, image: "copy2")
     75                 }
     76             } else {
     77                 Button {
     78                     UIPasteboard.general.string = lnurl
     79                 } label: {
     80                     Label(NSLocalizedString("Copy LNURL", comment: "Context menu option for copying a user's Lightning URL."), image: "copy")
     81                 }
     82             }
     83         }
     84         .disabled(lnurl == nil)
     85     }
     86 }
     87 
     88 #Preview {
     89     ZapButtonView(pubkey: test_pubkey, reactions_enabled: true, lud16: make_test_profile().lud16, lnurl: "test@sendzaps.lol", label: { reactions_enabled, lud16, lnurl in
     90         Image("zap.fill")
     91     })
     92 }