Load last draft or new post on launch

This commit is contained in:
Angelo Stavrow 2020-11-04 15:23:03 -05:00
parent 4d37cae4bf
commit 444859b2fb
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE

View File

@ -11,6 +11,13 @@ struct WriteFreely_MultiPlatformApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onAppear(perform: {
if let lastDraft = model.editor.fetchLastDraftFromUserDefaults() {
self.model.selectedPost = lastDraft
} else {
createNewLocalPost()
}
})
.environmentObject(model)
.environment(\.managedObjectContext, LocalStorageManager.persistentContainer.viewContext)
// .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info.
@ -39,4 +46,26 @@ struct WriteFreely_MultiPlatformApp: App {
}
#endif
}
private func createNewLocalPost() {
let managedPost = WFAPost(context: LocalStorageManager.persistentContainer.viewContext)
managedPost.createdDate = Date()
managedPost.title = ""
managedPost.body = ""
managedPost.status = PostStatus.local.rawValue
managedPost.collectionAlias = nil
switch model.preferences.font {
case 1:
managedPost.appearance = "sans"
case 2:
managedPost.appearance = "wrap"
default:
managedPost.appearance = "serif"
}
if let languageCode = Locale.current.languageCode {
managedPost.language = languageCode
managedPost.rtl = Locale.characterDirection(forLanguage: languageCode) == .rightToLeft
}
self.model.selectedPost = managedPost
}
}