nozaps: switch to global sheet when zapping

This fixes many popping bugs

Changelog-Fixed: Fix zap sheet popping
This commit is contained in:
William Casarin
2023-06-23 11:50:55 +02:00
parent 61b3ad2990
commit f090596067
11 changed files with 80 additions and 87 deletions

View File

@@ -37,7 +37,7 @@ struct InvoiceView: View {
var PayButton: some View {
Button {
if settings.show_wallet_selector {
showing_select_wallet = true
present_sheet(.select_wallet(invoice: invoice.string))
} else {
open_with_wallet(wallet: settings.default_wallet.model, invoice: invoice.string)
}
@@ -79,9 +79,6 @@ struct InvoiceView: View {
}
.padding(30)
}
.sheet(isPresented: $showing_select_wallet, onDismiss: {showing_select_wallet = false}) {
SelectWalletView(default_wallet: settings.default_wallet, showingSelectWallet: $showing_select_wallet, our_pubkey: our_pubkey, invoice: invoice.string)
}
}
}
@@ -116,3 +113,7 @@ struct InvoiceView_Previews: PreviewProvider {
}
}
func present_sheet(_ sheet: Sheets) {
notify(.present_sheet, sheet)
}

View File

@@ -32,7 +32,6 @@ struct ZapButton: View {
let lnurl: String
@ObservedObject var zaps: ZapsDataModel
@StateObject var button: ZapButtonModel = ZapButtonModel()
var our_zap: Zapping? {
zaps.zaps.first(where: { z in z.request.ev.pubkey == damus_state.pubkey })
@@ -133,43 +132,22 @@ struct ZapButton: View {
}
.accessibilityLabel(NSLocalizedString("Zap", comment: "Accessibility label for zap button"))
.simultaneousGesture(LongPressGesture().onEnded {_ in
button.showing_zap_customizer = true
// when we don't have nozaps mode enable, long press shows the zap customizer
if !damus_state.settings.nozaps {
present_sheet(.zap(target: target, lnurl: lnurl))
}
// long press does nothing in nozaps mode
})
.highPriorityGesture(TapGesture().onEnded {
tap()
// when we have appstore mode on, only show the zap customizer as "user zaps"
if damus_state.settings.nozaps {
present_sheet(.zap(target: target, lnurl: lnurl))
} else {
// otherwise we restore the original behavior of one-tap zaps
tap()
}
})
.sheet(isPresented: $button.showing_zap_customizer) {
CustomizeZapView(state: damus_state, target: target, lnurl: lnurl)
}
.sheet(isPresented: $button.showing_select_wallet, onDismiss: {button.showing_select_wallet = false}) {
SelectWalletView(default_wallet: damus_state.settings.default_wallet, showingSelectWallet: $button.showing_select_wallet, our_pubkey: damus_state.pubkey, invoice: button.invoice ?? "")
}
.onReceive(handle_notify(.zapping)) { notif in
let zap_ev = notif.object as! ZappingEvent
guard zap_ev.target.id == self.target.id else {
return
}
guard !zap_ev.is_custom else {
return
}
switch zap_ev.type {
case .failed:
break
case .got_zap_invoice(let inv):
if damus_state.settings.show_wallet_selector {
self.button.invoice = inv
self.button.showing_select_wallet = true
} else {
let wallet = damus_state.settings.default_wallet.model
open_with_wallet(wallet: wallet, invoice: inv)
}
case .sent_from_nwc:
break
}
}
}
}

View File

