I want to filter moves based on genre, release year, etc. And ultimately able to use multiple filters. How can I link my filter parameters with that of the API? Here is how my filterView looks like; let me take genre as an example.
let allGenres: [Filter] = [Filter(name: "Action"), Filter(name: "Comedy"), Filter(name: "Drama"), Filter(name: "Fantasy"), Filter(name: "Horror"), Filter(name: "Romance"), Filter(name: "Thriller"), Filter(name: "Sciencee Fiction"), Filter(name: "Mystery")]
struct FilterView: View {
@Environment(\.presentationMode) var presentationMode
@Binding var genres: String?
@State var selectedGenre: Set<Filter> = []
var body: some View {
NavigationView {
VStack {
List {
VStack {
MultiSelector(
label: Text("Genre"),
options: allGenres,
optionToString: { $0.name },
selected: $selectedGenre
)
}
}
Button(action: {
saveSelectedValues(genres: selectedGenre)
self.presentationMode.wrappedValue.dismiss()
}, label: {
Text("Done")
.foregroundColor(.white)
.frame(width:300, height: 50)
.background(Color.accentColor)
.cornerRadius(10)
})
}
.navigationTitle("Filter")
}
}
}
func saveSelectedValues(genres: Set<Filter>) {
// Convert the selected options to an array of their names
let selectedGenres = genres.map { $0.name }
// Save the selected values to UserDefaults, database, or any other storage mechanism
UserDefaults.standard.set(selectedGenres, forKey: "selectedGenre")
}
And at the same time how do I perserve the selected filters when the filterView get's called again.
This is the code for the filter button
struct RandomiserView: View {
@Binding var genres: String?
var body: some View {
NavigationView {
VStack {
Spacer()
.frame(height: 10)
Button("Filters") {
isShowingFilters.toggle()
}
.font(.headline)
.foregroundColor(.white)
.frame(width:300, height: 50)
.background(Color.accentColor)
.cornerRadius(10)
.sheet(isPresented: $isShowingFilters) {
FilterView(genres: $genres
}
Spacer()
If anyone has an idea what I'm doing wrong I'd appreciate it.