Move state-fetching logic to PostEditorModel for Mac app

This commit is contained in:
Angelo Stavrow 2021-01-25 11:30:52 -05:00
parent dcb18c86db
commit 20f12fa7cc
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 60 additions and 44 deletions

View File

@ -8,6 +8,9 @@ enum PostAppearance: String {
} }
struct PostEditorModel { struct PostEditorModel {
@AppStorage("showAllPostsFlag") var showAllPostsFlag: Bool = false
@AppStorage("selectedCollectionURL") var selectedCollectionURL: URL?
@AppStorage("selectedPostURL") var selectedPostURL: URL?
@AppStorage("lastDraftURL") private var lastDraftURL: URL? @AppStorage("lastDraftURL") private var lastDraftURL: URL?
func saveLastDraft(_ post: WFAPost) { func saveLastDraft(_ post: WFAPost) {
@ -29,4 +32,45 @@ struct PostEditorModel {
return post 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
}
} }

View File

@ -22,9 +22,6 @@ struct CheckForDebugModifier {
struct WriteFreely_MultiPlatformApp: App { struct WriteFreely_MultiPlatformApp: App {
@StateObject private var model = WriteFreelyModel() @StateObject private var model = WriteFreelyModel()
@AppStorage("showAllPostsFlag") var showAllPostsFlag: Bool = false
@AppStorage("selectedCollectionURL") var selectedCollectionURL: URL?
@AppStorage("selectedPostURL") var selectedPostURL: URL?
#if os(macOS) #if os(macOS)
// swiftlint:disable:next weak_delegate // swiftlint:disable:next weak_delegate
@ -37,9 +34,20 @@ struct WriteFreely_MultiPlatformApp: App {
ContentView() ContentView()
.onAppear(perform: { .onAppear(perform: {
#if os(macOS) #if os(macOS)
self.model.showAllPosts = showAllPostsFlag if model.editor.showAllPostsFlag {
self.model.selectedCollection = fetchSelectedCollectionFromAppStorage() DispatchQueue.main.async {
self.model.selectedPost = fetchSelectedPostFromAppStorage() 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 #endif
}) })
.environmentObject(model) .environmentObject(model)
@ -125,48 +133,12 @@ struct WriteFreely_MultiPlatformApp: App {
self.model.selectedCollection = nil self.model.selectedCollection = nil
} }
// Create the new-post managed object // Create the new-post managed object
let managedPost = WFAPost(context: LocalStorageManager.persistentContainer.viewContext) let managedPost = model.editor.generateNewLocalPost(withAppearance: model.preferences.font)
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
}
withAnimation { withAnimation {
// Set it as the selectedPost
DispatchQueue.main.asyncAfter(deadline: .now()) { DispatchQueue.main.asyncAfter(deadline: .now()) {
self.model.selectedPost = managedPost 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
}
} }