refactor: switch CustomizeZapView to use a model
Changelog-Fixed: Fix CustomizeZapView from randomly disappearing
This commit is contained in:
30
damus/Models/Zaps/CustomizeZapModel.swift
Normal file
30
damus/Models/Zaps/CustomizeZapModel.swift
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// CustomizeZapModel.swift
|
||||
// damus
|
||||
//
|
||||
// Created by William Casarin on 2023-06-22.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
class CustomizeZapModel: ObservableObject {
|
||||
@Published var comment: String = ""
|
||||
@Published var custom_amount: String = ""
|
||||
@Published var custom_amount_sats: Int? = nil
|
||||
@Published var zap_type: ZapType = .pub
|
||||
@Published var invoice: String = ""
|
||||
@Published var error: String? = nil
|
||||
@Published var showing_wallet_selector: Bool = false
|
||||
@Published var zapping: Bool = false
|
||||
@Published var show_zap_types: Bool = false
|
||||
|
||||
init() {
|
||||
}
|
||||
|
||||
func set_defaults(settings: UserSettingsStore) {
|
||||
self.zap_type = settings.default_zap_type
|
||||
self.custom_amount = String(settings.default_zap_amount)
|
||||
self.custom_amount_sats = settings.default_zap_amount
|
||||
}
|
||||
}
|
||||
@@ -48,18 +48,10 @@ struct CustomizeZapView: View {
|
||||
let state: DamusState
|
||||
let target: ZapTarget
|
||||
let lnurl: String
|
||||
@State var comment: String
|
||||
@State var custom_amount: String
|
||||
@State var custom_amount_sats: Int?
|
||||
@State var zap_type: ZapType
|
||||
@State var invoice: String
|
||||
@State var error: String?
|
||||
@State var showing_wallet_selector: Bool
|
||||
@State var zapping: Bool
|
||||
@State var show_zap_types: Bool = false
|
||||
|
||||
let zap_amounts: [ZapAmountItem]
|
||||
|
||||
@StateObject var model: CustomizeZapModel = CustomizeZapModel()
|
||||
@Environment(\.dismiss) var dismiss
|
||||
@Environment(\.colorScheme) var colorScheme
|
||||
|
||||
@@ -72,21 +64,12 @@ struct CustomizeZapView: View {
|
||||
}
|
||||
|
||||
init(state: DamusState, target: ZapTarget, lnurl: String) {
|
||||
self._comment = State(initialValue: "")
|
||||
self.target = target
|
||||
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._zap_type = State(initialValue: state.settings.default_zap_type)
|
||||
self._custom_amount = State(initialValue: String(state.settings.default_zap_amount))
|
||||
self._custom_amount_sats = State(initialValue: nil)
|
||||
self._zapping = State(initialValue: false)
|
||||
self.lnurl = lnurl
|
||||
self.state = state
|
||||
}
|
||||
|
||||
|
||||
func amount_parts(_ n: Int) -> [ZapAmountItem] {
|
||||
var i: Int = -1
|
||||
let start = n * 3
|
||||
@@ -101,7 +84,10 @@ struct CustomizeZapView: View {
|
||||
func AmountsPart(n: Int) -> some View {
|
||||
HStack(alignment: .center, spacing: 15) {
|
||||
ForEach(amount_parts(n)) { entry in
|
||||
ZapAmountButton(zapAmountItem: entry, action: {custom_amount_sats = entry.amount; custom_amount = String(entry.amount)})
|
||||
ZapAmountButton(zapAmountItem: entry, action: {
|
||||
model.custom_amount_sats = entry.amount
|
||||
model.custom_amount = String(entry.amount)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,17 +111,17 @@ struct CustomizeZapView: View {
|
||||
.font(.headline)
|
||||
.frame(width: 70, height: 70)
|
||||
.foregroundColor(fontColor())
|
||||
.background(custom_amount_sats == zapAmountItem.amount ? fillColor() : DamusColors.adaptableGrey)
|
||||
.background(model.custom_amount_sats == zapAmountItem.amount ? fillColor() : DamusColors.adaptableGrey)
|
||||
.cornerRadius(15)
|
||||
.overlay(RoundedRectangle(cornerRadius: 15)
|
||||
.stroke(DamusColors.purple.opacity(custom_amount_sats == zapAmountItem.amount ? 1.0 : 0.0), lineWidth: 2))
|
||||
.stroke(DamusColors.purple.opacity(model.custom_amount_sats == zapAmountItem.amount ? 1.0 : 0.0), lineWidth: 2))
|
||||
}
|
||||
}
|
||||
|
||||
var CustomZapTextField: some View {
|
||||
VStack(alignment: .center, spacing: 0) {
|
||||
TextField("", text: $custom_amount)
|
||||
.placeholder(when: custom_amount.isEmpty, alignment: .center) {
|
||||
TextField("", text: $model.custom_amount)
|
||||
.placeholder(when: model.custom_amount.isEmpty, alignment: .center) {
|
||||
Text(verbatim: 0.formatted())
|
||||
}
|
||||
.accentColor(.clear)
|
||||
@@ -143,16 +129,16 @@ struct CustomizeZapView: View {
|
||||
.minimumScaleFactor(0.01)
|
||||
.keyboardType(.numberPad)
|
||||
.multilineTextAlignment(.center)
|
||||
.onReceive(Just(custom_amount)) { newValue in
|
||||
.onChange(of: model.custom_amount) { newValue in
|
||||
if let parsed = handle_string_amount(new_value: newValue) {
|
||||
self.custom_amount = parsed.formatted()
|
||||
self.custom_amount_sats = parsed
|
||||
model.custom_amount = parsed.formatted()
|
||||
model.custom_amount_sats = parsed
|
||||
} else {
|
||||
self.custom_amount = ""
|
||||
self.custom_amount_sats = nil
|
||||
model.custom_amount = ""
|
||||
model.custom_amount_sats = nil
|
||||
}
|
||||
}
|
||||
Text(verbatim: satsString(custom_amount_sats ?? 0))
|
||||
Text(verbatim: satsString(model.custom_amount_sats ?? 0))
|
||||
.font(.system(size: 18, weight: .heavy))
|
||||
}
|
||||
}
|
||||
@@ -160,12 +146,12 @@ struct CustomizeZapView: View {
|
||||
var ZapReply: some View {
|
||||
HStack {
|
||||
if #available(iOS 16.0, *) {
|
||||
TextField(NSLocalizedString("Send a message with your zap...", comment: "Placeholder text for a comment to send as part of a zap to the user."), text: $comment, axis: .vertical)
|
||||
TextField(NSLocalizedString("Send a message with your zap...", comment: "Placeholder text for a comment to send as part of a zap to the user."), text: $model.comment, axis: .vertical)
|
||||
.autocorrectionDisabled(true)
|
||||
.textInputAutocapitalization(.never)
|
||||
.lineLimit(5)
|
||||
} else {
|
||||
TextField(NSLocalizedString("Send a message with your zap...", comment: "Placeholder text for a comment to send as part of a zap to the user."), text: $comment)
|
||||
TextField(NSLocalizedString("Send a message with your zap...", comment: "Placeholder text for a comment to send as part of a zap to the user."), text: $model.comment)
|
||||
.autocorrectionDisabled(true)
|
||||
.textInputAutocapitalization(.never)
|
||||
}
|
||||
@@ -179,24 +165,24 @@ struct CustomizeZapView: View {
|
||||
|
||||
var ZapButton: some View {
|
||||
VStack {
|
||||
if zapping {
|
||||
if model.zapping {
|
||||
Text("Zapping...", comment: "Text to indicate that the app is in the process of sending a zap.")
|
||||
} else {
|
||||
Button(NSLocalizedString("Zap", comment: "Button to send a zap.")) {
|
||||
let amount = custom_amount_sats
|
||||
send_zap(damus_state: state, target: target, lnurl: lnurl, is_custom: true, comment: comment, amount_sats: amount, zap_type: zap_type)
|
||||
self.zapping = true
|
||||
let amount = model.custom_amount_sats
|
||||
send_zap(damus_state: state, target: target, lnurl: lnurl, is_custom: true, comment: model.comment, amount_sats: amount, zap_type: model.zap_type)
|
||||
model.zapping = true
|
||||
}
|
||||
.disabled(custom_amount_sats == 0 || custom_amount.isEmpty)
|
||||
.disabled(model.custom_amount_sats == 0 || model.custom_amount.isEmpty)
|
||||
.font(.system(size: 28, weight: .bold))
|
||||
.frame(width: 130, height: 50)
|
||||
.foregroundColor(.white)
|
||||
.background(LINEAR_GRADIENT)
|
||||
.opacity(custom_amount_sats == 0 || custom_amount.isEmpty ? 0.5 : 1.0)
|
||||
.opacity(model.custom_amount_sats == 0 || model.custom_amount.isEmpty ? 0.5 : 1.0)
|
||||
.clipShape(Capsule())
|
||||
}
|
||||
|
||||
if let error {
|
||||
if let error = model.error {
|
||||
Text(error)
|
||||
.foregroundColor(.red)
|
||||
}
|
||||
@@ -212,30 +198,30 @@ struct CustomizeZapView: View {
|
||||
return
|
||||
}
|
||||
|
||||
self.zapping = false
|
||||
model.zapping = false
|
||||
|
||||
switch zap_ev.type {
|
||||
case .failed(let err):
|
||||
switch err {
|
||||
case .fetching_invoice:
|
||||
self.error = NSLocalizedString("Error fetching lightning invoice", comment: "Message to display when there was an error fetching a lightning invoice while attempting to zap.")
|
||||
model.error = NSLocalizedString("Error fetching lightning invoice", comment: "Message to display when there was an error fetching a lightning invoice while attempting to zap.")
|
||||
case .bad_lnurl:
|
||||
self.error = NSLocalizedString("Invalid lightning address", comment: "Message to display when there was an error attempting to zap due to an invalid lightning address.")
|
||||
model.error = NSLocalizedString("Invalid lightning address", comment: "Message to display when there was an error attempting to zap due to an invalid lightning address.")
|
||||
case .canceled:
|
||||
self.error = NSLocalizedString("Zap attempt from connected wallet was canceled.", comment: "Message to display when a zap from the user's connected wallet was canceled.")
|
||||
model.error = NSLocalizedString("Zap attempt from connected wallet was canceled.", comment: "Message to display when a zap from the user's connected wallet was canceled.")
|
||||
case .send_failed:
|
||||
self.error = NSLocalizedString("Zap attempt from connected wallet failed.", comment: "Message to display when sending a zap from the user's connected wallet failed.")
|
||||
model.error = NSLocalizedString("Zap attempt from connected wallet failed.", comment: "Message to display when sending a zap from the user's connected wallet failed.")
|
||||
}
|
||||
break
|
||||
case .got_zap_invoice(let inv):
|
||||
if state.settings.show_wallet_selector {
|
||||
self.invoice = inv
|
||||
self.showing_wallet_selector = true
|
||||
model.invoice = inv
|
||||
model.showing_wallet_selector = true
|
||||
} else {
|
||||
end_editing()
|
||||
let wallet = state.settings.default_wallet.model
|
||||
open_with_wallet(wallet: wallet, invoice: inv)
|
||||
self.showing_wallet_selector = false
|
||||
model.showing_wallet_selector = false
|
||||
dismiss()
|
||||
}
|
||||
case .sent_from_nwc:
|
||||
@@ -260,7 +246,7 @@ struct CustomizeZapView: View {
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.sheet(isPresented: $show_zap_types) {
|
||||
.sheet(isPresented: $model.show_zap_types) {
|
||||
if #available(iOS 16.0, *) {
|
||||
ZapPicker
|
||||
.presentationDetents([.medium])
|
||||
@@ -269,8 +255,11 @@ struct CustomizeZapView: View {
|
||||
ZapPicker
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $showing_wallet_selector) {
|
||||
SelectWalletView(default_wallet: state.settings.default_wallet, showingSelectWallet: $showing_wallet_selector, our_pubkey: state.pubkey, invoice: invoice)
|
||||
.sheet(isPresented: $model.showing_wallet_selector) {
|
||||
SelectWalletView(default_wallet: state.settings.default_wallet, showingSelectWallet: $model.showing_wallet_selector, our_pubkey: state.pubkey, invoice: model.invoice)
|
||||
}
|
||||
.onAppear {
|
||||
model.set_defaults(settings: state.settings)
|
||||
}
|
||||
.onReceive(handle_notify(.zapping)) { notif in
|
||||
receive_zap(notif: notif)
|
||||
@@ -283,9 +272,9 @@ struct CustomizeZapView: View {
|
||||
|
||||
func ZapTypeButton() -> some View {
|
||||
Button(action: {
|
||||
show_zap_types = true
|
||||
model.show_zap_types = true
|
||||
}) {
|
||||
switch zap_type {
|
||||
switch model.zap_type {
|
||||
case .pub:
|
||||
Image("globe")
|
||||
Text("Public", comment: "Button text to indicate that the zap type is a public zap.")
|
||||
@@ -308,7 +297,7 @@ struct CustomizeZapView: View {
|
||||
}
|
||||
|
||||
var ZapPicker: some View {
|
||||
ZapTypePicker(zap_type: $zap_type, settings: state.settings, profiles: state.profiles, pubkey: target.pubkey)
|
||||
ZapTypePicker(zap_type: $model.zap_type, settings: state.settings, profiles: state.profiles, pubkey: target.pubkey)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user