diff --git a/App/App_macOS.swift b/App/App_macOS.swift index 998278d1..0c13942f 100644 --- a/App/App_macOS.swift +++ b/App/App_macOS.swift @@ -169,6 +169,7 @@ struct RootView: View { @StateObject private var navigation = NavigationViewModel() @State private var currentNavItem: NavigationItem? @StateObject private var windowTracker = WindowTracker() + @State private var paymentButtonLabel: PayWithApplePayButtonLabel? private let primaryItems: [NavigationItem] = [.bookmarks] private let libraryItems: [NavigationItem] = [.opened, .categories, .downloads, .new] @@ -197,7 +198,7 @@ struct RootView: View { } .frame(minWidth: 160) .safeAreaInset(edge: .bottom) { - if Payment.paymentButtonType() != nil && Brand.hideDonation != true { + if paymentButtonLabel != nil && Brand.hideDonation != true { SupportKiwixButton { openWindow(id: "donation") } @@ -316,6 +317,11 @@ struct RootView: View { ZimMigration.forCustomApps() currentNavItem = .tab(objectID: navigation.currentTabId) } + // MARK: - payment button init + if Brand.hideDonation == false { + paymentButtonLabel = await Payment.paymentButtonTypeAsync() + } + // MARK: - migrations if !ProcessInfo.processInfo.arguments.contains("testing") { _ = MigrationService().migrateAll() diff --git a/Model/Payment.swift b/Model/Payment.swift index 7c4fc962..8d106373 100644 --- a/Model/Payment.swift +++ b/Model/Payment.swift @@ -118,8 +118,8 @@ struct Payment { .init(value: 10) ] - /// Checks Apple Pay capabilities, and returns the button label accrodingly - /// Setup button if no cards added yet, + /// Checks Apple Pay capabilities, and returns the button label accordingly + /// - Returns: Setup button if no cards added yet, /// nil if Apple Pay is not supported /// or donation button, if all is OK static func paymentButtonType() -> PayWithApplePayButtonLabel? { @@ -136,6 +136,20 @@ struct Payment { } return nil } + + /// Async version of ``paymentButtonType()`` with low priority + /// - Returns: Setup button if no cards added yet, + /// nil if Apple Pay is not supported + /// or donation button, if all is OK + static func paymentButtonTypeAsync() async -> PayWithApplePayButtonLabel? { + let task = Task(priority: .low) { + Self.paymentButtonType() + } + guard let buttonLabel = await task.result.get() else { + return nil + } + return buttonLabel + } func donationRequest(for selectedAmount: SelectedAmount) -> PKPaymentRequest { let request = PKPaymentRequest()