From 6beee8cf3216347836fc35b8bc01aafa9a667880 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Tue, 19 Jan 2021 10:33:25 -0500 Subject: [PATCH] Prevent AppStorage updates if there's no change --- Shared/PostCollection/CollectionListView.swift | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Shared/PostCollection/CollectionListView.swift b/Shared/PostCollection/CollectionListView.swift index abe1f8b..aebe15f 100644 --- a/Shared/PostCollection/CollectionListView.swift +++ b/Shared/PostCollection/CollectionListView.swift @@ -78,12 +78,26 @@ struct CollectionListView: View { ) .listStyle(SidebarListStyle()) .onChange(of: model.selectedCollection) { collection in - self.selectedCollectionURL = collection?.objectID.uriRepresentation() + if collection != fetchSelectedCollectionFromAppStorage() { + self.selectedCollectionURL = collection?.objectID.uriRepresentation() + } } .onChange(of: model.showAllPosts) { value in - self.showAllPostsFlag = model.showAllPosts + if value != showAllPostsFlag { + self.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 {