Commit 2639a0fb authored by Joey's avatar Joey

glucose: refactor insulin selection logic for the add dialog

Signed-off-by: Joey's avatarJoey <bevilacquajoey@gmail.com>
Change-Id: I6b79390e37d09e034ed92f6fa1a234f3e7f0ea1f
parent 92d30e0c
......@@ -19,10 +19,10 @@ import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.button.MaterialButton
import it.diab.data.entities.Glucose
import it.diab.data.entities.Insulin
import it.diab.data.extensions.asTimeFrame
import it.diab.ui.util.UIUtils
import it.diab.glucose.R
import it.diab.glucose.util.InsulinSelector
import it.diab.glucose.util.VibrationUtil
import it.diab.ui.util.UIUtils
class AddInsulinDialog(
private val activity: Activity,
......@@ -53,26 +53,14 @@ class AddInsulinDialog(
fun setInsulins(list: List<Insulin>) {
insulins = list.toTypedArray()
val names = Array(list.size) { "" }
val now = glucose.date.asTimeFrame()
var spinnerPosition = -1
val currentId = if (isBasal) glucose.insulinId1 else glucose.insulinId0
val currentValue = if (isBasal) glucose.insulinValue1 else glucose.insulinValue0
if (currentValue != 0f) {
valueEditText.setText(currentValue.toString())
}
for (i in insulins.indices) {
names[i] = "${insulins[i].name} (${activity.getString(insulins[i].timeFrame.string)})"
if (spinnerPosition == -1 && (insulins[i].uid == currentId ||
insulins[i].timeFrame === now)
) {
spinnerPosition = i
}
val names = Array(list.size) { i ->
"${insulins[i].name} (${activity.getString(insulins[i].timeFrame.string)})"
}
nameSpinner.adapter = ArrayAdapter<String>(
......@@ -80,6 +68,14 @@ class AddInsulinDialog(
androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, names
)
val spinnerPosition = InsulinSelector(glucose.timeFrame).run {
if (isBasal) {
suggestBasal(insulins, glucose.insulinId1)
} else {
suggestInsulin(insulins, glucose.insulinId0)
}
}
nameSpinner.setSelection(if (spinnerPosition == -1) 0 else spinnerPosition)
}
......
......@@ -240,7 +240,7 @@ class EditorActivity : AppCompatActivity() {
private fun onInsulinClick(isBasal: Boolean) {
AddInsulinDialog(this, viewModel.glucose, isBasal).apply {
setInsulins(viewModel.insulins.filter { it.isBasal == isBasal })
setInsulins(viewModel.insulins)
}.show(
{ insulin, value -> onInsulinApply(insulin, value, isBasal) },
{ onInsulinApply(insulin { uid = -1 }, 0f, isBasal) }
......
/*
* Copyright (c) 2019 Bevilacqua Joey
*
* Licensed under the GNU GPLv3 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.glucose.util
import it.diab.data.entities.Insulin
import it.diab.data.entities.TimeFrame
class InsulinSelector(private val timeFrame: TimeFrame) {
fun suggestInsulin(list: Array<Insulin>, currentId: Long) =
suggest(list, currentId, false)
fun suggestBasal(list: Array<Insulin>, currentId: Long) =
suggest(list, currentId, true)
private fun suggest(list: Array<Insulin>, currentId: Long, isBasal: Boolean): Int {
var suggested = -1
list.forEachIndexed { i, insulin ->
if (insulin.isBasal == isBasal) {
if (suggested == -1 || insulin.uid == currentId || insulin.timeFrame == timeFrame) {
suggested = i
}
}
}
return suggested
}
}
\ No newline at end of file
/*
* Copyright (c) 2019 Bevilacqua Joey
*
* Licensed under the GNU GPLv3 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.glucose.util
import it.diab.data.entities.TimeFrame
import it.diab.data.extensions.insulin
import org.junit.Assert.assertEquals
import org.junit.Test
class InsulinSelectorTest {
@Test
fun suggestInsulin() {
val list = arrayOf(
insulin {
timeFrame = TimeFrame.NIGHT
isBasal = true
},
insulin {
timeFrame = TimeFrame.DINNER
},
insulin {
uid = 2
timeFrame = TimeFrame.MORNING
}
)
assertEquals(1, InsulinSelector(TimeFrame.DINNER).suggestInsulin(list, -1))
assertEquals(2, InsulinSelector(TimeFrame.MORNING).suggestInsulin(list, 2))
assertEquals(1, InsulinSelector(TimeFrame.NIGHT).suggestInsulin(list, -1))
}
@Test
fun suggestBasal() {
val list = arrayOf(
insulin {
timeFrame = TimeFrame.NIGHT
isBasal = false
},
insulin {
timeFrame = TimeFrame.DINNER
isBasal = true
}
)
assertEquals(1, InsulinSelector(TimeFrame.DINNER).suggestBasal(list, -1))
assertEquals(0, InsulinSelector(TimeFrame.NIGHT).suggestInsulin(list, -1))
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment