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

@ -77,39 +77,35 @@ struct OpenFileHandler: ViewModifier {
invalidURLs.insert(url) invalidURLs.insert(url)
} }
} }
// action for zim files that can be opened (e.g. open main page) if case .library = context {
switch context { // don't need to open the main page
case .command, .file: } else {
for fileID in openedZimFileIDs { for fileID in openedZimFileIDs {
guard let url = await ZimFileService.shared.getMainPageURL(zimFileID: fileID) else { return } if let url = await ZimFileService.shared.getMainPageURL(zimFileID: fileID) {
#if os(macOS) // action for zim files that can be opened (e.g. open main page)
switch context { switch context {
case .command: #if os(iOS)
NotificationCenter.openURL(url, inNewTab: true) case .file(.none):
case .file: NotificationCenter.openURL(url, inNewTab: true)
// Note: inNewTab:true/false has no meaning here, the system will open a new window anyway case .file(.some(let deepLinkID)):
NotificationCenter.openURL(url, inNewTab: true, context: .file) NotificationCenter.openURL(
default: url,
break inNewTab: true,
context: .deepLink(id: deepLinkID)
)
#else
case .command:
NotificationCenter.openURL(url, inNewTab: true)
#endif
case .welcomeScreen:
NotificationCenter.openURL(url)
case .library:
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
} }
case .welcomeScreen:
for fileID in openedZimFileIDs {
guard let url = await ZimFileService.shared.getMainPageURL(zimFileID: fileID) else { return }
NotificationCenter.openURL(url)
}
default:
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