|
|
|
|
@@ -247,7 +247,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
return ranges
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun showSleepPatternBarGraphSlotted(state: GraphState) {
|
|
|
|
|
fun showSleepPatternBarGraph(state: GraphState) {
|
|
|
|
|
val ranges = toSleepRanges(state.events)
|
|
|
|
|
val values = ArrayList<BarEntry>()
|
|
|
|
|
val stack = ArrayList(List(state.endSpan - state.startSpan + 1) { IntArray(24 * 60 * 60 / SLEEP_PATTERN_GRANULARITY) })
|
|
|
|
|
@@ -311,13 +311,11 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun mapColor(occurrences: Int, maxOccurrences: Int): Int {
|
|
|
|
|
//Log.d(TAG, "$occurrences <= $maxOccurrences")
|
|
|
|
|
// occurrences: number of reported sleeps in a specific time slice
|
|
|
|
|
// occurences: number of reported sleeps in a specific time slice
|
|
|
|
|
// maxOccurrences: maximum number of days with data that can contribute to maxOccurrences
|
|
|
|
|
assert(maxOccurrences > 0)
|
|
|
|
|
assert(occurrences <= maxOccurrences)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// map to color
|
|
|
|
|
val q = occurrences.toFloat() / maxOccurrences.toFloat()
|
|
|
|
|
val i = q * (SLEEP_PATTERN_COLORS.size - 1).toFloat()
|
|
|
|
|
@@ -330,7 +328,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
for ((index, dayArray) in stack.withIndex()) {
|
|
|
|
|
val daysWithData = state.dayCounter.countDaysWithData(spanToUnix(state.startSpan + index), spanToUnix(state.startSpan + index + 1))
|
|
|
|
|
|
|
|
|
|
//Log.d(TAG, "index: $index: daysWithData: $daysWithData, dayArray: ${dayArray.joinToString { it.toString() }}")
|
|
|
|
|
//Log.d(TAG, "index: $index: dayArray: ${dayArray.joinToString { it.toString() }}")
|
|
|
|
|
val vals = ArrayList<Float>()
|
|
|
|
|
|
|
|
|
|
var prevIndex = -1 // time slice index
|
|
|
|
|
@@ -341,7 +339,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
prevValue = v
|
|
|
|
|
} else if (prevValue != v) {
|
|
|
|
|
vals.add((i - prevIndex).toFloat())
|
|
|
|
|
allColors.add(mapColor(prevValue.coerceAtMost(daysWithData), daysWithData))
|
|
|
|
|
allColors.add(mapColor(prevValue, daysWithData))
|
|
|
|
|
prevIndex = i
|
|
|
|
|
prevValue = v
|
|
|
|
|
}
|
|
|
|
|
@@ -357,7 +355,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
values.add(BarEntry(index.toFloat(), vals.toFloatArray()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Log.d(TAG, "daysWithData: ${state.dayCounter.daysWithData.joinToString()}")
|
|
|
|
|
Log.d(TAG, "daysWithData: ${state.dayCounter.daysWithData.joinToString()}")
|
|
|
|
|
|
|
|
|
|
barChart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
|
|
|
|
|
override fun onValueSelected(e: Entry?, h: Highlight?) {
|
|
|
|
|
@@ -375,7 +373,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((lastToastShown + TOAST_FREQUENCY_MS) > System.currentTimeMillis()) {
|
|
|
|
|
if ((lastToastShown + 3500) > System.currentTimeMillis()) {
|
|
|
|
|
// only show one Toast message after another
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
@@ -443,8 +441,6 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
barChart.invalidate()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sleep pattern bars that do not use time slots.
|
|
|
|
|
// This is useful/nicer for bars that only represent data of a singlur days.
|
|
|
|
|
fun showSleepPatternBarGraphDaily(state: GraphState) {
|
|
|
|
|
val ranges = toSleepRanges(state.events)
|
|
|
|
|
val values = ArrayList(List(state.endSpan - state.startSpan + 1) { BarEntry(it.toFloat(), FloatArray(0)) })
|
|
|
|
|
@@ -527,7 +523,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((lastToastShown + TOAST_FREQUENCY_MS) > System.currentTimeMillis()) {
|
|
|
|
|
if ((lastToastShown + 3500) > System.currentTimeMillis()) {
|
|
|
|
|
// only show one Toast message after another
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
@@ -562,7 +558,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
fun showSleepBarGraph(state: GraphState) {
|
|
|
|
|
val ranges = toSleepRanges(state.events)
|
|
|
|
|
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)}")
|
|
|
|
|
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
|
|
|
|
|
@@ -573,13 +569,12 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
val endIndex = unixToSpan(endUnix)
|
|
|
|
|
var mid = startUnix
|
|
|
|
|
|
|
|
|
|
//Log.d(TAG, "beginIndex: $begIndex, endIndex: $endIndex, startUnix: ${Date(startUnix * 1000)} ($startUnix), endUnix: ${Date(endUnix * 1000)} ($endUnix)")
|
|
|
|
|
//Log.d(TAG, "startUnix: ${Date(startUnix * 1000)}, endUnix: ${Date(endUnix * 1000)}, begIndex: $begIndex, endIndex: $endIndex (index diff: ${endIndex - begIndex})")
|
|
|
|
|
for (i in begIndex..endIndex) {
|
|
|
|
|
// i is the days/weeks/months since unix epoch
|
|
|
|
|
val spanBegin = spanToUnix(i)
|
|
|
|
|
val spanEnd = spanToUnix(i + 1)
|
|
|
|
|
//Log.d(TAG, "i: $i, mid: ${Date(mid * 1000)}, spanBegin: ${Date(spanBegin * 1000)}, spanEnd: ${Date(spanEnd * 1000)}, beginUnix: ${Date(startUnix * 1000)} endUnix: ${Date(endUnix * 1000)}")
|
|
|
|
|
//Log.d(TAG, "mid: ${Date(mid * 1000)}, spanBegin: ${Date(spanBegin * 1000)}, spanEnd: ${Date(spanEnd * 1000)}, beginUnix: ${Date(startUnix * 1000)} endUnix: ${Date(endUnix * 1000)}")
|
|
|
|
|
val sleepBegin = max(mid, spanBegin)
|
|
|
|
|
val sleepEnd = min(endUnix, spanEnd)
|
|
|
|
|
val index = i - state.startSpan
|
|
|
|
|
@@ -734,10 +729,8 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
fun countDaysWithData(beginUnix: Long, endUnix: Long): Int {
|
|
|
|
|
val beginDays = unixToDays(beginUnix)
|
|
|
|
|
val endDays = unixToDays(endUnix)
|
|
|
|
|
//Log.d(TAG, "countDaysWithData: beginDays: $beginDays, endDays: $endDays, ${Date(beginUnix * 1000)} - ${Date(endUnix * 1000)}")
|
|
|
|
|
var count = 0
|
|
|
|
|
for (i in (beginDays - startDays)..<(endDays - startDays)) {
|
|
|
|
|
//Log.d(TAG, "countDaysWithData: i: $i, size: ${daysWithData.size}")
|
|
|
|
|
count += if (daysWithData[i]) { 1 } else { 0 }
|
|
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
@@ -757,7 +750,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
data class GraphState(val events: List<LunaEvent>, val dayCounter: DayCounter, val startUnix: Long, val endUnix: Long, val startSpan: Int, val endSpan: Int)
|
|
|
|
|
|
|
|
|
|
// wrapper for comon graph setup
|
|
|
|
|
fun prepareGraph(type: LunaEvent.Type, callback: (GraphState) -> Unit) {
|
|
|
|
|
fun prepareGraph(type: LunaEvent.Type, cb: (GraphState) -> Unit) {
|
|
|
|
|
val events = MainActivity.allEvents.filter { it.type == type }.sortedBy { it.time }
|
|
|
|
|
|
|
|
|
|
if (events.isEmpty()) {
|
|
|
|
|
@@ -802,27 +795,20 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Log.d(TAG, "startDaysUnix: ${Date(daysToUnix(startDays) * 1000)}. endDaysUnix: ${Date(daysToUnix(endDays) * 1000)}")
|
|
|
|
|
Log.d(TAG, "startDaysUnix: ${Date(daysToUnix(startDays) * 1000)}. endDaysUnix: ${Date(daysToUnix(endDays) * 1000)}")
|
|
|
|
|
|
|
|
|
|
callback(GraphState(events, dayCounter, startUnix, endUnix, startSpan, endSpan))
|
|
|
|
|
cb(GraphState(events, dayCounter, startUnix, endUnix, startSpan, endSpan))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun showGraph() {
|
|
|
|
|
//Log.d(TAG, "showGraph: graphTypeSelection: $graphTypeSelection, timeRangeSelection: $timeRangeSelection")
|
|
|
|
|
Log.d(TAG, "showGraph: graphTypeSelection: $graphTypeSelection, timeRangeSelection: $timeRangeSelection")
|
|
|
|
|
|
|
|
|
|
when (graphTypeSelection) {
|
|
|
|
|
GraphType.BOTTLE_EVENTS,
|
|
|
|
|
GraphType.BOTTLE_SUM -> prepareGraph(LunaEvent.Type.BABY_BOTTLE) { state -> showBottleBarGraph(state) }
|
|
|
|
|
GraphType.SLEEP_EVENTS,
|
|
|
|
|
GraphType.SLEEP_SUM -> prepareGraph(LunaEvent.Type.SLEEP) { state -> showSleepBarGraph(state) }
|
|
|
|
|
GraphType.SLEEP_PATTERN -> prepareGraph(LunaEvent.Type.SLEEP) { state ->
|
|
|
|
|
if (timeRangeSelection == TimeRange.DAY) {
|
|
|
|
|
// specialized pattern bar for daily view
|
|
|
|
|
showSleepPatternBarGraphDaily(state)
|
|
|
|
|
} else {
|
|
|
|
|
showSleepPatternBarGraphSlotted(state)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
GraphType.SLEEP_PATTERN -> prepareGraph(LunaEvent.Type.SLEEP) { state -> showSleepPatternBarGraph(state) }
|
|
|
|
|
GraphType.MEDICINE_EVENTS -> prepareGraph(LunaEvent.Type.MEDICINE) { state -> showMedicineBarGraph(state) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -872,10 +858,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
const val TAG = "StatisticsActivity"
|
|
|
|
|
|
|
|
|
|
// 15 min steps
|
|
|
|
|
const val SLEEP_PATTERN_GRANULARITY = 15 * 60
|
|
|
|
|
|
|
|
|
|
// Time between toast messages (to prevent jams)
|
|
|
|
|
const val TOAST_FREQUENCY_MS = 3500
|
|
|
|
|
val SLEEP_PATTERN_GRANULARITY = 15 * 60
|
|
|
|
|
|
|
|
|
|
// color gradient
|
|
|
|
|
val SLEEP_PATTERN_COLORS = arrayOf(
|
|
|
|
|
@@ -884,22 +867,20 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
"#77B1BF".toColorInt(), "#66A7B7".toColorInt(), "#559DAF".toColorInt(), "#4493A7".toColorInt(),
|
|
|
|
|
"#33899F".toColorInt(), "#228097".toColorInt(), "#11768F".toColorInt(), "#006C87".toColorInt()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
private val dateTime = Calendar.getInstance() // scratch pad
|
|
|
|
|
var graphTypeSelection = GraphType.SLEEP_SUM
|
|
|
|
|
var timeRangeSelection = TimeRange.DAY
|
|
|
|
|
|
|
|
|
|
private val dateTime = Calendar.getInstance() // scratch pad
|
|
|
|
|
|
|
|
|
|
// convert month to seconds since epoch
|
|
|
|
|
fun unixToMonths(seconds: Long): Int {
|
|
|
|
|
//val dateTime = Calendar.getInstance()
|
|
|
|
|
dateTime.time = Date(seconds * 1000)
|
|
|
|
|
val years = dateTime.get(Calendar.YEAR)
|
|
|
|
|
val months = dateTime.get(Calendar.MONTH)
|
|
|
|
|
return 12 * years + months
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convert month to seconds since epoch
|
|
|
|
|
fun monthsToUnix(months: Int): Long {
|
|
|
|
|
//val dateTime = Calendar.getInstance()
|
|
|
|
|
dateTime.time = Date(0)
|
|
|
|
|
dateTime.set(Calendar.YEAR, months / 12)
|
|
|
|
|
dateTime.set(Calendar.MONTH, months % 12)
|
|
|
|
|
@@ -909,24 +890,18 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
return dateTime.time.time / 1000
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convert seconds to weeks since epoch
|
|
|
|
|
fun unixToWeeks(seconds: Long): Int {
|
|
|
|
|
//val dateTime = Calendar.getInstance()
|
|
|
|
|
dateTime.time = Date(seconds * 1000)
|
|
|
|
|
val years = dateTime.get(Calendar.YEAR) - 1970
|
|
|
|
|
val years = dateTime.get(Calendar.YEAR)
|
|
|
|
|
val weeks = dateTime.get(Calendar.WEEK_OF_YEAR)
|
|
|
|
|
val month = dateTime.get(Calendar.MONTH)
|
|
|
|
|
// dirty hack to get monotone number of weeks
|
|
|
|
|
if (month == 11 && weeks == 1) {
|
|
|
|
|
return 52 * (years + 1) + weeks
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 52 * years + weeks
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convert weeks to seconds since epoch
|
|
|
|
|
fun weeksToUnix(weeks: Int): Long {
|
|
|
|
|
//val dateTime = Calendar.getInstance()
|
|
|
|
|
dateTime.time = Date(0)
|
|
|
|
|
dateTime.set(Calendar.YEAR, 1970 + weeks / 52)
|
|
|
|
|
dateTime.set(Calendar.YEAR, weeks / 52)
|
|
|
|
|
dateTime.set(Calendar.WEEK_OF_YEAR, weeks % 52)
|
|
|
|
|
dateTime.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
|
|
|
|
|
dateTime.set(Calendar.HOUR, 0)
|
|
|
|
|
@@ -935,16 +910,17 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
return dateTime.time.time / 1000
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convert seconds to days since epoch
|
|
|
|
|
fun unixToDays(seconds: Long): Int {
|
|
|
|
|
//val dateTime = Calendar.getInstance()
|
|
|
|
|
dateTime.time = Date(seconds * 1000)
|
|
|
|
|
val years = dateTime.get(Calendar.YEAR)
|
|
|
|
|
val days = dateTime.get(Calendar.DAY_OF_YEAR)
|
|
|
|
|
return 365 * years + days
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convert days to seconds since epoch
|
|
|
|
|
// convert from days to Date
|
|
|
|
|
fun daysToUnix(days: Int): Long {
|
|
|
|
|
//val dateTime = Calendar.getInstance()
|
|
|
|
|
dateTime.time = Date(0)
|
|
|
|
|
dateTime.set(Calendar.YEAR, days / 365)
|
|
|
|
|
dateTime.set(Calendar.DAY_OF_YEAR, days % 365)
|
|
|
|
|
@@ -968,6 +944,42 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
return newArray
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
fun colorGradient(fromColor: Int, toColor: Int, percent: Int): Int {
|
|
|
|
|
assert(percent in 0..100)
|
|
|
|
|
|
|
|
|
|
val a1 = fromColor.shr(24).and(0xff)
|
|
|
|
|
val r1 = fromColor.shr(16).and(0xff)
|
|
|
|
|
val g1 = fromColor.shr(8).and(0xff)
|
|
|
|
|
val b1 = fromColor.shr(0).and(0xff)
|
|
|
|
|
|
|
|
|
|
//Log.d(TAG, "${a1.toHexString()} ${r1.toHexString()} ${g1.toHexString()} ${b1.toHexString()}")
|
|
|
|
|
|
|
|
|
|
val a2 = toColor.shr(24).and(0xff)
|
|
|
|
|
val r2 = toColor.shr(16).and(0xff)
|
|
|
|
|
val g2 = toColor.shr(8).and(0xff)
|
|
|
|
|
val b2 = toColor.shr(0).and(0xff)
|
|
|
|
|
|
|
|
|
|
//Log.d(TAG, "${a2.toHexString()} ${r2.toHexString()} ${g2.toHexString()} ${b2.toHexString()}")
|
|
|
|
|
|
|
|
|
|
val pc = (percent.toFloat() / 100F).coerceIn(0F, 1F)
|
|
|
|
|
val a = a1 + (pc * abs(a2 - a1)).toInt()
|
|
|
|
|
val r = r1 + (pc * abs(r2 - r1)).toInt()
|
|
|
|
|
val g = g1 + (pc * abs(g2 - g1)).toInt()
|
|
|
|
|
val b = a1 + (pc * abs(b2 - b1)).toInt()
|
|
|
|
|
|
|
|
|
|
//Log.d(TAG, "${a.toHexString()} ${r.toHexString()} ${g.toHexString()} ${b.toHexString()}")
|
|
|
|
|
val Red = r.shl(16).and(0x00FF0000)
|
|
|
|
|
val Green = g.shl(8).and(0x0000FF00)
|
|
|
|
|
val Blue = b.and(0x000000FF)
|
|
|
|
|
val aa = a.shl(24).and(0xFF000000.toInt())
|
|
|
|
|
val color = aa.or(Red).or(Green).or(Blue)
|
|
|
|
|
return color
|
|
|
|
|
//Log.d(TAG, "c: ${c.toHexString()} ${color.toInt().toHexString()}")
|
|
|
|
|
//return Color.argb(a, r, g, b)
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// for debugging
|
|
|
|
|
fun debugBarValues(values: ArrayList<BarEntry>) {
|
|
|
|
|
for (value in values) {
|
|
|
|
|
|