Fix slow side menu, by using low priority on checking apple pay status

This commit is contained in:
Balazs Perlaki-Horvath 2025-03-08 14:28:07 +01:00
parent be5bbb50ec
commit 58cc120315
2 changed files with 23 additions and 3 deletions

View File

@ -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()

View File

@ -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<PayWithApplePayButtonLabel?, Never>(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()