From 212f7ba4fe108f86510f7089c7bb0c3c942f5c14 Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Mon, 8 Apr 2024 08:48:14 +0200 Subject: [PATCH 1/2] Fix unlinked ZIM file tab state --- App/App_macOS.swift | 14 +++++++++++++- SwiftUI/Model/LibraryOperations.swift | 1 + ViewModel/NavigationViewModel.swift | 9 ++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/App/App_macOS.swift b/App/App_macOS.swift index 318b33204..ddf2e60dd 100644 --- a/App/App_macOS.swift +++ b/App/App_macOS.swift @@ -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 } } @@ -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 { @@ -185,7 +186,18 @@ struct RootView: View { navigation.currentItem = .reading browser.load(url: url) } + .onReceive(tabCloses) { item 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 { diff --git a/SwiftUI/Model/LibraryOperations.swift b/SwiftUI/Model/LibraryOperations.swift index b2dad67a9..60c6f8330 100644 --- a/SwiftUI/Model/LibraryOperations.swift +++ b/SwiftUI/Model/LibraryOperations.swift @@ -158,6 +158,7 @@ struct LibraryOperations { zimFile.fileURLBookmark = nil zimFile.isMissing = false } + zimFile.tabs.forEach { context.delete($0) } if context.hasChanges { try? context.save() } } } diff --git a/ViewModel/NavigationViewModel.swift b/ViewModel/NavigationViewModel.swift index 8491bddb4..5486b27d4 100644 --- a/ViewModel/NavigationViewModel.swift +++ b/ViewModel/NavigationViewModel.swift @@ -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 @@ -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 From 14ebb96993b4d643fc1650c432ec287194f99d2f Mon Sep 17 00:00:00 2001 From: codefactor-io Date: Tue, 9 Apr 2024 00:32:32 +0000 Subject: [PATCH 2/2] [CodeFactor] Apply fixes --- App/App_macOS.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/App/App_macOS.swift b/App/App_macOS.swift index ddf2e60dd..22a68fc94 100644 --- a/App/App_macOS.swift +++ b/App/App_macOS.swift @@ -186,7 +186,7 @@ struct RootView: View { navigation.currentItem = .reading browser.load(url: url) } - .onReceive(tabCloses) { item in + .onReceive(tabCloses) { _ in guard !navigation.isTerminating else { // tab closed by app termination return