Delete CollectionListModel in favour of FetchRequest in CollectionListView

This commit is contained in:
Angelo Stavrow 2022-05-28 07:17:33 -04:00
parent 15f84b04c0
commit 230f7a1076
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
3 changed files with 5 additions and 51 deletions

View File

@ -1,41 +0,0 @@
import SwiftUI
import CoreData
class CollectionListModel: NSObject, ObservableObject {
@Published var list: [WFACollection] = []
private let collectionsController: NSFetchedResultsController<WFACollection>
init(managedObjectContext: NSManagedObjectContext) {
collectionsController = NSFetchedResultsController(fetchRequest: WFACollection.collectionsFetchRequest,
managedObjectContext: managedObjectContext,
sectionNameKeyPath: nil,
cacheName: nil)
super.init()
collectionsController.delegate = self
do {
try collectionsController.performFetch()
list = collectionsController.fetchedObjects ?? []
} catch {
// FIXME: Errors cannot be thrown out of the CollectionListView property initializer
fatalError(LocalStoreError.couldNotFetchCollections.localizedDescription)
}
}
}
extension CollectionListModel: NSFetchedResultsControllerDelegate {
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
guard let collections = controller.fetchedObjects as? [WFACollection] else { return }
self.list = collections
}
}
extension WFACollection {
static var collectionsFetchRequest: NSFetchRequest<WFACollection> {
let request: NSFetchRequest<WFACollection> = WFACollection.createFetchRequest()
request.sortDescriptors = [NSSortDescriptor(keyPath: \WFACollection.title, ascending: true)]
return request
}
}

View File

