Git Product home page Git Product logo

Comments (3)

gantonious avatar gantonious commented on June 24, 2024 1

If you're okay with not having the background between the start and end days you can implement a custom SelectionMode to achieve the range behaviour. I drafted one below. You can update it to get the user experience you want:

class RangeSelectionMode(
    private val materialDayPicker: MaterialDayPicker
) : SelectionMode {

    override fun getSelectionStateAfterSelecting(lastSelectionState: SelectionState, dayToSelect: MaterialDayPicker.Weekday): SelectionState {
        return createRangedSelectionState(
            lastSelectionState = lastSelectionState,
            dayPressed = dayToSelect
        )
    }

    override fun getSelectionStateAfterDeselecting(lastSelectionState: SelectionState, dayToDeselect: MaterialDayPicker.Weekday): SelectionState {
        return createRangedSelectionState(
            lastSelectionState = lastSelectionState,
            dayPressed = dayToDeselect
        )
    }

    private fun createRangedSelectionState(lastSelectionState: SelectionState, dayPressed: MaterialDayPicker.Weekday): SelectionState {
        val previouslySelectedDays = lastSelectionState.selectedDays
        val orderedWeekdays = getWeekdaysOrderedForCurrentLocale()
        val ordinalsOfPreviouslySelectedDays = previouslySelectedDays.map { orderedWeekdays.indexOf(it) }

        val ordinalOfFirstDayInPreviousRange = ordinalsOfPreviouslySelectedDays.min()
        val ordinalOfLastDayInPreviousRange = ordinalsOfPreviouslySelectedDays.max()
        val ordinalOfSelectedDay = orderedWeekdays.indexOf(dayPressed)

        return when {
            ordinalOfFirstDayInPreviousRange == null || ordinalOfLastDayInPreviousRange == null -> {
                // We had no previous selection so just return the day pressed as the selection.
                SelectionState.withSingleDay(dayPressed)
            }
            ordinalOfFirstDayInPreviousRange == ordinalOfLastDayInPreviousRange && ordinalOfFirstDayInPreviousRange == ordinalOfSelectedDay -> {
                // User pressed the only day in the range selection. Return an empty selection.
                SelectionState()
            }
            ordinalOfSelectedDay == ordinalOfFirstDayInPreviousRange || ordinalOfSelectedDay == ordinalOfLastDayInPreviousRange -> {
                // User pressed the first or last item in range. Just deselect that item.
                lastSelectionState.withDayDeselected(dayPressed)
            }
            ordinalOfSelectedDay < ordinalOfFirstDayInPreviousRange -> {
                // User pressed a day on the left of the previous date range. Grow the starting point of the range to that.
                SelectionState(selectedDays = orderedWeekdays.subList(ordinalOfSelectedDay, ordinalOfLastDayInPreviousRange + 1))
            }
            else -> {
                // User pressed a day on the right of the start of the date range. Update the ending point to that position.
                SelectionState(selectedDays = orderedWeekdays.subList(ordinalOfFirstDayInPreviousRange, ordinalOfSelectedDay + 1))
            }
        }
    }

    private fun getWeekdaysOrderedForCurrentLocale(): List<MaterialDayPicker.Weekday> {
        return MaterialDayPicker.Weekday.getOrderedDaysOfWeek(materialDayPicker.locale)
    }
}

You can use it by doing:

materialDayPicker.selectionMode = RangeSelectionMode(materialDayPicker)

Sample usage:
range_selections

from materialdaypicker.

gantonious avatar gantonious commented on June 24, 2024

This is a great idea but I don't have the bandwidth right now to build it. You are more than welcome to add this functionality and submit a pull request.

from materialdaypicker.

SapuSeven avatar SapuSeven commented on June 24, 2024

Awesome, thank you very much! I'll use your suggested approach for now, maybe I'll implement the background thing and submit a pull request later.

from materialdaypicker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.