Reduce open file cases

This commit is contained in:
Balazs Perlaki-Horvath 2025-04-27 22:13:01 +02:00
parent b2613c2ca8
commit 6199536717
2 changed files with 29 additions and 31 deletions

View File

@ -140,12 +140,14 @@ enum ExternalLinkLoadingPolicy: String, CaseIterable, Identifiable, Defaults.Ser
enum OpenURLContext { enum OpenURLContext {
case deepLink(id: UUID?) case deepLink(id: UUID?)
case file
} }
enum OpenFileContext { enum OpenFileContext {
case command #if os(iOS)
case file(deepLinkId: UUID? = nil) case file(deepLinkId: UUID? = nil)
#else
case command
#endif
case welcomeScreen case welcomeScreen
case library case library
} }

View File

@ -78,38 +78,34 @@ struct OpenFileHandler: ViewModifier {
} }
} }
if case .library = context {
// don't need to open the main page
} else {
for fileID in openedZimFileIDs {
if let url = await ZimFileService.shared.getMainPageURL(zimFileID: fileID) {
// action for zim files that can be opened (e.g. open main page) // action for zim files that can be opened (e.g. open main page)
switch context { switch context {
case .command, .file: #if os(iOS)
for fileID in openedZimFileIDs { case .file(.none):
guard let url = await ZimFileService.shared.getMainPageURL(zimFileID: fileID) else { return } NotificationCenter.openURL(url, inNewTab: true)
#if os(macOS) case .file(.some(let deepLinkID)):
switch context { NotificationCenter.openURL(
url,
inNewTab: true,
context: .deepLink(id: deepLinkID)
)
#else
case .command: case .command:
NotificationCenter.openURL(url, inNewTab: true) NotificationCenter.openURL(url, inNewTab: true)
case .file:
// Note: inNewTab:true/false has no meaning here, the system will open a new window anyway
NotificationCenter.openURL(url, inNewTab: true, context: .file)
default:
break
}
#elseif os(iOS)
if case .file(.some(let deepLinkID)) = context {
NotificationCenter.openURL(url, inNewTab: true, context: .deepLink(id: deepLinkID))
} else {
NotificationCenter.openURL(url, inNewTab: true)
}
#endif #endif
}
case .welcomeScreen: case .welcomeScreen:
for fileID in openedZimFileIDs {
guard let url = await ZimFileService.shared.getMainPageURL(zimFileID: fileID) else { return }
NotificationCenter.openURL(url) NotificationCenter.openURL(url)
} case .library:
default:
break break
} }
}
}
}
// show alert if there are any files that cannot be opened // show alert if there are any files that cannot be opened
if !invalidURLs.isEmpty { if !invalidURLs.isEmpty {
isAlertPresented = true isAlertPresented = true