Created
December 19, 2017 05:38
-
-
Save nakamuuu/0c8b8fa7633bc1d931f3969731dae056 to your computer and use it in GitHub Desktop.
SpinnerDatePickerDialogFragment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:calendarViewShown="false" | |
android:datePickerMode="spinner" | |
android:spinnersShown="true" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Dialog | |
import android.content.Context | |
import android.os.Bundle | |
import android.support.v7.app.AlertDialog | |
import android.support.v7.app.AppCompatDialogFragment | |
import android.text.format.DateUtils | |
import android.view.View | |
import android.widget.DatePicker | |
import icepick.Icepick | |
import icepick.State | |
import java.util.Calendar | |
import java.util.Date | |
class SpinnerDatePickerDialogFragment : AppCompatDialogFragment(), DatePicker.OnDateChangedListener { | |
companion object { | |
@JvmField val TAG: String = SpinnerDatePickerDialogFragment::class.java.simpleName | |
private const val ARGS_YEAR = "year" | |
private const val ARGS_MONTH = "month" | |
private const val ARGS_DAY = "day" | |
@JvmStatic | |
fun newInstance(year: Int, monthOfYear: Int, dayOfMonth: Int) = SpinnerDatePickerDialogFragment().apply { | |
arguments = Bundle().apply { | |
putInt(ARGS_YEAR, year) | |
putInt(ARGS_MONTH, monthOfYear) | |
putInt(ARGS_DAY, dayOfMonth) | |
} | |
} | |
} | |
private var onDateSetListener: OnDateSetListener? = null | |
@State @JvmField internal var year = 0 | |
@State @JvmField internal var monthOfYear = 0 | |
@State @JvmField internal var dayOfMonth = 0 | |
override fun onAttach(context: Context?) { | |
super.onAttach(context) | |
onDateSetListener = when { | |
context is OnDateSetListener -> context | |
parentFragment is OnDateSetListener -> parentFragment as OnDateSetListener | |
else -> throw IllegalArgumentException("The context or parent fragment must implement " | |
+ OnDateSetListener::class.java) | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
Icepick.restoreInstanceState(this, savedInstanceState) | |
} | |
override fun onSaveInstanceState(outState: Bundle?) { | |
super.onSaveInstanceState(outState) | |
Icepick.saveInstanceState(this, outState) | |
} | |
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | |
if (savedInstanceState == null) { | |
year = arguments.getInt(ARGS_YEAR) | |
monthOfYear = arguments.getInt(ARGS_MONTH) | |
dayOfMonth = arguments.getInt(ARGS_DAY) | |
} | |
val datePicker = (View.inflate(context, R.layout.spinner_date_picker_dialog, null) as DatePicker).also { | |
it.init(year, monthOfYear, dayOfMonth, this) | |
it.maxDate = Date().time | |
} | |
return AlertDialog.Builder(context) | |
.setTitle(getTitle(year, monthOfYear, dayOfMonth)) | |
.setView(datePicker) | |
.setPositiveButton(R.string.ok, { _, _ -> | |
onDateSetListener?.onDateSet(datePicker, datePicker.year, datePicker.month, datePicker.dayOfMonth) | |
}) | |
.create() | |
} | |
override fun onDateChanged(view: DatePicker?, year: Int, monthOfYear: Int, dayOfMonth: Int) { | |
this.year = year | |
this.monthOfYear = monthOfYear | |
this.dayOfMonth = dayOfMonth | |
dialog?.setTitle(getTitle(year, monthOfYear, dayOfMonth)) | |
} | |
private fun getTitle(year: Int, monthOfYear: Int, dayOfMonth: Int) = DateUtils.formatDateTime( | |
context, | |
Calendar.getInstance().apply { | |
set(Calendar.YEAR, year) | |
set(Calendar.MONTH, monthOfYear) | |
set(Calendar.DAY_OF_MONTH, dayOfMonth) | |
}.timeInMillis, | |
DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_SHOW_YEAR or DateUtils.FORMAT_ABBREV_MONTH | |
) | |
interface OnDateSetListener { | |
fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int, dayOfMonth: Int) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment