Move lastDraft-setting logic to ContentView.onAppear handler

This commit is contained in:
Angelo Stavrow 2020-09-24 14:14:35 -04:00
parent 44fd09aa33
commit ce409486d4
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 24 additions and 28 deletions

View File

@ -61,34 +61,6 @@ class WriteFreelyModel: ObservableObject {
self.fetchUserCollections()
self.fetchUserPosts()
}
if let lastDraft = self.editor.fetchLastDraft() {
DispatchQueue.main.async {
self.selectedPost = lastDraft
}
} else {
let managedPost = WFAPost(context: LocalStorageManager.persistentContainer.viewContext)
managedPost.createdDate = Date()
managedPost.title = ""
managedPost.body = ""
managedPost.status = PostStatus.local.rawValue
switch self.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
}
DispatchQueue.main.async {
LocalStorageManager().saveContext()
self.selectedPost = managedPost
}
}
}
monitor.pathUpdateHandler = { path in

View File

@ -12,6 +12,30 @@ struct ContentView: View {
Text("Select a post, or create a new local draft.")
.foregroundColor(.secondary)
}
.onAppear(perform: {
if let lastDraft = self.model.editor.fetchLastDraft() {
model.selectedPost = lastDraft
} else {
let managedPost = WFAPost(context: LocalStorageManager.persistentContainer.viewContext)
managedPost.createdDate = Date()
managedPost.title = ""
managedPost.body = ""
managedPost.status = PostStatus.local.rawValue
switch self.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
}
model.selectedPost = managedPost
}
})
.environmentObject(model)
.alert(isPresented: $model.isPresentingDeleteAlert) {
Alert(