damus

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

commit 055b13c1cddcab01263b96758565aaa90fda6dbd
parent 357e8adf86199bd909c1e0d0e35568d1492e47c4
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 22 Apr 2023 12:10:10 -0700

Fix buggy zap amounts and wallet selector settings

Changelog-Fixed: Fix buggy zap amounts and wallet selector settings

Diffstat:
Mdamus/Components/InvoiceView.swift | 10++++++----
Mdamus/Components/InvoicesView.swift | 5+++--
Mdamus/Components/ZapButton.swift | 9+++++----
Mdamus/Models/UserSettingsStore.swift | 76+++++-----------------------------------------------------------------------
Mdamus/Views/NoteContentView.swift | 2+-
Mdamus/Views/Profile/ProfileView.swift | 2+-
Mdamus/Views/SelectWalletView.swift | 6+++---
Mdamus/Views/Settings/ZapSettingsView.swift | 7+++----
Mdamus/Views/Zaps/CustomizeZapView.swift | 18+++++++++---------
9 files changed, 36 insertions(+), 99 deletions(-)

diff --git a/damus/Components/InvoiceView.swift b/damus/Components/InvoiceView.swift @@ -14,6 +14,7 @@ struct InvoiceView: View { let invoice: Invoice @State var showing_select_wallet: Bool = false @State var copied = false + let settings: UserSettingsStore var CopyButton: some View { Button { @@ -36,10 +37,10 @@ struct InvoiceView: View { var PayButton: some View { Button { - if should_show_wallet_selector(our_pubkey) { + if settings.show_wallet_selector { showing_select_wallet = true } else { - open_with_wallet(wallet: get_default_wallet(our_pubkey).model, invoice: invoice.string) + open_with_wallet(wallet: settings.default_wallet.model, invoice: invoice.string) } } label: { RoundedRectangle(cornerRadius: 20, style: .circular) @@ -80,7 +81,7 @@ struct InvoiceView: View { .padding(30) } .sheet(isPresented: $showing_select_wallet, onDismiss: {showing_select_wallet = false}) { - SelectWalletView(showingSelectWallet: $showing_select_wallet, our_pubkey: our_pubkey, invoice: invoice.string) + SelectWalletView(default_wallet: settings.default_wallet, showingSelectWallet: $showing_select_wallet, our_pubkey: our_pubkey, invoice: invoice.string) } } } @@ -111,7 +112,8 @@ let test_invoice = Invoice(description: .description("this is a description"), a struct InvoiceView_Previews: PreviewProvider { static var previews: some View { - InvoiceView(our_pubkey: "", invoice: test_invoice) + InvoiceView(our_pubkey: "", invoice: test_invoice, settings: test_damus_state().settings) .frame(width: 300, height: 200) } } + diff --git a/damus/Components/InvoicesView.swift b/damus/Components/InvoicesView.swift @@ -10,6 +10,7 @@ import SwiftUI struct InvoicesView: View { let our_pubkey: String var invoices: [Invoice] + let settings: UserSettingsStore @State var open_sheet: Bool = false @State var current_invoice: Invoice? = nil @@ -17,7 +18,7 @@ struct InvoicesView: View { var body: some View { TabView { ForEach(invoices, id: \.string) { invoice in - InvoiceView(our_pubkey: our_pubkey, invoice: invoice) + InvoiceView(our_pubkey: our_pubkey, invoice: invoice, settings: settings) .tabItem { Text(invoice.string) } @@ -31,7 +32,7 @@ struct InvoicesView: View { struct InvoicesView_Previews: PreviewProvider { static var previews: some View { - InvoicesView(our_pubkey: "", invoices: [Invoice.init(description: .description("description"), amount: .specific(10000), string: "invstr", expiry: 100000, payment_hash: Data(), created_at: 1000000)]) + InvoicesView(our_pubkey: "", invoices: [Invoice.init(description: .description("description"), amount: .specific(10000), string: "invstr", expiry: 100000, payment_hash: Data(), created_at: 1000000)], settings: test_damus_state().settings) .frame(width: 300) } } diff --git a/damus/Components/ZapButton.swift b/damus/Components/ZapButton.swift @@ -101,7 +101,7 @@ struct ZapButton: View { CustomizeZapView(state: damus_state, event: event, lnurl: lnurl) } .sheet(isPresented: $showing_select_wallet, onDismiss: {showing_select_wallet = false}) { - SelectWalletView(showingSelectWallet: $showing_select_wallet, our_pubkey: damus_state.pubkey, invoice: invoice) + SelectWalletView(default_wallet: damus_state.settings.default_wallet, showingSelectWallet: $showing_select_wallet, our_pubkey: damus_state.pubkey, invoice: invoice) } .onReceive(handle_notify(.zapping)) { notif in let zap_ev = notif.object as! ZappingEvent @@ -118,11 +118,12 @@ struct ZapButton: View { case .failed: break case .got_zap_invoice(let inv): - if should_show_wallet_selector(damus_state.pubkey) { + if damus_state.settings.show_wallet_selector { self.invoice = inv self.showing_select_wallet = true } else { - open_with_wallet(wallet: get_default_wallet(damus_state.pubkey).model, invoice: inv) + let wallet = damus_state.settings.default_wallet.model + open_with_wallet(wallet: wallet, invoice: inv) } } @@ -173,7 +174,7 @@ func send_zap(damus_state: DamusState, event: NostrEvent, lnurl: String, is_cust damus_state.lnurls.endpoints[target.pubkey] = payreq } - let zap_amount = amount_sats ?? get_default_zap_amount(pubkey: damus_state.pubkey) + let zap_amount = amount_sats ?? damus_state.settings.default_zap_amount guard let inv = await fetch_zap_invoice(payreq, zapreq: zapreq, sats: zap_amount, zap_type: zap_type, comment: comment) else { DispatchQueue.main.async { diff --git a/damus/Models/UserSettingsStore.swift b/damus/Models/UserSettingsStore.swift @@ -9,6 +9,8 @@ import Foundation import Vault import UIKit +let fallback_zap_amount = 1000 + @propertyWrapper struct Setting<T: Equatable> { private let key: String private var value: T @@ -92,6 +94,9 @@ class UserSettingsStore: ObservableObject { @Setting(key: "zap_notification", default_value: true) var zap_notification: Bool + @Setting(key: "default_zap_amount", default_value: fallback_zap_amount) + var default_zap_amount: Int + @Setting(key: "mention_notification", default_value: true) var mention_notification: Bool @@ -226,78 +231,7 @@ struct DamusDeepLKeychainConfiguration: KeychainConfiguration { var accountName = "deepl_apikey" } -func should_show_wallet_selector(_ pubkey: String) -> Bool { - return UserDefaults.standard.object(forKey: "show_wallet_selector") as? Bool ?? true -} - func pk_setting_key(_ pubkey: String, key: String) -> String { return "\(pubkey)_\(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) -} - -let fallback_zap_amount = 1000 - -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 fallback_zap_amount - } - return amt -} - -func should_disable_image_animation() -> Bool { - return (UserDefaults.standard.object(forKey: "disable_animation") as? Bool) - ?? UIAccessibility.isReduceMotionEnabled - } - -func get_default_wallet(_ pubkey: String) -> Wallet { - if let defaultWalletName = UserDefaults.standard.string(forKey: "default_wallet"), - let default_wallet = Wallet(rawValue: defaultWalletName) - { - return default_wallet - } else { - return .system_default_wallet - } -} - -func get_media_uploader(_ pubkey: String) -> MediaUploader { - if let defaultMediaUploader = UserDefaults.standard.string(forKey: "default_media_uploader"), - let defaultMediaUploader = MediaUploader(rawValue: defaultMediaUploader) { - return defaultMediaUploader - } else { - return .nostrBuild - } -} - -private func get_translation_service(_ pubkey: String) -> TranslationService? { - guard let translation_service = UserDefaults.standard.string(forKey: "translation_service") else { - return nil - } - - return TranslationService(rawValue: translation_service) -} - -private func get_libretranslate_url(_ pubkey: String, server: LibreTranslateServer) -> String? { - if let url = server.model.url { - return url - } - - return UserDefaults.standard.object(forKey: "libretranslate_url") as? String -} - -private func get_libretranslate_server(_ pubkey: String) -> LibreTranslateServer? { - guard let server_name = UserDefaults.standard.string(forKey: "libretranslate_server") else { - return nil - } - - return LibreTranslateServer(rawValue: server_name) -} diff --git a/damus/Views/NoteContentView.swift b/damus/Views/NoteContentView.swift @@ -72,7 +72,7 @@ struct NoteContentView: View { } var invoicesView: some View { - InvoicesView(our_pubkey: damus_state.keypair.pubkey, invoices: artifacts.invoices) + InvoicesView(our_pubkey: damus_state.keypair.pubkey, invoices: artifacts.invoices, settings: damus_state.settings) } var translateView: some View { diff --git a/damus/Views/Profile/ProfileView.swift b/damus/Views/Profile/ProfileView.swift @@ -278,7 +278,7 @@ struct ProfileView: View { } .cornerRadius(24) .sheet(isPresented: $showing_select_wallet, onDismiss: {showing_select_wallet = false}) { - SelectWalletView(showingSelectWallet: $showing_select_wallet, our_pubkey: damus_state.pubkey, invoice: lnurl) + SelectWalletView(default_wallet: damus_state.settings.default_wallet, showingSelectWallet: $showing_select_wallet, our_pubkey: damus_state.pubkey, invoice: lnurl) } } diff --git a/damus/Views/SelectWalletView.swift b/damus/Views/SelectWalletView.swift @@ -8,6 +8,7 @@ import SwiftUI struct SelectWalletView: View { + let default_wallet: Wallet @Binding var showingSelectWallet: Bool let our_pubkey: String let invoice: String @@ -38,8 +39,7 @@ struct SelectWalletView: View { Section(NSLocalizedString("Select a Lightning wallet", comment: "Title of section for selecting a Lightning wallet to pay a Lightning invoice.")) { List{ Button() { - let wallet_model = get_default_wallet(our_pubkey).model - open_with_wallet(wallet: wallet_model, invoice: invoice) + open_with_wallet(wallet: default_wallet.model, invoice: invoice) } label: { HStack { Text("Default Wallet", comment: "Button to pay a Lightning invoice with the user's default Lightning wallet.").font(.body).foregroundColor(.blue) @@ -73,6 +73,6 @@ struct SelectWalletView_Previews: PreviewProvider { @State static var invoice: String = "" static var previews: some View { - SelectWalletView(showingSelectWallet: $show, our_pubkey: "", invoice: "") + SelectWalletView(default_wallet: .lnlink, showingSelectWallet: $show, our_pubkey: "", invoice: "") } } diff --git a/damus/Views/Settings/ZapSettingsView.swift b/damus/Views/Settings/ZapSettingsView.swift @@ -17,8 +17,7 @@ struct ZapSettingsView: View { init(pubkey: String, settings: UserSettingsStore) { self.pubkey = pubkey - let zap_amt = get_default_zap_amount(pubkey: pubkey).formatted() - _default_zap_amount = State(initialValue: zap_amt) + _default_zap_amount = State(initialValue: settings.default_zap_amount.formatted()) self._settings = ObservedObject(initialValue: settings) } @@ -59,10 +58,10 @@ struct ZapSettingsView: View { .onReceive(Just(default_zap_amount)) { newValue in if let parsed = handle_string_amount(new_value: newValue) { self.default_zap_amount = parsed.formatted() - set_default_zap_amount(pubkey: self.pubkey, amount: parsed) + settings.default_zap_amount = parsed } else { self.default_zap_amount = "" - set_default_zap_amount(pubkey: self.pubkey, amount: 0) + settings.default_zap_amount = 0 } } } diff --git a/damus/Views/Zaps/CustomizeZapView.swift b/damus/Views/Zaps/CustomizeZapView.swift @@ -24,13 +24,12 @@ struct ZapAmountItem: Identifiable, Hashable { } } -func get_default_zap_amount_item(_ pubkey: String) -> ZapAmountItem { - let def = get_default_zap_amount(pubkey: pubkey) +func get_default_zap_amount_item(_ def: Int) -> ZapAmountItem { return ZapAmountItem(amount: def, icon: "🤙") } -func get_zap_amount_items(pubkey: String) -> [ZapAmountItem] { - let def_item = get_default_zap_amount_item(pubkey) +func get_zap_amount_items(_ default_zap_amt: Int) -> [ZapAmountItem] { + let def_item = get_default_zap_amount_item(default_zap_amt) var entries = [ ZapAmountItem(amount: 500, icon: "🙂"), ZapAmountItem(amount: 5000, icon: "💜"), @@ -67,13 +66,13 @@ struct CustomizeZapView: View { init(state: DamusState, event: NostrEvent, lnurl: String) { self._comment = State(initialValue: "") self.event = event - self.zap_amounts = get_zap_amount_items(pubkey: state.pubkey) + self.zap_amounts = get_zap_amount_items(state.settings.default_zap_amount) self._error = State(initialValue: nil) self._invoice = State(initialValue: "") self._showing_wallet_selector = State(initialValue: false) self._custom_amount = State(initialValue: "") self._zap_type = State(initialValue: .pub) - let selected = get_default_zap_amount_item(state.pubkey) + let selected = get_default_zap_amount_item(state.settings.default_zap_amount) self._selected_amount = State(initialValue: selected) self._custom_amount_sats = State(initialValue: nil) self._zapping = State(initialValue: false) @@ -144,12 +143,13 @@ struct CustomizeZapView: View { } break case .got_zap_invoice(let inv): - if should_show_wallet_selector(state.pubkey) { + if state.settings.show_wallet_selector { self.invoice = inv self.showing_wallet_selector = true } else { end_editing() - open_with_wallet(wallet: get_default_wallet(state.pubkey).model, invoice: inv) + let wallet = state.settings.default_wallet.model + open_with_wallet(wallet: wallet, invoice: inv) self.showing_wallet_selector = false dismiss() } @@ -161,7 +161,7 @@ struct CustomizeZapView: View { var body: some View { MainContent .sheet(isPresented: $showing_wallet_selector) { - SelectWalletView(showingSelectWallet: $showing_wallet_selector, our_pubkey: state.pubkey, invoice: invoice) + SelectWalletView(default_wallet: state.settings.default_wallet, showingSelectWallet: $showing_wallet_selector, our_pubkey: state.pubkey, invoice: invoice) } .onReceive(handle_notify(.zapping)) { notif in receive_zap(notif: notif)