forked from TimCinel/ActionSheetPicker
-
-
Notifications
You must be signed in to change notification settings - Fork 736
Open
Description
I have set my ActionSheetDatePicker as the following, however, I can't seem to be able to set the selectedDate using the function initialCountdownDurationInSeconds()... Am I implementing this correctly or is this a bug?
@IBAction func durationAction(_ sender: UIButton) {
let countdownDuration = initialCountdownDurationInSeconds()
// Create the selectedDate by adding the countdown duration to the current date
let selectedDate = Date().addingTimeInterval(countdownDuration)
ActionSheetDatePicker.show(withTitle: "Select Time", datePickerMode: .countDownTimer, selectedDate: selectedDate, doneBlock: {
picker, value, index in
if let duration = value as? TimeInterval {
self.handleDatePickerSelection(with: duration)
}
return
}, cancel: { ActionMultipleStringCancelBlock in return }, origin: sender)
}
func initialCountdownDurationInSeconds() -> TimeInterval {
var totalSeconds = 0
// If there's existing duration text, use it to calculate the initial countdown duration.
// Otherwise, default to 1 minute.
guard let text = self.textInstantDuration.text, !text.isEmpty else {
return TimeInterval(60)
}
// Assume the following formats: "Xh Ym" or "Xh" or "Ym"
if let hourRange = text.range(of: "\\d+h", options: .regularExpression) {
let hourString = text[hourRange].dropLast() // Remove 'h'
if let hours = Int(hourString) {
totalSeconds += hours * 3600 // Convert hours to seconds
}
}
if let minuteRange = text.range(of: "\\d+m", options: .regularExpression) {
let minuteString = text[minuteRange].dropLast() // Remove 'm'
if let minutes = Int(minuteString) {
totalSeconds += minutes * 60 // Convert minutes to seconds
}
}
return TimeInterval(totalSeconds)
}
Metadata
Metadata
Assignees
Labels
No labels