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 { var PayButton: some View {
Button { Button {
if settings.show_wallet_selector { if settings.show_wallet_selector {
showing_select_wallet = true present_sheet(.select_wallet(invoice: invoice.string))
} else { } else {
open_with_wallet(wallet: settings.default_wallet.model, invoice: invoice.string) open_with_wallet(wallet: settings.default_wallet.model, invoice: invoice.string)
} }
@@ -79,9 +79,6 @@ struct InvoiceView: View {
} }
.padding(30) .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 let lnurl: String
@ObservedObject var zaps: ZapsDataModel @ObservedObject var zaps: ZapsDataModel
@StateObject var button: ZapButtonModel = ZapButtonModel()
var our_zap: Zapping? { var our_zap: Zapping? {
zaps.zaps.first(where: { z in z.request.ev.pubkey == damus_state.pubkey }) 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")) .accessibilityLabel(NSLocalizedString("Zap", comment: "Accessibility label for zap button"))
.simultaneousGesture(LongPressGesture().onEnded {_ in .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 { .highPriorityGesture(TapGesture().onEnded {
tap() // when we have appstore mode on, only show the zap customizer as "user zaps"
}) if damus_state.settings.nozaps {
.sheet(isPresented: $button.showing_zap_customizer) { present_sheet(.zap(target: target, lnurl: lnurl))
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 { } else {
let wallet = damus_state.settings.default_wallet.model // otherwise we restore the original behavior of one-tap zaps
open_with_wallet(wallet: wallet, invoice: inv) tap()
}
case .sent_from_nwc:
break
}
} }
})
} }
} }

View File

@@ -14,17 +14,38 @@ struct TimestampedProfile {
let event: NostrEvent let event: NostrEvent
} }
struct ZapSheet {
let target: ZapTarget
let lnurl: String
}
struct SelectWallet {
let invoice: String
}
enum Sheets: Identifiable { enum Sheets: Identifiable {
case post(PostAction) case post(PostAction)
case report(ReportTarget) case report(ReportTarget)
case event(NostrEvent) case event(NostrEvent)
case zap(ZapSheet)
case select_wallet(SelectWallet)
case filter 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 { var id: String {
switch self { switch self {
case .report: return "report" case .report: return "report"
case .post(let action): return "post-" + (action.ev?.id ?? "") case .post(let action): return "post-" + (action.ev?.id ?? "")
case .event(let ev): return "event-" + 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" case .filter: return "filter"
} }
} }
@@ -327,6 +348,10 @@ struct ContentView: View {
PostView(action: action, damus_state: damus_state!) PostView(action: action, damus_state: damus_state!)
case .event: case .event:
EventDetailView() 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: case .filter:
let timeline = selected_timeline let timeline = selected_timeline
if #available(iOS 16.0, *) { if #available(iOS 16.0, *) {
@@ -434,6 +459,31 @@ struct ContentView: View {
.onReceive(handle_notify(.unmute_thread)) { notif in .onReceive(handle_notify(.unmute_thread)) { notif in
home.filter_events() 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 .onChange(of: scenePhase) { (phase: ScenePhase) in
switch phase { switch phase {
case .background: case .background:

View File

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

View File

@@ -10,6 +10,4 @@ import Foundation
class ZapButtonModel: ObservableObject { class ZapButtonModel: ObservableObject {
var invoice: String? = nil var invoice: String? = nil
@Published var zapping: String = "" @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 zap_type: ZapType = .pub
@Published var invoice: String = "" @Published var invoice: String = ""
@Published var error: String? = nil @Published var error: String? = nil
@Published var showing_wallet_selector: Bool = false
@Published var zapping: Bool = false @Published var zapping: Bool = false
@Published var show_zap_types: Bool = false @Published var show_zap_types: Bool = false

View File

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

View File

@@ -96,7 +96,6 @@ struct ProfileView: View {
static let markdown = Markdown() static let markdown = Markdown()
@State var showing_select_wallet: Bool = false
@State var is_zoomed: Bool = false @State var is_zoomed: Bool = false
@State var show_share_sheet: Bool = false @State var show_share_sheet: Bool = false
@State var show_qr_code: Bool = false @State var show_qr_code: Bool = false
@@ -245,7 +244,7 @@ struct ProfileView: View {
func lnButton(lnurl: String, profile: Profile) -> some View { func lnButton(lnurl: String, profile: Profile) -> some View {
let button_img = profile.reactions == false ? "zap.fill" : "zap" let button_img = profile.reactions == false ? "zap.fill" : "zap"
return Button(action: { return Button(action: {
zap_button_model.showing_zap_customizer = true present_sheet(.zap(target: .profile(self.profile.pubkey), lnurl: lnurl))
}) { }) {
Image(button_img) Image(button_img)
.foregroundColor(button_img == "zap.fill" ? .orange : Color.primary) .foregroundColor(button_img == "zap.fill" ? .orange : Color.primary)
@@ -272,38 +271,6 @@ struct ProfileView: View {
} }
.cornerRadius(24) .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 { var dmButton: some View {

View File

@@ -9,7 +9,7 @@ import SwiftUI
struct SelectWalletView: View { struct SelectWalletView: View {
let default_wallet: Wallet let default_wallet: Wallet
@Binding var showingSelectWallet: Bool @Binding var active_sheet: Sheets?
let our_pubkey: String let our_pubkey: String
let invoice: String let invoice: String
@State var invoice_copied: Bool = false @State var invoice_copied: Bool = false
@@ -59,7 +59,7 @@ struct SelectWalletView: View {
}.padding(.vertical, 2.5) }.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: { }.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() 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 { struct SelectWalletView_Previews: PreviewProvider {
@State static var show: Bool = true @State static var active_sheet: Sheets? = nil
static var previews: some View { 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): case .got_zap_invoice(let inv):
if state.settings.show_wallet_selector { if state.settings.show_wallet_selector {
model.invoice = inv model.invoice = inv
model.showing_wallet_selector = true present_sheet(.select_wallet(invoice: inv))
} else { } else {
end_editing() end_editing()
let wallet = state.settings.default_wallet.model let wallet = state.settings.default_wallet.model
open_with_wallet(wallet: wallet, invoice: inv) open_with_wallet(wallet: wallet, invoice: inv)
model.showing_wallet_selector = false
dismiss() dismiss()
} }
case .sent_from_nwc: case .sent_from_nwc:
@@ -259,9 +258,6 @@ struct CustomizeZapView: View {
ZapPicker 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 { .onAppear {
model.set_defaults(settings: state.settings) model.set_defaults(settings: state.settings)
} }

View File

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