Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/src/main/java/com/podometer/service/StepTrackingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.util.concurrent.atomic.AtomicInteger
import kotlin.coroutines.cancellation.CancellationException
import javax.inject.Inject

Expand Down Expand Up @@ -105,6 +106,13 @@ class StepTrackingService : Service() {
private var classifierJob: Job? = null
private var orphanCleanupJob: Job? = null

/**
* Accumulates step counts between classifier evaluations so each
* [SensorWindow] can record how many steps occurred during its 30-second
* interval. Reset to zero each time the classifier persists a window.
*/
private val windowStepAccumulator = AtomicInteger(0)

/**
* Timestamp (ms) of the most recently processed step event. Used by
* [collectStepEvents] to spread burst step timestamps evenly over the
Expand Down Expand Up @@ -266,6 +274,7 @@ class StepTrackingService : Service() {
stepFrequencyTracker.recordStep(ts)
}
lastStepEventMs = nowMs
windowStepAccumulator.addAndGet(delta)

// Skip step accumulation while cycling is active.
if (cyclingSessionManager.isStepCountingPaused) {
Expand Down Expand Up @@ -372,14 +381,15 @@ class StepTrackingService : Service() {
val stepFreq = stepFrequencyTracker.computeStepFrequency(now)

// Persist raw sensor window for retroactive recomputation (7-day retention).
val stepsInWindow = windowStepAccumulator.getAndSet(0)
serviceScope.launch {
try {
sensorWindowRepository.insertWindow(
SensorWindow(
timestamp = now,
magnitudeVariance = features.magnitudeVariance,
stepFrequencyHz = stepFreq,
stepCount = 0, // per-window step count not tracked; reserved for future use
stepCount = stepsInWindow,
),
)
} catch (e: Exception) {
Expand Down
Loading