damus

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

commit c4f41220e5875fbe72454319200eae98bc7c0a75
parent 0f119d34e66073db83be4ad12893df8a03880c9c
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 23 Apr 2023 09:54:38 -0700

refactor: extract ZapTypePicker into its own file

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 4++++
Mdamus/Views/Zaps/CustomizeZapView.swift | 116+++++++++++++++++++------------------------------------------------------------
Adamus/Views/Zaps/ZapTypePicker.swift | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 125 insertions(+), 88 deletions(-)

diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -155,6 +155,7 @@ 4C9F18E229AA9B6C008C55EC /* CustomizeZapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9F18E129AA9B6C008C55EC /* CustomizeZapView.swift */; }; 4C9F18E429ABDE6D008C55EC /* MaybeAnonPfpView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C9F18E329ABDE6D008C55EC /* MaybeAnonPfpView.swift */; }; 4CA2EFA0280E37AC0044ACD8 /* TimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA2EF9F280E37AC0044ACD8 /* TimelineView.swift */; }; + 4CA3FA1029F593D000FDB3C3 /* ZapTypePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA3FA0F29F593D000FDB3C3 /* ZapTypePicker.swift */; }; 4CA5588329F33F5B00DC6A45 /* StringCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA5588229F33F5B00DC6A45 /* StringCodable.swift */; }; 4CAAD8AD298851D000060CEA /* AccountDeletion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CAAD8AC298851D000060CEA /* AccountDeletion.swift */; }; 4CAAD8B029888AD200060CEA /* RelayConfigView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CAAD8AF29888AD200060CEA /* RelayConfigView.swift */; }; @@ -564,6 +565,7 @@ 4C9F18E129AA9B6C008C55EC /* CustomizeZapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomizeZapView.swift; sourceTree = "<group>"; }; 4C9F18E329ABDE6D008C55EC /* MaybeAnonPfpView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaybeAnonPfpView.swift; sourceTree = "<group>"; }; 4CA2EF9F280E37AC0044ACD8 /* TimelineView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimelineView.swift; sourceTree = "<group>"; }; + 4CA3FA0F29F593D000FDB3C3 /* ZapTypePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZapTypePicker.swift; sourceTree = "<group>"; }; 4CA5588229F33F5B00DC6A45 /* StringCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringCodable.swift; sourceTree = "<group>"; }; 4CAAD8AC298851D000060CEA /* AccountDeletion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountDeletion.swift; sourceTree = "<group>"; }; 4CAAD8AF29888AD200060CEA /* RelayConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelayConfigView.swift; sourceTree = "<group>"; }; @@ -1253,6 +1255,7 @@ children = ( 4CE879572996C45300F758CC /* ZapsView.swift */, 4C9F18E129AA9B6C008C55EC /* CustomizeZapView.swift */, + 4CA3FA0F29F593D000FDB3C3 /* ZapTypePicker.swift */, ); path = Zaps; sourceTree = "<group>"; @@ -1573,6 +1576,7 @@ 4CFF8F6729CC9E3A008DB934 /* ImageView.swift in Sources */, 4C90BD18283A9EE5008EE7EF /* LoginView.swift in Sources */, 4CB8838B296F6E1E00DC99E7 /* NIP05Badge.swift in Sources */, + 4CA3FA1029F593D000FDB3C3 /* ZapTypePicker.swift in Sources */, 4C3EA66828FF5F9900C48A62 /* hex.c in Sources */, E9E4ED0B295867B900DD7078 /* ThreadView.swift in Sources */, 4CD348EF29C3659D00497EB2 /* ImageUploadModel.swift in Sources */, diff --git a/damus/Views/Zaps/CustomizeZapView.swift b/damus/Views/Zaps/CustomizeZapView.swift @@ -8,13 +8,6 @@ import SwiftUI import Combine -enum ZapType { - case pub - case anon - case priv - case non_zap -} - struct ZapAmountItem: Identifiable, Hashable { let amount: Int let icon: String @@ -88,85 +81,6 @@ struct CustomizeZapView: View { self.state = state } - func zap_type_desc(type: ZapType) -> String { - switch type { - case .pub: - 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.") - case .anon: - 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.") - case .priv: - let pk = event.pubkey - let prof = state.profiles.lookup(id: pk) - let name = Profile.displayName(profile: prof, pubkey: pk).username - 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) - case .non_zap: - 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.") - } - } - - var ZapTypePicker: some View { - VStack(spacing: 20) { - Text("Zap type") - .font(.system(size: 18, weight: .heavy)) - ZapTypeSelection(text: "Public", comment: "Picker option to indicate that a zap should be sent publicly and identify the user as who sent it.", img: "person.2.circle.fill", action: {zap_type = ZapType.pub}, type: ZapType.pub) - ZapTypeSelection(text: "Private", comment: "Picker option to indicate that a zap should be sent privately and not identify the user to the public.", img: "lock.circle.fill", action: {zap_type = ZapType.priv}, type: ZapType.priv) - ZapTypeSelection(text: "Anonymous", comment: "Picker option to indicate that a zap should be sent anonymously and not identify the user as who sent it.", img: "person.crop.circle.fill.badge.questionmark", action: {zap_type = ZapType.anon}, type: ZapType.anon) - ZapTypeSelection(text: "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: "bolt.circle.fill", action: {zap_type = ZapType.non_zap}, type: ZapType.non_zap) - } - .padding(.horizontal) - } - - func ZapTypeSelection(text: LocalizedStringKey, comment: StaticString, img: String, action: @escaping () -> (), type: ZapType) -> some View { - Button(action: action) { - VStack(alignment: .leading, spacing: 5) { - HStack { - Image(systemName: img) - .foregroundColor(.gray) - .font(.system(size: 24)) - Text(text, comment: comment) - .font(.system(size: 20, weight: .semibold)) - Spacer() - } - .padding(.horizontal) - Text(zap_type_desc(type: type)) - .padding(.horizontal) - .foregroundColor(.gray) - .font(.system(size: 16)) - } - } - .frame(minWidth: 400, maxWidth: .infinity, minHeight: 50, maxHeight: 70) - .foregroundColor(fontColor()) - .background(zap_type == type ? fillColor() : DamusColors.adaptableGrey) - .cornerRadius(15) - .overlay(RoundedRectangle(cornerRadius: 15) - .stroke(DamusColors.purple.opacity(zap_type == type ? 1.0 : 0.0), lineWidth: 2)) - } - - func ZapTypeButton() -> some View { - Button(action: { - show_zap_types = true - }) { - switch zap_type { - case .pub: - Image(systemName: "person.2") - Text("Public") - case .anon: - Image(systemName: "person.fill.questionmark") - Text("Anonymous") - case .priv: - Image(systemName: "lock") - Text("Private") - case .non_zap: - Image(systemName: "bolt") - Text("None") - } - } - .font(.headline) - .foregroundColor(fontColor()) - .padding(EdgeInsets(top: 5, leading: 15, bottom: 5, trailing: 15)) - .background(DamusColors.adaptableGrey) - .cornerRadius(15) - } func amount_parts(_ n: Int) -> [ZapAmountItem] { var i: Int = -1 @@ -331,6 +245,32 @@ struct CustomizeZapView: View { hideKeyboard() } } + + func ZapTypeButton() -> some View { + Button(action: { + show_zap_types = true + }) { + switch zap_type { + case .pub: + Image(systemName: "person.2") + Text("Public") + case .anon: + Image(systemName: "person.fill.questionmark") + Text("Anonymous") + case .priv: + Image(systemName: "lock") + Text("Private") + case .non_zap: + Image(systemName: "bolt") + Text("None") + } + } + .font(.headline) + .foregroundColor(fontColor()) + .padding(EdgeInsets(top: 5, leading: 15, bottom: 5, trailing: 15)) + .background(DamusColors.adaptableGrey) + .cornerRadius(15) + } var CustomZap: some View { VStack(alignment: .center, spacing: 20) { @@ -354,11 +294,11 @@ struct CustomizeZapView: View { } .sheet(isPresented: $show_zap_types) { if #available(iOS 16.0, *) { - ZapTypePicker + ZapTypePicker(zap_type: $zap_type, profiles: state.profiles, pubkey: event.pubkey) .presentationDetents([.medium]) .presentationDragIndicator(.visible) } else { - ZapTypePicker + ZapTypePicker(zap_type: $zap_type, profiles: state.profiles, pubkey: event.pubkey) } } } diff --git a/damus/Views/Zaps/ZapTypePicker.swift b/damus/Views/Zaps/ZapTypePicker.swift @@ -0,0 +1,93 @@ +// +// ZapTypePicker.swift +// damus +// +// Created by William Casarin on 2023-04-23. +// + +import SwiftUI + +enum ZapType { + case pub + case anon + case priv + case non_zap +} + +struct ZapTypePicker: View { + @Binding var zap_type: ZapType + let profiles: Profiles + let pubkey: String + + @Environment(\.colorScheme) var colorScheme + + func fillColor() -> Color { + colorScheme == .light ? DamusColors.white : DamusColors.black + } + + func fontColor() -> Color { + colorScheme == .light ? DamusColors.black : DamusColors.white + } + + var body: some View { + VStack(spacing: 20) { + Text("Zap type") + .font(.system(size: 18, weight: .heavy)) + ZapTypeSelection(text: "Public", comment: "Picker option to indicate that a zap should be sent publicly and identify the user as who sent it.", img: "person.2.circle.fill", action: {zap_type = ZapType.pub}, type: ZapType.pub) + ZapTypeSelection(text: "Private", comment: "Picker option to indicate that a zap should be sent privately and not identify the user to the public.", img: "lock.circle.fill", action: {zap_type = ZapType.priv}, type: ZapType.priv) + ZapTypeSelection(text: "Anonymous", comment: "Picker option to indicate that a zap should be sent anonymously and not identify the user as who sent it.", img: "person.crop.circle.fill.badge.questionmark", action: {zap_type = ZapType.anon}, type: ZapType.anon) + ZapTypeSelection(text: "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: "bolt.circle.fill", action: {zap_type = ZapType.non_zap}, type: ZapType.non_zap) + } + .padding(.horizontal) + } + + func ZapTypeSelection(text: LocalizedStringKey, comment: StaticString, img: String, action: @escaping () -> (), type: ZapType) -> some View { + Button(action: action) { + VStack(alignment: .leading, spacing: 5) { + HStack { + Image(systemName: img) + .foregroundColor(.gray) + .font(.system(size: 24)) + Text(text, comment: comment) + .font(.system(size: 20, weight: .semibold)) + Spacer() + } + .padding(.horizontal) + Text(zap_type_desc(type: type, profiles: profiles, pubkey: pubkey)) + .padding(.horizontal) + .foregroundColor(.gray) + .font(.system(size: 16)) + } + } + .frame(minWidth: 400, maxWidth: .infinity, minHeight: 50, maxHeight: 70) + .foregroundColor(fontColor()) + .background(zap_type == type ? fillColor() : DamusColors.adaptableGrey) + .cornerRadius(15) + .overlay(RoundedRectangle(cornerRadius: 15) + .stroke(DamusColors.purple.opacity(zap_type == type ? 1.0 : 0.0), lineWidth: 2)) + } +} + +struct ZapTypePicker_Previews: PreviewProvider { + @State static var zap_type: ZapType = .pub + static var previews: some View { + let ds = test_damus_state() + ZapTypePicker(zap_type: $zap_type, profiles: ds.profiles, pubkey: "bob") + } +} + +func zap_type_desc(type: ZapType, profiles: Profiles, pubkey: String) -> String { + switch type { + case .pub: + 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.") + case .anon: + 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.") + case .priv: + let prof = profiles.lookup(id: pubkey) + let name = Profile.displayName(profile: prof, pubkey: pubkey).username + 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) + case .non_zap: + 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.") + } +} +