T. Karter
T. Karter

Reputation: 738

How to filter a list of dates by year interactively in SwiftUI

I have a NSManagedObject Subclass coming from a core data entity MyEntity with:

@NSManaged public var name: String
@NSManaged public var date: Date

and a list. I prepared a ScrollView that holds Buttons (?) that could be used to filter the list

func groupYears(fetchedResults: FetchedResults<MyEntity>) -> [Int] {
    var result = [Int]()
    for fetchedResult in fetchedResults {
        let component = Calendar.current.dateComponents([.year], from: fetchedResult.date)
        if !result.contains(component.year!) {
            result.append(component.year!)
        }
    }
    return result
}
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \MyEntity.date, ascending: true)]) var fetchedResults: FetchedResults<MyEntity>
VStack {
    ScrollView(.horizontal, showsIndicators: false) {
        HStack(spacing: 15) {
            Button(action: {}) { Text("all") }
            ForEach(groupYears(fetchedResults: fetchedResults), id: \.self) { year in
                Button(action: {}) { Text(String(year)) }
            }
        }
    }
    List(fetchedResults, id: \.self) { result in
        VStack(alignment: .leading) {
            Text(result.name)
            Text(dateFormatter.string(from: result.date))
        }
    }
}

I want to click a button and the list should dynamically filter the related year.

How can I filter the list dynamically?

Upvotes: 1

Views: 1338

Answers (1)

pawello2222
pawello2222

Reputation: 54591

You can add a property to store your selected year:

@State var selectedYear: Int?
Button(action: {
    self.selectedYear = nil
}) { 
    Text("all") 
}
ForEach(groupYears(fetchedResults: fetchedResults), id: \.self) { year in
    Button(action: {
        self.selectedYear = year
    }) { 
        Text(String(year)) 
    }
}

And then filter the list by this selected year:

ForEach(fetchedResults.filter({ isDateInSelectedYear($0.date) }), id: \.self) { ...
func isDateInSelectedYear(_ date: Date) -> Bool {
    if selectedYear == nil {
        return true
    }
    return Calendar.current.component(.year, from: date) == selectedYear
}

Upvotes: 1

Related Questions