diff --git a/Shared/PersistenceManager.swift b/Shared/PersistenceManager.swift new file mode 100644 index 0000000..e75aed1 --- /dev/null +++ b/Shared/PersistenceManager.swift @@ -0,0 +1,35 @@ +import CoreData + +#if os(macOS) +import AppKit +#endif + +class PersistenceManager { + let persistentContainer: NSPersistentContainer = { + let container = NSPersistentContainer(name: "LocalStorageModel") + container.loadPersistentStores(completionHandler: { (_, error) in + if let error = error as NSError? { + fatalError("Unresolved error loading persistent store: \(error) - \(error.userInfo)") + } + }) + return container + }() + + init() { + let center = NotificationCenter.default + + #if os(iOS) + let notification = UIApplication.willResignActiveNotification + #elseif os(macOS) + let notification = NSApplication.willResignActiveNotification + #endif + + center.addObserver(forName: notification, object: nil, queue: nil) { [weak self] _ in + guard let self = self else { return } + + if self.persistentContainer.viewContext.hasChanges { + try? self.persistentContainer.viewContext.save() + } + } + } +} diff --git a/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj b/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj index 556bbe1..6eb09b0 100644 --- a/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj +++ b/WriteFreely-MultiPlatform.xcodeproj/project.pbxproj @@ -38,6 +38,8 @@ 1756DBB424FECDBB00207AB8 /* PostEditorStatusToolbarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB224FECDBB00207AB8 /* PostEditorStatusToolbarView.swift */; }; 1756DBB724FED3A400207AB8 /* LocalStorageModel.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB524FED3A400207AB8 /* LocalStorageModel.xcdatamodeld */; }; 1756DBB824FED3A400207AB8 /* LocalStorageModel.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB524FED3A400207AB8 /* LocalStorageModel.xcdatamodeld */; }; + 1756DBBA24FED45500207AB8 /* PersistenceManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB924FED45500207AB8 /* PersistenceManager.swift */; }; + 1756DBBB24FED45500207AB8 /* PersistenceManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1756DBB924FED45500207AB8 /* PersistenceManager.swift */; }; 1762DCB324EB086C0019C4EB /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1762DCB224EB086C0019C4EB /* CollectionListModel.swift */; }; 1762DCB424EB086C0019C4EB /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1762DCB224EB086C0019C4EB /* CollectionListModel.swift */; }; 1765F62A24E18EA200C9EBF0 /* SidebarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1765F62924E18EA200C9EBF0 /* SidebarView.swift */; }; @@ -96,6 +98,7 @@ 1756AE8024CB844500FD7257 /* View+Keyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+Keyboard.swift"; sourceTree = ""; }; 1756DBB224FECDBB00207AB8 /* PostEditorStatusToolbarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorStatusToolbarView.swift; sourceTree = ""; }; 1756DBB624FED3A400207AB8 /* LocalStorageModel.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = LocalStorageModel.xcdatamodel; sourceTree = ""; }; + 1756DBB924FED45500207AB8 /* PersistenceManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersistenceManager.swift; sourceTree = ""; }; 1762DCB224EB086C0019C4EB /* CollectionListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionListModel.swift; sourceTree = ""; }; 1765F62924E18EA200C9EBF0 /* SidebarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarView.swift; sourceTree = ""; }; 17A5388724DDA31F00DEFF9A /* MacAccountView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacAccountView.swift; sourceTree = ""; }; @@ -237,6 +240,7 @@ isa = PBXGroup; children = ( 17DF328124C87D3300BCE2E3 /* WriteFreely_MultiPlatformApp.swift */, + 1756DBB924FED45500207AB8 /* PersistenceManager.swift */, 17DF328324C87D3500BCE2E3 /* Assets.xcassets */, 17DF32D024C8B75C00BCE2E3 /* Account */, 1756AE7F24CB841200FD7257 /* Extensions */, @@ -550,6 +554,7 @@ buildActionMask = 2147483647; files = ( 17DF32AC24C87D3500BCE2E3 /* ContentView.swift in Sources */, + 1756DBBA24FED45500207AB8 /* PersistenceManager.swift in Sources */, 1756AE8124CB844500FD7257 /* View+Keyboard.swift in Sources */, 17120DAC24E1B99F002B9F6C /* AccountLoginView.swift in Sources */, 17120DA924E1B2F5002B9F6C /* AccountLogoutView.swift in Sources */, @@ -583,6 +588,7 @@ 171BFDF824D49FD400888236 /* PostCollection.swift in Sources */, 17DF32AD24C87D3500BCE2E3 /* ContentView.swift in Sources */, 1765F62B24E18EA200C9EBF0 /* SidebarView.swift in Sources */, + 1756DBBB24FED45500207AB8 /* PersistenceManager.swift in Sources */, 174D313324EC2831006CA9EE /* WriteFreelyModel.swift in Sources */, 1756AE7824CB2EDD00FD7257 /* PostEditorView.swift in Sources */, 17D435E924E3128F0036B539 /* PreferencesModel.swift in Sources */,