|
|
|
|
@@ -82,7 +82,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
barChart.axisRight.setDrawGridLines(false)
|
|
|
|
|
barChart.axisRight.setDrawLabels(false)
|
|
|
|
|
|
|
|
|
|
//barChart.xAxis.setDrawGridLines(true)
|
|
|
|
|
barChart.xAxis.setDrawGridLines(true)
|
|
|
|
|
barChart.xAxis.setDrawLabels(true)
|
|
|
|
|
barChart.xAxis.setDrawAxisLine(false)
|
|
|
|
|
|
|
|
|
|
@@ -98,7 +98,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
//Log.d("event", "new value: $newValue")
|
|
|
|
|
newValue ?: return
|
|
|
|
|
graphTypeSelection = GraphType.valueOf(newValue)
|
|
|
|
|
showGraph()
|
|
|
|
|
updateGraph()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
@@ -113,13 +113,13 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
newValue ?: return
|
|
|
|
|
timeRangeSelection = TimeRange.valueOf(newValue)
|
|
|
|
|
setSpans()
|
|
|
|
|
showGraph()
|
|
|
|
|
updateGraph()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
setSpans()
|
|
|
|
|
showGraph()
|
|
|
|
|
updateGraph()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun setSpans() {
|
|
|
|
|
@@ -249,7 +249,10 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
|
|
|
|
|
fun showSleepPatternBarGraph(state: GraphState) {
|
|
|
|
|
val ranges = toSleepRanges(state.events)
|
|
|
|
|
Log.d(TAG, "startUnix: ${Date(state.startUnix * 1000)}, endUnix: ${Date(state.endUnix * 1000)}")
|
|
|
|
|
|
|
|
|
|
val values = ArrayList<BarEntry>()
|
|
|
|
|
|
|
|
|
|
val stack = ArrayList(List(state.endSpan - state.startSpan + 1) { IntArray(24 * 60 * 60 / SLEEP_PATTERN_GRANULARITY) })
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "stack.size: ${stack.size}, array.size: ${stack[0].size}, dayCounter.daysWithData.size: ${state.dayCounter.daysWithData.size}")
|
|
|
|
|
@@ -269,8 +272,11 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
val sleepEnd = min(end, dayEnd)
|
|
|
|
|
|
|
|
|
|
if (sleepBegin != sleepEnd) {
|
|
|
|
|
//val index2 = i - spanBegin
|
|
|
|
|
//val duration = dayEnd - dayBegin
|
|
|
|
|
assert(dayBegin <= dayEnd)
|
|
|
|
|
assert(sleepBegin <= sleepEnd)
|
|
|
|
|
//val duration = sleepEnd - sleepBegin
|
|
|
|
|
val iBegin = (sleepBegin - dayBegin) / SLEEP_PATTERN_GRANULARITY
|
|
|
|
|
val iEnd = iBegin + (sleepEnd - sleepBegin) / SLEEP_PATTERN_GRANULARITY
|
|
|
|
|
Log.d(TAG, "index: $index, iBegin: $iBegin, iEnd: $iEnd, dayBegin: ${Date(dayBegin * 1000)}, dayEnd: ${Date(dayEnd * 1000)}, sleepBegin: ${Date(sleepBegin * 1000)}, sleepEnd: ${Date(sleepEnd * 1000)}")
|
|
|
|
|
@@ -359,66 +365,40 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
|
|
|
|
|
barChart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
|
|
|
|
|
override fun onValueSelected(e: Entry?, h: Highlight?) {
|
|
|
|
|
if (e == null || h == null) {
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if (e != null && h != null && e.x.toInt() != -1 && h.stackIndex != -1) {
|
|
|
|
|
if ((lastToastShown + 3500) > System.currentTimeMillis()) {
|
|
|
|
|
// only show one Toast message after another
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
val index = e.x.toInt()
|
|
|
|
|
val value = values[index]
|
|
|
|
|
val dayStartUnix = daysToUnix(unixToDays(state.startUnix) + e.x.toInt())
|
|
|
|
|
|
|
|
|
|
//Log.d(TAG, "startUnix: ${Date(startUnix * 1000)}, x: ${e.x.toInt()}, dayStartUnix: ${Date(dayStartUnix * 1000)}")
|
|
|
|
|
val startSeconds = SLEEP_PATTERN_GRANULARITY * value.yVals.sliceArray(0..<h.stackIndex).fold(0) { acc, y -> acc + y.toInt() }
|
|
|
|
|
val durationSeconds = SLEEP_PATTERN_GRANULARITY * value.yVals[h.stackIndex].toInt()
|
|
|
|
|
val endSeconds = startSeconds + durationSeconds
|
|
|
|
|
|
|
|
|
|
val format = SimpleDateFormat("HH:mm", Locale.getDefault())
|
|
|
|
|
val startTimeString = format.format((dayStartUnix + startSeconds) * 1000).toString()
|
|
|
|
|
val endTimeString = format.format((dayStartUnix + endSeconds) * 1000).toString()
|
|
|
|
|
val durationString = NumericUtils(applicationContext).formatEventQuantity(LunaEvent.Type.SLEEP, durationSeconds)
|
|
|
|
|
|
|
|
|
|
val daysWithData = stack[e.x.toInt()][startSeconds / SLEEP_PATTERN_GRANULARITY]
|
|
|
|
|
val daysWithDataMax = state.dayCounter.countDaysWithData(spanToUnix(state.startSpan + index), spanToUnix(state.startSpan + index + 1))
|
|
|
|
|
|
|
|
|
|
// percentage of days in this span where baby is asleep in this time slot
|
|
|
|
|
val pc = if (daysWithDataMax > 0) {
|
|
|
|
|
(100F * daysWithData.toFloat() / daysWithDataMax.toFloat()).toInt()
|
|
|
|
|
} else {
|
|
|
|
|
// no data for this day
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Toast.makeText(applicationContext, "$startTimeString - $endTimeString ($durationString) - ${pc}%", Toast.LENGTH_LONG).show()
|
|
|
|
|
lastToastShown = System.currentTimeMillis()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val index = e.x.toInt()
|
|
|
|
|
if (index !in 0..values.size) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val value = values[index]
|
|
|
|
|
if (value.yVals == null || h.stackIndex !in 0..value.yVals.size) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((lastToastShown + 3500) > System.currentTimeMillis()) {
|
|
|
|
|
// only show one Toast message after another
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val dayStartUnix = daysToUnix(unixToDays(state.startUnix) + index)
|
|
|
|
|
|
|
|
|
|
//Log.d(TAG, "startUnix: ${Date(startUnix * 1000)}, x: ${e.x.toInt()}, dayStartUnix: ${Date(dayStartUnix * 1000)}")
|
|
|
|
|
val startSeconds =
|
|
|
|
|
SLEEP_PATTERN_GRANULARITY * value.yVals.sliceArray(0..<h.stackIndex)
|
|
|
|
|
.fold(0) { acc, y -> acc + y.toInt() }
|
|
|
|
|
val durationSeconds =
|
|
|
|
|
SLEEP_PATTERN_GRANULARITY * value.yVals[h.stackIndex].toInt()
|
|
|
|
|
val endSeconds = startSeconds + durationSeconds
|
|
|
|
|
|
|
|
|
|
val format = SimpleDateFormat("HH:mm", Locale.getDefault())
|
|
|
|
|
val startTimeString =
|
|
|
|
|
format.format((dayStartUnix + startSeconds) * 1000).toString()
|
|
|
|
|
val endTimeString =
|
|
|
|
|
format.format((dayStartUnix + endSeconds) * 1000).toString()
|
|
|
|
|
val durationString = NumericUtils(applicationContext).formatEventQuantity(
|
|
|
|
|
LunaEvent.Type.SLEEP,
|
|
|
|
|
durationSeconds
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
val daysWithData =
|
|
|
|
|
stack[e.x.toInt()][startSeconds / SLEEP_PATTERN_GRANULARITY]
|
|
|
|
|
val daysWithDataMax = state.dayCounter.countDaysWithData(
|
|
|
|
|
spanToUnix(state.startSpan + index),
|
|
|
|
|
spanToUnix(state.startSpan + index + 1)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// percentage of days in this span where baby is asleep in this time slot
|
|
|
|
|
val pc = if (daysWithDataMax > 0) {
|
|
|
|
|
(100F * daysWithData.toFloat() / daysWithDataMax.toFloat()).toInt()
|
|
|
|
|
} else {
|
|
|
|
|
// no data for this day
|
|
|
|
|
0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Toast.makeText(
|
|
|
|
|
applicationContext,
|
|
|
|
|
"$startTimeString - $endTimeString ($durationString) - ${pc}%",
|
|
|
|
|
Toast.LENGTH_LONG
|
|
|
|
|
).show()
|
|
|
|
|
lastToastShown = System.currentTimeMillis()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onNothingSelected() {}
|
|
|
|
|
@@ -427,6 +407,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
val set1 = BarDataSet(values, "")
|
|
|
|
|
val data = BarData(set1)
|
|
|
|
|
set1.colors = allColors
|
|
|
|
|
//set1.colors = arrayListOf("#00000000".toColorInt(), "#72d7f5".toColorInt())
|
|
|
|
|
|
|
|
|
|
set1.setDrawValues(false) // usually too many values
|
|
|
|
|
set1.isHighlightEnabled = true
|
|
|
|
|
@@ -443,6 +424,9 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
|
|
|
|
|
fun showSleepPatternBarGraphDaily(state: GraphState) {
|
|
|
|
|
val ranges = toSleepRanges(state.events)
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "startUnix: ${Date(state.startUnix * 1000)}, endUnix: ${Date(state.endUnix * 1000)}")
|
|
|
|
|
|
|
|
|
|
val values = ArrayList(List(state.endSpan - state.startSpan + 1) { BarEntry(it.toFloat(), FloatArray(0)) })
|
|
|
|
|
|
|
|
|
|
// stack awake/sleep durations
|
|
|
|
|
@@ -509,37 +493,26 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
|
|
|
|
|
barChart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
|
|
|
|
|
override fun onValueSelected(e: Entry?, h: Highlight?) {
|
|
|
|
|
if (e == null || h == null) {
|
|
|
|
|
return
|
|
|
|
|
if (e != null && h != null && e.x.toInt() != -1 && h.stackIndex != -1) {
|
|
|
|
|
if ((lastToastShown + 3500) > System.currentTimeMillis()) {
|
|
|
|
|
// only show one Toast message after another
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val value = values[e.x.toInt()]
|
|
|
|
|
val duration = value.yVals[h.stackIndex].toInt()
|
|
|
|
|
val durationString = NumericUtils(applicationContext).formatEventQuantity(LunaEvent.Type.SLEEP, duration)
|
|
|
|
|
|
|
|
|
|
val offsetUnix = spanToUnix(state.startSpan + e.x.toInt()) // start of the time span (day/week/month)
|
|
|
|
|
val startUnix = offsetUnix + value.yVals.sliceArray(0..<h.stackIndex).fold(0) { acc, y -> acc + y.toInt() }
|
|
|
|
|
val endUnix = startUnix + duration
|
|
|
|
|
|
|
|
|
|
val format = SimpleDateFormat("HH:mm", Locale.getDefault())
|
|
|
|
|
val startTimeString = format.format(startUnix * 1000).toString()
|
|
|
|
|
val endTimeString = format.format(endUnix * 1000).toString()
|
|
|
|
|
Toast.makeText(applicationContext, "$startTimeString - $endTimeString ($durationString)", Toast.LENGTH_LONG).show()
|
|
|
|
|
lastToastShown = System.currentTimeMillis()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val index = e.x.toInt()
|
|
|
|
|
if (index !in 0..values.size) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val value = values[index]
|
|
|
|
|
if (value.yVals == null || h.stackIndex !in 0..value.yVals.size) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((lastToastShown + 3500) > System.currentTimeMillis()) {
|
|
|
|
|
// only show one Toast message after another
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val duration = value.yVals[h.stackIndex].toInt()
|
|
|
|
|
val durationString = NumericUtils(applicationContext).formatEventQuantity(LunaEvent.Type.SLEEP, duration)
|
|
|
|
|
|
|
|
|
|
val offsetUnix = spanToUnix(state.startSpan + e.x.toInt()) // start of the time span (day/week/month)
|
|
|
|
|
val startUnix = offsetUnix + value.yVals.sliceArray(0..<h.stackIndex).fold(0) { acc, y -> acc + y.toInt() }
|
|
|
|
|
val endUnix = startUnix + duration
|
|
|
|
|
|
|
|
|
|
val format = SimpleDateFormat("HH:mm", Locale.getDefault())
|
|
|
|
|
val startTimeString = format.format(startUnix * 1000).toString()
|
|
|
|
|
val endTimeString = format.format(endUnix * 1000).toString()
|
|
|
|
|
Toast.makeText(applicationContext, "$startTimeString - $endTimeString ($durationString)", Toast.LENGTH_LONG).show()
|
|
|
|
|
lastToastShown = System.currentTimeMillis()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onNothingSelected() {}
|
|
|
|
|
@@ -560,6 +533,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
val values = ArrayList(List(state.endSpan - state.startSpan + 1) { BarEntry(it.toFloat(), 0F) })
|
|
|
|
|
Log.d(TAG, "startUnix: ${Date(state.startUnix * 1000)}, endUnix: ${Date(state.endUnix * 1000)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (range in ranges) {
|
|
|
|
|
// a sleep event can span to another day
|
|
|
|
|
// distribute sleep time over the days
|
|
|
|
|
@@ -679,8 +653,21 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
set1.setDrawValues(true)
|
|
|
|
|
set1.isHighlightEnabled = false
|
|
|
|
|
|
|
|
|
|
//barChart.axisLeft.isSLEEP_PATTERN_GRANULARITYEnabled = true
|
|
|
|
|
//barChart.axisLeft.setSLEEP_PATTERN_GRANULARITY(0.8F)
|
|
|
|
|
val maximumRange = 20F
|
|
|
|
|
if (graphTypeSelection == GraphType.BOTTLE_SUM || graphTypeSelection == GraphType.BOTTLE_EVENTS) {
|
|
|
|
|
//val count = values.size.coerceIn(5, 20)
|
|
|
|
|
barChart.setVisibleXRangeMaximum(maximumRange) // show max 24 entries
|
|
|
|
|
barChart.xAxis.setLabelCount(maximumRange.toInt(), false)
|
|
|
|
|
//barChart.xAxis.isEnabled = false
|
|
|
|
|
barChart.xAxis.setCenterAxisLabels(true)
|
|
|
|
|
barChart.setScaleEnabled(false)
|
|
|
|
|
|
|
|
|
|
//barChart.axisLeft.isSLEEP_PATTERN_GRANULARITYEnabled = true
|
|
|
|
|
//barChart.axisLeft.setSLEEP_PATTERN_GRANULARITY(0.8F)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//val dataSets = ArrayList<IBarDataSet?>()
|
|
|
|
|
//dataSets.add(set1)
|
|
|
|
|
|
|
|
|
|
val data = BarData(set1)
|
|
|
|
|
//data.barWidth = 0.3F // 0.85 default // ratio of barWidth to totalWidth.
|
|
|
|
|
@@ -695,6 +682,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
val prefix = if (timeRangeSelection == TimeRange.DAY) { "" } else { "⌀ " }
|
|
|
|
|
return prefix + NumericUtils(applicationContext).formatEventQuantity(LunaEvent.Type.BABY_BOTTLE, value.toInt())
|
|
|
|
|
}
|
|
|
|
|
//GraphType.BOTTLE_SUM_AVERAGE -> "⌀ " + NumericUtils(applicationContext).formatEventQuantity(LunaEvent.TYPE_BABY_BOTTLE, value.toInt())
|
|
|
|
|
else -> {
|
|
|
|
|
Log.e(TAG, "unhandled graphTypeSelection")
|
|
|
|
|
value.toInt().toString()
|
|
|
|
|
@@ -704,20 +692,11 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// hm, does not work yet
|
|
|
|
|
Log.d(TAG, "last value: ${values.lastOrNull()!!.x}")
|
|
|
|
|
barChart.moveViewToX(100F)
|
|
|
|
|
|
|
|
|
|
data.setValueTextSize(12f)
|
|
|
|
|
barChart.setData(data)
|
|
|
|
|
|
|
|
|
|
barChart.moveViewToX(values.lastOrNull()!!.x)
|
|
|
|
|
|
|
|
|
|
val maximumRange = 16F
|
|
|
|
|
//val count = values.size.coerceIn(5, 20)
|
|
|
|
|
barChart.setVisibleXRangeMaximum(maximumRange) // show max 24 entries
|
|
|
|
|
barChart.xAxis.setLabelCount(maximumRange.toInt(), true)
|
|
|
|
|
//barChart.xAxis.isEnabled = false
|
|
|
|
|
barChart.xAxis.setCenterAxisLabels(true)
|
|
|
|
|
barChart.setScaleEnabled(false)
|
|
|
|
|
|
|
|
|
|
barChart.invalidate()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -800,8 +779,8 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
cb(GraphState(events, dayCounter, startUnix, endUnix, startSpan, endSpan))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun showGraph() {
|
|
|
|
|
Log.d(TAG, "showGraph: graphTypeSelection: $graphTypeSelection, timeRangeSelection: $timeRangeSelection")
|
|
|
|
|
fun updateGraph() {
|
|
|
|
|
Log.d(TAG, "updateGraph: graphTypeSelection: $graphTypeSelection, timeRangeSelection: $timeRangeSelection")
|
|
|
|
|
|
|
|
|
|
when (graphTypeSelection) {
|
|
|
|
|
GraphType.BOTTLE_EVENTS,
|
|
|
|
|
|