Skip to content

rafsanjani/datepickertimeline

Repository files navigation

DatePickerTimeline Android CI

A linear Android date picker library written in Jetpack compose.
Inspired by https://pub.dev/packages/date_picker_timeline

Installation

Add the dependency to your build.gradle or build.gradle.kts file

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies{
    implementation ("com.github.Rafsanjani:datepickertimeline:<latest_version>")
}

Usage

For a horizontal date picker with no customization

DatePickerTimeLine(
	modifier = Modifier,
	onDateSelected = {selectedDate: LocalDate->
		//do something with selected date
	}
) 

For a vertical date picker with no customization

import com.foreverrafs.datepicker.Orientation

DatePickerTimeLine(
	modifier = Modifier,
	orientation = Orientation.Vertical,
	onDateSelected = {selectedDate: LocalDate->
		//do something with selected date
	}
) 

You can pass an optional state to the picker and specify the initial selected date

DatePickerTimeLine(
	modifier = Modifier,
	state = rememberDatePickerState(initialDate = LocalDate.now()),
	onDateSelected = { selectedDate: LocalDate ->
		//do something with selected date
	}
) 

To customize the full list of properties

import com.foreverrafs.datepicker.Orientation

val today = LocalDate.now()
val datePickerState =  
    rememberDatePickerState(initialDate = LocalDate.now())
    
DatePickerTimeline(
  modifier = Modifier.wrapContentSize(),  
  onDateSelected = { selectedDate: LocalDate ->
		// do something with the selected date
  },  
  backgroundColor = Color.Yellow,  // the main background color
  state = datePickerState,  
  orientation = Orientation.Horizontal,  
  selectedBackgroundColor = Color.Green, // The background of the currently selected date  
  selectedTextColor = Color.White,  // Text color of currently selected date
  dateTextColor = Color.Black, //Text color of all dates  
  eventDates = listOf(  
        today.plusDays(1),  
        today.plusDays(3),  
        today.plusDays(5),  
        today.plusDays(8),  
  ),   
  todayLabel = {  
      Text(  
          modifier = Modifier.padding(10.dp),  
          text = "Today",  
          color = Color.White,  
          style = MaterialTheme.typography.h6  
      )
    },  
  pastDaysCount = 10,  // The number of previous dates to display, relative to the initial date. Defaults to 120
  eventIndicatorColor = Color.Brown // Indicator color for marked event dates.
)

Scrolling to different dates

You can store the DatePickerState object into a variable and use it to perform smooth scrolling to any date. This is very similar to how LazyListState works. If you try to scroll to an invalid date as constrained by the pastDaysCount property, you are scrolled to the first valid date in the calendar instead.

val today = LocalDate.now()
val datePickerState =  
    rememberDatePickerState(initialDate = LocalDate.now())
    
Column{
  DatePickerTimeline(
    state = datePickerState
  )
  
  Button(
    onClick = {
      // Scroll to Jan 01, 2022
      datePickerState.smoothScrollTodate(LocalDate.of(2022, 1, 1)
    }
  ){
    Text("Go to different date")
  }
}

Credits

License

MIT License

Copyright (c) 2021 Rafsanjani Aziz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.