Skip to content

Commit

Permalink
Allow selecting same date for start and end (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thuy Trinh authored Dec 27, 2016
1 parent 7194744 commit 1b13ebd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ A date time range picker for android
![Pick start time](images/Screenshot_1482250231.png) ![Have start date time and end date time](images/Screenshot_1482250242.png)

## Usage
Firstly, grab latest release of the library via [JitPack](https://jitpack.io/#skedgo/DateTimeRangePicker). And note that, it utilizes [Joda-Time](https://github.com/dlew/joda-time-android) to process some date time logic under the hood. So you might need to [set up Joda-Time properly](https://github.com/dlew/joda-time-android#usage).

With start and end date times specified:
```kotlin
val intent = DateTimeRangePickerActivity.newIntent(
Expand Down Expand Up @@ -36,6 +38,6 @@ At `onActivityResult()`, `DateTimeRangePickerActivity` will return an `Intent` d

## Demo
Run 2 following instrumentation tests on `DateTimeRangePickerActivityTest` to see the 2 usages:
* `showDateTimeRangePickerWithoutStartAndEndDateTimes()`
* `showDateTimeRangePickerWithStartAndEndDateTimes()`
* `withoutStartAndEndDateTimes()`
* `withStartAndEndDateTimes()`

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DateTimeRangePickerActivityTest {
})
}

@Test fun showDateTimeRangePickerWithoutStartAndEndDateTimes() {
@Test fun withoutStartAndEndDateTimes() {
activityTestRule.launchActivity(
DateTimeRangePickerActivity.newIntent(
InstrumentationRegistry.getInstrumentation().targetContext,
Expand All @@ -63,7 +63,7 @@ class DateTimeRangePickerActivityTest {
countDownLatch.await(10, TimeUnit.MINUTES)
}

@Test fun showDateTimeRangePickerWithStartAndEndDateTimes() {
@Test fun withStartAndEndDateTimes() {
activityTestRule.launchActivity(
DateTimeRangePickerActivity.newIntent(
InstrumentationRegistry.getInstrumentation().targetContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ class DateTimeRangePickerViewModel(private val timeFormatter: TimeFormatter) {
endDateTime.onNext(null)
return
}
startDateTime.onNext(DateTime(selectedDates.first().time))

val dateTimeZone = DateTimeZone.forTimeZone(timeZone)
val firstDateTime = DateTime(selectedDates.first().time, dateTimeZone)
startDateTime.onNext(firstDateTime)
when {
selectedDates.size == 1 -> endDateTime.onNext(null)
else -> endDateTime.onNext(DateTime(selectedDates.last().time))
selectedDates.size == 1 -> endDateTime.onNext(firstDateTime)
else -> endDateTime.onNext(DateTime(selectedDates.last().time, dateTimeZone))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,13 @@ class DateTimeRangePickerViewModelTest {
val resultIntent = viewModel.createResultIntent()
assertThat(resultIntent.getStringExtra("timeZone")).isEqualTo("CET")
}

@Test fun shouldPickOneDateForBothStartAndEndDateTimes() {
viewModel.timeZone = TimeZone.getTimeZone("CET")
val selectedDateTime = DateTime.now(DateTimeZone.forID("CET"));

viewModel.updateSelectedDates(listOf(selectedDateTime.toDate()))
assertThat(viewModel.startDateTime.value).isEqualTo(selectedDateTime)
assertThat(viewModel.endDateTime.value).isEqualTo(selectedDateTime)
}
}

0 comments on commit 1b13ebd

Please sign in to comment.