1
1
package org.simple.clinic.bloodsugar.unitselection
2
2
3
3
import android.app.Dialog
4
+ import android.app.Dialog.BUTTON_POSITIVE
4
5
import android.content.Context
5
6
import android.os.Bundle
6
7
import android.view.LayoutInflater
7
8
import android.view.View
8
9
import android.view.ViewGroup
9
10
import androidx.appcompat.app.AlertDialog
10
- import androidx.fragment.app.FragmentManager
11
11
import com.google.android.material.dialog.MaterialAlertDialogBuilder
12
- import com.jakewharton.rxbinding3.widget.checkedChanges
13
12
import com.spotify.mobius.functions.Consumer
14
13
import io.reactivex.Observable
15
14
import io.reactivex.rxkotlin.cast
@@ -43,34 +42,6 @@ class BloodSugarUnitSelectionDialog : BaseDialog<
43
42
@Inject
44
43
lateinit var router: Router
45
44
46
- companion object {
47
-
48
- private const val FRAGMENT_TAG = " blood_sugar_unit_selection_tag"
49
- private const val KEY_UNIT_PREF = " bloodSugarUnitSelection"
50
-
51
- fun show (fragmentManager : FragmentManager , bloodSugarUnitPreference : BloodSugarUnitPreference ) {
52
- val existingFragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG )
53
-
54
- if (existingFragment != null ) {
55
- fragmentManager
56
- .beginTransaction()
57
- .remove(existingFragment)
58
- .commitNowAllowingStateLoss()
59
- }
60
-
61
- val arguments = Bundle ()
62
- arguments.putSerializable(KEY_UNIT_PREF , bloodSugarUnitPreference)
63
-
64
- val fragment = BloodSugarUnitSelectionDialog ()
65
- fragment.arguments = arguments
66
-
67
- fragmentManager
68
- .beginTransaction()
69
- .add(fragment, FRAGMENT_TAG )
70
- .commitNowAllowingStateLoss()
71
- }
72
- }
73
-
74
45
override fun bindView (
75
46
layoutInflater : LayoutInflater ,
76
47
container : ViewGroup ?
@@ -86,6 +57,8 @@ class BloodSugarUnitSelectionDialog : BaseDialog<
86
57
viewEffectsConsumer = viewEffectsConsumer
87
58
).build()
88
59
60
+ override fun viewEffectHandler () = BloodSugarUnitSelectionViewEffectHandler (this )
61
+
89
62
override fun events (): Observable <BloodSugarUnitSelectionEvent > = hotEvents.cast()
90
63
91
64
override fun onAttach (context : Context ) {
@@ -94,38 +67,33 @@ class BloodSugarUnitSelectionDialog : BaseDialog<
94
67
}
95
68
96
69
override fun onCreateDialog (savedInstanceState : Bundle ? ): Dialog {
97
- val radioIdToBloodSugarUnits = mapOf (
98
- R .id.bloodSugarUnitMg to BloodSugarUnitPreference .Mg ,
99
- R .id.bloodSugarUnitMmol to BloodSugarUnitPreference .Mmol
100
- )
101
-
102
70
return MaterialAlertDialogBuilder (requireContext())
103
71
.setTitle(R .string.blood_sugar_unit_selection_choose)
104
- .setPositiveButton(R .string.blood_sugar_unit_selection_done) { _, _ ->
105
- val bloodSugarUnitSelectionValue = radioIdToBloodSugarUnits.getValue(bloodSugarUnitGroup.checkedRadioButtonId)
106
- hotEvents.onNext(DoneClicked (bloodSugarUnitSelectionValue))
107
- }
72
+ .setPositiveButton(R .string.blood_sugar_unit_selection_done, null )
108
73
.create()
109
74
}
110
75
76
+ override fun onResume () {
77
+ super .onResume()
78
+ if (dialog != null ) {
79
+ val radioIdToBloodSugarUnits = mapOf (
80
+ R .id.bloodSugarUnitMg to BloodSugarUnitPreference .Mg ,
81
+ R .id.bloodSugarUnitMmol to BloodSugarUnitPreference .Mmol
82
+ )
83
+ val alertDialog = (dialog as AlertDialog )
84
+ alertDialog.getButton(BUTTON_POSITIVE ).setOnClickListener {
85
+ val bloodSugarUnitSelectionValue = radioIdToBloodSugarUnits.getValue(bloodSugarUnitGroup.checkedRadioButtonId)
86
+ hotEvents.onNext(DoneClicked (bloodSugarUnitSelectionValue))
87
+ }
88
+ }
89
+ }
90
+
111
91
override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View ? {
112
92
val view = super .onCreateView(inflater, container, savedInstanceState)
113
93
(dialog as ? AlertDialog )?.setView(view)
114
94
return view
115
95
}
116
96
117
- private fun radioButtonClicks (): Observable <BloodSugarUnitSelectionEvent > {
118
- return bloodSugarUnitGroup
119
- .checkedChanges()
120
- .map { checkedId ->
121
- when (checkedId) {
122
- R .id.bloodSugarUnitMg -> SaveBloodSugarUnitPreference (BloodSugarUnitPreference .Mg )
123
- R .id.bloodSugarUnitMmol -> SaveBloodSugarUnitPreference (BloodSugarUnitPreference .Mmol )
124
- else -> SaveBloodSugarUnitPreference (BloodSugarUnitPreference .Mg )
125
- }
126
- }
127
- }
128
-
129
97
override fun closeDialog () {
130
98
router.pop()
131
99
}
0 commit comments