mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Move state-fetching logic to PostEditorModel for Mac app
This commit is contained in:
parent
dcb18c86db
commit
20f12fa7cc
@ -8,6 +8,9 @@ enum PostAppearance: String {
|
||||
}
|
||||
|
||||
struct PostEditorModel {
|
||||
@AppStorage("showAllPostsFlag") var showAllPostsFlag: Bool = false
|
||||
@AppStorage("selectedCollectionURL") var selectedCollectionURL: URL?
|
||||
@AppStorage("selectedPostURL") var selectedPostURL: URL?
|
||||
@AppStorage("lastDraftURL") private var lastDraftURL: URL?
|
||||
|
||||
func saveLastDraft(_ post: WFAPost) {
|
||||
@ -29,4 +32,45 @@ struct PostEditorModel {
|
||||
|
||||
return post
|
||||
}
|
||||
|
||||
func generateNewLocalPost(withAppearance appearance: Int) -> WFAPost {
|
||||
let managedPost = WFAPost(context: LocalStorageManager.persistentContainer.viewContext)
|
||||
managedPost.createdDate = Date()
|
||||
managedPost.title = ""
|
||||
managedPost.body = ""
|
||||
managedPost.status = PostStatus.local.rawValue
|
||||
managedPost.collectionAlias = nil
|
||||
switch appearance {
|
||||
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
|
||||
}
|
||||
return managedPost
|
||||
}
|
||||
|
||||
private func fetchManagedObject(from objectURL: URL) -> NSManagedObject? {
|
||||
let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator
|
||||
guard let managedObjectID = coordinator.managedObjectID(forURIRepresentation: objectURL) else { return nil }
|
||||
let object = LocalStorageManager.persistentContainer.viewContext.object(with: managedObjectID)
|
||||
return object
|
||||
}
|
||||
|
||||
func fetchSelectedPostFromAppStorage() -> WFAPost? {
|
||||
guard let postURL = selectedPostURL else { return nil }
|
||||
guard let post = fetchManagedObject(from: postURL) as? WFAPost else { return nil }
|
||||
return post
|
||||
}
|
||||
|
||||
func fetchSelectedCollectionFromAppStorage() -> WFACollection? {
|
||||
guard let collectionURL = selectedCollectionURL else { return nil }
|
||||
guard let collection = fetchManagedObject(from: collectionURL) as? WFACollection else { return nil }
|
||||
return collection
|
||||
}
|
||||
}
|
||||
|
@ -22,9 +22,6 @@ struct CheckForDebugModifier {
|
||||
|
||||
struct WriteFreely_MultiPlatformApp: App {
|
||||
@StateObject private var model = WriteFreelyModel()
|
||||
@AppStorage("showAllPostsFlag") var showAllPostsFlag: Bool = false
|
||||
@AppStorage("selectedCollectionURL") var selectedCollectionURL: URL?
|
||||
@AppStorage("selectedPostURL") var selectedPostURL: URL?
|
||||
|
||||
#if os(macOS)
|
||||
// swiftlint:disable:next weak_delegate
|
||||
@ -37,9 +34,20 @@ struct WriteFreely_MultiPlatformApp: App {
|
||||
ContentView()
|
||||
.onAppear(perform: {
|
||||
#if os(macOS)
|
||||
self.model.showAllPosts = showAllPostsFlag
|
||||
self.model.selectedCollection = fetchSelectedCollectionFromAppStorage()
|
||||
self.model.selectedPost = fetchSelectedPostFromAppStorage()
|
||||
if model.editor.showAllPostsFlag {
|
||||
DispatchQueue.main.async {
|
||||
self.model.selectedCollection = nil
|
||||
self.model.showAllPosts = true
|
||||
}
|
||||
} else {
|
||||
DispatchQueue.main.async {
|
||||
self.model.selectedCollection = model.editor.fetchSelectedCollectionFromAppStorage()
|
||||
self.model.showAllPosts = false
|
||||
}
|
||||
}
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||
self.model.selectedPost = model.editor.fetchSelectedPostFromAppStorage()
|
||||
}
|
||||
#endif
|
||||
})
|
||||
.environmentObject(model)
|
||||
@ -125,48 +133,12 @@ struct WriteFreely_MultiPlatformApp: App {
|
||||
self.model.selectedCollection = nil
|
||||
}
|
||||
// Create the new-post managed object
|
||||
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
|
||||
}
|
||||
let managedPost = model.editor.generateNewLocalPost(withAppearance: model.preferences.font)
|
||||
withAnimation {
|
||||
// Set it as the selectedPost
|
||||
DispatchQueue.main.asyncAfter(deadline: .now()) {
|
||||
self.model.selectedPost = managedPost
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func fetchSelectedPostFromAppStorage() -> WFAPost? {
|
||||
guard let objectURL = selectedPostURL else { return nil }
|
||||
let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator
|
||||
guard let managedObjectID = coordinator.managedObjectID(forURIRepresentation: objectURL) else { return nil }
|
||||
guard let object = LocalStorageManager.persistentContainer.viewContext.object(
|
||||
with: managedObjectID
|
||||
) as? WFAPost else { return nil }
|
||||
return object
|
||||
}
|
||||
|
||||
private func fetchSelectedCollectionFromAppStorage() -> WFACollection? {
|
||||
guard let objectURL = selectedCollectionURL else { return nil }
|
||||
let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator
|
||||
guard let managedObjectID = coordinator.managedObjectID(forURIRepresentation: objectURL) else { return nil }
|
||||
guard let object = LocalStorageManager.persistentContainer.viewContext.object(
|
||||
with: managedObjectID
|
||||
) as? WFACollection else { return nil }
|
||||
return object
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user