Skip to content

Commit 2cb1087

Browse files
authored
Merge pull request #1135 from kiwix/1104-side-menu-optimalisation
1104 side menu optimalization
2 parents 31c4d52 + 58cc120 commit 2cb1087

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

App/App_macOS.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ struct RootView: View {
167167
@Environment(\.openWindow) var openWindow
168168
@Environment(\.controlActiveState) var controlActiveState
169169
@StateObject private var navigation = NavigationViewModel()
170+
@State private var currentNavItem: NavigationItem?
170171
@StateObject private var windowTracker = WindowTracker()
172+
@State private var paymentButtonLabel: PayWithApplePayButtonLabel?
171173

172174
private let primaryItems: [NavigationItem] = [.bookmarks]
173175
private let libraryItems: [NavigationItem] = [.opened, .categories, .downloads, .new]
@@ -179,7 +181,7 @@ struct RootView: View {
179181

180182
var body: some View {
181183
NavigationSplitView {
182-
List(selection: $navigation.currentItem) {
184+
List(selection: $currentNavItem) {
183185
ForEach(
184186
[NavigationItem.tab(objectID: navigation.currentTabId)] + primaryItems,
185187
id: \.self
@@ -196,7 +198,7 @@ struct RootView: View {
196198
}
197199
.frame(minWidth: 160)
198200
.safeAreaInset(edge: .bottom) {
199-
if Payment.paymentButtonType() != nil && Brand.hideDonation != true {
201+
if paymentButtonLabel != nil && Brand.hideDonation != true {
200202
SupportKiwixButton {
201203
openWindow(id: "donation")
202204
}
@@ -229,6 +231,9 @@ struct RootView: View {
229231
.modifier(OpenFileHandler())
230232
.modifier(SaveContentHandler())
231233
.environmentObject(navigation)
234+
.onChange(of: currentNavItem) { newValue in
235+
navigation.currentItem = newValue
236+
}
232237
.onOpenURL { url in
233238
if url.isFileURL {
234239
// from opening an external file
@@ -267,7 +272,7 @@ struct RootView: View {
267272
}
268273
guard controlActiveState == .key else { return }
269274
let tabID = navigation.currentTabId
270-
navigation.currentItem = .tab(objectID: tabID)
275+
currentNavItem = .tab(objectID: tabID)
271276
BrowserViewModel.getCached(tabID: tabID).load(url: url)
272277
}
273278
.onReceive(tabCloses) { publisher in
@@ -303,15 +308,20 @@ struct RootView: View {
303308
switch AppType.current {
304309
case .kiwix:
305310
await LibraryOperations.reopen()
306-
navigation.currentItem = .tab(objectID: navigation.currentTabId)
311+
currentNavItem = .tab(objectID: navigation.currentTabId)
307312
LibraryOperations.scanDirectory(URL.documentDirectory)
308313
LibraryOperations.applyFileBackupSetting()
309314
DownloadService.shared.restartHeartbeatIfNeeded()
310315
case let .custom(zimFileURL):
311316
await LibraryOperations.open(url: zimFileURL)
312317
ZimMigration.forCustomApps()
313-
navigation.currentItem = .tab(objectID: navigation.currentTabId)
318+
currentNavItem = .tab(objectID: navigation.currentTabId)
319+
}
320+
// MARK: - payment button init
321+
if Brand.hideDonation == false {
322+
paymentButtonLabel = await Payment.paymentButtonTypeAsync()
314323
}
324+
315325
// MARK: - migrations
316326
if !ProcessInfo.processInfo.arguments.contains("testing") {
317327
_ = MigrationService().migrateAll()

Model/Payment.swift

Lines changed: 16 additions & 2 deletions
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)