Compare commits
1 Commits
hide-hellt
...
tyiu/zap-n
| Author | SHA1 | Date | |
|---|---|---|---|
|
73ac677711
|
@@ -173,7 +173,7 @@ func send_zap(damus_state: DamusState, event: NostrEvent, lnurl: String, is_cust
|
|||||||
damus_state.lnurls.endpoints[target.pubkey] = payreq
|
damus_state.lnurls.endpoints[target.pubkey] = payreq
|
||||||
}
|
}
|
||||||
|
|
||||||
let zap_amount = amount_sats ?? get_default_zap_amount(pubkey: damus_state.pubkey) ?? 1000
|
let zap_amount = amount_sats ?? get_default_zap_amount(pubkey: damus_state.pubkey)
|
||||||
|
|
||||||
guard let inv = await fetch_zap_invoice(payreq, zapreq: zapreq, sats: zap_amount, zap_type: zap_type, comment: comment) else {
|
guard let inv = await fetch_zap_invoice(payreq, zapreq: zapreq, sats: zap_amount, zap_type: zap_type, comment: comment) else {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
|||||||
@@ -26,11 +26,13 @@ func set_default_zap_amount(pubkey: String, amount: Int) {
|
|||||||
UserDefaults.standard.setValue(amount, forKey: key)
|
UserDefaults.standard.setValue(amount, forKey: key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func get_default_zap_amount(pubkey: String) -> Int? {
|
let fallback_zap_amount = 1000
|
||||||
|
|
||||||
|
func get_default_zap_amount(pubkey: String) -> Int {
|
||||||
let key = default_zap_setting_key(pubkey: pubkey)
|
let key = default_zap_setting_key(pubkey: pubkey)
|
||||||
let amt = UserDefaults.standard.integer(forKey: key)
|
let amt = UserDefaults.standard.integer(forKey: key)
|
||||||
if amt == 0 {
|
if amt == 0 {
|
||||||
return nil
|
return fallback_zap_amount
|
||||||
}
|
}
|
||||||
return amt
|
return amt
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,14 +144,15 @@ struct ConfigView_Previews: PreviewProvider {
|
|||||||
|
|
||||||
|
|
||||||
func handle_string_amount(new_value: String) -> Int? {
|
func handle_string_amount(new_value: String) -> Int? {
|
||||||
let digits = Set("0123456789")
|
let filtered = new_value.filter {
|
||||||
let filtered = new_value.filter { digits.contains($0) }
|
$0.isNumber
|
||||||
|
}
|
||||||
|
|
||||||
if filtered == "" {
|
if filtered == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let amt = Int(filtered) else {
|
guard let amt = NumberFormatter().number(from: filtered) as? Int else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ struct ZapSettingsView: View {
|
|||||||
|
|
||||||
@State var default_zap_amount: String
|
@State var default_zap_amount: String
|
||||||
@Environment(\.dismiss) var dismiss
|
@Environment(\.dismiss) var dismiss
|
||||||
|
|
||||||
init(pubkey: String, settings: UserSettingsStore) {
|
init(pubkey: String, settings: UserSettingsStore) {
|
||||||
self.pubkey = pubkey
|
self.pubkey = pubkey
|
||||||
let zap_amt = get_default_zap_amount(pubkey: pubkey).map({ "\($0)" }) ?? "1000"
|
let zap_amt = get_default_zap_amount(pubkey: pubkey).formatted()
|
||||||
_default_zap_amount = State(initialValue: zap_amt)
|
_default_zap_amount = State(initialValue: zap_amt)
|
||||||
self._settings = ObservedObject(initialValue: settings)
|
self._settings = ObservedObject(initialValue: settings)
|
||||||
}
|
}
|
||||||
@@ -42,12 +42,15 @@ struct ZapSettingsView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Section(NSLocalizedString("Default Zap Amount in sats", comment: "Title for section in zap settings that controls the default zap amount in sats.")) {
|
Section(NSLocalizedString("Default Zap Amount in sats", comment: "Title for section in zap settings that controls the default zap amount in sats.")) {
|
||||||
TextField(String("1000"), text: $default_zap_amount)
|
TextField(fallback_zap_amount.formatted(), text: $default_zap_amount)
|
||||||
.keyboardType(.numberPad)
|
.keyboardType(.numberPad)
|
||||||
.onReceive(Just(default_zap_amount)) { newValue in
|
.onReceive(Just(default_zap_amount)) { newValue in
|
||||||
if let parsed = handle_string_amount(new_value: newValue) {
|
if let parsed = handle_string_amount(new_value: newValue) {
|
||||||
self.default_zap_amount = String(parsed)
|
self.default_zap_amount = parsed.formatted()
|
||||||
set_default_zap_amount(pubkey: self.pubkey, amount: parsed)
|
set_default_zap_amount(pubkey: self.pubkey, amount: parsed)
|
||||||
|
} else {
|
||||||
|
self.default_zap_amount = ""
|
||||||
|
set_default_zap_amount(pubkey: self.pubkey, amount: 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ struct ZapAmountItem: Identifiable, Hashable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func get_default_zap_amount_item(_ pubkey: String) -> ZapAmountItem {
|
func get_default_zap_amount_item(_ pubkey: String) -> ZapAmountItem {
|
||||||
let def = get_default_zap_amount(pubkey: pubkey) ?? 1000
|
let def = get_default_zap_amount(pubkey: pubkey)
|
||||||
return ZapAmountItem(amount: def, icon: "🤙")
|
return ZapAmountItem(amount: def, icon: "🤙")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,13 +181,17 @@ struct CustomizeZapView: View {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Section(content: {
|
Section(content: {
|
||||||
TextField(String("100000"), text: $custom_amount)
|
// Use the selected sats amount as the placeholder text so that the UI is less confusing.
|
||||||
|
// User can type in their custom amount, which hides the placeholder.
|
||||||
|
TextField(selected_amount.amount.formatted(), text: $custom_amount)
|
||||||
.keyboardType(.numberPad)
|
.keyboardType(.numberPad)
|
||||||
.onReceive(Just(custom_amount)) { newValue in
|
.onReceive(Just(custom_amount)) { newValue in
|
||||||
|
|
||||||
if let parsed = handle_string_amount(new_value: newValue) {
|
if let parsed = handle_string_amount(new_value: newValue) {
|
||||||
self.custom_amount = String(parsed)
|
self.custom_amount = parsed.formatted()
|
||||||
self.custom_amount_sats = parsed
|
self.custom_amount_sats = parsed
|
||||||
|
} else {
|
||||||
|
self.custom_amount = ""
|
||||||
|
self.custom_amount_sats = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, header: {
|
}, header: {
|
||||||
|
|||||||
@@ -97,9 +97,9 @@ class damusTests: XCTestCase {
|
|||||||
|
|
||||||
func testSaveDefaultZapAmount() {
|
func testSaveDefaultZapAmount() {
|
||||||
let pubkey = "test_pubkey"
|
let pubkey = "test_pubkey"
|
||||||
let amt = 1000
|
let amt = 1234
|
||||||
set_default_zap_amount(pubkey: pubkey, amount: amt)
|
set_default_zap_amount(pubkey: pubkey, amount: amt)
|
||||||
let loaded = get_default_zap_amount(pubkey: pubkey)!
|
let loaded = get_default_zap_amount(pubkey: pubkey)
|
||||||
XCTAssertEqual(loaded, amt)
|
XCTAssertEqual(loaded, amt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user