damus

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

commit 4f2bacfaabb12ee297d27c97c3846a94eda7578f
parent 5a8b29b5cc88ea0d11fe96fec98c83cf92887812
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 10 Feb 2023 13:52:27 -0800

Configurable zap amount

Diffstat:
Mdamus/Components/ZapButton.swift | 4++--
Mdamus/Models/UserSettingsStore.swift | 19+++++++++++++------
Mdamus/Util/Zap.swift | 3++-
Mdamus/Views/ConfigView.swift | 14++++++++++++++
MdamusTests/damusTests.swift | 8++++++++
5 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/damus/Components/ZapButton.swift b/damus/Components/ZapButton.swift @@ -52,8 +52,8 @@ struct ZapButton: View { damus_state.lnurls.endpoints[target.pubkey] = payreq } - let tip_amount = get_default_tip_amount(pubkey: damus_state.pubkey) - guard let inv = await fetch_zap_invoice(payreq, zapreq: zapreq, amount: tip_amount) else { + let zap_amount = get_default_zap_amount(pubkey: damus_state.pubkey) ?? 1000 + guard let inv = await fetch_zap_invoice(payreq, zapreq: zapreq, sats: zap_amount) else { DispatchQueue.main.async { zapping = false } diff --git a/damus/Models/UserSettingsStore.swift b/damus/Models/UserSettingsStore.swift @@ -16,15 +16,22 @@ func pk_setting_key(_ pubkey: String, key: String) -> String { return "\(pubkey)_\(key)" } -let tip_amount_key = "default_tip_amount" -func set_default_tip_amount(pubkey: String, amount: Int64) { - let key = pk_setting_key(pubkey, key: tip_amount_key) +func default_zap_setting_key(pubkey: String) -> String { + return pk_setting_key(pubkey, key: "default_zap_amount") +} + +func set_default_zap_amount(pubkey: String, amount: Int) { + let key = default_zap_setting_key(pubkey: pubkey) UserDefaults.standard.setValue(amount, forKey: key) } -func get_default_tip_amount(pubkey: String) -> Int64 { - let key = "\(pubkey)_\(tip_amount_key)" - return UserDefaults.standard.object(forKey: key) as? Int64 ?? 1000000 +func get_default_zap_amount(pubkey: String) -> Int? { + let key = default_zap_setting_key(pubkey: pubkey) + let amt = UserDefaults.standard.integer(forKey: key) + if amt == 0 { + return nil + } + return amt } diff --git a/damus/Util/Zap.swift b/damus/Util/Zap.swift @@ -285,12 +285,13 @@ func fetch_static_payreq(_ lnurl: String) async -> LNUrlPayRequest? { return endpoint } -func fetch_zap_invoice(_ payreq: LNUrlPayRequest, zapreq: NostrEvent, amount: Int64) async -> String? { +func fetch_zap_invoice(_ payreq: LNUrlPayRequest, zapreq: NostrEvent, sats: Int) async -> String? { guard var base_url = payreq.callback.flatMap({ URLComponents(string: $0) }) else { return nil } let zappable = payreq.allowsNostr ?? false + let amount: Int64 = Int64(sats) * 1000 var query = [URLQueryItem(name: "amount", value: "\(amount)")] diff --git a/damus/Views/ConfigView.swift b/damus/Views/ConfigView.swift @@ -21,6 +21,7 @@ struct ConfigView: View { @State var privkey_copied: Bool = false @State var pubkey_copied: Bool = false @State var delete_text: String = "" + @State var default_zap_amount: String @ObservedObject var settings: UserSettingsStore @@ -28,6 +29,8 @@ struct ConfigView: View { init(state: DamusState) { self.state = state + let zap_amt = get_default_zap_amount(pubkey: state.pubkey).map({ "\($0)" }) ?? "1000" + _default_zap_amount = State(initialValue: zap_amt) _privkey = State(initialValue: self.state.keypair.privkey_bech32 ?? "") _settings = ObservedObject(initialValue: state.settings) } @@ -124,6 +127,11 @@ struct ConfigView: View { } } } + + + Section(NSLocalizedString("Default Zap Amount in sats", comment: "Section title for zap configuration")) { + TextField("1000", text: $default_zap_amount) + } Section(NSLocalizedString("Translations", comment: "Section title for selecting the translation service.")) { Picker(NSLocalizedString("Service", comment: "Prompt selection of translation service provider."), selection: $settings.translation_service) { @@ -194,6 +202,12 @@ struct ConfigView: View { } } } + .onChange(of: default_zap_amount) { val in + guard let amt = Int(val) else { + return + } + set_default_zap_amount(pubkey: state.pubkey, amount: amt) + } .navigationTitle(NSLocalizedString("Settings", comment: "Navigation title for Settings view.")) .navigationBarTitleDisplayMode(.large) .alert(NSLocalizedString("Permanently Delete Account", comment: "Alert for deleting the users account."), isPresented: $confirm_delete_account) { diff --git a/damusTests/damusTests.swift b/damusTests/damusTests.swift @@ -88,6 +88,14 @@ class damusTests: XCTestCase { XCTAssertEqual(parsed, expected) } + func testSaveDefaultZapAmount() { + let pubkey = "test_pubkey" + let amt = 1000 + set_default_zap_amount(pubkey: pubkey, amount: amt) + let loaded = get_default_zap_amount(pubkey: pubkey)! + XCTAssertEqual(loaded, amt) + } + func testSaveRelayFilters() { var filters = Set<RelayFilter>()