/
AutocheckPeriodTimePickerDialogFragment.kt
47 lines (39 loc) · 2.01 KB
/
AutocheckPeriodTimePickerDialogFragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.franckrj.jvnotif.dialogs
import android.app.Activity
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v7.app.AlertDialog
import com.franckrj.jvnotif.R
class AutocheckPeriodTimePickerDialogFragment : DialogFragment() {
companion object {
val ARG_CURRENT_REFRESH_TIME: String = "com.franckrj.jvnotif.autocheckperiodtimepicker.current_refresh_time"
val ARG_ALL_REFRESH_TIMES: String = "com.franckrj.jvnotif.autocheckperiodtimepicker.all_refresh_times"
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity!!)
val currentRefreshTime: Long = (arguments?.getLong(ARG_CURRENT_REFRESH_TIME, 0) ?: 0)
val equivalentInMsForChoices: LongArray = (arguments?.getLongArray(ARG_ALL_REFRESH_TIMES) ?: LongArray(0))
/* Retourne -1 si l'élément n'est pas trouvé, ce qui correspond à aucun item sélectionné dans le
* singleChoiceItems, ce qui est le comportement attendu. */
val currentSelectedItem: Int = equivalentInMsForChoices.indexOf(currentRefreshTime)
builder.setTitle(R.string.checkForNewMp)
@Suppress("ObjectLiteralToLambda")
builder.setSingleChoiceItems(R.array.choicesForAutocheckPeriodTime, currentSelectedItem, object : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface?, which: Int) {
if (which >= 0 && which < equivalentInMsForChoices.size) {
val parentActivity: Activity? = activity
if (parentActivity is NewCheckPeriodTimePicked) {
parentActivity.getNewCheckPeriodTime(equivalentInMsForChoices[which])
}
}
dialog?.dismiss()
}
})
return builder.create()
}
interface NewCheckPeriodTimePicked {
fun getNewCheckPeriodTime(newCheckPeriodTime: Long)
}
}