From d76de07636e66bf6db86ae71c209950a6f94a3f6 Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Sun, 3 Nov 2024 00:20:02 +0100 Subject: [PATCH] Handle window close on transaction complete --- App/App_macOS.swift | 53 +++++++++++++++++++----------- Model/Payment.swift | 10 ++++-- Views/Payment/PaymentSummary.swift | 11 +++++-- 3 files changed, 48 insertions(+), 26 deletions(-) diff --git a/App/App_macOS.swift b/App/App_macOS.swift index 17140198..c0d05026 100644 --- a/App/App_macOS.swift +++ b/App/App_macOS.swift @@ -33,6 +33,7 @@ struct Kiwix: App { @StateObject private var libraryRefreshViewModel = LibraryViewModel() private let notificationCenterDelegate = NotificationCenterDelegate() private var amountSelected = PassthroughSubject() + @State private var selectedAmount: SelectedAmount? @StateObject var formReset = FormReset() init() { @@ -83,30 +84,43 @@ 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() + Group { + if let selectedAmount { + PaymentSummary(selectedAmount: selectedAmount) { + // after upgrading to macOS 14, use: + // @Environment(\.dismissWindow) var dismissWindow + // and call: + // 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 - debugPrint("amountSelected: \(amount)") - // after upgrading to macOS 14, use: - // @Environment(\.dismissWindow) var dismissWindow - // and call: - // dismissWindow(id: "donation") - NSApplication.shared.windows.first { window in - window.identifier?.rawValue == "donation" - }?.close() + } + .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(.contentSize) + .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: diff --git a/Model/Payment.swift b/Model/Payment.swift index 1108f075..f7cc062a 100644 --- a/Model/Payment.swift +++ b/Model/Payment.swift @@ -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 } diff --git a/Views/Payment/PaymentSummary.swift b/Views/Payment/PaymentSummary.swift index 0d8742c9..0ff3df6f 100644 --- a/Views/Payment/PaymentSummary.swift +++ b/Views/Payment/PaymentSummary.swift @@ -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: {}) }