Skip to content

Commit

Permalink
Merge pull request #5 from andrewjapar/bug/fix_mismatch_date_shown
Browse files Browse the repository at this point in the history
Implement showDayOfWeekTitle feature and fix major bugs

- Fix mismatch day with date in calendarPicker
- Fix wrong calculation in calendarPicker
- Support disable day of week title
  • Loading branch information
andrewjapar authored Jan 31, 2020
2 parents 0115850 + f1391e2 commit d4068e3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repositories {
}

dependencies {
implementation 'com.andrewjapar.rangedatepicker:rangedatepicker:0.2.1'
implementation 'com.andrewjapar.rangedatepicker:rangedatepicker:0.2.2'
}
```

Expand All @@ -36,10 +36,11 @@ val startDate = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault())
val endDate = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault())
endDate.add(Calendar.MONTH, 6) // Add 6 months ahead from current date

calendar_view.apply {
calendar_view.modify {
setMode(SelectionMode.RANGE) // You can set it via XML
setRangeDate(startDate.time, endDate.time)
setSelectionDate(startDate.time, endDate.time)
showDayOfWeekTitle(false) // If you want to disable day of the week title, just make it false
}
```
### 3. Don't forget to set the listener to get range selection date
Expand Down
20 changes: 10 additions & 10 deletions app/src/main/java/com/andrewjapar/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)

val firstCalendarDate = Calendar.getInstance()
firstCalendarDate.add(Calendar.MONTH, 0)
firstCalendarDate.set(2019, 9, 1)

val secondCalendarDate = Calendar.getInstance()
secondCalendarDate.time = firstCalendarDate.time
Expand All @@ -22,17 +22,17 @@ class MainActivity : AppCompatActivity() {
thirdCalendarDate.time = firstCalendarDate.time
thirdCalendarDate.add(Calendar.MONTH, 2)

calendar_view.setOnRangeSelectedListener { startDate, endDate, startLabel, endLabel ->
departure_date.text = startLabel
return_date.text = endLabel
}
calendar_view.modify {
setOnRangeSelectedListener { startDate, endDate, startLabel, endLabel ->
departure_date.text = startLabel
return_date.text = endLabel
}

calendar_view.setOnStartSelectedListener { startDate, label ->
departure_date.text = label
return_date.text = "-"
}
setOnStartSelectedListener { startDate, label ->
departure_date.text = label
return_date.text = "-"
}

calendar_view.apply {
setRangeDate(firstCalendarDate.time, secondCalendarDate.time)
setSelectionDate(firstCalendarDate.time, thirdCalendarDate.time)
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/shadow_overlay"
app:picker_type="range" />
app:pickerType="range" />

<View
android:id="@+id/shadow_overlay"
Expand Down
2 changes: 1 addition & 1 deletion rangedatepicker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ tasks.withType(Javadoc) {
}

group = 'com.andrewjapar.rangedatepicker'
version = '0.2.0'
version = '0.2.2'

ext {
bintrayRepo = 'Android'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CalendarPicker : RecyclerView {
private var mStartDateSelection: SelectedDate? = null
private var mEndDateSelection: SelectedDate? = null
private var mPickerSelectionType = SelectionMode.RANGE
private var mShowDayOfWeekTitle = true

private var mOnStartSelectedListener: (startDate: Date, label: String) -> Unit = { _, _ -> }
private var mOnRangeSelectedListener: (startDate: Date, endDate: Date, startLabel: String, endLabel: String) -> Unit =
Expand Down Expand Up @@ -70,14 +71,22 @@ class CalendarPicker : RecyclerView {
initListener()
}

fun modify(func: CalendarPicker.() -> Unit): CalendarPicker {
this.func()
this.refreshData()

return this
}

fun setRangeDate(startDate: Date, endDate: Date) {
require(startDate.time <= endDate.time) { "startDate can't be higher than endDate" }

startCalendar.time = startDate
endCalendar.time = endDate
startCalendar.withTime(startDate)
endCalendar.withTime(endDate)
}

mCalendarData = buildCalendarData()
calendarAdapter.setData(mCalendarData)
fun showDayOfWeekTitle(show: Boolean) {
mShowDayOfWeekTitle = show
}

fun setSelectionDate(startDate: Date, endDate: Date? = null) {
Expand Down Expand Up @@ -126,21 +135,27 @@ class CalendarPicker : RecyclerView {
}
}
adapter = calendarAdapter
refreshData()
}

private fun refreshData() {
mCalendarData = buildCalendarData()
calendarAdapter.setData(mCalendarData)
}

private fun extractAttributes(attrs: AttributeSet) {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.CalendarPicker)
mPickerSelectionType =
SelectionMode.values()[typedArray.getInt(R.styleable.CalendarPicker_picker_type, 0)]
SelectionMode.values()[typedArray.getInt(R.styleable.CalendarPicker_pickerType, 0)]
mShowDayOfWeekTitle =
typedArray.getBoolean(R.styleable.CalendarPicker_showDayOfWeekTitle, true)
typedArray.recycle()
}

private fun buildCalendarData(): MutableList<CalendarEntity> {
val calendarData = mutableListOf<CalendarEntity>()
val cal = getInstance(timeZone, locale)
cal.time = startCalendar.time
cal.withTime(startCalendar.time)

val monthDifference = endCalendar.totalMonthDifference(startCalendar)

Expand All @@ -159,9 +174,9 @@ class CalendarPicker : RecyclerView {
DateState.WEEKDAY
}
when (day) {
cal.firstDayOfWeek -> {
1 -> {
calendarData.add(CalendarEntity.Month(cal.toPrettyMonthString()))
calendarData.add(CalendarEntity.Week)
if (mShowDayOfWeekTitle) calendarData.add(CalendarEntity.Week)
calendarData.addAll(createStartEmptyView(dayOfWeek))
calendarData.add(
CalendarEntity.Day(
Expand Down Expand Up @@ -213,7 +228,7 @@ class CalendarPicker : RecyclerView {
}

val listEmpty = mutableListOf<CalendarEntity.Empty>()
repeat((0..numberOfEmptyView).count()) { listEmpty.add(CalendarEntity.Empty) }
repeat((0 until numberOfEmptyView).count()) { listEmpty.add(CalendarEntity.Empty) }
return listEmpty
}

Expand All @@ -229,7 +244,7 @@ class CalendarPicker : RecyclerView {
}

val listEmpty = mutableListOf<CalendarEntity.Empty>()
repeat((0..numberOfEmptyView).count()) { listEmpty.add(CalendarEntity.Empty) }
repeat((0 until numberOfEmptyView).count()) { listEmpty.add(CalendarEntity.Empty) }
return listEmpty
}

Expand Down Expand Up @@ -324,6 +339,15 @@ class CalendarPicker : RecyclerView {
)
}

private fun Calendar.withTime(date: Date) {
clear()
time = date
set(HOUR_OF_DAY, 0)
set(MINUTE, 0)
set(SECOND, 0)
set(MILLISECOND, 0)
}

internal data class SelectedDate(val day: CalendarEntity.Day, val position: Int)

enum class SelectionMode { SINGLE, RANGE }
Expand Down
3 changes: 2 additions & 1 deletion rangedatepicker/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CalendarPicker">
<attr name="picker_type" format="enum">
<attr name="pickerType" format="enum">
<enum name="single" value="0" />
<enum name="range" value="1" />
</attr>
<attr name="showDayOfWeekTitle" format="boolean" />
</declare-styleable>
</resources>

0 comments on commit d4068e3

Please sign in to comment.