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

[DatePicker] Date May 31 not showing up, causes crash #1159

Closed
molundb opened this issue Mar 27, 2020 · 2 comments
Closed

[DatePicker] Date May 31 not showing up, causes crash #1159

molundb opened this issue Mar 27, 2020 · 2 comments
Labels

Comments

@molundb
Copy link

molundb commented Mar 27, 2020

Description:

May 31 is not shown in the calendar unless you open May and scroll down. This causes crashes. No other month or date seems to have this problem.

Test Steps:

  1. Open calendar (will open in March since it's March today)
  2. Use arrows to go to May

Actual Result: May 31 is not shown (you have to scroll down to see it)
Expected Result: May 31 is shown

Here are the ways I've found to make May 31 show up without scrolling down:

  • Go to May. Go to March. Go to May.
  • Go to July. Go to May.
  • Select a start date.
  • Open the edit dates view (by pressing the pen). Close it.
  • Change year to 2021. Change back to 2020. Go to May.

Steps to make the calendar crash:

  1. Open calendar
  2. Select range including May 31
  3. Apply
  4. Open calendar
  5. Go to May

Actual Result: Crash

Expected behavior:

May 31 not shown
Screenshot 2020-03-27 at 11 50 00

May 31 shown after start date has been selected
Screenshot 2020-03-27 at 11 50 09

Crash
Open the calendar
Screenshot 2020-03-27 at 12 06 20

Select a start date before May 31
Screenshot 2020-03-27 at 12 06 27

May 31 is shown because a date has been selected
Screenshot 2020-03-27 at 12 06 39

Select an and date after and including May 31, so that May 31 is part of the range
Screenshot 2020-03-27 at 12 06 50

Apply the filter. Open the calendar again.
Screenshot 2020-03-27 at 12 06 50

Go to May using the arrows. When in April and you press the right arrow to try to go to May, the app crashes.
Screenshot red circle

Crash
Screenshot 2020-03-27 at 12 07 15

Source code:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setDatePickerBuilder()
        setDatePickerButton()
        ...
    }

override fun onResume() {
        super.onResume()
        setDatePickerSelection()
        setDatePicker()
       ....
    }

private fun setDatePickerBuilder() {
        builder = MaterialDatePicker.Builder.dateRangePicker()
        builder?.apply {
            setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR)
            setTitleText(getString(R.string.time_frame_ger))
            val theme = resolveOrThrow(requireContext(), R.attr.materialCalendarTheme)
            setTheme(theme)

            val constraintsBuilder = CalendarConstraints.Builder()
            constraintsBuilder.setValidator(DateValidatorPointForward.now())

            val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
            constraintsBuilder.setStart(calendar.timeInMillis)
            calendar.add(Calendar.YEAR, 1)
            constraintsBuilder.setEnd(calendar.timeInMillis)

            setCalendarConstraints(constraintsBuilder.build())
            setDatePickerSelection()
        }
        setDatePicker()
    }

private fun setDatePicker() {
        picker = builder?.build()
        picker?.apply {
            addOnPositiveButtonClickListener {
                val result = it as Pair<*, *>
                val startDate = Date(result.first as Long)
                val endDate = Date(result.second as Long)
                experiencesViewModel.setFilterDataRange(startDate, endDate)
                loadExperiences(true)
            }
            addOnNegativeButtonClickListener {
                experiencesViewModel.resetFilterDataDateRange()
                loadExperiences(true)
                setDatePickerBuilder()
            }
            addOnDismissListener {
                dateButton.isEnabled = true
            }
        }
    }

private fun setDatePickerSelection() {
        val filterDate = experiencesViewModel.filterData.filterDate
        val filterEndDate = experiencesViewModel.filterData.filterEndDate

        // Add an offset of 10 hours to prevent timezone daylight saving causing the calendar to show one day earlier than the selected one
        val dayOffset = 1000 * 60 * 60 * 10
        if (filterDate != null) {
            if (filterEndDate == null) {
                val selectedDate = Date(filterDate.time + dayOffset)
                builder?.setSelection(Pair(selectedDate.time, selectedDate.time))
            } else {
                builder?.setSelection(Pair(filterDate.time, filterEndDate.time))
            }
        }
    }

private fun setDatePickerButton() {
        dateButton.setOnClickListener {
            dateButton.isEnabled = false
            startDatePicker()
        }
    }

private fun startDatePicker() {
        picker?.show(childFragmentManager, picker.toString())
    }

Android API version: 27 in emulator

Material Library version: 1.2.0-alpha05

Device: Pixel 3 emulator

To help us triage faster, please check to make sure you are using the latest version of the library.

We also happily accept pull requests.

@molundb molundb added the bug label Mar 27, 2020
@ymarian
Copy link
Contributor

ymarian commented Apr 7, 2020

I copied pretty much all your code followed thse steps and I cannot repro. Can you share a stacktrace and maybe a sample app?
picker

@Angelk90
Copy link

@ymarian , @molundb , Can I ask you one questions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants