Hey, currently working on my first SwiftUI app in college and would appreciate any help. I have an issue where when dates are loaded from firebase into the "selectedDates" array of my MultiDatePicker, the onChange function does not recognize dates that are already in the "selectedDates" array, instead re-adding them to the array. I.e. if I have loaded July 19th into the multidatePicker, it displays on the calendar, but clicking July 19th on the multidatepicker calendar view doesn't remove it from the "selectedDates" array. This is the code for the view:
VStack {
MultiDatePicker("Select dates", selection: $selectedDates)
.padding()
.onChange(of: selectedDates) { oldValue, newValue in
saveDates(dates: selectedDates)
}
List(savedDates, id: \.self) { date in
Text("\(date, formatter: dateFormatter)")
}
.listStyle(.plain)
}
.onAppear {
loadSavedDates()
}
Where selectedDates is a state variable:
@State var selectedDates: Set<DateComponents> = []
If nothing is on Firebase, the selection and deselection of dates happens fine, but if I am loading dates from firebase then the multidatepicker doesn't detect de-selection of dates. Here's my code for loading the dates from firebase:
func loadSavedDates() {
let db = Firestore.firestore()
let uid = try! AuthenticationManager.shared.getAuthenticatedUser().uid
print("User ID: \(uid)")
print(widget.id.uuidString)
db.collection("spaces")
.document(spaceId)
.collection("dates")
.document(widget.id.uuidString)//widget.id.uuidString
.getDocument {document, error in
if let document = document {
if let dateStrings = document.data()?[uid] as? [String] {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
self.savedDates = dateStrings.compactMap {
dateFormatter.date(from: $0) }.sorted().
let calendar = Calendar.current
self.selectedDates = Set(self.savedDates.map {
calendar.dateComponents([.year, .month, .day], from: $0) }).
for component in selectedDates {
print(component.isValidDate)
}
}
} else {
print("Document does not exist")
}
}
As you can see, I believe I am setting the selectedDates array correctly with datecomponents. Is there a problem with my code or is there just no way to pass dates from Firebase into multidatepicker?