Merge pull request #120 from writeas/restore-launch-to-editor-functionality

Restore launch to editor functionality
This commit is contained in:
Angelo Stavrow 2020-11-06 14:47:21 -05:00 committed by GitHub
commit 9e3cc1f9d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 2 deletions

View File

@ -24,7 +24,7 @@ struct PostEditorModel {
let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator
guard let postManagedObjectID = coordinator.managedObjectID(forURIRepresentation: postURL) else { return nil }
guard let post = LocalStorageManager.persistentContainer.viewContext.object(
with: postManagedObjectID
with: postManagedObjectID
) as? WFAPost else { return nil }
return post

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

View File

@ -136,15 +136,28 @@ struct PostEditorView: View {
model.move(post: post, from: selectedCollection, to: newCollection)
}
})
.onChange(of: post.status, perform: { value in
if value != PostStatus.published.rawValue {
self.model.editor.saveLastDraft(post)
} else {
self.model.editor.clearLastDraft()
}
DispatchQueue.main.async {
LocalStorageManager().saveContext()
}
})
.onAppear(perform: {
self.selectedCollection = collections.first { $0.alias == post.collectionAlias }
if post.status != PostStatus.published.rawValue {
self.model.editor.saveLastDraft(post)
DispatchQueue.main.async {
self.model.editor.saveLastDraft(post)
}
} else {
self.model.editor.clearLastDraft()
}
})
.onDisappear(perform: {
self.model.editor.clearLastDraft()
if post.title.count == 0
&& post.body.count == 0
&& post.status == PostStatus.local.rawValue