Skip to content

Commit

Permalink
refactor: Line breaks for time separations linked to TimeFrame (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-0410 authored Dec 23, 2024
1 parent 45e50b8 commit 16a8503
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
14 changes: 14 additions & 0 deletions app/src/main/java/com/geeksville/mesh/model/MetricsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ enum class TimeFrame(
}
}

/**
* Used to detect a significant time separation between [Telemetry]s.
*/
fun timeThreshold(): Long {
return when (this.ordinal) {
TWENTY_FOUR_HOURS.ordinal ->
TimeUnit.HOURS.toSeconds(6)
FORTY_EIGHT_HOURS.ordinal ->
TimeUnit.HOURS.toSeconds(12)
else ->
TimeUnit.DAYS.toSeconds(1)
}
}

/**
* Calculates the needed [Dp] depending on the amount of time being plotted.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ private fun DeviceMetricsChart(
path = path,
oldestTime = oldest.time,
timeRange = timeDiff,
width = width
width = width,
timeThreshold = selectedTime.timeThreshold()
) { i ->
val telemetry = telemetries.getOrNull(i) ?: telemetries.last()
val ratio = telemetry.deviceMetrics.batteryLevel / MAX_PERCENT_VALUE
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/com/geeksville/mesh/util/GraphUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawContext
import com.geeksville.mesh.TelemetryProtos.Telemetry
import java.util.concurrent.TimeUnit

private const val TIME_SEPARATION_THRESHOLD = 2L

object GraphUtil {

Expand Down Expand Up @@ -59,6 +56,7 @@ object GraphUtil {
* @param path [Path] that will be used to draw
* @param timeRange The time range for the data set
* @param width of the [DrawContext]
* @param timeThreshold to determine significant breaks in time between [Telemetry]s
* @param calculateY (`index`) -> `y` coordinate
*/
fun createPath(
Expand All @@ -68,6 +66,7 @@ object GraphUtil {
oldestTime: Int,
timeRange: Int,
width: Float,
timeThreshold: Long,
calculateY: (Int) -> Float
): Int {
var i = index
Expand All @@ -78,7 +77,7 @@ object GraphUtil {
val nextTelemetry = telemetries.getOrNull(i + 1) ?: telemetries.last()

/* Check to see if we have a significant time break between telemetries. */
if (nextTelemetry.time - telemetry.time > TimeUnit.HOURS.toSeconds(TIME_SEPARATION_THRESHOLD)) {
if (nextTelemetry.time - telemetry.time > timeThreshold) {
i++
break
}
Expand Down

0 comments on commit 16a8503

Please sign in to comment.