Hi all,
I'm working to implement something we learned during the Hot Prospects project.
I'd like to implement dynamic filtering on a set of trending TV data received from an API by country - ideally I would have a drop down that would allow users to select the trending TV shows in their country of choosing. I've pasted a simplified version of my code below - essentially, I'm noticing that adding a !.contains("US") filter works as expected (no shows that contain "US" in their origin array make it through), but simply a .contains("US") filter does not. Is there some nuance that I'm missing? This is true if I filter on something other than origin country as well - name, for instance. There's got to be some boolean logic that I'm missing here, but it seems like what the filter would deem 'true' would just change between the two versions of .contains statements. What am I overlooking?
import SwiftUI
struct GridViewPostersTrendingTest: View {
enum FilterType {
case none, country
}
@State private var trendingShows = [TrendingShows.example]
let filter: FilterType
var body: some View {
ScrollView {
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 4), content: {
ForEach(filteredProspects.removingDuplicates()) { series in
NavigationLink {
SeriesView(tvId: series.id)
} label: {
VStack {
GridSeriesPosterHyp(image: series.posterPath ?? "", name: series.name ?? "")
}
.padding(.bottom, 4)
}
}
})
}
.navigationBarTitle("Trending Shows (US)", displayMode: .inline)
}
var filteredProspects: [TrendingShows] {
switch filter {
case .none:
return trendingShows
case .country:
return trendingShows.filter { (!($0.originCountry ?? []).contains("US")) }
}
}
}
Sample of the JSON I'm looking at below:
[
{
"id": 114461,
"name": "Ahsoka",
"origin_country": [
"US"
]
},
{
"id": 205715,
"name": "Gen V",
"origin_country": [
"US"
]
},
{
"id": 72710,
"name": "The Continental: From the World of John Wick",
"origin_country": [
"US"
]
},
{
"id": 84958,
"name": "Loki",
"origin_country": [
"US"
]
},
{
"id": 37854,
"name": "One Piece",
"origin_country": [
"JP"
]
}
]