AndroidAPS/app/src/main/java/info/nightscout/androidaps/utils/TIR.kt

27 lines
1.1 KiB
Kotlin
Raw Normal View History

2019-10-08 21:20:04 +02:00
package info.nightscout.androidaps.utils
2019-12-08 21:55:49 +01:00
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import kotlin.math.roundToInt
2019-10-22 13:24:35 +02:00
class TIR(val date: Long, val lowThreshold: Double, val highThreshold: Double) {
2019-12-08 21:55:49 +01:00
internal var below = 0
internal var inRange = 0
internal var above = 0
internal var error = 0
internal var count = 0
2019-10-08 21:20:04 +02:00
2019-10-22 13:24:35 +02:00
fun error() = run { error++ }
fun below() = run { below++; count++ }
fun inRange() = run { inRange++; count++ }
fun above() = run { above++; count++ }
2019-12-08 21:55:49 +01:00
fun belowPct() = if (count > 0) (below.toDouble() / count * 100.0).roundToInt() else 0
fun inRangePct() = if (count > 0) (inRange.toDouble() / count * 100.0).roundToInt() else 0
fun abovePct() = if (count > 0) (above.toDouble() / count * 100.0).roundToInt() else 0
2019-12-09 19:03:26 +01:00
fun toText(): String = MainApp.gs(R.string.tirformat, DateUtil.dateStringShort(date), belowPct(), inRangePct(), abovePct())
fun toText(days: Int): String = MainApp.gs(R.string.tirformat, "%02d".format(days) + " " + MainApp.gs(R.string.days), belowPct(), inRangePct(), abovePct())
2019-10-08 21:20:04 +02:00
}