mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Merge pull request #120 from writeas/restore-launch-to-editor-functionality
Restore launch to editor functionality
This commit is contained in:
commit
9e3cc1f9d4
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user