Skip to content

Commit

Permalink
Add source code
Browse files Browse the repository at this point in the history
  • Loading branch information
Thuy Trinh committed Dec 20, 2016
1 parent 50ffe46 commit 2907ab8
Show file tree
Hide file tree
Showing 27 changed files with 802 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# OS X files
.DS_Store

# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm

*.iml

## Directory-based project format:
.idea/

## File-based project format:
*.ipr
*.iws

# Android Studio files
local.properties
.gradle
build/
66 changes: 66 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
buildscript {
ext.androidPluginVersion = '2.2.3'
ext.kotlinVersion = '1.0.5-3'
ext.supportLibraryVersion = '25.1.0'

repositories {
jcenter()
}

dependencies {
// See changelog: http://developer.android.com/tools/revisions/gradle-plugin.html.
classpath "com.android.tools.build:gradle:$androidPluginVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

repositories {
jcenter()
maven { url "https://jitpack.io" }
}

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
dataBinding { enabled = true }

defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

kapt { generateStubs = true }

dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:1.7.0'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'org.robolectric:robolectric:3.1.4'

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

compile 'io.reactivex:rxjava:1.2.0'
compile 'net.danlew:android.joda:2.9.3.1'
compile 'com.github.skedgo:activity-animations:v1.0'
compile 'com.squareup:android-times-square:1.6.5@aar'
compile "com.android.support:appcompat-v7:$supportLibraryVersion"
compile "com.android.support:design:$supportLibraryVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
kapt "com.android.databinding:compiler:$androidPluginVersion"
}
17 changes: 17 additions & 0 deletions proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/thuy/Dev/Tools/android-sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
11 changes: 11 additions & 0 deletions src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="skedgo.datetimerangepicker">

<application
android:name=".TestApp"
android:theme="@style/TestAppTheme">
<activity
android:name="skedgo.datetimerangepicker.DateTimeRangePickerActivity"
android:screenOrientation="portrait" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package skedgo.datetimerangepicker

import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.support.test.InstrumentationRegistry
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.util.*
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

@RunWith(AndroidJUnit4::class)
class DateTimeRangePickerActivityTest {
@Rule @JvmField val activityTestRule = ActivityTestRule<DateTimeRangePickerActivity>(
DateTimeRangePickerActivity::class.java,
true,
false
)
val countDownLatch: CountDownLatch by lazy { CountDownLatch(1) }

@Before fun setUp() {
val app = InstrumentationRegistry.getTargetContext().applicationContext as Application
app.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
override fun onActivityPaused(activity: Activity?) {
}

override fun onActivityStarted(activity: Activity?) {
}

override fun onActivityDestroyed(activity: Activity?) {
countDownLatch.countDown()
}

override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
}

override fun onActivityStopped(activity: Activity?) {
}

override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
}

override fun onActivityResumed(activity: Activity?) {
}
})
}

@Test fun showDateTimeRangePickerWithoutStartAndEndDateTimes() {
activityTestRule.launchActivity(
DateTimeRangePickerActivity.newIntent(
InstrumentationRegistry.getInstrumentation().targetContext,
TimeZone.getDefault(),
null, null
)
)
countDownLatch.await(10, TimeUnit.MINUTES)
}
}
11 changes: 11 additions & 0 deletions src/androidTest/java/skedgo/datetimerangepicker/TestApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package skedgo.datetimerangepicker

import android.app.Application
import net.danlew.android.joda.JodaTimeAndroid

class TestApp : Application() {
override fun onCreate() {
super.onCreate()
JodaTimeAndroid.init(this)
}
}
9 changes: 9 additions & 0 deletions src/androidTest/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="TestAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#334D6E</item>
<item name="colorPrimaryDark">#213247</item>
<item name="colorAccent">#23b15e</item>
</style>
</resources>
1 change: 1 addition & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="skedgo.datetimerangepicker" />
18 changes: 18 additions & 0 deletions src/main/java/skedgo/datetimerangepicker/Bindings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package skedgo.datetimerangepicker

import android.databinding.BindingAdapter
import android.support.v7.widget.Toolbar
import android.view.View

