mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Add methods to save, clear, and retrieve last draft from UserDefaults
This commit is contained in:
parent
06510f181e
commit
4ef40453d5
@ -1,4 +1,4 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import CoreData
|
||||
|
||||
enum PostAppearance: String {
|
||||
@ -8,5 +8,25 @@ enum PostAppearance: String {
|
||||
}
|
||||
|
||||
struct PostEditorModel {
|
||||
@AppStorage("lastDraftURL") private var lastDraftURL: URL?
|
||||
|
||||
func saveLastDraft(_ post: WFAPost) {
|
||||
self.lastDraftURL = post.status != PostStatus.published.rawValue ? post.objectID.uriRepresentation() : nil
|
||||
}
|
||||
|
||||
func clearLastDraft() {
|
||||
self.lastDraftURL = nil
|
||||
}
|
||||
|
||||
func fetchLastDraftFromUserDefaults() -> WFAPost? {
|
||||
guard let postURL = lastDraftURL else { return nil }
|
||||
|
||||
let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator
|
||||
guard let postManagedObjectID = coordinator.managedObjectID(forURIRepresentation: postURL) else { return nil }
|
||||
guard let post = LocalStorageManager.persistentContainer.viewContext.object(
|
||||
with: postManagedObjectID
|
||||
) as? WFAPost else { return nil }
|
||||
|
||||
return post
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user