measure: actually clear the level calibration on clear
- Commit
02ca9d15b62debc34076094dc32f03d83c1794c3- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:15 UTC (28 days ago)
- Parents
9d525a8
Symptom: after clearing the level zero and restarting the app, the level still reported itself calibrated and showed NaN angles. Root cause: clearCalibration persisted Double.NaN under the three offset keys. DataStoreUtils.getDouble returns a stored NaN as a non-null Double, so loadPreferenceValuesImpl treated the level as calibrated and TiltSensor subtracted NaN from every reading. Fix: remove the offset keys on clear (DataStoreUtils.removeKeys), and in the loader treat a stored NaN as absent so installs already carrying the sentinel recover on next launch. Verification: :measure:compileDevKotlin and :measure:testDevUnitTest green in the final batched run.
Diff
measure/src/main/java/com/vayunmathur/measure/platform/MeasureSensorOps.kt
diff --git a/measure/src/main/java/com/vayunmathur/measure/platform/MeasureSensorOps.kt b/measure/src/main/java/com/vayunmathur/measure/platform/MeasureSensorOps.ktindex f76f197562..96ce37c620 100644--- a/measure/src/main/java/com/vayunmathur/measure/platform/MeasureSensorOps.kt+++ b/measure/src/main/java/com/vayunmathur/measure/platform/MeasureSensorOps.kt@@ -35,9 +35,11 @@ internal fun MeasureViewModel.loadPreferenceValuesImpl(): PreferenceValues { diagnostics = ds.getBoolean(KEY_DIAGNOSTICS, false), haptics = ds.getBoolean(KEY_HAPTICS, true), keepOn = ds.getBoolean(KEY_KEEP_SCREEN_ON, true),- levelPitch = ds.getDouble(KEY_LEVEL_PITCH),- levelRoll = ds.getDouble(KEY_LEVEL_ROLL),- levelEdge = ds.getDouble(KEY_LEVEL_EDGE),+ // Guard against NaN sentinels written by older versions on+ // calibration clear: NaN is a stored value, not an absence.+ levelPitch = ds.getDouble(KEY_LEVEL_PITCH)?.takeUnless { it.isNaN() },+ levelRoll = ds.getDouble(KEY_LEVEL_ROLL)?.takeUnless { it.isNaN() },+ levelEdge = ds.getDouble(KEY_LEVEL_EDGE)?.takeUnless { it.isNaN() }, ) }
measure/src/main/java/com/vayunmathur/measure/platform/MeasureViewModel.kt
diff --git a/measure/src/main/java/com/vayunmathur/measure/platform/MeasureViewModel.kt b/measure/src/main/java/com/vayunmathur/measure/platform/MeasureViewModel.ktindex a6b4c7ff6b..3863bfb3c9 100644--- a/measure/src/main/java/com/vayunmathur/measure/platform/MeasureViewModel.kt+++ b/measure/src/main/java/com/vayunmathur/measure/platform/MeasureViewModel.kt@@ -103,9 +103,11 @@ class MeasureViewModel(app: Application) : AndroidViewModel(app), levelState.value = levelState.value.copy(isCalibrated = false) settingsState.value = settingsState.value.copy(levelCalibrated = false) viewModelScope.launch {- ds.setDouble(KEY_LEVEL_PITCH, Double.NaN)- ds.setDouble(KEY_LEVEL_ROLL, Double.NaN)- ds.setDouble(KEY_LEVEL_EDGE, Double.NaN)+ // Remove the keys rather than writing NaN sentinels: a stored+ // NaN reads back as a non-null Double, so the loader treated+ // the level as calibrated forever and subtracted NaN offsets+ // from every reading after a restart.+ ds.removeKeys(listOf(KEY_LEVEL_PITCH, KEY_LEVEL_ROLL, KEY_LEVEL_EDGE)) } }