object Bindings {
@BindingAdapter("isDone") @JvmStatic
fun setIsDone(v: Toolbar, isDone: Boolean) {
val item = v.menu.findItem(R.id.dateTimeRangePickerDoneItem)
item!!.isEnabled = isDone
}

@BindingAdapter("isVisible") @JvmStatic
fun setIsVisible(v: View, isVisible: Boolean) {
v.visibility = if (isVisible) View.VISIBLE else View.GONE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package skedgo.datetimerangepicker

import android.app.Activity
import android.app.TimePickerDialog
import android.content.Context
import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v4.util.Pair
import android.text.format.DateFormat
import com.squareup.timessquare.CalendarPickerView
import org.joda.time.DateTime
import skedgo.anim.AnimatedTransitionActivity
import skedgo.datetimerangepicker.databinding.DateTimeRangePickerBinding
import java.util.*

class DateTimeRangePickerActivity : AnimatedTransitionActivity() {
companion object {
fun newIntent(
context: Context?,
timeZone: TimeZone?,
startTimeInMillis: Long?,
endTimeInMillis: Long?): Intent {
val intent = Intent(context!!, DateTimeRangePickerActivity::class.java)
startTimeInMillis?.let { intent.putExtra(DateTimeRangePickerViewModel.KEY_START_TIME_IN_MILLIS, it) }
endTimeInMillis?.let { intent.putExtra(DateTimeRangePickerViewModel.KEY_END_TIME_IN_MILLIS, it) }
intent.putExtra(DateTimeRangePickerViewModel.KEY_TIME_ZONE, timeZone!!.id)
return intent
}
}

private val viewModel: DateTimeRangePickerViewModel by lazy {
DateTimeRangePickerViewModel(TimeFormatter(applicationContext))
}
private val binding: DateTimeRangePickerBinding by lazy {
DataBindingUtil.setContentView<DateTimeRangePickerBinding>(
this,
R.layout.date_time_range_picker
)
}

override fun onCreate(savedInstanceState: Bundle?) {
setEnteringAnimation(Pair(R.anim.entering_slide_up, R.anim.exiting_fade_out))
setExitingAnimation(Pair(R.anim.entering_fade_in, R.anim.exiting_slide_down))
super.onCreate(savedInstanceState)

viewModel.handleArgs(intent.extras)
binding.setViewModel(viewModel)

val toolbar = binding.toolbar
toolbar.inflateMenu(R.menu.date_time_range_picker)
toolbar.setNavigationOnClickListener { v -> finish() }
toolbar.setOnMenuItemClickListener { item ->
when {
item.itemId == R.id.dateTimeRangePickerDoneItem -> {
setResult(Activity.RESULT_OK, viewModel.createResultIntent())
finish()
}
}
true
}

val calendarPickerView = binding.calendarPickerView
calendarPickerView.init(viewModel.minDate, viewModel.maxDate)
.inMode(CalendarPickerView.SelectionMode.RANGE)
viewModel.startDateTime.value?.let { calendarPickerView.selectDate(it.toDate()) }
viewModel.endDateTime.value?.let { calendarPickerView.selectDate(it.toDate()) }

calendarPickerView.setOnDateSelectedListener(object : CalendarPickerView.OnDateSelectedListener {
override fun onDateSelected(date: Date) {
viewModel.updateSelectedDates(calendarPickerView.selectedDates)
}

override fun onDateUnselected(date: Date) {
viewModel.updateSelectedDates(calendarPickerView.selectedDates)
}
})

binding.pickStartTimeView.setOnClickListener { v ->
showTimePicker(viewModel.startDateTime.value, viewModel.onStartTimeSelected)
}
binding.pickEndTimeView.setOnClickListener { v ->
showTimePicker(viewModel.endDateTime.value, viewModel.onEndTimeSelected)
}
}

private fun showTimePicker(
initialTime: DateTime,
listener: TimePickerDialog.OnTimeSetListener) {
TimePickerDialog(
this,
listener,
initialTime.hourOfDay,
initialTime.minuteOfHour,
DateFormat.is24HourFormat(this)
).show()
}
}
Loading

0 comments on commit 2907ab8

Please sign in to comment.