Add methods to save, clear, and retrieve last draft from UserDefaults

This commit is contained in:
Angelo Stavrow 2020-10-30 15:26:32 -04:00
parent 06510f181e
commit 4ef40453d5
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE

View File

@ -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
}
}