Im trying to filter rows in the list that match what im looking for,
I have added a filter form that toggles the following:
parking
x --Paid (true)
o--free (false)
o--street (false)
Views
o--forest (false)
o--dunes (false)
x--water (true)
Traffic
o--low traffic (false)
o--moderate traffic (false)
x--high traffic (true)
Experience
x--beginner (true)
o--intermediate (false)
o--expert (false)
My goal is to have a filter that connects to some sort of list that ultimately returns the true options that specifically matches. Im extremely new and this is the code i copied from the tutorial for Landmarks List, but im trying to edit it. Sorry if its all over the place im new to coding.
below is a link to the tutorial: https://developer.apple.com/tutorials/swiftui/composing-complex-interfaces
struct FilterList: View {
var landmark: Landmark
@EnvironmentObject var landmarksVM: LandmarksViewModel
@State private var showpaidOnly = false
@State private var showfreeOnly = false
@State private var showstreetOnly = false
@State private var showforestOnly = false
@State private var showdunesOnly = false
@State private var showwaterOnly = false
@State private var showlowtrafficOnly = false
@State private var showmoderatetrafficOnly = false
@State private var showhightrafficOnly = false
@State private var showbeginnerOnly = false
@State private var showintermediateOnly = false
@State private var showexpertOnly = false
@Binding var rating: Int
@State private var review: String = ""
var filteredLandmarks: [Landmark] {
landmarksVM.landmarks.filter { landmark in
(!showpaidOnly || landmark.ispaid)
(!showfreeOnly || landmark.isfree)
(!showstreetOnly || landmark.isstreet)
(!showforest || landmark.isforest)
(!showdunesOnly || landmark.isdunes)
(!showwaterOnly || landmark.iswater)
(!showlowtrafficOnly || landmark.islowtraffic)
(!showmoderatetrafficOnly || landmark.ismoderatetraffic)
(!showhightrafficOnly || landmark.ishightraffic)
(!showbeginnerOnly || landmark.isbeginner)
(!showintermediateOnly || landmark.isintermediate)
(!showexpertOnly || landmark.isexpert)
}
}
var body: some View {
NavigationView {
List {
ForEach(filteredLandmarks){
thislandmark in
NavigationLink (
destination: LandmarkDetail(landmark: thislansmark, rating: .constant(0))){
}
}
}
.navigationTitle("Filtered Landmarks")
}
if landmark.isFavorite {
Image (systemName: "heart.fill")
.foregroundColor(.purple)
}
}
}
struct FilterList_Previews: PreviewProvider {
static var previews: some View {
LandmarkList( rating: .constant(0))
.environmentObject(LandmarksViewModel())
}
}