EditQuickWizardDialog -> jetpack

This commit is contained in:
Milos Kozak 2021-02-01 20:36:58 +01:00
parent acf3b23bf7
commit 99185f3d1f
4 changed files with 394 additions and 208 deletions

View file

@ -1,29 +1,33 @@
package info.nightscout.androidaps.plugins.general.overview.dialogs
import android.app.TimePickerDialog
import android.os.Bundle
import android.text.format.DateFormat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.widget.AdapterView
import android.widget.ArrayAdapter
import dagger.android.support.DaggerDialogFragment
import info.nightscout.androidaps.R
import info.nightscout.androidaps.utils.wizard.QuickWizard
import info.nightscout.androidaps.utils.wizard.QuickWizardEntry
import info.nightscout.androidaps.databinding.OverviewEditquickwizardDialogBinding
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.overview.events.EventQuickWizardChange
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.SafeParse
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.extensions.selectedItemPosition
import info.nightscout.androidaps.utils.extensions.setEnableForChildren
import info.nightscout.androidaps.utils.extensions.setSelection
import info.nightscout.androidaps.utils.wizard.QuickWizard
import info.nightscout.androidaps.utils.wizard.QuickWizardEntry
import kotlinx.android.synthetic.main.okcancel.*
import kotlinx.android.synthetic.main.overview_editquickwizard_dialog.*
import org.json.JSONException
import java.util.*
import javax.inject.Inject
class EditQuickWizardDialog : DaggerDialogFragment() {
class EditQuickWizardDialog : DaggerDialogFragment(), View.OnClickListener {
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var quickWizard: QuickWizard
@ -31,37 +35,43 @@ class EditQuickWizardDialog : DaggerDialogFragment() {
var position = -1
var fromSeconds: Int = 0
var toSeconds: Int = 0
private var _binding: OverviewEditquickwizardDialogBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
savedInstanceState: Bundle?): View {
dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE)
dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
isCancelable = true
dialog?.setCanceledOnTouchOutside(false)
return inflater.inflate(R.layout.overview_editquickwizard_dialog, container, false)
_binding = OverviewEditquickwizardDialogBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
(arguments ?: savedInstanceState)?.let { bundle ->
position = bundle.getInt("position", -1)
}
val entry = if (position ==-1) quickWizard.newEmptyItem() else quickWizard[position]
val entry = if (position == -1) quickWizard.newEmptyItem() else quickWizard[position]
ok.setOnClickListener {
if (overview_editquickwizard_from_spinner.selectedItem == null) return@setOnClickListener
if (overview_editquickwizard_to_spinner.selectedItem == null) return@setOnClickListener
try {
entry.storage.put("buttonText", overview_editquickwizard_button_edit.text.toString())
entry.storage.put("carbs", SafeParse.stringToInt(overview_editquickwizard_carbs_edit.text.toString()))
val validFromInt = DateUtil.toSeconds(overview_editquickwizard_from_spinner.selectedItem.toString())
entry.storage.put("validFrom", validFromInt)
val validToInt = DateUtil.toSeconds(overview_editquickwizard_to_spinner.selectedItem.toString())
entry.storage.put("validTo", validToInt)
entry.storage.put("useBG", overview_editquickwizard_usebg_spinner.selectedItemPosition)
entry.storage.put("useCOB", overview_editquickwizard_usecob_spinner.selectedItemPosition)
entry.storage.put("useBolusIOB", overview_editquickwizard_usebolusiob_spinner.selectedItemPosition)
entry.storage.put("useBasalIOB", overview_editquickwizard_usebasaliob_spinner.selectedItemPosition)
entry.storage.put("useTrend", overview_editquickwizard_usetrend_spinner.selectedItemPosition)
entry.storage.put("useSuperBolus", overview_editquickwizard_usesuperbolus_spinner.selectedItemPosition)
entry.storage.put("useTempTarget", overview_editquickwizard_usetemptarget_spinner.selectedItemPosition)
entry.storage.put("buttonText", binding.buttonEdit.text.toString())
entry.storage.put("carbs", SafeParse.stringToInt(binding.carbsEdit.text.toString()))
entry.storage.put("validFrom", fromSeconds)
entry.storage.put("validTo", toSeconds)
entry.storage.put("useBG", binding.useBg.selectedItemPosition)
entry.storage.put("useCOB", binding.useCob.selectedItemPosition)
entry.storage.put("useBolusIOB", binding.useBolusIob.selectedItemPosition)
entry.storage.put("useBasalIOB", binding.useBasalIob.selectedItemPosition)
entry.storage.put("useTrend", binding.useTrend.selectedItemPosition)
entry.storage.put("useSuperBolus", binding.useSuperBolus.selectedItemPosition)
entry.storage.put("useTempTarget", binding.useTempTarget.selectedItemPosition)
} catch (e: JSONException) {
aapsLogger.error("Unhandled exception", e)
}
@ -72,41 +82,58 @@ class EditQuickWizardDialog : DaggerDialogFragment() {
}
cancel.setOnClickListener { dismiss() }
var posFrom = 0
var posTo = 95
val timeList = ArrayList<CharSequence>()
var pos = 0
var t = 0
while (t < 24 * 60 * 60) {
timeList.add(dateUtil.timeString(DateUtil.toDate(t)))
if (entry.validFrom() == t) posFrom = pos
if (entry.validTo() == t) posTo = pos
pos++
t += 15 * 60
// create an OnTimeSetListener
val fromTimeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute ->
fromSeconds = (T.hours(hour.toLong()).secs() + T.mins(minute.toLong()).secs()).toInt()
from.text = dateUtil.timeString(DateUtil.toDate(fromSeconds))
}
timeList.add(dateUtil.timeString(DateUtil.toDate(24 * 60 * 60 - 60)))
val adapter = context?.let { context -> ArrayAdapter(context, R.layout.spinner_centered, timeList) }
overview_editquickwizard_from_spinner.adapter = adapter
overview_editquickwizard_to_spinner.adapter = adapter
overview_editquickwizard_button_edit.setText(entry.buttonText())
overview_editquickwizard_carbs_edit.setText(entry.carbs().toString())
overview_editquickwizard_from_spinner.setSelection(posFrom)
overview_editquickwizard_to_spinner.setSelection(posTo)
overview_editquickwizard_usebg_spinner.setSelection(entry.useBG())
overview_editquickwizard_usecob_spinner.setSelection(entry.useCOB())
overview_editquickwizard_usebolusiob_spinner.setSelection(entry.useBolusIOB())
overview_editquickwizard_usebasaliob_spinner.setSelection(entry.useBasalIOB())
overview_editquickwizard_usetrend_spinner.setSelection(entry.useTrend())
overview_editquickwizard_usesuperbolus_spinner.setSelection(entry.useSuperBolus())
overview_editquickwizard_usetemptarget_spinner.setSelection(entry.useTempTarget())
overview_editquickwizard_usecob_spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) = processCob()
override fun onNothingSelected(parent: AdapterView<*>) {}
binding.from.setOnClickListener {
context?.let {
TimePickerDialog(it, fromTimeSetListener,
T.secs(fromSeconds.toLong()).hours().toInt(),
T.secs((fromSeconds % 3600).toLong()).mins().toInt(),
DateFormat.is24HourFormat(context)
).show()
}
}
fromSeconds = entry.validFrom()
from.text = dateUtil.timeString(DateUtil.toDate(fromSeconds))
val toTimeSetListener = TimePickerDialog.OnTimeSetListener { _, hour, minute ->
toSeconds = (T.hours(hour.toLong()).secs() + T.mins(minute.toLong()).secs()).toInt()
binding.from.text = dateUtil.timeString(DateUtil.toDate(toSeconds))
}
binding.to.setOnClickListener {
context?.let {
TimePickerDialog(it, toTimeSetListener,
T.secs(fromSeconds.toLong()).hours().toInt(),
T.secs((fromSeconds % 3600).toLong()).mins().toInt(),
DateFormat.is24HourFormat(context)
).show()
}
}
toSeconds = entry.validFrom()
binding.to.text = dateUtil.timeString(DateUtil.toDate(toSeconds))
binding.buttonEdit.setText(entry.buttonText())
binding.carbsEdit.setText(entry.carbs().toString())
binding.useBg.setSelection(entry.useBG())
binding.useCob.setSelection(entry.useCOB())
binding.useBolusIob.setSelection(entry.useBolusIOB())
binding.useBasalIob.setSelection(entry.useBasalIOB())
binding.useTrend.setSelection(entry.useTrend())
binding.useSuperBolus.setSelection(entry.useSuperBolus())
binding.useTempTarget.setSelection(entry.useTempTarget())
use_cob_yes.setOnClickListener(this)
use_cob_no.setOnClickListener(this)
processCob()
}
override fun onClick(v: View?) {
processCob()
}
@ -120,15 +147,20 @@ class EditQuickWizardDialog : DaggerDialogFragment() {
outState.putInt("position", position)
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
private fun processCob() {
if (overview_editquickwizard_usecob_spinner.selectedItemPosition == QuickWizardEntry.YES) {
overview_editquickwizard_usebolusiob_spinner.isEnabled = false
overview_editquickwizard_usebasaliob_spinner.isEnabled = false
overview_editquickwizard_usebolusiob_spinner.setSelection(QuickWizardEntry.YES)
overview_editquickwizard_usebasaliob_spinner.setSelection(QuickWizardEntry.YES)
if (binding.useCob.selectedItemPosition == QuickWizardEntry.YES) {
binding.useBolusIob.setEnableForChildren(false)
binding.useBasalIob.setEnableForChildren(false)
binding.useBolusIob.setSelection(QuickWizardEntry.YES)
binding.useBasalIob.setSelection(QuickWizardEntry.YES)
} else {
overview_editquickwizard_usebolusiob_spinner.isEnabled = true
overview_editquickwizard_usebasaliob_spinner.isEnabled = true
binding.useBolusIob.setEnableForChildren(true)
binding.useBasalIob.setEnableForChildren(true)
}
}
}

View file

@ -0,0 +1,16 @@
package info.nightscout.androidaps.utils.extensions
import android.widget.RadioGroup
import androidx.appcompat.widget.AppCompatRadioButton
import androidx.core.view.forEach
val RadioGroup.selectedItemPosition: Int
get() = this.indexOfChild(this.findViewById(this.checkedRadioButtonId))
fun RadioGroup.setSelection(index: Int) {
(this.getChildAt(index) as AppCompatRadioButton).isChecked = true
}
fun RadioGroup.setEnableForChildren(state : Boolean) {
forEach { child -> child.isEnabled = state}
}

View file

@ -1,67 +1,110 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".plugins.general.overview.dialogs.EditQuickWizardDialog">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/dialog_title_background"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/quickwizard"
app:srcCompat="@drawable/ic_quickwizard" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="@string/quickwizardsettings"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:labelFor="@+id/button_edit"
android:text="@string/overview_editquickwizard_buttontext"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<EditText
android:id="@+id/overview_editquickwizard_button_edit"
android:id="@+id/button_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="name"
android:ems="10"
android:inputType="text" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:labelFor="@+id/carbs_edit"
android:text="@string/overview_editquickwizard_carbs"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<EditText
android:id="@+id/overview_editquickwizard_carbs_edit"
android:id="@+id/carbs_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="number"
android:ems="10"
android:inputType="numberDecimal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/overview_editquickwizard_valid"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="@+id/overview_editquickwizard_from_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="@string/overview_editquickwizard_valid"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<Spinner
android:id="@+id/overview_editquickwizard_to_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/from"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_horizontal|center_vertical"
android:padding="10dp"
android:text="08:20pm"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="-"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<TextView
android:id="@+id/to"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_horizontal|center_vertical"
android:padding="10dp"
android:text="08:20pm"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</LinearLayout>
@ -72,11 +115,23 @@
android:text="@string/overview_editquickwizard_usebg"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<Spinner
android:id="@+id/overview_editquickwizard_usebg_spinner"
android:layout_width="wrap_content"
<RadioGroup
android:id="@+id/use_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/quickWizardYesNo"/>
android:text="@string/yes" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
@ -84,11 +139,23 @@
android:text="@string/overview_editquickwizard_usebolusiob"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<Spinner
android:id="@+id/overview_editquickwizard_usebolusiob_spinner"
android:layout_width="wrap_content"
<RadioGroup
android:id="@+id/use_bolus_iob"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/quickWizardYesNo"/>
android:text="@string/yes" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
@ -96,11 +163,33 @@
android:text="@string/overview_editquickwizard_usebasaliob"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<Spinner
android:id="@+id/overview_editquickwizard_usebasaliob_spinner"
android:layout_width="wrap_content"
<RadioGroup
android:id="@+id/use_basal_iob"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/quickWizardYesNoPosNeg"/>
android:text="@string/yes" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/positiveonly" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/negativeonly" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
@ -108,11 +197,26 @@
android:text="@string/overview_editquickwizard_usecob"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<Spinner
android:id="@+id/overview_editquickwizard_usecob_spinner"
android:layout_width="wrap_content"
<RadioGroup
android:id="@+id/use_cob"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp">
<RadioButton
android:id="@+id/use_cob_yes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/quickWizardYesNo"/>
android:text="@string/yes" />
<RadioButton
android:id="@+id/use_cob_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
@ -120,11 +224,33 @@
android:text="@string/overview_editquickwizard_usetrend"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<Spinner
android:id="@+id/overview_editquickwizard_usetrend_spinner"
android:layout_width="wrap_content"
<RadioGroup
android:id="@+id/use_trend"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/quickWizardYesNoPosNeg"/>
android:text="@string/yes" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/positiveonly" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/negativeonly" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
@ -132,11 +258,24 @@
android:text="@string/overview_editquickwizard_usesuperbolus"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<Spinner
android:id="@+id/overview_editquickwizard_usesuperbolus_spinner"
android:layout_width="wrap_content"
<RadioGroup
android:id="@+id/use_super_bolus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/quickWizardYesNo"/>
android:text="@string/yes" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
@ -144,16 +283,27 @@
android:text="@string/overview_editquickwizard_usetemptarget"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<Spinner
android:id="@+id/overview_editquickwizard_usetemptarget_spinner"
android:layout_width="wrap_content"
<RadioGroup
android:id="@+id/use_temp_target"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/quickWizardYesNo"/>
android:text="@string/yes" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no" />
</RadioGroup>
<include layout="@layout/okcancel" />
</LinearLayout>
</ScrollView>
</FrameLayout>
</ScrollView>

View file

@ -87,18 +87,6 @@
<item>@string/key_pregnant</item>
</string-array>
<string-array name="quickWizardYesNo">
<item>@string/yes</item>
<item>@string/no</item>
</string-array>
<string-array name="quickWizardYesNoPosNeg">
<item>@string/yes</item>
<item>@string/no</item>
<item>@string/positiveonly</item>
<item>@string/negativeonly</item>
</string-array>
<string-array name="location">
<item>@string/use_passive_location</item>
<item>@string/use_network_location</item>