Skip to content

Commit

Permalink
Keep last selected times when changing dates (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thuy Trinh authored Dec 27, 2016
1 parent 1b13ebd commit 707310a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,25 @@ class DateTimeRangePickerViewModel(private val timeFormatter: TimeFormatter) {
}

val dateTimeZone = DateTimeZone.forTimeZone(timeZone)
val firstDateTime = DateTime(selectedDates.first().time, dateTimeZone)
startDateTime.onNext(firstDateTime)
when {
selectedDates.size == 1 -> endDateTime.onNext(firstDateTime)
else -> endDateTime.onNext(DateTime(selectedDates.last().time, dateTimeZone))
val startDateTimeValue = { DateTime(selectedDates.first().time, dateTimeZone) }
startDateTime.onNext(when {
startDateTime.hasValue() -> {
startDateTimeValue()
.withTime(startDateTime.value!!.toLocalTime())
}
else -> startDateTimeValue()
})

val endDateTimeValue = when {
selectedDates.size == 1 -> startDateTimeValue()
else -> DateTime(selectedDates.last().time, dateTimeZone)
}
endDateTime.onNext(when {
endDateTime.hasValue() -> {
endDateTimeValue.withTime(endDateTime.value!!.toLocalTime())
}
else -> endDateTimeValue
})
}

fun handleArgs(arguments: Bundle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DateTimeRangePickerViewModelTest {
assertThat(viewModel.endDateTime.value!!).isEqualTo(endDateTime)
}

@Test fun shouldCreateCorrectResultIntent() {
@Test fun shouldPutTimeZoneIdToResultIntent() {
whenever(timeFormatter.printTime(any())).thenReturn("")

val startDateTime = DateTime.now()
Expand All @@ -66,4 +66,58 @@ class DateTimeRangePickerViewModelTest {
assertThat(viewModel.startDateTime.value).isEqualTo(selectedDateTime)
assertThat(viewModel.endDateTime.value).isEqualTo(selectedDateTime)
}

@Test fun shouldKeepTimesAfterPickingSameDateForBothStartAndEnd() {
viewModel.timeZone = TimeZone.getTimeZone("CET")

val startDateTime = DateTime(DateTimeZone.forID("CET"))
.withYear(2014).withMonthOfYear(12).withDayOfMonth(22)
.withHourOfDay(10).withMinuteOfHour(30).withSecondOfMinute(20)
viewModel.startDateTime.onNext(startDateTime)

val endDateTime = DateTime(DateTimeZone.forID("CET"))
.withYear(2014).withMonthOfYear(12).withDayOfMonth(23)
.withHourOfDay(12).withMinuteOfHour(30).withSecondOfMinute(30)
viewModel.endDateTime.onNext(endDateTime)

viewModel.updateSelectedDates(listOf(
DateTime(DateTimeZone.forID("CET"))
.withYear(2014).withMonthOfYear(12).withDayOfMonth(25)
.withHourOfDay(4).withMinuteOfHour(0).withSecondOfMinute(10)
.toDate()
))
assertThat(viewModel.startDateTime.value.toLocalTime())
.isEqualTo(startDateTime.toLocalTime())
assertThat(viewModel.endDateTime.value.toLocalTime())
.isEqualTo(endDateTime.toLocalTime())
}

@Test fun shouldKeepTimesAfterPickingDifferentDatesForBothStartAndEnd() {
viewModel.timeZone = TimeZone.getTimeZone("CET")

val startDateTime = DateTime(DateTimeZone.forID("CET"))
.withYear(2014).withMonthOfYear(12).withDayOfMonth(22)
.withHourOfDay(10).withMinuteOfHour(30).withSecondOfMinute(20)
viewModel.startDateTime.onNext(startDateTime)

val endDateTime = DateTime(DateTimeZone.forID("CET"))
.withYear(2014).withMonthOfYear(12).withDayOfMonth(23)
.withHourOfDay(12).withMinuteOfHour(30).withSecondOfMinute(30)
viewModel.endDateTime.onNext(endDateTime)

viewModel.updateSelectedDates(listOf(
DateTime(DateTimeZone.forID("CET"))
.withYear(2014).withMonthOfYear(12).withDayOfMonth(25)
.withHourOfDay(4).withMinuteOfHour(0).withSecondOfMinute(10)
.toDate(),
DateTime(DateTimeZone.forID("CET"))
.withYear(2014).withMonthOfYear(12).withDayOfMonth(26)
.withHourOfDay(5).withMinuteOfHour(30).withSecondOfMinute(40)
.toDate()
))
assertThat(viewModel.startDateTime.value.toLocalTime())
.isEqualTo(startDateTime.toLocalTime())
assertThat(viewModel.endDateTime.value.toLocalTime())
.isEqualTo(endDateTime.toLocalTime())
}
}

0 comments on commit 707310a

Please sign in to comment.