From 707310aa5b8b7b40fc88b2aff0beb2c15387231c Mon Sep 17 00:00:00 2001 From: Thuy Trinh Date: Tue, 27 Dec 2016 22:30:01 +0700 Subject: [PATCH] Keep last selected times when changing dates (#9) --- .../DateTimeRangePickerViewModel.kt | 23 ++++++-- .../DateTimeRangePickerViewModelTest.kt | 56 ++++++++++++++++++- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/src/main/java/skedgo/datetimerangepicker/DateTimeRangePickerViewModel.kt b/src/main/java/skedgo/datetimerangepicker/DateTimeRangePickerViewModel.kt index 8155042..56aa4ad 100644 --- a/src/main/java/skedgo/datetimerangepicker/DateTimeRangePickerViewModel.kt +++ b/src/main/java/skedgo/datetimerangepicker/DateTimeRangePickerViewModel.kt @@ -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) { diff --git a/src/test/java/skedgo/datetimerangepicker/DateTimeRangePickerViewModelTest.kt b/src/test/java/skedgo/datetimerangepicker/DateTimeRangePickerViewModelTest.kt index 2dfe5e0..4eb3d9e 100644 --- a/src/test/java/skedgo/datetimerangepicker/DateTimeRangePickerViewModelTest.kt +++ b/src/test/java/skedgo/datetimerangepicker/DateTimeRangePickerViewModelTest.kt @@ -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() @@ -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()) + } }