Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some minor issues #4

Merged
merged 4 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,36 @@ A date time range picker for android
![No start date time and no end date time](images/Screenshot_1482250212.png) ![Pick start date](images/Screenshot_1482250219.png)

![Pick start time](images/Screenshot_1482250231.png) ![Have start date time and end date time](images/Screenshot_1482250242.png)

## Usage
With start and end date times specified:
```kotlin
val intent = DateTimeRangePickerActivity.newIntent(
context,
TimeZone.getDefault(),
DateTime.now().millis,
DateTime.now().plusDays(2).millis
)
activity.startActivityForResult(intent, RQC_PICK_DATE_TIME_RANGE)
```

Or without start and end date times:
```kotlin
val intent = DateTimeRangePickerActivity.newIntent(
context,
TimeZone.getDefault(),
null, null
)
activity.startActivityForResult(intent, RQC_PICK_DATE_TIME_RANGE)
```

At `onActivityResult()`, `DateTimeRangePickerActivity` will return an `Intent` data having following:
* `startTimeInMillis` as `Long`
* `endTimeInMillis` as `Long`
* `timeZone` as `String`

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

1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dependencies {
testCompile 'org.assertj:assertj-core:1.7.0'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'org.robolectric:robolectric:3.1.4'
testCompile 'com.nhaarman:mockito-kotlin:1.0.1'

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Bundle
import android.support.test.InstrumentationRegistry
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import org.joda.time.DateTime
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -24,6 +25,7 @@ class DateTimeRangePickerActivityTest {
val countDownLatch: CountDownLatch by lazy { CountDownLatch(1) }

@Before fun setUp() {
// To terminate the test after pressing back.
val app = InstrumentationRegistry.getTargetContext().applicationContext as Application
app.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
override fun onActivityPaused(activity: Activity?) {
Expand Down Expand Up @@ -60,4 +62,16 @@ class DateTimeRangePickerActivityTest {
)
countDownLatch.await(10, TimeUnit.MINUTES)
}

@Test fun showDateTimeRangePickerWithStartAndEndDateTimes() {
activityTestRule.launchActivity(
DateTimeRangePickerActivity.newIntent(
InstrumentationRegistry.getInstrumentation().targetContext,
TimeZone.getDefault(),
DateTime.now().millis,
DateTime.now().plusDays(2).millis
)
)
countDownLatch.await(10, TimeUnit.MINUTES)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class DateTimeRangePickerViewModel(private val timeFormatter: TimeFormatter) {
}
if (arguments.containsKey(KEY_END_TIME_IN_MILLIS)) {
val endInMillis = arguments.getLong(KEY_END_TIME_IN_MILLIS)
startDateTime.onNext(DateTime(endInMillis, DateTimeZone.forTimeZone(timeZone)))
endDateTime.onNext(DateTime(endInMillis, DateTimeZone.forTimeZone(timeZone)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/skedgo/datetimerangepicker/TimeFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.joda.time.format.DateTimeFormat
import java.util.*

open class TimeFormatter(val context: Context) {
fun printTime(dateTime: DateTime): String {
open fun printTime(dateTime: DateTime): String {
val formatter = if (DateFormat.is24HourFormat(context))
DateTimeFormat.forPattern("H:mm").withLocale(Locale.getDefault())
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package skedgo.datetimerangepicker

import android.os.Bundle
import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.whenever
import org.assertj.core.api.Assertions.assertThat
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.mock
import org.robolectric.annotation.Config
import java.util.*

@RunWith(TestRunner::class)
@Config(constants = BuildConfig::class, sdk = intArrayOf(21))
class DateTimeRangePickerViewModelTest {
val timeFormatter: TimeFormatter by lazy { mock(TimeFormatter::class.java) }
val timeFormatter: TimeFormatter = mock()
val viewModel: DateTimeRangePickerViewModel by lazy {
DateTimeRangePickerViewModel(timeFormatter)
}
Expand All @@ -22,4 +26,21 @@ class DateTimeRangePickerViewModelTest {
viewModel.handleArgs(args)
assertThat(viewModel.timeZone).isEqualTo(TimeZone.getTimeZone("CET"))
}

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

val startDateTime = DateTime(DateTimeZone.forID("CET"))
val endDateTime = startDateTime.plusDays(2)

val args = Bundle()
args.putString("timeZone", "CET")
args.putLong("startTimeInMillis", startDateTime.millis)
args.putLong("endTimeInMillis", endDateTime.millis)

viewModel.handleArgs(args)

assertThat(viewModel.startDateTime.value!!).isEqualTo(startDateTime)
assertThat(viewModel.endDateTime.value!!).isEqualTo(endDateTime)
}
}
12 changes: 11 additions & 1 deletion src/test/java/skedgo/datetimerangepicker/TestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.junit.runners.model.InitializationError
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.manifest.AndroidManifest
import org.robolectric.res.FileFsFile

/**
* To workaround https://github.com/robolectric/robolectric/issues/1430.
Expand All @@ -16,8 +17,17 @@ class TestRunner @Throws(InitializationError::class)
constructor(klass: Class<*>) : RobolectricTestRunner(klass) {
override fun getAppManifest(config: Config): AndroidManifest {
val appManifest = super.getAppManifest(config)

// After upgrading to gradle plugin v2.2.0, the file path of
// the AM file no longer contains `full` but `aapt`.
// As a result, the RobolectricTestRunner complained
// that it didn't find the AM file, resulting in a bunch of failures.
// So this patch was born to fix that issue. Note that it might be
// incompatible w/ future releases of Robolectric.
val newAndroidManifestFilePath = appManifest.androidManifestFile.path.replace("full", "aapt")

return object : AndroidManifest(
appManifest.androidManifestFile,
FileFsFile.from(newAndroidManifestFilePath),
appManifest.resDirectory,
appManifest.assetsDirectory
) {
Expand Down