Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selectedDate for .countDownTimer not working as expected #560

Open
Lucalaure opened this issue Nov 15, 2023 · 0 comments
Open

selectedDate for .countDownTimer not working as expected #560

Lucalaure opened this issue Nov 15, 2023 · 0 comments

Comments

@Lucalaure
Copy link

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)
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant