In the sidebar column of the NavigationSplitView I'd like to have several sections. All the items are fetched with Core Data.
- View all photos (Photos entity in Core Data)
- Folder (Folder entity in Core Data)
-
- Folder A
-
- Folder B
-
- Folder C
- Tags (Tag entity in Core Data)
-
- Cat
-
- Dog
In the content view, I'd like show the items based on the selection in the sidebar. I tried the following with some success but I think there should be another way of doing this.
NavigationSplitView() {
List(selection: $navigationModel.selectedCategory, content: {
NavigationLink(value: Category(type: .all, predicate: NSPredicate(value: true), title: "View all items") ) {
Text("View all items")
}
Section {
ForEach(folders){ folder in
NavigationLink(value: Category(type: .folder, predicate: NSPredicate(format: "folder == %@", folder), title: folder.name) ) {
FolderRow(folder: folder)
// ... and so on. Another section for tags
And in the content:
ZStack{
List(selection: $navigationModel.selectedPhoto) {
ContentView(photos: FetchRequest(
sortDescriptors: [sortDescriptor],
predicate: navigationModel.selectedCategory?.predicate,
animation: .default)
//....
How can refractor this code with a better solution? The problem that I'm encountering is that the selection
argument in the List
accepts only one type of object. But I want to select more that one type of object. Another issue is that this solution won't conform to Codable
if I'm thinking correctly and reseting the app state on the next launch would be cumbersome.