diff --git a/Shared/Models/WriteFreelyModel.swift b/Shared/Models/WriteFreelyModel.swift index f5b1011..d392e02 100644 --- a/Shared/Models/WriteFreelyModel.swift +++ b/Shared/Models/WriteFreelyModel.swift @@ -24,6 +24,8 @@ final class WriteFreelyModel: ObservableObject { @Published var isPresentingSettingsView: Bool = false #endif + static var shared = WriteFreelyModel() + var loginErrorMessage: String? // swiftlint:disable line_length diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index 09d389f..a60322f 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -22,10 +22,6 @@ struct ContentView: View { withAnimation { // Un-set the currently selected post self.model.selectedPost = nil - - // Navigate to the Drafts list - self.model.showAllPosts = false - self.model.selectedCollection = nil } // Create the new-post managed object let managedPost = model.editor.generateNewLocalPost(withFont: model.preferences.font) diff --git a/Shared/PostEditor/PostEditorModel.swift b/Shared/PostEditor/PostEditorModel.swift index 014681f..8219ead 100644 --- a/Shared/PostEditor/PostEditorModel.swift +++ b/Shared/PostEditor/PostEditorModel.swift @@ -32,7 +32,7 @@ struct PostEditorModel { managedPost.title = "" managedPost.body = "" managedPost.status = PostStatus.local.rawValue - managedPost.collectionAlias = nil + managedPost.collectionAlias = WriteFreelyModel.shared.selectedCollection?.alias switch appearance { case 1: managedPost.appearance = "sans" diff --git a/Shared/WriteFreely_MultiPlatformApp.swift b/Shared/WriteFreely_MultiPlatformApp.swift index bca2dd9..900a267 100644 --- a/Shared/WriteFreely_MultiPlatformApp.swift +++ b/Shared/WriteFreely_MultiPlatformApp.swift @@ -22,7 +22,7 @@ struct CheckForDebugModifier { } struct WriteFreely_MultiPlatformApp: App { - @StateObject private var model = WriteFreelyModel() + @StateObject private var model = WriteFreelyModel.shared #if os(macOS) // swiftlint:disable:next weak_delegate @@ -132,10 +132,6 @@ struct WriteFreely_MultiPlatformApp: App { withAnimation { // Un-set the currently selected post self.model.selectedPost = nil - - // Navigate to the Drafts list - self.model.showAllPosts = false - self.model.selectedCollection = nil } // Create the new-post managed object let managedPost = model.editor.generateNewLocalPost(withFont: model.preferences.font) diff --git a/macOS/PostEditor/PostTextEditingView.swift b/macOS/PostEditor/PostTextEditingView.swift index feb6e5c..fabae24 100644 --- a/macOS/PostEditor/PostTextEditingView.swift +++ b/macOS/PostEditor/PostTextEditingView.swift @@ -5,6 +5,9 @@ struct PostTextEditingView: View { @Binding var updatingFromServer: Bool @State private var appearance: PostAppearance = .serif @State private var combinedText = "" + @State private var hasBeenEdited: Bool = false + + let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() var body: some View { ZStack(alignment: .topLeading) { @@ -54,10 +57,18 @@ struct PostTextEditingView: View { self.combinedText = "# \(post.title)\n\n\(post.body)" } }) + .onReceive(timer) { _ in + if !post.body.isEmpty && hasBeenEdited { + DispatchQueue.main.async { + LocalStorageManager().saveContext() + hasBeenEdited = false + } + } + } } private func onEditingChanged() { - // Add code here to take action when the user first starts typing. + hasBeenEdited = true } private func onTextChange(_ text: String) { @@ -70,10 +81,16 @@ struct PostTextEditingView: View { if updatingFromServer { self.updatingFromServer = false } + hasBeenEdited = true } private func onCommit() { - // Add code here to take action when the user navigates away from the post. + if !post.body.isEmpty && hasBeenEdited { + DispatchQueue.main.async { + LocalStorageManager().saveContext() + } + } + hasBeenEdited = false } private func extractTitle(_ text: String) { @@ -95,5 +112,4 @@ struct PostTextEditingView: View { self.post.body = text } } - }