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