I am new in SwiftUI and I am facing problem with selecting Single date with MultiDatePicker
my code is like this
@State private var dateRange: ClosedRange<Date>? = nil
var body: some View {
MultiDatePicker(dateRange: self.$dateRange)
Button(action: {
withAnimation(.linear(duration: 0.3)) {
getDateRangeGuest(dateRange: dateRange)
}
}, label: {
Text("OK")
.font(.title3)
})
}
func getDateRangeGuest(dateRange: ClosedRange<Date>?){
if let range = dateRange {
var newRange = String()
newRange = "\(range)"
print(newRange)
}else{
print(dateRange)
}
}
Is it is possible to select also a Single date in MultiDatePicker?
I want it in range
not in Set<DateComponents> array
I use WWCalendarTimeSelector calender. As it is in Swift I finally created its SwiftUI Version
import SwiftUI
import WWCalendarTimeSelector
class CalendarSelectorDelegate: WWCalendarTimeSelectorProtocol {
@Binding var singleDate: Date?
@Binding var selectedDateString: String
@Binding var multipleDates: [Date]
@Binding var startDateGlobal: String
@Binding var endDateGlobal: String
init(singleDate: Binding<Date?>, selectedDateString: Binding<String>, multipleDates: Binding<[Date]>, startDateGlobal: Binding<String>, endDateGlobal: Binding<String>) {
_singleDate = singleDate
_selectedDateString = selectedDateString
_multipleDates = multipleDates
_startDateGlobal = startDateGlobal
_endDateGlobal = endDateGlobal
}
func WWCalendarTimeSelectorDone(_ selector: WWCalendarTimeSelector, date: Date) {
print("Selected \n\(date)\n---")
singleDate = date
selectedDateString = date.stringFromFormat("dd/MM/yyyy")
}
func WWCalendarTimeSelectorDone(_ selector: WWCalendarTimeSelector, dates: [Date]) {
print("Selected Multiple Dates \n\(dates)\n---")
var startDateSend = String()
var endDateSend = String()
if let date = dates.first {
singleDate = date
let firstDate = date.stringFromFormat("yyyy-MM-dd")
let newDate = dates.last
let lastDate = newDate!.stringFromFormat("yyyy-MM-dd")
selectedDateString = "\(firstDate) - \(lastDate)"
startDateSend = date.stringFromFormat("yyyy-MM-dd")
endDateSend = newDate!.stringFromFormat("yyyy-MM-dd")
} else {
selectedDateString = "No Date Selected"
}
print("Label Value = ", selectedDateString)
multipleDates = dates
startDateGlobal = startDateSend
endDateGlobal = endDateSend
}
}
struct ContentView: View {
@State private var singleDate: Date?
@State private var selectedDateString = ""
@State private var multipleDates: [Date] = []
@State private var startDateGlobal = ""
@State private var endDateGlobal = ""
var body: some View {
VStack {
Text(selectedDateString)
.padding()
Button(action: {
showCalendar()
}) {
Text("Select Date")
}
}
}
func showCalendar() {
let selector = WWCalendarTimeSelector.instantiate()
selector.optionSelectionType = WWCalendarTimeSelectorSelection.range
let delegate = CalendarSelectorDelegate(singleDate: $singleDate, selectedDateString: $selectedDateString, multipleDates: $multipleDates, startDateGlobal: $startDateGlobal, endDateGlobal: $endDateGlobal)
selector.delegate = delegate
if let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
scene.windows.first?.rootViewController?.present(selector, animated: true, completion: nil)
}
}
}