1+ package com.wstxda.switchai.ui.component
2+
3+ import android.os.Bundle
4+ import android.widget.Toast
5+ import androidx.appcompat.app.AlertDialog
6+ import androidx.preference.PreferenceDialogFragmentCompat
7+ import com.wstxda.switchai.R
8+ import com.wstxda.switchai.fragment.preferences.MultiSelectListPreference
9+ import com.wstxda.switchai.utils.Constants
10+
11+ class AssistantManagerDialog : PreferenceDialogFragmentCompat () {
12+
13+ private lateinit var pref: MultiSelectListPreference
14+ private val currentSelections = HashSet <String >()
15+ private var toastShownForInvalidSelection = false
16+
17+ companion object {
18+ fun newInstance (key : String ) = AssistantManagerDialog ().apply {
19+ arguments = Bundle ().apply { putString(ARG_KEY , key) }
20+ }
21+ }
22+
23+ override fun onCreate (savedInstanceState : Bundle ? ) {
24+ super .onCreate(savedInstanceState)
25+ pref = preference as MultiSelectListPreference
26+
27+ currentSelections.clear()
28+ val restored = savedInstanceState?.getStringArray(Constants .ASSISTANT_MANAGER_DIALOG )
29+ if (restored != null ) {
30+ currentSelections.addAll(restored)
31+ } else {
32+ currentSelections.addAll(pref.values)
33+ }
34+ }
35+
36+ override fun onSaveInstanceState (outState : Bundle ) {
37+ super .onSaveInstanceState(outState)
38+ outState.putStringArray(
39+ Constants .ASSISTANT_MANAGER_DIALOG , currentSelections.toTypedArray()
40+ )
41+ }
42+
43+ override fun onPrepareDialogBuilder (builder : AlertDialog .Builder ) {
44+ super .onPrepareDialogBuilder(builder)
45+
46+ val entries = pref.entries
47+ val entryValues = pref.entryValues
48+ val checkedItems = BooleanArray (entryValues.size) { index ->
49+ currentSelections.contains(entryValues[index].toString())
50+ }
51+
52+ builder.setMultiChoiceItems(entries, checkedItems) { _, which, isChecked ->
53+ val value = entryValues[which].toString()
54+ if (isChecked) currentSelections.add(value) else currentSelections.remove(value)
55+ updatePositiveButtonState()
56+ }
57+ }
58+
59+ override fun onStart () {
60+ super .onStart()
61+ updatePositiveButtonState()
62+ }
63+
64+ private fun updatePositiveButtonState () {
65+ val dialog = dialog as ? AlertDialog ? : return
66+ val okButton = dialog.getButton(AlertDialog .BUTTON_POSITIVE )
67+
68+ val selectionCount = currentSelections.size
69+ val valid = selectionCount in pref.minSelection.. pref.maxSelection
70+ okButton.isEnabled = valid
71+
72+ if (! valid) {
73+ if (! toastShownForInvalidSelection) {
74+ toastShownForInvalidSelection = true
75+
76+ val messageRes = when {
77+ selectionCount < pref.minSelection -> R .string.error_min_selection
78+ selectionCount > pref.maxSelection -> R .string.error_max_selection
79+ else -> 0
80+ }
81+
82+ if (messageRes != 0 ) {
83+ Toast .makeText(
84+ requireContext(), getString(
85+ messageRes,
86+ if (selectionCount < pref.minSelection) pref.minSelection else pref.maxSelection
87+ ), Toast .LENGTH_SHORT
88+ ).show()
89+ }
90+ }
91+ } else {
92+ toastShownForInvalidSelection = false
93+ }
94+ }
95+
96+ override fun onDialogClosed (positiveResult : Boolean ) {
97+ if (positiveResult) {
98+ if (pref.callChangeListener(currentSelections)) {
99+ pref.values = currentSelections
100+ }
101+ }
102+ }
103+ }
0 commit comments