Combining condition to show something,

Hi Team, I would like to combine or mix and match two filter

here is the code but seems like I cannot do what I want... I want to be able to show only what the filter is made for so if TECO and favorite is active then I would only see favourite and not TECO project , and if TECO is not active but favourite is active then it will only show all favourite project and if TECO is active it will only show all project which are not TECO regardless if they are favortie

import SwiftUI

struct ProjectList: View {
    @State private var showTecoOnly = false
    @State private var showFavoriteOnly = false
    
    var filteredTECOProjects: [Project] {
           projects.filter { project in
               (!showTecoOnly || !project.isTeco)
           }
       }
    var filteredProjects: [Project] {
           projects.filter { project in
               (!showFavoriteOnly || project.isFavorite)
           }
       }
    
    var body: some View {
        NavigationSplitView {
            List {
                Toggle(isOn: $showTecoOnly) {
                                   Text("Hide TECO")
                               }
                Toggle(isOn: $showFavoriteOnly) {
                                   Text("Show Favorite")
                               }
                ForEach (filteredTECOProjects ) && ForEach (filteredProjects ) { project in
                NavigationLink {
                    ProjectDetail(project: project)
                } label: {
                    ProjectRow(project: project)
                }
            }
                Divider()
            }
            .animation(.default, value: filteredTECOProjects)
            .navigationTitle("PROJECTS")
        } detail: {
            Text("Select a project")
            }
        }
    }


#Preview {
    ProjectList()
}
Answered by darkpaw in 778713022

I think part of the issue is that your "Hide TECO" switch says it hides TECO projects, but your Bool variable is called showTecoOnly. I've simplified it a bit, and avoided name clashes. See if this works for you; I've added some test data at the top and it seems to work for me:

import SwiftUI

var projects = [
	Project(name: "Project1, isTeco = false, isFavorite = false", isTeco: false, isFavorite: false),
	Project(name: "Project2, isTeco = true, isFavorite = false", isTeco: true, isFavorite: false),
	Project(name: "Project3, isTeco = true, isFavorite = true", isTeco: true, isFavorite: true),
	Project(name: "Project4, isTeco = false, isFavorite = true", isTeco: false, isFavorite: true)
]

struct ProjectList: View {
	@State private var hideTeco = false
	@State private var showFavorite = false

	var body: some View {
		let filteredProjects: [Project] =
			if(hideTeco && showFavorite) {
				// If TECO and favorite are active then I would only see favourite and not TECO projects
				projects.filter { project in
					!project.isTeco && project.isFavorite
				}

			} else if(!hideTeco && showFavorite) {
				// If TECO is not active but favourite is active then it will show all favourite projects
				projects.filter { project in
					project.isFavorite
				}

			} else if(hideTeco && !showFavorite) {
				// If TECO is active it will show all projects which are not TECO regardless if they are favorite
				projects.filter { project in
					!project.isTeco
				}

			} else {
				[]
			}

		NavigationSplitView {
			List {
				Toggle(isOn: $hideTeco) {
					Text("Hide TECO")
				}
				Toggle(isOn: $showFavorite) {
					Text("Show Favorite")
				}

				ForEach(filteredProjects) { project in
					NavigationLink {
						ProjectDetail(project: project)
					} label: {
						ProjectRow(project: project)
					}
				}
			}
			.navigationTitle("PROJECTS")

		} detail: {
			Text("Select a project")
		}
	}
}

#Preview {
	ProjectList()
}

struct Project: Identifiable {
	var id: UUID = UUID()
	var name: String
	var isTeco: Bool
	var isFavorite: Bool
}

struct ProjectDetail: View {
	var project: Project
	var body: some View {
		Text("ProjectDetail: \(project.name)")
	}
}

struct ProjectRow: View {
	var project: Project
	var body: some View {
		Text(project.name)
	}
}
Accepted Answer

I think part of the issue is that your "Hide TECO" switch says it hides TECO projects, but your Bool variable is called showTecoOnly. I've simplified it a bit, and avoided name clashes. See if this works for you; I've added some test data at the top and it seems to work for me:

import SwiftUI

var projects = [
	Project(name: "Project1, isTeco = false, isFavorite = false", isTeco: false, isFavorite: false),
	Project(name: "Project2, isTeco = true, isFavorite = false", isTeco: true, isFavorite: false),
	Project(name: "Project3, isTeco = true, isFavorite = true", isTeco: true, isFavorite: true),
	Project(name: "Project4, isTeco = false, isFavorite = true", isTeco: false, isFavorite: true)
]

struct ProjectList: View {
	@State private var hideTeco = false
	@State private var showFavorite = false

	var body: some View {
		let filteredProjects: [Project] =
			if(hideTeco && showFavorite) {
				// If TECO and favorite are active then I would only see favourite and not TECO projects
				projects.filter { project in
					!project.isTeco && project.isFavorite
				}

			} else if(!hideTeco && showFavorite) {
				// If TECO is not active but favourite is active then it will show all favourite projects
				projects.filter { project in
					project.isFavorite
				}

			} else if(hideTeco && !showFavorite) {
				// If TECO is active it will show all projects which are not TECO regardless if they are favorite
				projects.filter { project in
					!project.isTeco
				}

			} else {
				[]
			}

		NavigationSplitView {
			List {
				Toggle(isOn: $hideTeco) {
					Text("Hide TECO")
				}
				Toggle(isOn: $showFavorite) {
					Text("Show Favorite")
				}

				ForEach(filteredProjects) { project in
					NavigationLink {
						ProjectDetail(project: project)
					} label: {
						ProjectRow(project: project)
					}
				}
			}
			.navigationTitle("PROJECTS")

		} detail: {
			Text("Select a project")
		}
	}
}

#Preview {
	ProjectList()
}

struct Project: Identifiable {
	var id: UUID = UUID()
	var name: String
	var isTeco: Bool
	var isFavorite: Bool
}

struct ProjectDetail: View {
	var project: Project
	var body: some View {
		Text("ProjectDetail: \(project.name)")
	}
}

struct ProjectRow: View {
	var project: Project
	var body: some View {
		Text(project.name)
	}
}
Combining condition to show something,
 
 
Q