From 5f67f92643f2b95008300b9992a0cd24ea308197 Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Fri, 11 Oct 2024 21:24:30 +0200 Subject: [PATCH] close ZIM files --- SwiftUI/Patches.swift | 7 +++++++ ViewModel/BrowserViewModel.swift | 20 ++++++++++++++++++++ Views/Library/ZimFileDetail.swift | 8 ++++++++ 3 files changed, 35 insertions(+) diff --git a/SwiftUI/Patches.swift b/SwiftUI/Patches.swift index bca8e79e4..03496d328 100644 --- a/SwiftUI/Patches.swift +++ b/SwiftUI/Patches.swift @@ -71,6 +71,7 @@ extension Notification.Name { static let alert = Notification.Name("alert") static let openFiles = Notification.Name("openFiles") static let openURL = Notification.Name("openURL") + static let closeZIM = Notification.Name("closeZIM") static let exportFileData = Notification.Name("exportFileData") static let saveContent = Notification.Name("saveContent") static let toggleSidebar = Notification.Name("toggleSidebar") @@ -93,6 +94,12 @@ extension NotificationCenter { ) } + static func closeZIM(_ zimId: UUID) { + NotificationCenter.default.post(name: .closeZIM, + object: nil, + userInfo: ["zimId": zimId]) + } + static func openFiles(_ urls: [URL], context: OpenFileContext) { NotificationCenter.default.post(name: .openFiles, object: nil, userInfo: ["urls": urls, "context": context]) } diff --git a/ViewModel/BrowserViewModel.swift b/ViewModel/BrowserViewModel.swift index 40dc48a38..a7e5ad78e 100644 --- a/ViewModel/BrowserViewModel.swift +++ b/ViewModel/BrowserViewModel.swift @@ -91,6 +91,8 @@ final class BrowserViewModel: NSObject, ObservableObject, #endif let webView: WKWebView private var isLoadingObserver: NSKeyValueObservation? + private let closeZimPublisher = NotificationCenter.default.publisher(for: .closeZIM) + private var cancellables = Set() private var canGoBackObserver: NSKeyValueObservation? private var canGoForwardObserver: NSKeyValueObservation? private var titleURLObserver: AnyCancellable? @@ -171,6 +173,24 @@ final class BrowserViewModel: NSObject, ObservableObject, } } } + closeZimPublisher.sink { [weak self] notification in + guard let zimIdToClose = notification.userInfo?["zimId"] as? UUID else { + return + } + guard let url = self?.url, url.isZIMURL, + let currentHost = url.host(), + let currentZimId = UUID(uuidString: currentHost), + zimIdToClose == currentZimId else { + return + } + Task { + await self?.webView.closeAllMediaPresentations() + #if os(macOS) + self?.webView.window?.close() + #endif + } + }.store(in: &cancellables) + } /// Get the webpage in a binary format diff --git a/Views/Library/ZimFileDetail.swift b/Views/Library/ZimFileDetail.swift index 208026b77..608b07355 100644 --- a/Views/Library/ZimFileDetail.swift +++ b/Views/Library/ZimFileDetail.swift @@ -143,7 +143,9 @@ struct ZimFileDetail: View { message: Text("zim_file.action.unlink.message".localized), primaryButton: .destructive(Text("zim_file.action.unlink.button.title".localized)) { Task { + let tabIds = zimFile.tabs.map { $0.objectID } await LibraryOperations.unlink(zimFileID: zimFile.fileID) + closeZIM(zimFileId: zimFile.fileID) #if os(iOS) dismiss() #endif @@ -154,6 +156,11 @@ struct ZimFileDetail: View { } } + @MainActor + func closeZIM(zimFileId: UUID) { + NotificationCenter.closeZIM(zimFileId) + } + var deleteAction: some View { Action(title: "zim_file.action.delete.title".localized, isDestructive: true) { isPresentingDeleteAlert = true @@ -164,6 +171,7 @@ struct ZimFileDetail: View { primaryButton: .destructive(Text("zim_file.action.delete.button.title".localized)) { Task { await LibraryOperations.delete(zimFileID: zimFile.fileID) + closeZIM(zimFileId: zimFile.fileID) #if os(iOS) dismiss() #endif