I have asked this question numerous times, Im new to coding so I'm not sure how to clearly explain my issue. It would be nice if people could respond and not downgrade my question so they can help me figure out what its called I'm talking about.
I have created a filter form that has a toggle option for the filters i want to apply to my list. Now im having issues creating a list of locations that meet those specific filters. I have no idea how to do this I've googled and watched many videos, but i can't find anything that is doing what my vision is.
For example im using the Landmarks SwiftUI tutorial and that tutorial has a list of landmarks that can be filtered by favorite and not favorite, im trying to do something similar, but im not sure how to do it.
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) { landmark in
NavigationLink (destination: LandmarkDetail(landmark: landmark, rating: .constant(0))){
Text(landmark.name)
}
}
}
.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())
}
}
EDIT *** LANDMARK MODEL***
import Foundation
import SwiftUI
import CoreLocation
struct Landmark: Hashable, Codable, Identifiable {
var id: Int
var name: String
var city: String
var state: String
var description: String
var isFavorite: Bool
var ispaid: Bool
var isfree: Bool
var isforest: Bool
var isdunes: Bool
var islowtraffic: Bool
var ismoderatetraffic: Bool
var ishightraffic: Bool
var isbeginner: Bool
var isintermediate: Bool
var isexpert: Bool
private var imageName: String
var image: Image {
Image(imageName)
}
private var coordinates: Coordinates
var locationCoordinate: CLLocationCoordinate2D{
CLLocationCoordinate2D(
latitude: coordinates.latitude,
longitude: coordinates.longitude)
}
struct Coordinates: Hashable, Codable {
var latitude: Double
var longitude: Double
}
}