Handle window close on transaction complete

This commit is contained in:
Balazs Perlaki-Horvath 2024-11-03 00:20:02 +01:00 committed by Kelson
parent 9c5c66faab
commit d76de07636
3 changed files with 48 additions and 26 deletions

View File

@ -33,6 +33,7 @@ struct Kiwix: App {
@StateObject private var libraryRefreshViewModel = LibraryViewModel()
private let notificationCenterDelegate = NotificationCenterDelegate()
private var amountSelected = PassthroughSubject<SelectedAmount?, Never>()
@State private var selectedAmount: SelectedAmount?
@StateObject var formReset = FormReset()
init() {
@ -83,18 +84,9 @@ struct Kiwix: App {
.frame(width: 550, height: 400)
}
Window("Donate", id: "donation") {
PaymentForm(amountSelected: amountSelected)
.frame(width: 320, height: 320)
.onReceive(NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)) { notification in
if let window = notification.object as? NSWindow,
window.identifier?.rawValue == "donation" {
debugPrint("closing donation")
formReset.reset()
}
}
.environmentObject(formReset)
.onReceive(amountSelected) { amount in
debugPrint("amountSelected: \(amount)")
Group {
if let selectedAmount {
PaymentSummary(selectedAmount: selectedAmount) {
// after upgrading to macOS 14, use:
// @Environment(\.dismissWindow) var dismissWindow
// and call:
@ -103,10 +95,32 @@ struct Kiwix: App {
window.identifier?.rawValue == "donation"
}?.close()
}
} else {
PaymentForm(amountSelected: amountSelected)
.frame(width: 320, height: 320)
}
.windowResizability(.contentSize)
}
.onReceive(amountSelected) { amount in
selectedAmount = amount
}
.onReceive(NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)) { notification in
if let window = notification.object as? NSWindow,
window.identifier?.rawValue == "donation" {
debugPrint("closing donation")
formReset.reset()
selectedAmount = nil
}
}
.environmentObject(formReset)
// .onReceive(amountSelected) { amount in
// debugPrint("amountSelected: \(amount)")
//
// }
}
.windowResizability(.contentMinSize)
.windowStyle(.titleBar)
.commandsRemoved()
.defaultSize(width: 320, height: 400)
}
private class NotificationCenterDelegate: NSObject, UNUserNotificationCenterDelegate {
@ -138,7 +152,6 @@ struct RootView: View {
private let tabCloses = NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)
/// Close other tabs then the ones received
private let keepOnlyTabs = NotificationCenter.default.publisher(for: .keepOnlyTabs)
private let payment = Payment()
var body: some View {
NavigationSplitView {
@ -157,6 +170,7 @@ struct RootView: View {
}
}
}
.frame(minWidth: 160)
.safeAreaInset(edge: .bottom) {
SupportKiwixButton {
openWindow(id: "donation")
@ -170,7 +184,6 @@ struct RootView: View {
// .frame(width: 200, height: 30, alignment: .center)
// .padding()
}
.frame(minWidth: 160)
} detail: {
switch navigation.currentItem {
case .loading:

View File

@ -19,6 +19,8 @@ import SwiftUI
struct Payment {
let onComplete: () -> Void
static let merchantId = "merchant.org.kiwix"
static let supportedNetworks: [PKPaymentNetwork] = [.masterCard, .visa, .discover, .amex, .chinaUnionPay, .electron, .girocard, .mada]
static let capabilities: PKMerchantCapability = [.threeDSecure, .credit, .debit, .emv]
@ -78,6 +80,7 @@ struct Payment {
case .willAuthorize:
break
case .didAuthorize(let payment, let resultHandler):
debugPrint("payment success: \(payment)")
// server.process(with: payment) { serverResult in
// guard case .success = serverResult else {
// // handle error
@ -85,12 +88,13 @@ struct Payment {
// return
// }
// // handle success
// let result = PKPaymentAuthorizationResult(status: .success, errors: nil)
// resultHandler(result)
let result = PKPaymentAuthorizationResult(status: .success, errors: nil)
resultHandler(result)
onComplete()
// }
break
case .didFinish:
break
onComplete()
@unknown default:
break
}

View File

@ -19,7 +19,12 @@ import PassKit
struct PaymentSummary: View {
let selectedAmount: SelectedAmount
private let payment = Payment()
private let payment: Payment
init(selectedAmount: SelectedAmount, onComplete: @escaping () -> Void) {
self.selectedAmount = selectedAmount
payment = Payment(onComplete: onComplete)
}
var body: some View {
VStack {
@ -44,7 +49,7 @@ struct PaymentSummary: View {
.frame(width: 186, height: 44)
.padding()
} else {
Text("We are sorry to see, that your device does not support Apple Pay.")
Text("We are sorry, your device does not support Apple Pay.")
.foregroundStyle(.red)
.font(.callout)
}
@ -53,5 +58,5 @@ struct PaymentSummary: View {
}
#Preview {
PaymentSummary(selectedAmount: SelectedAmount(value: 34, currency: "CHF", isMonthly: true))
PaymentSummary(selectedAmount: SelectedAmount(value: 34, currency: "CHF", isMonthly: true), onComplete: {})
}