forked from penguin86/luna-tracker
Compare commits
19 Commits
e820c11e68
...
remove_sta
| Author | SHA1 | Date | |
|---|---|---|---|
| a8305b8fc9 | |||
| 43f0519487 | |||
| 1bcfc18a71 | |||
| e345e98668 | |||
| 09f5a4e27b | |||
| b3b432525e | |||
| d832aa4330 | |||
| 672ae37049 | |||
| 672e58c028 | |||
| d2729af30f | |||
| 85567cce77 | |||
| 80a51ea8ef | |||
| 28679a4a66 | |||
| f73d3562a9 | |||
| 8a2932b1e7 | |||
| 2af8989777 | |||
| b417fe48a6 | |||
| a887d9f29f | |||
| 44748506ff |
@@ -65,5 +65,4 @@ dependencies {
|
|||||||
androidTestImplementation(libs.androidx.ui.test.junit4)
|
androidTestImplementation(libs.androidx.ui.test.junit4)
|
||||||
debugImplementation(libs.androidx.ui.tooling)
|
debugImplementation(libs.androidx.ui.tooling)
|
||||||
debugImplementation(libs.androidx.ui.test.manifest)
|
debugImplementation(libs.androidx.ui.test.manifest)
|
||||||
implementation(libs.mpandroidchart.vv310)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,10 +30,6 @@
|
|||||||
android:name=".SettingsActivity"
|
android:name=".SettingsActivity"
|
||||||
android:label="@string/settings_title"
|
android:label="@string/settings_title"
|
||||||
android:theme="@style/Theme.LunaTracker"/>
|
android:theme="@style/Theme.LunaTracker"/>
|
||||||
<activity
|
|
||||||
android:name=".StatisticsActivity"
|
|
||||||
android:label="@string/statistics_title"
|
|
||||||
android:theme="@style/Theme.LunaTracker"/>
|
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
@@ -155,7 +155,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort all event types by frequency or ordinal
|
// sort all event types by frequency and ordinal
|
||||||
val eventTypesSorted = LunaEvent.Type.entries.toList().sortedWith(
|
val eventTypesSorted = LunaEvent.Type.entries.toList().sortedWith(
|
||||||
compareBy({ -1 * (eventTypeStats[it] ?: 0) }, { it.ordinal })
|
compareBy({ -1 * (eventTypeStats[it] ?: 0) }, { it.ordinal })
|
||||||
).filter { it != LunaEvent.Type.UNKNOWN }
|
).filter { it != LunaEvent.Type.UNKNOWN }
|
||||||
@@ -276,7 +276,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
numberPicker.value = event.quantity / 10
|
numberPicker.value = event.quantity / 10
|
||||||
|
|
||||||
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
||||||
val pickedTime = datePickerHelper(event.time, dateTV)
|
val pickedTime = dateTimePicker(event.time, dateTV)
|
||||||
|
|
||||||
if (!showTime) {
|
if (!showTime) {
|
||||||
dateTV.visibility = View.GONE
|
dateTV.visibility = View.GONE
|
||||||
@@ -314,7 +314,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
weightET.setText(event.quantity.toString())
|
weightET.setText(event.quantity.toString())
|
||||||
|
|
||||||
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
||||||
val pickedTime = datePickerHelper(event.time, dateTV)
|
val pickedTime = dateTimePicker(event.time, dateTV)
|
||||||
|
|
||||||
if (!showTime) {
|
if (!showTime) {
|
||||||
dateTV.visibility = View.GONE
|
dateTV.visibility = View.GONE
|
||||||
@@ -365,7 +365,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
||||||
val pickedTime = datePickerHelper(event.time, dateTV)
|
val pickedTime = dateTimePicker(event.time, dateTV)
|
||||||
if (!showTime) {
|
if (!showTime) {
|
||||||
dateTV.visibility = View.GONE
|
dateTV.visibility = View.GONE
|
||||||
}
|
}
|
||||||
@@ -389,7 +389,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
alertDialog.show()
|
alertDialog.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun datePickerHelper(time: Long, dateTextView: TextView, onChange: (Long) -> Unit = {}): Calendar {
|
// Pick a date/time.
|
||||||
|
fun dateTimePicker(time: Long, dateTextView: TextView, onChange: (Long) -> Unit = {}): Calendar {
|
||||||
dateTextView.text = DateUtils.formatDateTime(time)
|
dateTextView.text = DateUtils.formatDateTime(time)
|
||||||
|
|
||||||
val dateTime = Calendar.getInstance()
|
val dateTime = Calendar.getInstance()
|
||||||
@@ -421,76 +422,82 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun addSleepEvent(event: LunaEvent) {
|
fun addSleepEvent(event: LunaEvent) {
|
||||||
askSleepValue(event) { saveEvent(event) }
|
askSleepValue(event, true) { saveEvent(event) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun askSleepValue(event: LunaEvent, onPositive: () -> Unit) {
|
fun askSleepValue(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) {
|
||||||
val d = AlertDialog.Builder(this)
|
val d = AlertDialog.Builder(this)
|
||||||
val dialogView = layoutInflater.inflate(R.layout.dialog_edit_duration, null)
|
val dialogView = layoutInflater.inflate(R.layout.dialog_edit_duration, null)
|
||||||
d.setTitle(event.getDialogTitle(this))
|
d.setTitle(event.getDialogTitle(this))
|
||||||
d.setMessage(event.getDialogMessage(this))
|
|
||||||
d.setView(dialogView)
|
d.setView(dialogView)
|
||||||
|
|
||||||
val durationTextView = dialogView.findViewById<TextView>(R.id.dialog_date_duration)
|
val durationTextView = dialogView.findViewById<TextView>(R.id.dialog_date_duration)
|
||||||
val durationNowButton = dialogView.findViewById<Button>(R.id.dialog_date_duration_now)
|
val datePickerBegin = dialogView.findViewById<TextView>(R.id.dialog_date_picker_begin)
|
||||||
val datePicker = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
val datePickerEnd = dialogView.findViewById<TextView>(R.id.dialog_date_picker_end)
|
||||||
val durationMinus5Button = dialogView.findViewById<Button>(R.id.dialog_date_duration_minus5)
|
|
||||||
val durationPlus5Button = dialogView.findViewById<Button>(R.id.dialog_date_duration_plus5)
|
|
||||||
|
|
||||||
val currentDurationTextColor = durationTextView.currentTextColor
|
val currentDurationTextColor = durationTextView.currentTextColor
|
||||||
val invalidDurationTextColor = ContextCompat.getColor(this, R.color.danger)
|
val invalidDurationTextColor = ContextCompat.getColor(this, R.color.danger)
|
||||||
|
|
||||||
var duration = event.quantity
|
// in seconds
|
||||||
|
var sleepStart = event.time
|
||||||
|
var sleepEnd = event.time + event.quantity
|
||||||
|
|
||||||
fun isValidTime(timeSeconds: Long, durationSeconds: Int): Boolean {
|
fun isValidTimeSpan(timeBeginUnix: Long, timeEndUnix: Long): Boolean {
|
||||||
val now = System.currentTimeMillis() / 1000
|
val now = System.currentTimeMillis() / 1000
|
||||||
return (timeSeconds + durationSeconds) <= now && durationSeconds < (24 * 60 * 60)
|
return (timeBeginUnix > 0)
|
||||||
|
&& (timeEndUnix > 0)
|
||||||
|
&& (timeBeginUnix <= timeEndUnix)
|
||||||
|
&& (timeBeginUnix <= now)
|
||||||
|
&& (timeEndUnix <= now)
|
||||||
|
&& (timeEndUnix - timeBeginUnix) < (24 * 60 * 60)
|
||||||
}
|
}
|
||||||
|
|
||||||
val onDateChange = { time: Long ->
|
// prevent printing of seconds
|
||||||
durationTextView.setTextColor(currentDurationTextColor)
|
fun adjustToMinute(unixTime: Long): Long {
|
||||||
|
return unixTime - (unixTime % 60)
|
||||||
|
}
|
||||||
|
|
||||||
if (duration == 0) {
|
fun updateDuration() {
|
||||||
|
durationTextView.setTextColor(currentDurationTextColor)
|
||||||
|
val duration = sleepEnd - sleepStart
|
||||||
|
if (duration == 0L) {
|
||||||
// baby is sleeping
|
// baby is sleeping
|
||||||
durationTextView.text = "💤"
|
durationTextView.text = "💤"
|
||||||
} else {
|
} else {
|
||||||
durationTextView.text = DateUtils.formatTimeDuration(applicationContext, duration.toLong())
|
durationTextView.text = DateUtils.formatTimeDuration(applicationContext, duration)
|
||||||
if (!isValidTime(time, duration)) {
|
if (!isValidTimeSpan(sleepStart, sleepEnd)) {
|
||||||
durationTextView.setTextColor(invalidDurationTextColor)
|
durationTextView.setTextColor(invalidDurationTextColor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val pickedDateTime = datePickerHelper(event.time, datePicker, onDateChange)
|
val pickedDateTimeBegin = dateTimePicker(event.time, datePickerBegin) { time: Long ->
|
||||||
|
sleepStart = adjustToMinute(time)
|
||||||
onDateChange(pickedDateTime.time.time / 1000)
|
updateDuration()
|
||||||
|
|
||||||
fun adjust(minutes: Int) {
|
|
||||||
duration += minutes * 60
|
|
||||||
if (duration < 0) {
|
|
||||||
duration = 0
|
|
||||||
}
|
|
||||||
onDateChange(pickedDateTime.time.time / 1000)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
durationMinus5Button.setOnClickListener { adjust(-5) }
|
val pickedDateTimeEnd = dateTimePicker(event.time + event.quantity, datePickerEnd) { time: Long ->
|
||||||
durationPlus5Button.setOnClickListener { adjust(5) }
|
sleepEnd = adjustToMinute(time)
|
||||||
|
updateDuration()
|
||||||
|
}
|
||||||
|
|
||||||
durationNowButton.setOnClickListener {
|
sleepStart = adjustToMinute(pickedDateTimeBegin.time.time / 1000)
|
||||||
val now = System.currentTimeMillis() / 1000
|
sleepEnd = adjustToMinute(pickedDateTimeEnd.time.time / 1000)
|
||||||
val start = pickedDateTime.time.time / 1000
|
updateDuration()
|
||||||
if (now > start) {
|
|
||||||
duration = (now - start).toInt()
|
if (showTime) {
|
||||||
duration -= duration % 60 // prevent printing of seconds
|
datePickerEnd.visibility = View.GONE
|
||||||
onDateChange(pickedDateTime.time.time / 1000)
|
durationTextView.visibility = View.GONE
|
||||||
}
|
//d.setMessage("")
|
||||||
|
} else {
|
||||||
|
durationTextView.visibility = View.VISIBLE
|
||||||
|
d.setMessage(event.getDialogMessage(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
d.setPositiveButton(android.R.string.ok) { dialogInterface, i ->
|
d.setPositiveButton(android.R.string.ok) { dialogInterface, i ->
|
||||||
val time = pickedDateTime.time.time / 1000
|
if (isValidTimeSpan(sleepStart, sleepEnd)) {
|
||||||
if (isValidTime(time, duration)) {
|
event.time = sleepStart
|
||||||
event.time = time
|
event.quantity = (sleepEnd - sleepStart).toInt()
|
||||||
event.quantity = duration
|
|
||||||
onPositive()
|
onPositive()
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this, R.string.toast_date_error, Toast.LENGTH_SHORT).show()
|
Toast.makeText(this, R.string.toast_date_error, Toast.LENGTH_SHORT).show()
|
||||||
@@ -507,6 +514,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun addAmountEvent(event: LunaEvent) {
|
fun addAmountEvent(event: LunaEvent) {
|
||||||
|
setToPreviousQuantity(event)
|
||||||
askAmountValue(event, true) { saveEvent(event) }
|
askAmountValue(event, true) { saveEvent(event) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -523,11 +531,11 @@ class MainActivity : AppCompatActivity() {
|
|||||||
R.array.AmountLabels,
|
R.array.AmountLabels,
|
||||||
android.R.layout.simple_spinner_dropdown_item
|
android.R.layout.simple_spinner_dropdown_item
|
||||||
)
|
)
|
||||||
// set pre-selected item and ensure the quantity to index is in bounds
|
|
||||||
spinner.setSelection(event.quantity.coerceIn(0, spinner.count - 1))
|
spinner.setSelection(event.quantity.coerceIn(0, spinner.count - 1))
|
||||||
|
|
||||||
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
||||||
val pickedTime = datePickerHelper(event.time, dateTV)
|
val pickedTime = dateTimePicker(event.time, dateTV)
|
||||||
if (!showTime) {
|
if (!showTime) {
|
||||||
dateTV.visibility = View.GONE
|
dateTV.visibility = View.GONE
|
||||||
}
|
}
|
||||||
@@ -560,7 +568,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
d.setView(dialogView)
|
d.setView(dialogView)
|
||||||
|
|
||||||
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
||||||
val pickedDateTime = datePickerHelper(event.time, dateTV)
|
val pickedDateTime = dateTimePicker(event.time, dateTV)
|
||||||
if (!showTime) {
|
if (!showTime) {
|
||||||
dateTV.visibility = View.GONE
|
dateTV.visibility = View.GONE
|
||||||
}
|
}
|
||||||
@@ -595,7 +603,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
val qtyET = dialogView.findViewById<EditText>(R.id.notes_qty_edittext)
|
val qtyET = dialogView.findViewById<EditText>(R.id.notes_qty_edittext)
|
||||||
|
|
||||||
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
||||||
val pickedTime = datePickerHelper(event.time, dateTV)
|
val pickedTime = dateTimePicker(event.time, dateTV)
|
||||||
|
|
||||||
if (!showTime) {
|
if (!showTime) {
|
||||||
dateTV.visibility = View.GONE
|
dateTV.visibility = View.GONE
|
||||||
@@ -807,7 +815,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
quantityTextView.text = NumericUtils(this).formatEventQuantity(event)
|
quantityTextView.text = NumericUtils(this).formatEventQuantity(event)
|
||||||
notesTextView.text = event.notes
|
notesTextView.text = event.notes
|
||||||
if (event.type == LunaEvent.Type.SLEEP && event.quantity > 0) {
|
if (event.type == LunaEvent.Type.SLEEP && event.quantity > 0) {
|
||||||
dateEndTextView.text = DateUtils.formatDateTime(event.time + event.quantity)
|
dateEndTextView.text = DateUtils.formatDateTime(event.getEndTime())
|
||||||
dateEndTextView.visibility = View.VISIBLE
|
dateEndTextView.visibility = View.VISIBLE
|
||||||
} else {
|
} else {
|
||||||
dateEndTextView.visibility = View.GONE
|
dateEndTextView.visibility = View.GONE
|
||||||
@@ -821,7 +829,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
updateValues()
|
updateValues()
|
||||||
|
|
||||||
datePickerHelper(event.time, dateTextView, { newTime: Long ->
|
dateTimePicker(event.time, dateTextView, { newTime: Long ->
|
||||||
event.time = newTime
|
event.time = newTime
|
||||||
updateValues()
|
updateValues()
|
||||||
})
|
})
|
||||||
@@ -835,7 +843,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
LunaEvent.Type.PUKE -> askAmountValue(event, false, updateValues)
|
LunaEvent.Type.PUKE -> askAmountValue(event, false, updateValues)
|
||||||
LunaEvent.Type.TEMPERATURE -> askTemperatureValue(event, false, updateValues)
|
LunaEvent.Type.TEMPERATURE -> askTemperatureValue(event, false, updateValues)
|
||||||
LunaEvent.Type.NOTE -> askNotes(event, false, updateValues)
|
LunaEvent.Type.NOTE -> askNotes(event, false, updateValues)
|
||||||
LunaEvent.Type.SLEEP -> askSleepValue(event, updateValues)
|
LunaEvent.Type.SLEEP -> askSleepValue(event, false, updateValues)
|
||||||
else -> {
|
else -> {
|
||||||
Log.w(TAG, "Unexpected type: ${event.type}")
|
Log.w(TAG, "Unexpected type: ${event.type}")
|
||||||
}
|
}
|
||||||
@@ -865,7 +873,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
val previousEvent = getPreviousSameEvent(event, allEvents)
|
val previousEvent = getPreviousSameEvent(event, allEvents)
|
||||||
if (previousEvent != null) {
|
if (previousEvent != null) {
|
||||||
val emoji = previousEvent.getHeaderEmoji(applicationContext)
|
val emoji = previousEvent.getHeaderEmoji(applicationContext)
|
||||||
val time = DateUtils.formatTimeDuration(applicationContext, event.time - previousEvent.time)
|
val time = DateUtils.formatTimeDuration(applicationContext, event.getStartTime() - previousEvent.getEndTime())
|
||||||
previousTextView.text = String.format("⬅️ %s %s", emoji, time)
|
previousTextView.text = String.format("⬅️ %s %s", emoji, time)
|
||||||
previousTextView.setOnClickListener {
|
previousTextView.setOnClickListener {
|
||||||
alertDialog.cancel()
|
alertDialog.cancel()
|
||||||
@@ -880,7 +888,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
val nextEvent = getNextSameEvent(event, allEvents)
|
val nextEvent = getNextSameEvent(event, allEvents)
|
||||||
if (nextEvent != null) {
|
if (nextEvent != null) {
|
||||||
val emoji = nextEvent.getHeaderEmoji(applicationContext)
|
val emoji = nextEvent.getHeaderEmoji(applicationContext)
|
||||||
val time = DateUtils.formatTimeDuration(applicationContext, nextEvent.time - event.time)
|
val time = DateUtils.formatTimeDuration(applicationContext, nextEvent.getStartTime() - event.getEndTime())
|
||||||
nextTextView.text = String.format("%s %s ➡️", time, emoji)
|
nextTextView.text = String.format("%s %s ➡️", time, emoji)
|
||||||
nextTextView.setOnClickListener {
|
nextTextView.setOnClickListener {
|
||||||
alertDialog.cancel()
|
alertDialog.cancel()
|
||||||
@@ -1276,18 +1284,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
val inflater = LayoutInflater.from(anchor.context)
|
val inflater = LayoutInflater.from(anchor.context)
|
||||||
contentView = inflater.inflate(R.layout.more_events_popup, null)
|
contentView = inflater.inflate(R.layout.more_events_popup, null)
|
||||||
|
|
||||||
// Add statistics (hard coded)
|
|
||||||
contentView.findViewById<View>(R.id.button_statistics).setOnClickListener {
|
|
||||||
if (logbook != null && !pauseLogbookUpdate) {
|
|
||||||
val i = Intent(applicationContext, StatisticsActivity::class.java)
|
|
||||||
i.putExtra("LOOGBOOK_NAME", logbook!!.name)
|
|
||||||
startActivity(i)
|
|
||||||
} else {
|
|
||||||
Toast.makeText(applicationContext, "No logbook selected!", Toast.LENGTH_SHORT).show()
|
|
||||||
}
|
|
||||||
dismiss()
|
|
||||||
}
|
|
||||||
|
|
||||||
val linearLayout = contentView.findViewById<LinearLayout>(R.id.layout_list)
|
val linearLayout = contentView.findViewById<LinearLayout>(R.id.layout_list)
|
||||||
|
|
||||||
// Add buttons to create other events
|
// Add buttons to create other events
|
||||||
|
|||||||
@@ -1,997 +0,0 @@
|
|||||||
package it.danieleverducci.lunatracker
|
|
||||||
|
|
||||||
import android.graphics.Color
|
|
||||||
import android.os.Bundle
|
|
||||||
import android.util.Log
|
|
||||||
import android.view.View
|
|
||||||
import android.widget.AdapterView
|
|
||||||
import android.widget.ArrayAdapter
|
|
||||||
import android.widget.Spinner
|
|
||||||
import android.widget.TextView
|
|
||||||
import android.widget.Toast
|
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
|
||||||
import androidx.core.graphics.toColorInt
|
|
||||||
import com.github.mikephil.charting.charts.BarChart
|
|
||||||
import com.github.mikephil.charting.data.BarData
|
|
||||||
import com.github.mikephil.charting.data.BarDataSet
|
|
||||||
import com.github.mikephil.charting.data.BarEntry
|
|
||||||
import com.github.mikephil.charting.data.Entry
|
|
||||||
import com.github.mikephil.charting.formatter.ValueFormatter
|
|
||||||
import com.github.mikephil.charting.highlight.Highlight
|
|
||||||
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
|
|
||||||
import com.github.mikephil.charting.listener.OnChartValueSelectedListener
|
|
||||||
import it.danieleverducci.lunatracker.entities.LunaEvent
|
|
||||||
import utils.DateUtils
|
|
||||||
import utils.NumericUtils
|
|
||||||
import java.text.SimpleDateFormat
|
|
||||||
import java.util.Calendar
|
|
||||||
import java.util.Date
|
|
||||||
import java.util.Locale
|
|
||||||
import kotlin.math.abs
|
|
||||||
import kotlin.math.max
|
|
||||||
import kotlin.math.min
|
|
||||||
|
|
||||||
class StatisticsActivity : AppCompatActivity() {
|
|
||||||
var lastToastShown = 0L
|
|
||||||
|
|
||||||
lateinit var barChart: BarChart
|
|
||||||
lateinit var noDataTextView: TextView
|
|
||||||
lateinit var graphTypeSpinner: Spinner
|
|
||||||
lateinit var timeRangeSpinner: Spinner
|
|
||||||
|
|
||||||
lateinit var unixToSpan: (Long) -> Int
|
|
||||||
lateinit var spanToUnix: (Int) -> Long
|
|
||||||
|
|
||||||
enum class GraphType {
|
|
||||||
BOTTLE_EVENTS,
|
|
||||||
BOTTLE_SUM,
|
|
||||||
SLEEP_SUM,
|
|
||||||
SLEEP_EVENTS,
|
|
||||||
SLEEP_PATTERN,
|
|
||||||
MEDICINE_EVENTS
|
|
||||||
}
|
|
||||||
|
|
||||||
enum class TimeRange {
|
|
||||||
DAY,
|
|
||||||
WEEK,
|
|
||||||
MONTH
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
|
||||||
super.onCreate(savedInstanceState)
|
|
||||||
|
|
||||||
setContentView(R.layout.activity_statistics)
|
|
||||||
|
|
||||||
val logbookName = intent.getStringExtra("LOOGBOOK_NAME")
|
|
||||||
if (logbookName == null) {
|
|
||||||
finish()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
noDataTextView = findViewById(R.id.no_data)
|
|
||||||
|
|
||||||
barChart = findViewById(R.id.bar_chart)
|
|
||||||
barChart.setBackgroundColor(Color.WHITE)
|
|
||||||
barChart.description.text = logbookName
|
|
||||||
barChart.setDrawValueAboveBar(false)
|
|
||||||
|
|
||||||
barChart.axisLeft.setAxisMinimum(0F)
|
|
||||||
barChart.axisLeft.setDrawGridLines(false)
|
|
||||||
barChart.axisLeft.setDrawLabels(false)
|
|
||||||
|
|
||||||
barChart.axisRight.setDrawGridLines(false)
|
|
||||||
barChart.axisRight.setDrawLabels(false)
|
|
||||||
|
|
||||||
//barChart.xAxis.setDrawGridLines(true)
|
|
||||||
barChart.xAxis.setDrawLabels(true)
|
|
||||||
barChart.xAxis.setDrawAxisLine(false)
|
|
||||||
|
|
||||||
graphTypeSpinner = findViewById(R.id.graph_type_selection)
|
|
||||||
timeRangeSpinner = findViewById(R.id.time_range_selection)
|
|
||||||
|
|
||||||
setupSpinner(graphTypeSelection.name,
|
|
||||||
R.id.graph_type_selection,
|
|
||||||
R.array.StatisticsTypeLabels,
|
|
||||||
R.array.StatisticsTypeValues,
|
|
||||||
object : SpinnerItemSelected {
|
|
||||||
override fun call(newValue: String?) {
|
|
||||||
//Log.d("event", "new value: $newValue")
|
|
||||||
newValue ?: return
|
|
||||||
graphTypeSelection = GraphType.valueOf(newValue)
|
|
||||||
showGraph()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
setupSpinner(timeRangeSelection.name,
|
|
||||||
R.id.time_range_selection,
|
|
||||||
R.array.StatisticsTimeLabels,
|
|
||||||
R.array.StatisticsTimeValues,
|
|
||||||
object : SpinnerItemSelected {
|
|
||||||
override fun call(newValue: String?) {
|
|
||||||
//Log.d("event", "new value: $newValue")
|
|
||||||
newValue ?: return
|
|
||||||
timeRangeSelection = TimeRange.valueOf(newValue)
|
|
||||||
setSpans()
|
|
||||||
showGraph()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
setSpans()
|
|
||||||
showGraph()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setSpans() {
|
|
||||||
unixToSpan = when (timeRangeSelection) {
|
|
||||||
TimeRange.DAY -> { unix: Long -> unixToDays(unix) }
|
|
||||||
TimeRange.WEEK -> { unix: Long -> unixToWeeks(unix) }
|
|
||||||
TimeRange.MONTH -> { unix: Long -> unixToMonths(unix) }
|
|
||||||
}
|
|
||||||
|
|
||||||
spanToUnix = when (timeRangeSelection) {
|
|
||||||
TimeRange.DAY -> { span: Int -> daysToUnix(span) }
|
|
||||||
TimeRange.WEEK -> { span: Int -> weeksToUnix(span) }
|
|
||||||
TimeRange.MONTH -> { span: Int -> monthsToUnix(span) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun showMedicineBarGraph(state: GraphState) {
|
|
||||||
val values = HashMap<String, ArrayList<BarEntry>>()
|
|
||||||
|
|
||||||
for (event in state.events) {
|
|
||||||
val index = unixToSpan(event.time) - state.startSpan
|
|
||||||
val key = event.notes.trim().lowercase()
|
|
||||||
val array = values.getOrPut(key) {
|
|
||||||
ArrayList(List(state.endSpan - state.startSpan + 1) { BarEntry(it.toFloat(), 0F) })
|
|
||||||
}
|
|
||||||
|
|
||||||
array[index].y += 1F
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.d(TAG, "values.size: ${values.size}")
|
|
||||||
for ((key, value) in values) {
|
|
||||||
Log.d(TAG, "key: $key, value.size: ${value.size} ,value: ${value.joinToString { it.y.toLong().toString() }}")
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure legend names are not too long
|
|
||||||
fun shorten(notes: String): String {
|
|
||||||
return if (notes.length > 16) {
|
|
||||||
notes.take(13) + "..."
|
|
||||||
} else {
|
|
||||||
notes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun chooseColor(notes: String): Int {
|
|
||||||
return (abs(notes.hashCode()) * 16777215) or (0xFF shl 24)
|
|
||||||
}
|
|
||||||
|
|
||||||
val sets = arrayListOf<IBarDataSet>()
|
|
||||||
for ((key, value) in values.entries) {
|
|
||||||
if (key.startsWith("v")) {
|
|
||||||
val description = shorten(key)
|
|
||||||
val barDataSet = BarDataSet(value, description)
|
|
||||||
barDataSet.color = chooseColor(key)
|
|
||||||
sets.add(barDataSet)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val data = BarData(sets)
|
|
||||||
//data.groupBars(0F, 0.2F, 0.1F);
|
|
||||||
data.setValueTextSize(12f)
|
|
||||||
data.barWidth = 1F
|
|
||||||
//data.groupBars(0F, 1F, 1F)
|
|
||||||
|
|
||||||
data.setValueFormatter(object : ValueFormatter() {
|
|
||||||
override fun getFormattedValue(value: Float): String {
|
|
||||||
return if (value == 0F) {
|
|
||||||
""
|
|
||||||
} else {
|
|
||||||
value.toInt().toString()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
barChart.setScaleEnabled(true)
|
|
||||||
barChart.legend.isEnabled = true
|
|
||||||
|
|
||||||
//barChart.xAxis.setLabelCount(min(values.size, 24), false);
|
|
||||||
//val maxCount = min(maxIndex, 30) // values.size
|
|
||||||
//Log.d(TAG, "maxCount: $maxCount")
|
|
||||||
barChart.setVisibleXRangeMaximum(20F) //maxCount.toFloat()) // show max 24 entries
|
|
||||||
barChart.xAxis.setLabelCount(30, true)
|
|
||||||
//barChart.xAxis.isEnabled = false
|
|
||||||
barChart.xAxis.setCenterAxisLabels(true)
|
|
||||||
barChart.setScaleEnabled(false)
|
|
||||||
|
|
||||||
//barChart.axisLeft.isSLEEP_PATTERN_GRANULARITYEnabled = true
|
|
||||||
//barChart.axisLeft.setSLEEP_PATTERN_GRANULARITY(0.8F)
|
|
||||||
|
|
||||||
barChart.setData(data)
|
|
||||||
barChart.invalidate()
|
|
||||||
}
|
|
||||||
|
|
||||||
data class SleepRange(val start: Long, var end: Long)
|
|
||||||
|
|
||||||
fun toSleepRanges(events: List<LunaEvent>): ArrayList<SleepRange> {
|
|
||||||
val ranges = arrayListOf<SleepRange>()
|
|
||||||
val now = System.currentTimeMillis() / 1000
|
|
||||||
|
|
||||||
// Transform events into time ranges.
|
|
||||||
// Merge overlapping times and extend
|
|
||||||
// ongoing sleep events until now.
|
|
||||||
var warningShown = false
|
|
||||||
for (event in events) {
|
|
||||||
val startTime = event.time
|
|
||||||
val endTime = if (event.quantity == 0) {
|
|
||||||
now
|
|
||||||
} else {
|
|
||||||
event.time + event.quantity
|
|
||||||
}
|
|
||||||
|
|
||||||
// handle overlap
|
|
||||||
val previousRange = ranges.lastOrNull()
|
|
||||||
if (previousRange != null && previousRange.end > startTime) {
|
|
||||||
// cap previous range to avoid overlap
|
|
||||||
previousRange.end = startTime
|
|
||||||
if (!warningShown) {
|
|
||||||
Toast.makeText(applicationContext, "Overlapping sleep event at ${DateUtils.formatDateTime(startTime)}", Toast.LENGTH_SHORT).show()
|
|
||||||
warningShown = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ranges.add(SleepRange(startTime, endTime))
|
|
||||||
}
|
|
||||||
|
|
||||||
return ranges
|
|
||||||
}
|
|
||||||
|
|
||||||
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) })
|
|
||||||
|
|
||||||
Log.d(TAG, "stack.size: ${stack.size}, array.size: ${stack[0].size}, dayCounter.daysWithData.size: ${state.dayCounter.daysWithData.size}")
|
|
||||||
|
|
||||||
fun stackValuePattern(index: Int, spanBegin: Long, spanEnd: Long, begin: Long, end: Long) {
|
|
||||||
val beginDays = unixToDays(begin)
|
|
||||||
val endDays = unixToDays(end)
|
|
||||||
var mid = begin
|
|
||||||
|
|
||||||
//Log.d(TAG, "stackValuePattern: ${beginDays}..${endDays}")
|
|
||||||
|
|
||||||
for (i in beginDays..endDays) {
|
|
||||||
// i is the days/weeks/months since unix epoch
|
|
||||||
val dayBegin = daysToUnix(i)
|
|
||||||
val dayEnd = daysToUnix(i + 1)
|
|
||||||
val sleepBegin = max(mid, dayBegin)
|
|
||||||
val sleepEnd = min(end, dayEnd)
|
|
||||||
|
|
||||||
if (sleepBegin != sleepEnd) {
|
|
||||||
assert(dayBegin <= dayEnd)
|
|
||||||
assert(sleepBegin <= sleepEnd)
|
|
||||||
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)}")
|
|
||||||
for (j in iBegin..iEnd) {
|
|
||||||
stack[index][j.toInt()] += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mid = sleepEnd
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (range in ranges) {
|
|
||||||
// a sleep event can span to another day
|
|
||||||
// distribute sleep time over the days
|
|
||||||
val startUnix = range.start
|
|
||||||
val endUnix = range.end
|
|
||||||
val begIndex = unixToSpan(startUnix)
|
|
||||||
val endIndex = unixToSpan(endUnix)
|
|
||||||
var mid = startUnix
|
|
||||||
|
|
||||||
//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, "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
|
|
||||||
val duration = sleepEnd - sleepBegin
|
|
||||||
//Log.d(TAG, "[$index] sleepBegin: ${Date(sleepBegin * 1000)}, sleepEnd: ${Date(sleepEnd * 1000)}, ${DateUtils.formatTimeDuration(this, duration)}")
|
|
||||||
|
|
||||||
state.dayCounter.setDaysWithData(sleepBegin, sleepEnd)
|
|
||||||
stackValuePattern(index, spanBegin, spanEnd, sleepBegin, sleepEnd)
|
|
||||||
|
|
||||||
mid = sleepEnd
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun mapColor(occurrences: Int, maxOccurrences: Int): Int {
|
|
||||||
// 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()
|
|
||||||
return SLEEP_PATTERN_COLORS[i.toInt()]
|
|
||||||
}
|
|
||||||
|
|
||||||
val allColors = ArrayList<Int>()
|
|
||||||
|
|
||||||
// convert array of time slots that represent a day to value and color arrays used by chart library
|
|
||||||
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: dayArray: ${dayArray.joinToString { it.toString() }}")
|
|
||||||
val vals = ArrayList<Float>()
|
|
||||||
|
|
||||||
var prevIndex = -1 // time slice index
|
|
||||||
var prevValue = -1 // number of entries we have found for time slice
|
|
||||||
for ((i, v) in dayArray.withIndex()) {
|
|
||||||
if (i == 0) {
|
|
||||||
prevIndex = i
|
|
||||||
prevValue = v
|
|
||||||
} else if (prevValue != v) {
|
|
||||||
vals.add((i - prevIndex).toFloat())
|
|
||||||
allColors.add(mapColor(prevValue, daysWithData))
|
|
||||||
prevIndex = i
|
|
||||||
prevValue = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (prevIndex != -1) {
|
|
||||||
vals.add((dayArray.size - prevIndex).toFloat())
|
|
||||||
allColors.add(mapColor(prevValue, daysWithData))
|
|
||||||
}
|
|
||||||
|
|
||||||
//Log.d(TAG, "Range $index, vals: ${vals.joinToString { it.toInt().toString() }}") //, allColors: ${allColors.joinToString { it.toString() }}")
|
|
||||||
|
|
||||||
values.add(BarEntry(index.toFloat(), vals.toFloatArray()))
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.d(TAG, "daysWithData: ${state.dayCounter.daysWithData.joinToString()}")
|
|
||||||
|
|
||||||
barChart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
|
|
||||||
override fun onValueSelected(e: Entry?, h: Highlight?) {
|
|
||||||
if (e == null || h == null) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
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() {}
|
|
||||||
})
|
|
||||||
|
|
||||||
val set1 = BarDataSet(values, "")
|
|
||||||
val data = BarData(set1)
|
|
||||||
set1.colors = allColors
|
|
||||||
|
|
||||||
set1.setDrawValues(false) // usually too many values
|
|
||||||
set1.isHighlightEnabled = true
|
|
||||||
|
|
||||||
set1.setDrawIcons(false)
|
|
||||||
barChart.legend.isEnabled = false
|
|
||||||
barChart.setScaleEnabled(false)
|
|
||||||
barChart.xAxis.setLabelCount(min(values.size, 24))
|
|
||||||
data.setValueTextSize(12f)
|
|
||||||
barChart.setData(data)
|
|
||||||
|
|
||||||
barChart.invalidate()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun showSleepPatternBarGraphDaily(state: GraphState) {
|
|
||||||
val ranges = toSleepRanges(state.events)
|
|
||||||
val values = ArrayList(List(state.endSpan - state.startSpan + 1) { BarEntry(it.toFloat(), FloatArray(0)) })
|
|
||||||
|
|
||||||
// stack awake/sleep durations
|
|
||||||
fun stackValuePattern(index: Int, spanBegin: Long, spanEnd: Long, begin: Long, end: Long) {
|
|
||||||
assert(begin in spanBegin..spanEnd)
|
|
||||||
assert(end in spanBegin..spanEnd)
|
|
||||||
assert(begin <= end)
|
|
||||||
|
|
||||||
val x = values[index].x
|
|
||||||
val yVals = values[index].yVals // alternating sleep/awake durations
|
|
||||||
val y = yVals.fold(0F) { acc, next -> acc + next }
|
|
||||||
|
|
||||||
// y value is seconds when last awake
|
|
||||||
val awakeDuration = max(begin - spanBegin - y.toLong(), 0L)
|
|
||||||
val sleepDuration = end - begin
|
|
||||||
|
|
||||||
if ((awakeDuration + sleepDuration) > (spanEnd - spanBegin)) {
|
|
||||||
Log.e(TAG, "Invalid sleep duration, exceeds day/week or month bounds => ignore value")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// update value
|
|
||||||
val newYVals = appendToFloatArray(yVals, awakeDuration.toFloat(), sleepDuration.toFloat())
|
|
||||||
values[index] = BarEntry(x, newYVals)
|
|
||||||
}
|
|
||||||
|
|
||||||
for (range in ranges) {
|
|
||||||
// a sleep event can span to another day
|
|
||||||
// distribute sleep time over the days
|
|
||||||
val startUnix = range.start
|
|
||||||
val endUnix = range.end
|
|
||||||
val begIndex = unixToSpan(startUnix)
|
|
||||||
val endIndex = unixToSpan(endUnix)
|
|
||||||
var mid = startUnix
|
|
||||||
|
|
||||||
//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, "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
|
|
||||||
val duration = sleepEnd - sleepBegin
|
|
||||||
//Log.d(TAG, "[$index] sleepBegin: ${Date(sleepBegin * 1000)}, sleepEnd: ${Date(sleepEnd * 1000)}, ${DateUtils.formatTimeDuration(this, duration)}")
|
|
||||||
|
|
||||||
state.dayCounter.setDaysWithData(sleepBegin, sleepEnd)
|
|
||||||
stackValuePattern(index, spanBegin, spanEnd, sleepBegin, sleepEnd)
|
|
||||||
|
|
||||||
mid = sleepEnd
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val set1 = BarDataSet(values, "")
|
|
||||||
val data = BarData(set1)
|
|
||||||
|
|
||||||
// awake phase color is transparent
|
|
||||||
set1.colors = arrayListOf("#00000000".toColorInt(), "#72d7f5".toColorInt())
|
|
||||||
set1.setDrawValues(false) // usually too many values
|
|
||||||
set1.isHighlightEnabled = true
|
|
||||||
|
|
||||||
//barChart.xAxis.setCenterAxisLabels(true)
|
|
||||||
|
|
||||||
barChart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
|
|
||||||
override fun onValueSelected(e: Entry?, h: Highlight?) {
|
|
||||||
if (e == null || h == null) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
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() {}
|
|
||||||
})
|
|
||||||
|
|
||||||
set1.setDrawIcons(false)
|
|
||||||
barChart.legend.isEnabled = false
|
|
||||||
barChart.setScaleEnabled(false)
|
|
||||||
barChart.xAxis.setLabelCount(min(values.size, 24))
|
|
||||||
data.setValueTextSize(12f)
|
|
||||||
barChart.setData(data)
|
|
||||||
|
|
||||||
barChart.invalidate()
|
|
||||||
}
|
|
||||||
|
|
||||||
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)}")
|
|
||||||
|
|
||||||
for (range in ranges) {
|
|
||||||
// a sleep event can span to another day
|
|
||||||
// distribute sleep time over the days
|
|
||||||
val startUnix = range.start
|
|
||||||
val endUnix = range.end
|
|
||||||
val begIndex = unixToSpan(startUnix)
|
|
||||||
val endIndex = unixToSpan(endUnix)
|
|
||||||
var mid = startUnix
|
|
||||||
|
|
||||||
//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, "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
|
|
||||||
val duration = sleepEnd - sleepBegin
|
|
||||||
//Log.d(TAG, "[$index] sleepBegin: ${Date(sleepBegin * 1000)}, sleepEnd: ${Date(sleepEnd * 1000)}, ${DateUtils.formatTimeDuration(this, duration)}")
|
|
||||||
|
|
||||||
state.dayCounter.setDaysWithData(sleepBegin, sleepEnd)
|
|
||||||
|
|
||||||
if (graphTypeSelection == GraphType.SLEEP_SUM) {
|
|
||||||
values[index].y += duration.toFloat()
|
|
||||||
} else if (graphTypeSelection == GraphType.SLEEP_EVENTS) {
|
|
||||||
values[index].y += 1F
|
|
||||||
} else {
|
|
||||||
Log.e(TAG, "Unexpected graph type.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
mid = sleepEnd
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (graphTypeSelection == GraphType.SLEEP_SUM) {
|
|
||||||
for (index in values.indices) {
|
|
||||||
val daysWithData = state.dayCounter.countDaysWithData(spanToUnix(state.startSpan + index), spanToUnix(state.startSpan + index + 1))
|
|
||||||
if (daysWithData == 0) {
|
|
||||||
assert(values[index].y == 0F)
|
|
||||||
} else {
|
|
||||||
values[index].y /= daysWithData
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val set1 = BarDataSet(values, "")
|
|
||||||
val data = BarData(set1)
|
|
||||||
set1.setDrawValues(true)
|
|
||||||
set1.isHighlightEnabled = false
|
|
||||||
|
|
||||||
data.setValueFormatter(object : ValueFormatter() {
|
|
||||||
override fun getFormattedValue(value: Float): String {
|
|
||||||
return when (graphTypeSelection) {
|
|
||||||
GraphType.SLEEP_EVENTS -> value.toInt().toString()
|
|
||||||
GraphType.SLEEP_SUM -> {
|
|
||||||
val prefix = if (timeRangeSelection == TimeRange.DAY) { "" } else { "⌀ " }
|
|
||||||
return prefix + NumericUtils(applicationContext).formatEventQuantity(LunaEvent.Type.SLEEP, value.toInt())
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
Log.e(TAG, "unhandled graphTypeSelection $graphTypeSelection")
|
|
||||||
value.toInt().toString()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
set1.setDrawIcons(false)
|
|
||||||
barChart.legend.isEnabled = false
|
|
||||||
barChart.setScaleEnabled(false)
|
|
||||||
barChart.xAxis.setLabelCount(min(values.size, 24))
|
|
||||||
data.setValueTextSize(12f)
|
|
||||||
barChart.setData(data)
|
|
||||||
|
|
||||||
barChart.invalidate()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun showBottleBarGraph(state: GraphState) {
|
|
||||||
val values = ArrayList(List(state.endSpan - state.startSpan + 1) { BarEntry(it.toFloat(), 0F) })
|
|
||||||
|
|
||||||
// needed?
|
|
||||||
for (i in values.indices) {
|
|
||||||
values[i].x = i.toFloat()
|
|
||||||
}
|
|
||||||
|
|
||||||
for (event in state.events) {
|
|
||||||
val index = unixToSpan(event.time) - state.startSpan
|
|
||||||
|
|
||||||
state.dayCounter.setDaysWithData(event.time, event.time)
|
|
||||||
|
|
||||||
// setDaysWithData(sleepBegin, sleepEnd)
|
|
||||||
if (graphTypeSelection == GraphType.BOTTLE_EVENTS) {
|
|
||||||
values[index].y += 1F
|
|
||||||
} else if (graphTypeSelection == GraphType.BOTTLE_SUM) {
|
|
||||||
values[index].y += event.quantity.toFloat()
|
|
||||||
} else {
|
|
||||||
Log.e(TAG, "unhandled graphTypeSelection: $graphTypeSelection")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (graphTypeSelection == GraphType.BOTTLE_SUM) {
|
|
||||||
for (index in values.indices) {
|
|
||||||
val daysWithData = state.dayCounter.countDaysWithData(spanToUnix(state.startSpan + index), spanToUnix(state.startSpan + index + 1))
|
|
||||||
//Log.d(TAG, "index: $index, daysWithData: $daysWithData")
|
|
||||||
if (daysWithData == 0) {
|
|
||||||
assert(values[index].y == 0F)
|
|
||||||
} else {
|
|
||||||
values[index].y /= daysWithData
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val set1 = BarDataSet(values, "")
|
|
||||||
|
|
||||||
set1.setDrawValues(true)
|
|
||||||
set1.isHighlightEnabled = false
|
|
||||||
|
|
||||||
//barChart.axisLeft.isSLEEP_PATTERN_GRANULARITYEnabled = true
|
|
||||||
//barChart.axisLeft.setSLEEP_PATTERN_GRANULARITY(0.8F)
|
|
||||||
|
|
||||||
val data = BarData(set1)
|
|
||||||
//data.barWidth = 0.3F // 0.85 default // ratio of barWidth to totalWidth.
|
|
||||||
//Log.d(TAG, "data.barWidth: ${data.barWidth}")
|
|
||||||
|
|
||||||
data.setValueFormatter(object : ValueFormatter() {
|
|
||||||
override fun getFormattedValue(value: Float): String {
|
|
||||||
//Log.d(TAG, "getFormattedValue ${dataTypeSelectionValue} ${eventTypeSelectionValue}")
|
|
||||||
return when (graphTypeSelection) {
|
|
||||||
GraphType.BOTTLE_EVENTS -> value.toInt().toString()
|
|
||||||
GraphType.BOTTLE_SUM -> {
|
|
||||||
val prefix = if (timeRangeSelection == TimeRange.DAY) { "" } else { "⌀ " }
|
|
||||||
return prefix + NumericUtils(applicationContext).formatEventQuantity(LunaEvent.Type.BABY_BOTTLE, value.toInt())
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
Log.e(TAG, "unhandled graphTypeSelection")
|
|
||||||
value.toInt().toString()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// hm, does not work yet
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
class DayCounter(val startDays: Int, val stopDays: Int) {
|
|
||||||
val daysWithData = BooleanArray(stopDays - startDays + 1)
|
|
||||||
|
|
||||||
// count days in a span that have data
|
|
||||||
// e.g. return 7 (days) for applied span of a week where there is data for every day
|
|
||||||
fun countDaysWithData(beginUnix: Long, endUnix: Long): Int {
|
|
||||||
val beginDays = unixToDays(beginUnix)
|
|
||||||
val endDays = unixToDays(endUnix)
|
|
||||||
var count = 0
|
|
||||||
for (i in (beginDays - startDays)..<(endDays - startDays)) {
|
|
||||||
count += if (daysWithData[i]) { 1 } else { 0 }
|
|
||||||
}
|
|
||||||
return count
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setDaysWithData(beginUnix: Long, endUnix: Long) {
|
|
||||||
val beginDays = unixToDays(beginUnix)
|
|
||||||
val endDays = unixToDays(endUnix)
|
|
||||||
assert(beginDays <= endDays)
|
|
||||||
assert(startDays <= beginDays)
|
|
||||||
for (i in (beginDays - startDays)..(endDays - startDays)) {
|
|
||||||
daysWithData[i] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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, cb: (GraphState) -> Unit) {
|
|
||||||
val events = MainActivity.allEvents.filter { it.type == type }.sortedBy { it.time }
|
|
||||||
|
|
||||||
if (events.isEmpty()) {
|
|
||||||
barChart.visibility = View.GONE
|
|
||||||
noDataTextView.visibility = View.VISIBLE
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
barChart.visibility = View.VISIBLE
|
|
||||||
noDataTextView.visibility = View.GONE
|
|
||||||
}
|
|
||||||
|
|
||||||
// unix time span of all events
|
|
||||||
val startUnix = events.minOf { it.time }
|
|
||||||
val endUnix = System.currentTimeMillis() / 1000
|
|
||||||
|
|
||||||
// convert to days, weeks or months
|
|
||||||
val startSpan = unixToSpan(startUnix)
|
|
||||||
val endSpan = unixToSpan(endUnix)
|
|
||||||
|
|
||||||
// days when the a day/week/month starts/ends
|
|
||||||
val startDays = unixToDays(spanToUnix(startSpan))
|
|
||||||
val endDays = unixToDays(spanToUnix(endSpan + 1)) // until end of next week
|
|
||||||
val dayCounter = DayCounter(startDays, endDays)
|
|
||||||
|
|
||||||
// print dates
|
|
||||||
barChart.xAxis.valueFormatter = object: ValueFormatter() {
|
|
||||||
override fun getFormattedValue(value: Float): String {
|
|
||||||
val index = value.toInt()
|
|
||||||
val unixSeconds = spanToUnix(startSpan + index)
|
|
||||||
|
|
||||||
val dateTime = Calendar.getInstance()
|
|
||||||
dateTime.time = Date(1000L * unixSeconds)
|
|
||||||
val year = dateTime.get(Calendar.YEAR)
|
|
||||||
val month = dateTime.get(Calendar.MONTH) + 1 // month starts at 0
|
|
||||||
val week = dateTime.get(Calendar.WEEK_OF_YEAR)
|
|
||||||
val day = dateTime.get(Calendar.DAY_OF_MONTH)
|
|
||||||
return when (timeRangeSelection) {
|
|
||||||
TimeRange.DAY -> "$day/$month/$year"
|
|
||||||
TimeRange.WEEK -> "$week/$year"
|
|
||||||
TimeRange.MONTH -> "$month/$year"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.d(TAG, "startDaysUnix: ${Date(daysToUnix(startDays) * 1000)}. endDaysUnix: ${Date(daysToUnix(endDays) * 1000)}")
|
|
||||||
|
|
||||||
cb(GraphState(events, dayCounter, startUnix, endUnix, startSpan, endSpan))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun showGraph() {
|
|
||||||
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 -> showSleepPatternBarGraph(state) }
|
|
||||||
GraphType.MEDICINE_EVENTS -> prepareGraph(LunaEvent.Type.MEDICINE) { state -> showMedicineBarGraph(state) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private interface SpinnerItemSelected {
|
|
||||||
fun call(newValue: String?)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun setupSpinner(
|
|
||||||
currentValue: String,
|
|
||||||
spinnerId: Int,
|
|
||||||
arrayId: Int,
|
|
||||||
arrayValuesId: Int,
|
|
||||||
callback: SpinnerItemSelected
|
|
||||||
) {
|
|
||||||
val arrayValues = resources.getStringArray(arrayValuesId)
|
|
||||||
val spinner = findViewById<Spinner>(spinnerId)
|
|
||||||
val spinnerAdapter =
|
|
||||||
ArrayAdapter.createFromResource(this, arrayId, R.layout.statistics_spinner_item)
|
|
||||||
|
|
||||||
//Log.d(TAG, "spinner ${arrayValues.indexOf(currentValue)} (${arrayValues.joinToString { it }})")
|
|
||||||
|
|
||||||
spinner.adapter = spinnerAdapter
|
|
||||||
spinner.setSelection(arrayValues.indexOf(currentValue))
|
|
||||||
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
|
||||||
var check = 0
|
|
||||||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, pos: Int, id: Long) {
|
|
||||||
if (pos >= arrayValues.size) {
|
|
||||||
Toast.makeText(
|
|
||||||
this@StatisticsActivity,
|
|
||||||
"pos out of bounds: $arrayValues", Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (check++ > 0) {
|
|
||||||
callback.call(arrayValues[pos])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onNothingSelected(parent: AdapterView<*>?) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val TAG = "StatisticsActivity"
|
|
||||||
|
|
||||||
// 15 min steps
|
|
||||||
val SLEEP_PATTERN_GRANULARITY = 15 * 60
|
|
||||||
|
|
||||||
// color gradient
|
|
||||||
val SLEEP_PATTERN_COLORS = arrayOf(
|
|
||||||
"#FFFFFF".toColorInt(), "#EEF5F7".toColorInt(), "#DDEBEF".toColorInt(), "#CCE2E7".toColorInt(),
|
|
||||||
"#BBD8DF".toColorInt(), "#AACED7".toColorInt(), "#99C4CF".toColorInt(), "#88BAC7".toColorInt(),
|
|
||||||
"#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
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
dateTime.set(Calendar.HOUR, 0)
|
|
||||||
dateTime.set(Calendar.MINUTE, 0)
|
|
||||||
dateTime.set(Calendar.SECOND, 0)
|
|
||||||
return dateTime.time.time / 1000
|
|
||||||
}
|
|
||||||
|
|
||||||
fun unixToWeeks(seconds: Long): Int {
|
|
||||||
//val dateTime = Calendar.getInstance()
|
|
||||||
dateTime.time = Date(seconds * 1000)
|
|
||||||
val years = dateTime.get(Calendar.YEAR)
|
|
||||||
val weeks = dateTime.get(Calendar.WEEK_OF_YEAR)
|
|
||||||
return 52 * years + weeks
|
|
||||||
}
|
|
||||||
|
|
||||||
fun weeksToUnix(weeks: Int): Long {
|
|
||||||
//val dateTime = Calendar.getInstance()
|
|
||||||
dateTime.time = Date(0)
|
|
||||||
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)
|
|
||||||
dateTime.set(Calendar.MINUTE, 0)
|
|
||||||
dateTime.set(Calendar.SECOND, 0)
|
|
||||||
return dateTime.time.time / 1000
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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)
|
|
||||||
dateTime.set(Calendar.HOUR, 0)
|
|
||||||
dateTime.set(Calendar.MINUTE, 0)
|
|
||||||
dateTime.set(Calendar.SECOND, 0)
|
|
||||||
return dateTime.time.time / 1000
|
|
||||||
}
|
|
||||||
|
|
||||||
fun appendToFloatArray(array: FloatArray, vararg values: Float): FloatArray {
|
|
||||||
// create new array
|
|
||||||
val newArray = FloatArray(array.size + values.size)
|
|
||||||
// copy old values
|
|
||||||
for (i in array.indices) {
|
|
||||||
newArray[i] = array[i]
|
|
||||||
}
|
|
||||||
// add new values
|
|
||||||
for (i in values.indices) {
|
|
||||||
newArray[array.size + i] = values[i]
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
val yVals = value.yVals
|
|
||||||
if (yVals != null) {
|
|
||||||
val y = yVals.fold(0F) { acc, next -> acc + next }
|
|
||||||
val yVals = yVals.joinToString { it.toString() }
|
|
||||||
Log.d(TAG, "value: ${value.x} $y ($yVals)")
|
|
||||||
} else {
|
|
||||||
Log.d(TAG, "value: ${value.x} ${value.y}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -58,12 +58,7 @@ class LunaEventRecyclerAdapter: RecyclerView.Adapter<LunaEventRecyclerAdapter.Lu
|
|||||||
LunaEvent.Type.NOTE -> item.notes
|
LunaEvent.Type.NOTE -> item.notes
|
||||||
else -> item.getRowItemTitle(context)
|
else -> item.getRowItemTitle(context)
|
||||||
}
|
}
|
||||||
val endTime = if (item.type == LunaEvent.Type.SLEEP) {
|
holder.time.text = DateUtils.formatTimeAgo(context, item.getEndTime())
|
||||||
item.quantity + item.time
|
|
||||||
} else {
|
|
||||||
item.time
|
|
||||||
}
|
|
||||||
holder.time.text = DateUtils.formatTimeAgo(context, endTime)
|
|
||||||
var quantityText = numericUtils.formatEventQuantity(item)
|
var quantityText = numericUtils.formatEventQuantity(item)
|
||||||
|
|
||||||
// if the event is weight, show difference with the last one
|
// if the event is weight, show difference with the last one
|
||||||
|
|||||||
@@ -115,6 +115,18 @@ class LunaEvent: Comparable<LunaEvent> {
|
|||||||
return getDialogMessage(context, type)
|
return getDialogMessage(context, type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getStartTime(): Long {
|
||||||
|
return time
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getEndTime(): Long {
|
||||||
|
return if (type == Type.SLEEP) {
|
||||||
|
time + quantity
|
||||||
|
} else {
|
||||||
|
time
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun toJson(): JSONObject {
|
fun toJson(): JSONObject {
|
||||||
return jo
|
return jo
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,11 +56,11 @@ class DateUtils {
|
|||||||
return builder.toString()
|
return builder.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (years > 0) {
|
if (years != 0L) {
|
||||||
return format(years, days, R.string.year_ago, R.string.years_ago, R.string.day_ago, R.string.days_ago)
|
return format(years, days, R.string.year_ago, R.string.years_ago, R.string.day_ago, R.string.days_ago)
|
||||||
} else if (days > 0) {
|
} else if (days != 0L) {
|
||||||
return format(days, hours, R.string.day_ago, R.string.days_ago, R.string.hour_ago, R.string.hours_ago)
|
return format(days, hours, R.string.day_ago, R.string.days_ago, R.string.hour_ago, R.string.hours_ago)
|
||||||
} else if (hours > 0) {
|
} else if (hours != 0L) {
|
||||||
return format(hours, minutes, R.string.hour_ago, R.string.hours_ago, R.string.minute_ago, R.string.minutes_ago)
|
return format(hours, minutes, R.string.hour_ago, R.string.hours_ago, R.string.minute_ago, R.string.minutes_ago)
|
||||||
} else {
|
} else {
|
||||||
return format(minutes, seconds, R.string.minute_ago, R.string.minute_ago, R.string.second_ago, R.string.seconds_ago)
|
return format(minutes, seconds, R.string.minute_ago, R.string.minute_ago, R.string.second_ago, R.string.seconds_ago)
|
||||||
|
|||||||
@@ -1,50 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:orientation="vertical"
|
|
||||||
android:padding="10dp">
|
|
||||||
|
|
||||||
<FrameLayout
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_weight="1">
|
|
||||||
|
|
||||||
<com.github.mikephil.charting.charts.HorizontalBarChart
|
|
||||||
android:id="@+id/bar_chart"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent" />
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/no_data"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:visibility="gone"
|
|
||||||
android:gravity="center"
|
|
||||||
android:text="@string/statistics_no_data"/>
|
|
||||||
|
|
||||||
</FrameLayout>
|
|
||||||
|
|
||||||
<LinearLayout
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_weight="0"
|
|
||||||
android:orientation="horizontal">
|
|
||||||
|
|
||||||
<Spinner
|
|
||||||
android:id="@+id/graph_type_selection"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:gravity="center"/>
|
|
||||||
|
|
||||||
<Spinner
|
|
||||||
android:id="@+id/time_range_selection"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:gravity="center"/>
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
@@ -7,46 +7,23 @@
|
|||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/dialog_date_picker_begin"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:layout_marginTop="10dp"/>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/dialog_date_duration"
|
android:id="@+id/dialog_date_duration"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="20dp"
|
||||||
android:textSize="20sp"
|
android:textSize="20sp"
|
||||||
android:text="💤"/>
|
android:text="💤"/>
|
||||||
|
|
||||||
<LinearLayout
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:gravity="center"
|
|
||||||
android:layout_marginTop="20dp"
|
|
||||||
android:layout_marginHorizontal="10dp"
|
|
||||||
android:orientation="horizontal">
|
|
||||||
|
|
||||||
<Button
|
|
||||||
android:id="@+id/dialog_date_duration_minus5"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="-5"/>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
android:id="@+id/dialog_date_duration_now"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="@string/now"/>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
android:id="@+id/dialog_date_duration_plus5"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="+5"/>
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/dialog_date_picker"
|
android:id="@+id/dialog_date_picker_end"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_gravity="center"
|
android:layout_gravity="center"
|
||||||
|
|||||||
@@ -10,17 +10,7 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
<!-- Buttons are inserted dynamically -->
|
||||||
<TextView
|
|
||||||
android:id="@+id/button_statistics"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:padding="10dp"
|
|
||||||
android:background="@drawable/dropdown_list_item_background"
|
|
||||||
style="@style/OverflowMenuText"
|
|
||||||
android:text="📊 Statistics"/>
|
|
||||||
|
|
||||||
<!-- Other buttons are inserted dynamically -->
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
@@ -6,34 +6,4 @@
|
|||||||
<item>@string/amount_normal</item>
|
<item>@string/amount_normal</item>
|
||||||
<item>@string/amount_plenty</item>
|
<item>@string/amount_plenty</item>
|
||||||
</string-array>
|
</string-array>
|
||||||
|
|
||||||
<string-array name="StatisticsTypeLabels">
|
|
||||||
<item>@string/statistics_bottle_sum</item>
|
|
||||||
<item>@string/statistics_bottle_events</item>
|
|
||||||
<item>@string/statistics_sleep_sum</item>
|
|
||||||
<item>@string/statistics_sleep_events</item>
|
|
||||||
<item>@string/statistics_sleep_pattern</item>
|
|
||||||
<item>@string/statistics_medicine_events</item>
|
|
||||||
</string-array>
|
|
||||||
|
|
||||||
<string-array name="StatisticsTypeValues">
|
|
||||||
<item>BOTTLE_SUM</item>
|
|
||||||
<item>BOTTLE_EVENTS</item>
|
|
||||||
<item>SLEEP_SUM</item>
|
|
||||||
<item>SLEEP_EVENTS</item>
|
|
||||||
<item>SLEEP_PATTERN</item>
|
|
||||||
<item>MEDICINE_EVENTS</item>
|
|
||||||
</string-array>
|
|
||||||
|
|
||||||
<string-array name="StatisticsTimeLabels">
|
|
||||||
<item>Day</item>
|
|
||||||
<item>Week</item>
|
|
||||||
<item>Month</item>
|
|
||||||
</string-array>
|
|
||||||
|
|
||||||
<string-array name="StatisticsTimeValues">
|
|
||||||
<item>DAY</item>
|
|
||||||
<item>WEEK</item>
|
|
||||||
<item>MONTH</item>
|
|
||||||
</string-array>
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -88,9 +88,6 @@
|
|||||||
<string name="no_connection_go_to_settings">Settings</string>
|
<string name="no_connection_go_to_settings">Settings</string>
|
||||||
<string name="no_connection_retry">Retry</string>
|
<string name="no_connection_retry">Retry</string>
|
||||||
|
|
||||||
<string name="statistics_title">Statistics</string>
|
|
||||||
<string name="statistics_no_data">No Data</string>
|
|
||||||
|
|
||||||
<string name="settings_dynamic_menu">Dynamic Menu</string>
|
<string name="settings_dynamic_menu">Dynamic Menu</string>
|
||||||
<string name="settings_dynamic_menu_desc">Populate the header menu with the most used events.</string>
|
<string name="settings_dynamic_menu_desc">Populate the header menu with the most used events.</string>
|
||||||
<string name="settings_title">Settings</string>
|
<string name="settings_title">Settings</string>
|
||||||
@@ -141,13 +138,6 @@
|
|||||||
<string name="measurement_unit_temperature_base_imperial" translatable="false">°F</string>
|
<string name="measurement_unit_temperature_base_imperial" translatable="false">°F</string>
|
||||||
<string name="measurement_unit_temperature_base_metric" translatable="false">°C</string>
|
<string name="measurement_unit_temperature_base_metric" translatable="false">°C</string>
|
||||||
|
|
||||||
<string name="statistics_bottle_events">Bottle Events</string>
|
|
||||||
<string name="statistics_bottle_sum">Bottle Per Day</string>
|
|
||||||
<string name="statistics_medicine_events">Medicine Events</string>
|
|
||||||
<string name="statistics_sleep_sum">Sleep Per Day</string>
|
|
||||||
<string name="statistics_sleep_events">Sleep Events</string>
|
|
||||||
<string name="statistics_sleep_pattern">Sleep Pattern</string>
|
|
||||||
|
|
||||||
<string name="row_luna_event_description">Description</string>
|
<string name="row_luna_event_description">Description</string>
|
||||||
<string name="row_luna_event_quantity">Qty</string>
|
<string name="row_luna_event_quantity">Qty</string>
|
||||||
<string name="row_luna_event_time">Time</string>
|
<string name="row_luna_event_time">Time</string>
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ lifecycleRuntimeKtx = "2.6.1"
|
|||||||
activityCompose = "1.8.0"
|
activityCompose = "1.8.0"
|
||||||
composeBom = "2024.04.01"
|
composeBom = "2024.04.01"
|
||||||
appcompat = "1.7.0"
|
appcompat = "1.7.0"
|
||||||
mpandroidchart = "v4.2.2"
|
|
||||||
mpandroidchartVersion = "v3.1.0"
|
|
||||||
recyclerview = "1.3.2"
|
recyclerview = "1.3.2"
|
||||||
material = "1.12.0"
|
material = "1.12.0"
|
||||||
sardineAndroid = "v0.9"
|
sardineAndroid = "v0.9"
|
||||||
@@ -33,8 +31,6 @@ androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit
|
|||||||
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
|
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
|
||||||
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
|
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
|
||||||
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
|
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
|
||||||
mpandroidchart = { module = "com.github.PhilJay:MPAndroidChart", version.ref = "mpandroidchart" }
|
|
||||||
mpandroidchart-vv310 = { module = "com.github.PhilJay:MPAndroidChart", version.ref = "mpandroidchartVersion" }
|
|
||||||
sardine-android = { module = "com.github.thegrizzlylabs:sardine-android", version.ref = "sardineAndroid" }
|
sardine-android = { module = "com.github.thegrizzlylabs:sardine-android", version.ref = "sardineAndroid" }
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
|
|||||||
Reference in New Issue
Block a user