@ -3,9 +3,10 @@ import SwiftUI
struct CollectionListView: View { struct CollectionListView: View {
@EnvironmentObject var model: WriteFreelyModel @EnvironmentObject var model: WriteFreelyModel
@EnvironmentObject var errorHandling: ErrorHandling @EnvironmentObject var errorHandling: ErrorHandling
@ObservedObject var collections = CollectionListModel( // @ObservedObject var collections = CollectionListModel(
managedObjectContext: LocalStorageManager.standard.container.viewContext // managedObjectContext: LocalStorageManager.standard.container.viewContext
) // )
@FetchRequest(sortDescriptors: []) var collections: FetchedResults<WFACollection>
@State var selectedCollection: WFACollection? @State var selectedCollection: WFACollection?
var body: some View { var body: some View {
@ -14,7 +15,7 @@ struct CollectionListView: View {
NavigationLink("All Posts", destination: PostListView(selectedCollection: nil, showAllPosts: true)) NavigationLink("All Posts", destination: PostListView(selectedCollection: nil, showAllPosts: true))
NavigationLink("Drafts", destination: PostListView(selectedCollection: nil, showAllPosts: false)) NavigationLink("Drafts", destination: PostListView(selectedCollection: nil, showAllPosts: false))
Section(header: Text("Your Blogs")) { Section(header: Text("Your Blogs")) {
ForEach(collections.list, id: \.self) { collection in ForEach(collections, id: \.self) { collection in
NavigationLink(destination: PostListView(selectedCollection: collection, showAllPosts: false), NavigationLink(destination: PostListView(selectedCollection: collection, showAllPosts: false),
tag: collection, tag: collection,
selection: $selectedCollection, selection: $selectedCollection,

View File

@ -7,8 +7,6 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
170A7EC126F5186A00F1CBD4 /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */; };
170A7EC226F5186A00F1CBD4 /* CollectionListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */; };
170DFA34251BBC44001D82A0 /* PostEditorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */; }; 170DFA34251BBC44001D82A0 /* PostEditorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */; };
170DFA35251BBC44001D82A0 /* PostEditorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */; }; 170DFA35251BBC44001D82A0 /* PostEditorModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */; };
17120DA124E19839002B9F6C /* AccountView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17A5388D24DDEC7400DEFF9A /* AccountView.swift */; }; 17120DA124E19839002B9F6C /* AccountView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17A5388D24DDEC7400DEFF9A /* AccountView.swift */; };
@ -179,7 +177,6 @@
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
1709ADDF251B9A110053AF79 /* EditorLaunchingPolicy.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = EditorLaunchingPolicy.md; sourceTree = "<group>"; }; 1709ADDF251B9A110053AF79 /* EditorLaunchingPolicy.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = EditorLaunchingPolicy.md; sourceTree = "<group>"; };
170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionListModel.swift; sourceTree = "<group>"; };
170DFA33251BBC44001D82A0 /* PostEditorModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorModel.swift; sourceTree = "<group>"; }; 170DFA33251BBC44001D82A0 /* PostEditorModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorModel.swift; sourceTree = "<group>"; };
17120DA424E19CBF002B9F6C /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; }; 17120DA424E19CBF002B9F6C /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
17120DA824E1B2F5002B9F6C /* AccountLogoutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountLogoutView.swift; sourceTree = "<group>"; }; 17120DA824E1B2F5002B9F6C /* AccountLogoutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountLogoutView.swift; sourceTree = "<group>"; };
@ -603,7 +600,6 @@
17DF32D224C8B78D00BCE2E3 /* PostCollection */ = { 17DF32D224C8B78D00BCE2E3 /* PostCollection */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
170A7EC026F5186A00F1CBD4 /* CollectionListModel.swift */,
171BFDF924D4AF8300888236 /* CollectionListView.swift */, 171BFDF924D4AF8300888236 /* CollectionListView.swift */,
); );
path = PostCollection; path = PostCollection;
@ -926,7 +922,6 @@
17B996DA2502D23E0017B536 /* WFAPost+CoreDataProperties.swift in Sources */, 17B996DA2502D23E0017B536 /* WFAPost+CoreDataProperties.swift in Sources */,
1756AE7724CB2EDD00FD7257 /* PostEditorView.swift in Sources */, 1756AE7724CB2EDD00FD7257 /* PostEditorView.swift in Sources */,
17DF32D524C8CA3400BCE2E3 /* PostStatusBadgeView.swift in Sources */, 17DF32D524C8CA3400BCE2E3 /* PostStatusBadgeView.swift in Sources */,
170A7EC126F5186A00F1CBD4 /* CollectionListModel.swift in Sources */,
17D435E824E3128F0036B539 /* PreferencesModel.swift in Sources */, 17D435E824E3128F0036B539 /* PreferencesModel.swift in Sources */,
1756AE7A24CB65DF00FD7257 /* PostListView.swift in Sources */, 1756AE7A24CB65DF00FD7257 /* PostListView.swift in Sources */,
17B996D82502D23E0017B536 /* WFAPost+CoreDataClass.swift in Sources */, 17B996D82502D23E0017B536 /* WFAPost+CoreDataClass.swift in Sources */,
@ -966,7 +961,6 @@
17120DAD24E1B99F002B9F6C /* AccountLoginView.swift in Sources */, 17120DAD24E1B99F002B9F6C /* AccountLoginView.swift in Sources */,
17D4926727947D780035BD7E /* MacUpdatesViewModel.swift in Sources */, 17D4926727947D780035BD7E /* MacUpdatesViewModel.swift in Sources */,
17466626256C0D0600629997 /* MacEditorTextView.swift in Sources */, 17466626256C0D0600629997 /* MacEditorTextView.swift in Sources */,
170A7EC226F5186A00F1CBD4 /* CollectionListModel.swift in Sources */,
1727526B2809991A003D0A6A /* ErrorHandling.swift in Sources */, 1727526B2809991A003D0A6A /* ErrorHandling.swift in Sources */,
17E5DF8A2543610700DCDC9B /* PostTextEditingView.swift in Sources */, 17E5DF8A2543610700DCDC9B /* PostTextEditingView.swift in Sources */,
17C42E71250AAFD500072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift in Sources */, 17C42E71250AAFD500072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift in Sources */,