Skip to content

Commit

Permalink
Merge pull request #724 from kiwix/723-unlinked-zim-file-can-cause-in…
Browse files Browse the repository at this point in the history
…valid-tab-state-on-macos

Fix unlinked ZIM file tab data
  • Loading branch information
kelson42 authored Apr 9, 2024
2 parents 82b984d + 14ebb96 commit 5024548
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
14 changes: 13 additions & 1 deletion App/App_macOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import CoreKiwix

#if os(macOS)
final class AppDelegate: NSObject, NSApplicationDelegate {
@MainActor func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
true
}
}
Expand Down Expand Up @@ -101,6 +101,7 @@ struct RootView: View {
private let libraryItems: [NavigationItem] = [.opened, .categories, .downloads, .new]
private let openURL = NotificationCenter.default.publisher(for: .openURL)
private let appTerminates = NotificationCenter.default.publisher(for: NSApplication.willTerminateNotification)
private let tabCloses = NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)

var body: some View {
NavigationView {
Expand Down Expand Up @@ -185,7 +186,18 @@ struct RootView: View {
navigation.currentItem = .reading
browser.load(url: url)
}
.onReceive(tabCloses) { _ in
guard !navigation.isTerminating else {
// tab closed by app termination
return
}
if let tabID = browser.tabID {
// tab closed by user
navigation.deleteTab(tabID: tabID)
}
}
.onReceive(appTerminates) { _ in
navigation.isTerminating = true
browser.persistAllTabIdsFromWindows()
}.task {
switch AppType.current {
Expand Down
1 change: 1 addition & 0 deletions SwiftUI/Model/LibraryOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ struct LibraryOperations {
zimFile.fileURLBookmark = nil
zimFile.isMissing = false
}
zimFile.tabs.forEach { context.delete($0) }
if context.hasChanges { try? context.save() }
}
}
Expand Down
9 changes: 8 additions & 1 deletion ViewModel/NavigationViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import WebKit
final class NavigationViewModel: ObservableObject {
// remained optional due to focusedSceneValue conformance
@Published var currentItem: NavigationItem? = .loading
#if os(macOS)
var isTerminating: Bool = false
#endif

// MARK: - Tab Management

Expand Down Expand Up @@ -59,7 +62,11 @@ final class NavigationViewModel: ObservableObject {
func tabIDFor(url: URL?) -> NSManagedObjectID {
guard let url,
let coordinator = Database.viewContext.persistentStoreCoordinator,
let tabID = coordinator.managedObjectID(forURIRepresentation: url) else {
let tabID = coordinator.managedObjectID(forURIRepresentation: url),
// make sure it's not went missing
let tab = try? Database.viewContext.existingObject(with: tabID) as? Tab,
tab.zimFile != nil
else {
return createTab()
}
return tabID
Expand Down

0 comments on commit 5024548

Please sign in to comment.