@@ -14,17 +14,38 @@ struct TimestampedProfile {
let event: NostrEvent
}
struct ZapSheet {
let target: ZapTarget
let lnurl: String
}
struct SelectWallet {
let invoice: String
}
enum Sheets: Identifiable {
case post(PostAction)
case report(ReportTarget)
case event(NostrEvent)
case zap(ZapSheet)
case select_wallet(SelectWallet)
case filter
static func zap(target: ZapTarget, lnurl: String) -> Sheets {
return .zap(ZapSheet(target: target, lnurl: lnurl))
}
static func select_wallet(invoice: String) -> Sheets {
return .select_wallet(SelectWallet(invoice: invoice))
}
var id: String {
switch self {
case .report: return "report"
case .post(let action): return "post-" + (action.ev?.id ?? "")
case .event(let ev): return "event-" + ev.id
case .zap(let sheet): return "zap-" + sheet.target.id
case .select_wallet: return "select-wallet"
case .filter: return "filter"
}
}
@@ -327,6 +348,10 @@ struct ContentView: View {
PostView(action: action, damus_state: damus_state!)
case .event:
EventDetailView()
case .zap(let zapsheet):
CustomizeZapView(state: damus_state!, target: zapsheet.target, lnurl: zapsheet.lnurl)
case .select_wallet(let select):
SelectWalletView(default_wallet: damus_state!.settings.default_wallet, active_sheet: $active_sheet, our_pubkey: damus_state!.pubkey, invoice: select.invoice)
case .filter:
let timeline = selected_timeline
if #available(iOS 16.0, *) {
@@ -434,6 +459,31 @@ struct ContentView: View {
.onReceive(handle_notify(.unmute_thread)) { notif in
home.filter_events()
}
.onReceive(handle_notify(.present_sheet)) { notif in
let sheet = notif.object as! Sheets
self.active_sheet = sheet
}
.onReceive(handle_notify(.zapping)) { notif in
let zap_ev = notif.object as! ZappingEvent
guard !zap_ev.is_custom else {
return
}
switch zap_ev.type {
case .failed:
break
case .got_zap_invoice(let inv):
if damus_state!.settings.show_wallet_selector {
present_sheet(.select_wallet(invoice: inv))
} else {
let wallet = damus_state!.settings.default_wallet.model
open_with_wallet(wallet: wallet, invoice: inv)
}
case .sent_from_nwc:
break
}
}
.onChange(of: scenePhase) { (phase: ScenePhase) in
switch phase {
case .background:

View File

@@ -52,6 +52,7 @@ class HomeModel {
var notifications = NotificationsModel()
var notification_status = NotificationStatusModel()
var events: EventHolder = EventHolder()
var zap_button: ZapButtonModel = ZapButtonModel()
init() {
self.damus_state = DamusState.empty

View File

@@ -10,6 +10,4 @@ import Foundation
class ZapButtonModel: ObservableObject {
var invoice: String? = nil
@Published var zapping: String = ""
@Published var showing_select_wallet: Bool = false
@Published var showing_zap_customizer: Bool = false
}

View File

@@ -15,7 +15,6 @@ class CustomizeZapModel: ObservableObject {
@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

View File

@@ -77,6 +77,9 @@ extension Notification.Name {
static var update_stats: Notification.Name {
return Notification.Name("update_stats")
}
static var present_sheet: Notification.Name {
return Notification.Name("present_sheet")
}
static var zapping: Notification.Name {
return Notification.Name("zapping")
}

View File

@@ -96,7 +96,6 @@ struct ProfileView: View {
static let markdown = Markdown()
@State var showing_select_wallet: Bool = false
@State var is_zoomed: Bool = false
@State var show_share_sheet: Bool = false
@State var show_qr_code: Bool = false
@@ -245,7 +244,7 @@ struct ProfileView: View {
func lnButton(lnurl: String, profile: Profile) -> some View {
let button_img = profile.reactions == false ? "zap.fill" : "zap"
return Button(action: {
zap_button_model.showing_zap_customizer = true
present_sheet(.zap(target: .profile(self.profile.pubkey), lnurl: lnurl))
}) {
Image(button_img)
.foregroundColor(button_img == "zap.fill" ? .orange : Color.primary)
@@ -272,38 +271,6 @@ struct ProfileView: View {
}
.cornerRadius(24)
.sheet(isPresented: $zap_button_model.showing_zap_customizer) {
CustomizeZapView(state: damus_state, target: ZapTarget.profile(self.profile.pubkey), lnurl: lnurl)
}
.sheet(isPresented: $zap_button_model.showing_select_wallet, onDismiss: {zap_button_model.showing_select_wallet = false}) {
SelectWalletView(default_wallet: damus_state.settings.default_wallet, showingSelectWallet: $zap_button_model.showing_select_wallet, our_pubkey: damus_state.pubkey, invoice: zap_button_model.invoice ?? "")
}
.onReceive(handle_notify(.zapping)) { notif in
let zap_ev = notif.object as! ZappingEvent
guard zap_ev.target.id == self.profile.pubkey else {
return
}
guard !zap_ev.is_custom else {
return
}
switch zap_ev.type {
case .failed:
break
case .got_zap_invoice(let inv):
if damus_state.settings.show_wallet_selector {
zap_button_model.invoice = inv
zap_button_model.showing_select_wallet = true
} else {
let wallet = damus_state.settings.default_wallet.model
open_with_wallet(wallet: wallet, invoice: inv)
}
case .sent_from_nwc:
break
}
}
}
var dmButton: some View {

View File

@@ -9,7 +9,7 @@ import SwiftUI
struct SelectWalletView: View {
let default_wallet: Wallet
@Binding var showingSelectWallet: Bool
@Binding var active_sheet: Sheets?
let our_pubkey: String
let invoice: String
@State var invoice_copied: Bool = false
@@ -59,7 +59,7 @@ struct SelectWalletView: View {
}.padding(.vertical, 2.5)
}
}.navigationBarTitle(Text("Pay the Lightning invoice", comment: "Navigation bar title for view to pay Lightning invoice."), displayMode: .inline).navigationBarItems(trailing: Button(action: {
self.showingSelectWallet = false
self.active_sheet = nil
}) {
Text("Done", comment: "Button to dismiss wallet selection view for paying Lightning invoice.").bold()
})
@@ -68,9 +68,9 @@ struct SelectWalletView: View {
}
struct SelectWalletView_Previews: PreviewProvider {
@State static var show: Bool = true
@State static var active_sheet: Sheets? = nil
static var previews: some View {
SelectWalletView(default_wallet: .lnlink, showingSelectWallet: $show, our_pubkey: "", invoice: "")
SelectWalletView(default_wallet: .lnlink, active_sheet: $active_sheet, our_pubkey: "", invoice: "")
}
}

View File

@@ -216,12 +216,11 @@ struct CustomizeZapView: View {
case .got_zap_invoice(let inv):
if state.settings.show_wallet_selector {
model.invoice = inv
model.showing_wallet_selector = true
present_sheet(.select_wallet(invoice: inv))
} else {
end_editing()
let wallet = state.settings.default_wallet.model
open_with_wallet(wallet: wallet, invoice: inv)
model.showing_wallet_selector = false
dismiss()
}
case .sent_from_nwc:
@@ -259,9 +258,6 @@ struct CustomizeZapView: View {
ZapPicker
}
}
.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)
}

View File

@@ -14,7 +14,7 @@ struct ZapUserView: View {
var body: some View {
HStack(alignment: .center) {
Text("Zap")
.font(.largeTitle)
.font(.title2)
UserView(damus_state: state, pubkey: pubkey, spacer: false)
}