For this assignment you will be reimplementing the ToDo app in SwiftUI. The app this time will have the same structure as before - a first view that displays ToDos for today and a second view to create a new ToDo.
Below you will find code for a ToDo class, which represents the tasks the user will be creating and viewing in the app.
class ToDo : Codable, Equatable, Identifiable {
var id = UUID()
static func == (lhs: ToDo, rhs: ToDo) -> Bool {
let cal = Calendar.current
return lhs.todo == rhs.todo && cal.isDate(lhs.date,inSameDayAs:rhs.date)
}
var todo : String
var date : Date
init() {
todo = ""
date = Date.now
}
init(text str : String,time tm : Date) {
todo = str
date = tm
}
}
In your model class you should maintain two lists, a list todos that stores all of the ToDo items you have created, and a second list today that contains only the ToDos that should be displayed for today's date. Here is some helpful code to filter the todos list down into a today list:
func filter() {
today = []
let now = Date.now
let cal = Calendar.current
for td in todos {
if cal.isDate(td.date, inSameDayAs: now) {
today.append(td)
}
}
}
You will be using a DatePicker component to allow the user to specify a date for each new ToDo they create. Assuming you have a state variable that looks like
@State var when : Date = Date.now
The following code will give you the correct form of the DatePicker to use in your second view:
DatePicker(
"When:",
selection: $when,
displayedComponents: [.date]
)