daily pastebin goal
60%
SHARE
TWEET

Untitled

a guest Nov 21st, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.app.DatePickerDialog
  2. import android.app.Dialog
  3. import android.os.Bundle
  4. import android.support.v4.app.DialogFragment
  5. import java.util.*
  6.  
  7.  
  8. class ChooseDayDialog : DialogFragment() {
  9.  
  10.     companion object {
  11.  
  12.         private val KEY_INITIAL_TIMESTAMP = "KEY_INITIAL_TIMESTAMP"
  13.         private val KEY_REQUEST_CODE = "KEY_REQUEST_CODE"
  14.  
  15.         private val DEFAULT_REQUEST_CODE = 20
  16.  
  17.         fun createInstance(initialTimestamp: Long,
  18.                            requestCode: Int = DEFAULT_REQUEST_CODE) =
  19.                 ChooseDayDialog().apply {
  20.                     arguments = Bundle().apply {
  21.                         putLong(KEY_INITIAL_TIMESTAMP, initialTimestamp)
  22.                         putInt(KEY_REQUEST_CODE, requestCode)
  23.                     }
  24.                 }
  25.  
  26.     }
  27.  
  28.     interface OnDayChosenListener {
  29.  
  30.         fun onDayChosen(requestCode: Int, year: Int, month: Int, dayOfMonth: Int)
  31.  
  32.     }
  33.  
  34.     private lateinit var listener: OnDayChosenListener
  35.  
  36.     override fun onCreate(savedInstanceState: Bundle?) {
  37.         super.onCreate(savedInstanceState)
  38.         val activity = activity
  39.         if (activity is OnDayChosenListener) {
  40.             listener = activity
  41.         } else {
  42.             throw IllegalStateException("Activity must implement OnDayChosenListener")
  43.         }
  44.     }
  45.  
  46.     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
  47.         val timestamp = arguments?.getLong(KEY_INITIAL_TIMESTAMP, -1L) ?: -1L
  48.         if (timestamp == -1L) {
  49.             throw IllegalStateException("no initial time given")
  50.         }
  51.         val requestCode = arguments?.getInt(KEY_REQUEST_CODE, DEFAULT_REQUEST_CODE)
  52.                 ?: DEFAULT_REQUEST_CODE
  53.  
  54.         val calendar = Calendar.getInstance().apply {
  55.             timeInMillis = timestamp
  56.         }
  57.         return DatePickerDialog(activity,
  58.                 DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
  59.                     listener.onDayChosen(requestCode, year, month, dayOfMonth)
  60.                 },
  61.                 calendar.get(Calendar.YEAR),
  62.                 calendar.get(Calendar.MONTH),
  63.                 calendar.get(Calendar.DAY_OF_MONTH))
  64.     }
  65.  
  66. }
RAW Paste Data
Top