I'm convinced I'm overthinking this and ending up in a loop of not being able to solve this issue. The short of it is I have an array which includes dates which I am displaying in a list in my main view. The entries will fire notifications to users based on the time of the date. I want to be able to remove entries from the Array or View where the date is older than the current date and only show users the up to date entries. Almost everything I've found online doesn't seem to apply to this and I'm going round in circles trying to resolve this.
Theres a lot to unpack with my code so I've condensed it down.
struct EventListView: View {
var body: some View {
NavigationView {
//Loads the array from a CSV - no issues with this, been working with it for
all other functions in the app.
var events = loadCSV(from: "Eventtest")
List(events, id: \.id) { event in
//This function returns a true/false bool which I want to use to remove
from view/array
let eventinpast = dateInPast(value: event.date)
HStack{
VStack(alignment: .leading, spacing: 5){
Text(event.name)
.font(.subheadline)
.foregroundColor(.secondary)
Text(event.event) //confusing but this is correct.
.font(.subheadline)
.foregroundColor(.secondary)
Text(event.date)
Text(event.time)
}.frame(width: 200.0, alignment: .topLeading)
}
}
}
}
}
I've tried to change the List to include a "ForEach" but this doesn't resolve the issue as everything I've found online for this revolves around a user action to delete/remove, but I want the bool to decide if the event item is displayed/deleted or not.
I've removed my attempts for trying to delete/remove items because at this point I'm tripping myself up constantly and I'm not convinced I'm looking at the problem from the right perspective. Most recent attempt has been following this: https://www.hackingwithswift.com/books/ios-swiftui/building-a-list-we-can-delete-from But instead of using .onDelete I tried .onAppear but no deals. Sorry if proposing this problem is rambling, I'm quite drained from trying to get what I thought would be really simple functionality working.
Any help would be greatly appreciated! Thank you.
CodePudding user response:
Sometimes it is better to do the model stuff first.
var events = loadCSV(from: "Eventtest")
events = events.filter() { dateInPast(value: $0.date) }
CodePudding user response:
how about SHOWING only the relevant events:
var events = loadCSV(from: "Eventtest").filter { !dateInPast(value: $0.date) }
or
List(events.filter { !dateInPast(value: $0.date) }, id: \.id) { event in