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

View File

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

View File

@ -19,7 +19,12 @@ import PassKit
struct PaymentSummary: View { struct PaymentSummary: View {
let selectedAmount: SelectedAmount 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 { var body: some View {
VStack { VStack {
@ -44,7 +49,7 @@ struct PaymentSummary: View {
.frame(width: 186, height: 44) .frame(width: 186, height: 44)
.padding() .padding()
} else { } 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) .foregroundStyle(.red)
.font(.callout) .font(.callout)
} }
@ -53,5 +58,5 @@ struct PaymentSummary: View {
} }
#Preview { #Preview {
PaymentSummary(selectedAmount: SelectedAmount(value: 34, currency: "CHF", isMonthly: true)) PaymentSummary(selectedAmount: SelectedAmount(value: 34, currency: "CHF", isMonthly: true), onComplete: {})
} }