PubkeyView.swift (3094B)
1 // 2 // PubkeyView.swift 3 // damus 4 // 5 // Created by William Casarin on 2022-05-04. 6 // 7 8 import SwiftUI 9 10 struct PubkeyView: View { 11 let pubkey: Pubkey 12 13 @Environment(\.colorScheme) var colorScheme 14 15 @State private var isCopied = false 16 17 func keyColor() -> Color { 18 colorScheme == .light ? DamusColors.black : DamusColors.white 19 } 20 21 private func copyPubkey(_ pubkey: String) { 22 UIPasteboard.general.string = pubkey 23 UIImpactFeedbackGenerator(style: .medium).impactOccurred() 24 withAnimation { 25 isCopied = true 26 DispatchQueue.main.asyncAfter(deadline: .now() + 3) { 27 withAnimation { 28 isCopied = false 29 } 30 } 31 } 32 } 33 34 func pubkey_context_menu(pubkey: Pubkey) -> some View { 35 return self.contextMenu { 36 Button { 37 UIPasteboard.general.string = pubkey.npub 38 } label: { 39 Label(NSLocalizedString("Copy Account ID", comment: "Context menu option for copying the ID of the account that created the note."), image: "copy2") 40 } 41 } 42 } 43 44 var body: some View { 45 let bech32 = pubkey.npub 46 47 HStack { 48 Text(verbatim: "\(abbrev_pubkey(bech32, amount: 16))") 49 .font(.footnote) 50 .foregroundColor(keyColor()) 51 .padding(5) 52 .padding([.leading], 5) 53 54 HStack { 55 if isCopied { 56 Image("check-circle") 57 .resizable() 58 .foregroundColor(DamusColors.green) 59 .frame(width: 20, height: 20) 60 Text(NSLocalizedString("Copied", comment: "Label indicating that a user's key was copied.")) 61 .font(.footnote) 62 .layoutPriority(1) 63 .foregroundColor(DamusColors.green) 64 } else { 65 Button { 66 copyPubkey(bech32) 67 } label: { 68 Label { 69 Text("Public key", comment: "Label indicating that the text is a user's public account key.") 70 } icon: { 71 Image("copy2") 72 .resizable() 73 .contentShape(Rectangle()) 74 .foregroundColor(colorScheme == .light ? DamusColors.darkGrey : DamusColors.lightGrey) 75 .frame(width: 20, height: 20) 76 } 77 .labelStyle(IconOnlyLabelStyle()) 78 .symbolRenderingMode(.hierarchical) 79 80 } 81 } 82 } 83 .padding([.trailing], 10) 84 } 85 .background(RoundedRectangle(cornerRadius: 11).foregroundColor(colorScheme == .light ? DamusColors.adaptableGrey : DamusColors.neutral1)) 86 } 87 } 88 89 #Preview { 90 PubkeyView(pubkey: test_pubkey) 91 }