Skip to content

Commit 4ef4045

Browse files
committed
Add methods to save, clear, and retrieve last draft from UserDefaults
1 parent 06510f1 commit 4ef4045

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

Shared/PostEditor/PostEditorModel.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Foundation
1+
import SwiftUI
22
import CoreData
33

44
enum PostAppearance: String {
@@ -8,5 +8,25 @@ enum PostAppearance: String {
88
}
99

1010
struct PostEditorModel {
11+
@AppStorage("lastDraftURL") private var lastDraftURL: URL?
1112

13+
func saveLastDraft(_ post: WFAPost) {
14+
self.lastDraftURL = post.status != PostStatus.published.rawValue ? post.objectID.uriRepresentation() : nil
15+
}
16+
17+
func clearLastDraft() {
18+
self.lastDraftURL = nil
19+
}
20+
21+
func fetchLastDraftFromUserDefaults() -> WFAPost? {
22+
guard let postURL = lastDraftURL else { return nil }
23+
24+
let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator
25+
guard let postManagedObjectID = coordinator.managedObjectID(forURIRepresentation: postURL) else { return nil }
26+
guard let post = LocalStorageManager.persistentContainer.viewContext.object(
27+
with: postManagedObjectID
28+
) as? WFAPost else { return nil }
29+
30+
return post
31+
}
1232
}

0 commit comments

Comments
 (0)