From 34b14ba46c17977a45af0b2203c21adb3916ee95 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Mon, 25 Jan 2021 13:46:30 -0500 Subject: [PATCH] Move state-fetching logic to PostEditorModel for iOS app --- .../PostCollection/CollectionListView.swift | 33 ++++++++----------- Shared/PostList/PostListFilteredView.swift | 17 ++-------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/Shared/PostCollection/CollectionListView.swift b/Shared/PostCollection/CollectionListView.swift index 8096584..79d7047 100644 --- a/Shared/PostCollection/CollectionListView.swift +++ b/Shared/PostCollection/CollectionListView.swift @@ -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 { diff --git a/Shared/PostList/PostListFilteredView.swift b/Shared/PostList/PostListFilteredView.swift index d6b35ac..dd11691 100644 --- a/Shared/PostList/PostListFilteredView.swift +++ b/Shared/PostList/PostListFilteredView.swift @@ -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 var fetchRequest: FetchRequest @@ -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) {