ZapTypePicker.swift (5277B)
1 // 2 // ZapTypePicker.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-04-23. 6 // 7 8 import SwiftUI 9 10 struct ZapTypePicker: View { 11 @Binding var zap_type: ZapType 12 @ObservedObject var settings: UserSettingsStore 13 let profiles: Profiles 14 let pubkey: Pubkey 15 16 @Environment(\.colorScheme) var colorScheme 17 18 func fillColor() -> Color { 19 colorScheme == .light ? DamusColors.white : DamusColors.black 20 } 21 22 func fontColor() -> Color { 23 colorScheme == .light ? DamusColors.black : DamusColors.white 24 } 25 26 var is_default: Bool { 27 zap_type == settings.default_zap_type 28 } 29 30 var body: some View { 31 VStack(spacing: 20) { 32 HStack { 33 Text("Zap type", comment: "Text to indicate that the buttons below it is for choosing the type of zap to send.") 34 .font(.system(size: 25, weight: .heavy)) 35 Spacer() 36 if !is_default { 37 Button(action: { 38 settings.default_zap_type = zap_type 39 }) { 40 Label(NSLocalizedString("Make Default", comment: "Button label to indicate that tapping it will make the selected zap type be the default for future zaps."), image: "checkmark.circle.fill") 41 } 42 } 43 } 44 ZapTypeSelection(text: NSLocalizedString("Public", comment: "Picker option to indicate that a zap should be sent publicly and identify the user as who sent it."), img: "globe", action: {zap_type = ZapType.pub}, type: ZapType.pub) 45 ZapTypeSelection(text: NSLocalizedString("Private", comment: "Picker option to indicate that a zap should be sent privately and not identify the user to the public."), img: "lock", action: {zap_type = ZapType.priv}, type: ZapType.priv) 46 ZapTypeSelection(text: NSLocalizedString("Anonymous", comment: "Picker option to indicate that a zap should be sent anonymously and not identify the user as who sent it."), img: "question", action: {zap_type = ZapType.anon}, type: ZapType.anon) 47 ZapTypeSelection(text: NSLocalizedString("None", comment: "Picker option to indicate that sats should be sent to the user's wallet as a regular Lightning payment, not as a zap."), img: "zap", action: {zap_type = ZapType.non_zap}, type: ZapType.non_zap) 48 } 49 .padding(.horizontal) 50 } 51 52 func ZapTypeSelection(text: String, img: String, action: @escaping () -> (), type: ZapType) -> some View { 53 Button(action: action) { 54 VStack(alignment: .leading, spacing: 5) { 55 HStack { 56 Image(img) 57 .resizable() 58 .aspectRatio(contentMode: .fit) 59 .frame(width: 20, height: 20) 60 .foregroundColor(.gray) 61 62 Text(text) 63 .font(.system(size: 20, weight: .semibold)) 64 65 Spacer() 66 } 67 .padding(.horizontal) 68 69 let zapTypeDescription = zap_type_desc(type: type, profiles: profiles, pubkey: pubkey) 70 Text(zapTypeDescription) 71 .padding(.horizontal) 72 .foregroundColor(.gray) 73 .font(.system(size: 16)) 74 } 75 } 76 .frame(maxWidth: .infinity, minHeight: 50, maxHeight: 70) 77 .foregroundColor(fontColor()) 78 .background(zap_type == type ? fillColor() : DamusColors.adaptableGrey) 79 .cornerRadius(15) 80 .overlay(RoundedRectangle(cornerRadius: 15) 81 .stroke(DamusColors.purple.opacity(zap_type == type ? 1.0 : 0.0), lineWidth: 2)) 82 } 83 } 84 85 struct ZapTypePicker_Previews: PreviewProvider { 86 @State static var zap_type: ZapType = .pub 87 static var previews: some View { 88 let ds = test_damus_state 89 ZapTypePicker(zap_type: $zap_type, settings: ds.settings, profiles: ds.profiles, pubkey: test_pubkey) 90 } 91 } 92 93 func zap_type_desc(type: ZapType, profiles: Profiles, pubkey: Pubkey) -> String { 94 switch type { 95 case .pub: 96 return NSLocalizedString("Everyone will see that you zapped", comment: "Description of public zap type where the zap is sent publicly and identifies the user who sent it.") 97 case .anon: 98 return NSLocalizedString("No one will see that you zapped", comment: "Description of anonymous zap type where the zap is sent anonymously and does not identify the user who sent it.") 99 case .priv: 100 let prof_txn = profiles.lookup(id: pubkey) 101 let prof = prof_txn?.unsafeUnownedValue 102 let name = Profile.displayName(profile: prof, pubkey: pubkey).username.truncate(maxLength: 50) 103 return String.localizedStringWithFormat(NSLocalizedString("private_zap_description", value: "Only '%@' will see that you zapped them", comment: "Description of private zap type where the zap is sent privately and does not identify the user to the public."), name) 104 case .non_zap: 105 return NSLocalizedString("No zaps will be sent, only a lightning payment.", comment: "Description of non-zap type where sats are sent to the user's wallet as a regular Lightning payment, not as a zap.") 106 } 107 } 108