thanks for the answer, but it didn't do anything.
I'll try to clarify my model (work in progress 🙂).
Legal.swift file is structured as follows:
struct Legal: Hashable, Codable, Identifiable {
var name: String
var place: String
var category: Category
var isFavorite: Bool
var penalty: String
enum Category: String, CaseIterable, Codable, Hashable {
case categoryone = "category One"
case categorytwo = "category Two"
case categorythree = "category Three"
}
}
UserData.swift file is structured as follows:
import Combine
import SwiftUI
final class UserData: ObservableObject {
@Published var showFavoritesOnly = false
@Published var legals = legalData
}
A few examples from my legal.json file:
[
{
"name": "infringement 1",
"category": "Category 1",
"place": "UK",
"id": 7004,
"isFavorite": false,
"penalty": "473 EUR",
},
{
"name": "infringement 2",
"category": "Category 2",
"place": "France",
"id": 1046,
"isFavorite": false,
"penalty": "116 EUR"
}
]
Home.swift file = home view is structured as follows:
import SwiftUI
struct Home: View {
@EnvironmentObject private var userData: UserData
var body: some View {
NavigationView {
List {
NavigationLink(destination: CategoryoneList()) {
Text("1. Category one")
}
NavigationLink(destination: CategorytwoList()) {
Text("2. Category two")
}
NavigationLink(destination: CategorythreeList()) {
Text("3. Category three")
}
NavigationLink(destination: LegalList()) {
Text("4. Overview").bold().underline()
}
}
.navigationBarTitle("View", displayMode: .inline)
}
}
}
LegalList.swift file contains all the legal provisions (disregarding the categories)
This is structured as follows:
struct LegalList: View {
@EnvironmentObject private var userData: UserData
var body: some View {
List {
Toggle(isOn: $userData.showFavoritesOnly) {
Text("Show Favorites").bold().underline()
}
ForEach(userData.legals) { legal in
if !self.userData.showFavoritesOnly || legal.isFavorite {
NavigationLink(
destination: LegalDetail(legal: legal)
.environmentObject(self.userData)
) {
LegalRow(legal: legal)
}
}
}
}
.navigationBarTitle("4. Overview", displayMode: .inline)
}
}
LegalDetail.swift file is structured as follows:
struct LegalDetail: View {
@EnvironmentObject var userData: UserData
var legal: Legal
var legalIndex: Int {
userData.legals.firstIndex(where: { $0.id == legal.id })!
}
var body: some View {
List {
VStack {
HStack(alignment: .top) {
Text(legal.name).bold().underline()
}
.padding()
.foregroundColor(Color.green)
Spacer()
VStack(alignment: .leading) {
Text(legal.place)
Divider()
}
Text("Penalty").bold().underline()
.padding()
.foregroundColor(Color.orange)
VStack(alignment: .leading) {
Text(legal.penalty)
Divider()
}
}
}.navigationBarTitle(legal.name)
.navigationBarItems(trailing:
Button(action: {
self.userData.legals[self.legalIndex]
.isFavorite.toggle()
}) {
if self.userData.legals[self.legalIndex]
.isFavorite {
Image(systemName: "star.fill")
.foregroundColor(Color.yellow)
} else {
Image(systemName: "star")
.foregroundColor(Color.gray)
}
}.font(.system(size: 27))
)
}
LegalRow.swift file is structured as follows:
struct LegalRow: View {
var legal: Legal
var body: some View {
HStack {
Text(legal.name)
Spacer()
if legal.isFavorite {
Image(systemName: "star.fill")
.imageScale(.medium)
.foregroundColor(.yellow)
}
}
}
}
The question now is how to make a seperate view for Category one, two and three, with the list of only the legal provisions of that category.
Regarding the question of the allElements, this was a suggestion of Claude31.
My apologies for the long post and again thanks in advance