Skip to content

Commit

Permalink
close ZIM files
Browse files Browse the repository at this point in the history
  • Loading branch information
BPerlakiH committed Oct 18, 2024
1 parent c78d229 commit 5f67f92
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions SwiftUI/Patches.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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])
}
Expand Down
20 changes: 20 additions & 0 deletions ViewModel/BrowserViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyCancellable>()
private var canGoBackObserver: NSKeyValueObservation?
private var canGoForwardObserver: NSKeyValueObservation?
private var titleURLObserver: AnyCancellable?
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions Views/Library/ZimFileDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 5f67f92

Please sign in to comment.