Skip to content

Commit 58cc120

Browse files
committed
Fix slow side menu, by using low priority on checking apple pay status
1 parent be5bbb5 commit 58cc120

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

App/App_macOS.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ struct RootView: View {
169169
@StateObject private var navigation = NavigationViewModel()
170170
@State private var currentNavItem: NavigationItem?
171171
@StateObject private var windowTracker = WindowTracker()
172+
@State private var paymentButtonLabel: PayWithApplePayButtonLabel?
172173

173174
private let primaryItems: [NavigationItem] = [.bookmarks]
174175
private let libraryItems: [NavigationItem] = [.opened, .categories, .downloads, .new]
@@ -197,7 +198,7 @@ struct RootView: View {
197198
}
198199
.frame(minWidth: 160)
199200
.safeAreaInset(edge: .bottom) {
200-
if Payment.paymentButtonType() != nil && Brand.hideDonation != true {
201+
if paymentButtonLabel != nil && Brand.hideDonation != true {
201202
SupportKiwixButton {
202203
openWindow(id: "donation")
203204
}
@@ -316,6 +317,11 @@ struct RootView: View {
316317
ZimMigration.forCustomApps()
317318
currentNavItem = .tab(objectID: navigation.currentTabId)
318319
}
320+
// MARK: - payment button init
321+
if Brand.hideDonation == false {
322+
paymentButtonLabel = await Payment.paymentButtonTypeAsync()
323+
}
324+
319325
// MARK: - migrations
320326
if !ProcessInfo.processInfo.arguments.contains("testing") {
321327
_ = MigrationService().migrateAll()

Model/Payment.swift

+16-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ struct Payment {
118118
.init(value: 10)
119119
]
120120

121-
/// Checks Apple Pay capabilities, and returns the button label accrodingly
122-
/// Setup button if no cards added yet,
121+
/// Checks Apple Pay capabilities, and returns the button label accordingly
122+
/// - Returns: Setup button if no cards added yet,
123123
/// nil if Apple Pay is not supported
124124
/// or donation button, if all is OK
125125
static func paymentButtonType() -> PayWithApplePayButtonLabel? {
@@ -136,6 +136,20 @@ struct Payment {
136136
}
137137
return nil
138138
}
139+
140+
/// Async version of ``paymentButtonType()`` with low priority
141+
/// - Returns: Setup button if no cards added yet,
142+
/// nil if Apple Pay is not supported
143+
/// or donation button, if all is OK
144+
static func paymentButtonTypeAsync() async -> PayWithApplePayButtonLabel? {
145+
let task = Task<PayWithApplePayButtonLabel?, Never>(priority: .low) {
146+
Self.paymentButtonType()
147+
}
148+
guard let buttonLabel = await task.result.get() else {
149+
return nil
150+
}
151+
return buttonLabel
152+
}
139153

140154
func donationRequest(for selectedAmount: SelectedAmount) -> PKPaymentRequest {
141155
let request = PKPaymentRequest()

0 commit comments

Comments
 (0)