Prevent AppStorage updates if there's no change

This commit is contained in:
Angelo Stavrow 2021-01-19 10:33:25 -05:00
parent 083d69d143
commit 6beee8cf32
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE

View File

@ -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 {