Move state-fetching logic to PostEditorModel for iOS app

This commit is contained in:
Angelo Stavrow 2021-01-25 13:46:30 -05:00
parent 4ad076ccab
commit 34b14ba46c
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 17 additions and 33 deletions

View File

@ -2,8 +2,6 @@ import SwiftUI
struct CollectionListView: View {
@EnvironmentObject var model: WriteFreelyModel
@AppStorage("showAllPostsFlag") var showAllPostsFlag: Bool = false
@AppStorage("selectedCollectionURL") var selectedCollectionURL: URL?
@FetchRequest(
entity: WFACollection.entity(),
@ -79,33 +77,30 @@ struct CollectionListView: View {
.listStyle(SidebarListStyle())
.onAppear(perform: {
#if os(iOS)
DispatchQueue.main.async {
self.model.showAllPosts = showAllPostsFlag
self.model.selectedCollection = fetchSelectedCollectionFromAppStorage()
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
}
}
#endif
})
.onChange(of: model.selectedCollection) { collection in
if collection != fetchSelectedCollectionFromAppStorage() {
self.selectedCollectionURL = collection?.objectID.uriRepresentation()
if collection != model.editor.fetchSelectedCollectionFromAppStorage() {
self.model.editor.selectedCollectionURL = collection?.objectID.uriRepresentation()
}
}
.onChange(of: model.showAllPosts) { value in
if value != showAllPostsFlag {
self.showAllPostsFlag = model.showAllPosts
if value != model.editor.showAllPostsFlag {
self.model.editor.showAllPostsFlag = model.showAllPosts
}
}
}
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
}
}
struct CollectionListView_LoggedOutPreviews: PreviewProvider {

View File

@ -2,7 +2,6 @@ import SwiftUI
struct PostListFilteredView: View {
@EnvironmentObject var model: WriteFreelyModel
@AppStorage("selectedPostURL") var selectedPostURL: URL?
@Binding var postCount: Int
@FetchRequest(entity: WFACollection.entity(), sortDescriptors: []) var collections: FetchedResults<WFACollection>
var fetchRequest: FetchRequest<WFAPost>
@ -63,14 +62,14 @@ struct PostListFilteredView: View {
.onAppear(perform: {
self.postCount = fetchRequest.wrappedValue.count
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.model.selectedPost = fetchSelectedPostFromAppStorage()
self.model.selectedPost = model.editor.fetchSelectedPostFromAppStorage()
}
})
.onChange(of: fetchRequest.wrappedValue.count, perform: { value in
self.postCount = value
})
.onChange(of: model.selectedPost) { post in
if post != fetchSelectedPostFromAppStorage() {
if post != model.editor.fetchSelectedPostFromAppStorage() {
saveSelectedPostURL(post)
}
}
@ -137,17 +136,7 @@ struct PostListFilteredView: View {
}
private func saveSelectedPostURL(_ post: WFAPost?) {
self.selectedPostURL = post?.objectID.uriRepresentation()
}
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
self.model.editor.selectedPostURL = post?.objectID.uriRepresentation()
}
func delete(_ post: WFAPost) {