Modern Apps git

← Modern-Apps.git

things: device pickers and scale reset are dialog routes

Commit
d4f5738a0b24559674beb1d6951a35c3d590e911
Author
Vayun Mathur <[email protected]>
When
2026-12-02 01:23 UTC (4 days ago)
Parents
dc3e7ef
DialogAsRoute (4): DevicesPage no longer holds a picking
state or renders DevicePickerDialog locally, and the scale
card no longer holds confirm-reset state. The picker moved
to stateless ui/dialogs/DevicePickerDialog (plus
ResetScaleConfirmDialog), the page/card request
Route.BottlePickerDialog, Route.ScalePickerDialog and
Route.ResetScaleConfirmDialog through new callbacks wired
in Navigation, and the DialogPage() entries feed the
pickers from DeviceController's live scan state (read
inside the entries, as Navigation's caching note requires)
and apply picks/resets through the controller callbacks.

Screenshot: 5-bottle-picker-dialog, 6-scale-picker-dialog
and 7-reset-scale-confirm-dialog in MetadataPreviews render
the dialog contents with fixed device literals.

FixtureDeterminism (8, dev lint): the two unseeded
UUID.randomUUID() fake characteristics are fixed UUIDs and
the Health Connect Instant.now() arguments are a fixed
epoch-milli instant (explicit parameters, no assertions on
the values); DeviceSessionsTest carries a FIXTURE
RELATIVE-TIME EXCEPTION because DeviceController clamps and
stamps readings against its real wall clock.

Tests adapted to the route-request contract (page/card
tests assert the requests; new tests pick and confirm in
the dialogs themselves).

Verified from a clean build dir with --no-build-cache:
./gradlew :things:lint :things:lintVitalRelease
:things:testDevUnitTest :things:compileReleaseKotlin all
green (160 tests, 0 failures).

Diff

things/AGENTS.md
diff --git a/things/AGENTS.md b/things/AGENTS.mdindex 459a8bac12..1fe8e5e6a0 100644--- a/things/AGENTS.md+++ b/things/AGENTS.md@@ -33,6 +33,7 @@ _Install: ./install things (dev by default)._ ### ui/ — home, devices, permissions - com/vayunmathur/things/ui/HomePage.kt - com/vayunmathur/things/ui/DevicesPage.kt+- com/vayunmathur/things/ui/dialogs/DevicePickerDialog.kt + ResetScaleConfirmDialog.kt (stateless content for the `Route.BottlePickerDialog`/`ScalePickerDialog`/`ResetScaleConfirmDialog` routes) - com/vayunmathur/things/ui/PermissionsPage.kt - com/vayunmathur/things/ui/PermissionsRationaleActivity.kt 
things/src/main/java/com/vayunmathur/things/Navigation.kt
diff --git a/things/src/main/java/com/vayunmathur/things/Navigation.kt b/things/src/main/java/com/vayunmathur/things/Navigation.ktindex ca871cc81c..7507563f12 100644--- a/things/src/main/java/com/vayunmathur/things/Navigation.kt+++ b/things/src/main/java/com/vayunmathur/things/Navigation.kt@@ -1,7 +1,11 @@ package com.vayunmathur.things  import androidx.compose.runtime.Composable+import com.vayunmathur.library.util.DialogPage import com.vayunmathur.library.util.MainNavigation+import com.vayunmathur.things.platform.requestScaleReset+import com.vayunmathur.things.ui.dialogs.DevicePickerDialog+import com.vayunmathur.things.ui.dialogs.ResetScaleConfirmDialog import com.vayunmathur.library.util.SiblingPage import com.vayunmathur.library.util.rememberNavBackStack import com.vayunmathur.things.platform.BleManager@@ -37,6 +41,7 @@ fun Navigation(                 onForgetBottle = onForgetBottle,                 onForgetScale = onForgetScale,                 onOpenDevices = { backStack.add(Route.Devices) },+                onOpenResetConfirm = { backStack.add(Route.ResetScaleConfirmDialog) },             )         }         entry<Route.Devices>(SiblingPage()) {@@ -50,6 +55,41 @@ fun Navigation(                 } else {                     null                 },+                onOpenBottlePicker = { backStack.add(Route.BottlePickerDialog) },+                onOpenScalePicker = { backStack.add(Route.ScalePickerDialog) },+            )+        }+        entry<Route.BottlePickerDialog>(metadata = DialogPage()) {+            DevicePickerDialog(+                searching = DeviceController.scanning.value,+                entries = DeviceController.discoveredDevices.map { it.name to it.address },+                onPick = { address ->+                    DeviceController.discoveredDevices.firstOrNull { it.address == address }+                        ?.let(onDeviceClick)+                    backStack.pop()+                },+                onDismiss = { backStack.pop() },+            )+        }+        entry<Route.ScalePickerDialog>(metadata = DialogPage()) {+            DevicePickerDialog(+                searching = DeviceController.scaleScanning.value,+                entries = DeviceController.scaleDevices.map { it.name to it.address },+                onPick = { address ->+                    DeviceController.scaleDevices.firstOrNull { it.address == address }+                        ?.let(onScaleDeviceClick)+                    backStack.pop()+                },+                onDismiss = { backStack.pop() },+            )+        }+        entry<Route.ResetScaleConfirmDialog>(metadata = DialogPage()) {+            ResetScaleConfirmDialog(+                onConfirm = {+                    DeviceController.requestScaleReset()+                    backStack.pop()+                },+                onDismiss = { backStack.pop() },             )         }     }
things/src/main/java/com/vayunmathur/things/Route.kt
diff --git a/things/src/main/java/com/vayunmathur/things/Route.kt b/things/src/main/java/com/vayunmathur/things/Route.ktindex 33e6c34266..9dee592f9d 100644--- a/things/src/main/java/com/vayunmathur/things/Route.kt+++ b/things/src/main/java/com/vayunmathur/things/Route.kt@@ -13,4 +13,16 @@ sealed interface Route : NavKey {     /** Hydration home destination. */     @Serializable     data object Home : Route++    /** Bottle picker dialog over [Devices], fed by the live BLE scan. */+    @Serializable+    data object BottlePickerDialog : Route++    /** Scale picker dialog over [Devices], fed by the live BLE scan. */+    @Serializable+    data object ScalePickerDialog : Route++    /** Scale-reset confirmation dialog over [Home]. */+    @Serializable+    data object ResetScaleConfirmDialog : Route }
things/src/main/java/com/vayunmathur/things/ui/DevicesEntry.kt
diff --git a/things/src/main/java/com/vayunmathur/things/ui/DevicesEntry.kt b/things/src/main/java/com/vayunmathur/things/ui/DevicesEntry.ktindex cfd61031bd..5dc5b9b5ec 100644--- a/things/src/main/java/com/vayunmathur/things/ui/DevicesEntry.kt+++ b/things/src/main/java/com/vayunmathur/things/ui/DevicesEntry.kt@@ -12,6 +12,8 @@ internal fun DevicesEntry(     onScaleScanClick: () -> Unit,     onScaleDeviceClick: (ScaleBleManager.ScaleBleDevice) -> Unit,     onNavigateBack: (() -> Unit)?,+    onOpenBottlePicker: () -> Unit = {},+    onOpenScalePicker: () -> Unit = {}, ) {     DevicesPage(         scanning = DeviceController.scanning.value,@@ -23,5 +25,7 @@ internal fun DevicesEntry(         onScaleScanClick = onScaleScanClick,         onScaleDeviceClick = onScaleDeviceClick,         onNavigateBack = onNavigateBack,+        onOpenBottlePicker = onOpenBottlePicker,+        onOpenScalePicker = onOpenScalePicker,     ) }
things/src/main/java/com/vayunmathur/things/ui/DevicesPage.kt
diff --git a/things/src/main/java/com/vayunmathur/things/ui/DevicesPage.kt b/things/src/main/java/com/vayunmathur/things/ui/DevicesPage.ktindex 06da241f87..bc0e1c6083 100644--- a/things/src/main/java/com/vayunmathur/things/ui/DevicesPage.kt+++ b/things/src/main/java/com/vayunmathur/things/ui/DevicesPage.kt@@ -1,32 +1,21 @@ package com.vayunmathur.things.ui -import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding-import androidx.compose.foundation.lazy.LazyColumn-import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.runtime.Composable-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp-import com.vayunmathur.library.ui.AlertDialog import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.DesktopMaxWidthContainer import com.vayunmathur.library.ui.ExperimentalMaterial3Api-import com.vayunmathur.library.ui.ListItem import com.vayunmathur.library.ui.MaterialTheme-import com.vayunmathur.library.ui.R as UiR import com.vayunmathur.library.ui.Text-import com.vayunmathur.library.ui.TextButton import com.vayunmathur.things.R import com.vayunmathur.things.platform.BleManager import com.vayunmathur.things.platform.ScaleBleManager@@ -50,9 +39,9 @@ fun DevicesPage(     onScaleScanClick: () -> Unit,     onScaleDeviceClick: (ScaleBleManager.ScaleBleDevice) -> Unit,     onNavigateBack: (() -> Unit)?,+    onOpenBottlePicker: () -> Unit = {},+    onOpenScalePicker: () -> Unit = {}, ) {-    // Which picker is open, if any. Null closes both.-    var picking by remember { mutableStateOf<String?>(null) }      AppScaffold(         title = stringResource(R.string.add_device),@@ -68,8 +57,8 @@ fun DevicesPage(         ) {             Button(                 onClick = {-                    picking = PICK_BOTTLE                     onScanClick()+                    onOpenBottlePicker()                 },                 modifier = Modifier                     .fillMaxWidth()@@ -79,8 +68,8 @@ fun DevicesPage(             }             Button(                 onClick = {-                    picking = PICK_SCALE                     onScaleScanClick()+                    onOpenScalePicker()                 },                 modifier = Modifier                     .fillMaxWidth()@@ -92,62 +81,6 @@ fun DevicesPage(         }     } -    when (picking) {-        PICK_BOTTLE -> DevicePickerDialog(-            searching = scanning,-            entries = discoveredDevices.map { it.name to it.address },-            onPick = { address ->-                discoveredDevices.firstOrNull { it.address == address }?.let(onDeviceClick)-                picking = null-            },-            onDismiss = { picking = null },-        )-        PICK_SCALE -> DevicePickerDialog(-            searching = scaleScanning,-            entries = scaleDevices.map { it.name to it.address },-            onPick = { address ->-                scaleDevices.firstOrNull { it.address == address }?.let(onScaleDeviceClick)-                picking = null-            },-            onDismiss = { picking = null },-        )-    }-} -@Composable-private fun DevicePickerDialog(-    searching: Boolean,-    entries: List<Pair<String, String>>,-    onPick: (String) -> Unit,-    onDismiss: () -> Unit,-) {-    AlertDialog(-        onDismissRequest = onDismiss,-        title = { Text(stringResource(R.string.select_device)) },-        text = {-            if (entries.isEmpty()) {-                Text(-                    stringResource(if (searching) R.string.searching else R.string.no_devices_found),-                    style = MaterialTheme.typography.bodyMedium,-                )-            } else {-                LazyColumn(modifier = Modifier.heightIn(max = 320.dp)) {-                    itemsIndexed(entries) { _, entry ->-                        val (name, address) = entry-                        ListItem(-                            headlineContent = { Text(name) },-                            supportingContent = { Text(address) },-                            modifier = Modifier.clickable { onPick(address) },-                        )-                    }-                }-            }-        },-        confirmButton = {-            TextButton(onClick = onDismiss) { Text(stringResource(UiR.string.cancel)) }-        },-    ) } -private const val PICK_BOTTLE = "bottle"-private const val PICK_SCALE = "scale"
things/src/main/java/com/vayunmathur/things/ui/HomeEntry.kt
diff --git a/things/src/main/java/com/vayunmathur/things/ui/HomeEntry.kt b/things/src/main/java/com/vayunmathur/things/ui/HomeEntry.ktindex 9cbbbd93bb..efe8c93a35 100644--- a/things/src/main/java/com/vayunmathur/things/ui/HomeEntry.kt+++ b/things/src/main/java/com/vayunmathur/things/ui/HomeEntry.kt@@ -10,6 +10,7 @@ internal fun HomeEntry(     onForgetBottle: () -> Unit,     onForgetScale: () -> Unit,     onOpenDevices: () -> Unit,+    onOpenResetConfirm: () -> Unit = {}, ) {     HomePage(         bottlePaired = DeviceController.bottlePaired.value,@@ -49,6 +50,7 @@ internal fun HomeEntry(         onForgetScale = onForgetScale,         onResetScale = { DeviceController.requestScaleReset() },         onOpenDevices = onOpenDevices,+        onOpenResetConfirm = onOpenResetConfirm,     ) } 
things/src/main/java/com/vayunmathur/things/ui/HomePage.kt
diff --git a/things/src/main/java/com/vayunmathur/things/ui/HomePage.kt b/things/src/main/java/com/vayunmathur/things/ui/HomePage.ktindex 2bdac83960..98bea0bd9b 100644--- a/things/src/main/java/com/vayunmathur/things/ui/HomePage.kt+++ b/things/src/main/java/com/vayunmathur/things/ui/HomePage.kt@@ -9,10 +9,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.runtime.Composable-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource@@ -22,7 +19,6 @@ import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.Card import com.vayunmathur.library.ui.Checkbox-import com.vayunmathur.library.ui.ConfirmDialog import com.vayunmathur.library.ui.DesktopMaxWidthContainer import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.FilterChip@@ -31,7 +27,6 @@ import com.vayunmathur.library.ui.IconWidgets import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.OutlinedButton import com.vayunmathur.library.ui.OutlinedTextField-import com.vayunmathur.library.ui.R as UiR import com.vayunmathur.library.ui.Text import com.vayunmathur.things.R import com.vayunmathur.things.platform.DeviceController.LinkState@@ -75,6 +70,7 @@ fun HomePage(     onForgetScale: () -> Unit,     onResetScale: () -> Unit,     onOpenDevices: () -> Unit,+    onOpenResetConfirm: () -> Unit = {}, ) {     AppScaffold(         title = stringResource(R.string.app_name),@@ -123,6 +119,7 @@ fun HomePage(                         onAthleteChange = onScaleAthleteChange,                         onForget = onForgetScale,                         onReset = onResetScale,+                        onOpenResetConfirm = onOpenResetConfirm,                     )                 }             }@@ -253,8 +250,8 @@ private fun ScaleCard(     onAthleteChange: (Boolean) -> Unit,     onForget: () -> Unit,     onReset: () -> Unit,+    onOpenResetConfirm: () -> Unit = {}, ) {-    var confirmReset by remember { mutableStateOf(false) }     Card(modifier = Modifier.fillMaxWidth()) {         Column(modifier = Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {             Text(stringResource(R.string.scale_title), style = MaterialTheme.typography.titleSmall)@@ -303,27 +300,12 @@ private fun ScaleCard(                 stringResource(R.string.syncs_to_health_connect),                 style = MaterialTheme.typography.bodySmall             )-            OutlinedButton(onClick = { confirmReset = true }) {+            OutlinedButton(onClick = onOpenResetConfirm) {                 Text(stringResource(R.string.scale_reset_users))             }             OutlinedButton(onClick = onForget) { Text(stringResource(R.string.forget_device)) }         }     } -    if (confirmReset) {-        ConfirmDialog(-            title = stringResource(R.string.scale_reset_title),-            message = stringResource(R.string.scale_reset_message),-            confirmLabel = stringResource(UiR.string.reset),-            // Without an explicit dismiss label the dialog renders no cancel button, which is a-            // poor way to guard something destructive.-            dismissLabel = stringResource(UiR.string.cancel),-            destructive = true,-            onConfirm = {-                confirmReset = false-                onReset()-            },-            onDismiss = { confirmReset = false },-        )-    }+ }
things/src/main/java/com/vayunmathur/things/ui/dialogs/DevicePickerDialog.kt
diff --git a/things/src/main/java/com/vayunmathur/things/ui/dialogs/DevicePickerDialog.kt b/things/src/main/java/com/vayunmathur/things/ui/dialogs/DevicePickerDialog.ktnew file mode 100644index 0000000000..24f057bb9c--- /dev/null+++ b/things/src/main/java/com/vayunmathur/things/ui/dialogs/DevicePickerDialog.kt@@ -0,0 +1,53 @@+package com.vayunmathur.things.ui.dialogs++import androidx.compose.foundation.clickable+import androidx.compose.foundation.layout.heightIn+import androidx.compose.foundation.lazy.LazyColumn+import androidx.compose.foundation.lazy.itemsIndexed+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.AlertDialog+import com.vayunmathur.library.ui.ListItem+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.TextButton+import com.vayunmathur.library.ui.R as UiR+import com.vayunmathur.things.R++/** BLE device picker for the picker dialog routes, stateless for previews/tests. */+@Composable+fun DevicePickerDialog(+    searching: Boolean,+    entries: List<Pair<String, String>>,+    onPick: (String) -> Unit,+    onDismiss: () -> Unit,+) {+    AlertDialog(+        onDismissRequest = onDismiss,+        title = { Text(stringResource(R.string.select_device)) },+        text = {+            if (entries.isEmpty()) {+                Text(+                    stringResource(if (searching) R.string.searching else R.string.no_devices_found),+                    style = MaterialTheme.typography.bodyMedium,+                )+            } else {+                LazyColumn(modifier = Modifier.heightIn(max = 320.dp)) {+                    itemsIndexed(entries) { _, entry ->+                        val (name, address) = entry+                        ListItem(+                            headlineContent = { Text(name) },+                            supportingContent = { Text(address) },+                            modifier = Modifier.clickable { onPick(address) },+                        )+                    }+                }+            }+        },+        confirmButton = {+            TextButton(onClick = onDismiss) { Text(stringResource(UiR.string.cancel)) }+        },+    )+}
things/src/main/java/com/vayunmathur/things/ui/dialogs/ResetScaleConfirmDialog.kt
diff --git a/things/src/main/java/com/vayunmathur/things/ui/dialogs/ResetScaleConfirmDialog.kt b/things/src/main/java/com/vayunmathur/things/ui/dialogs/ResetScaleConfirmDialog.ktnew file mode 100644index 0000000000..4e181566f9--- /dev/null+++ b/things/src/main/java/com/vayunmathur/things/ui/dialogs/ResetScaleConfirmDialog.kt@@ -0,0 +1,23 @@+package com.vayunmathur.things.ui.dialogs++import androidx.compose.runtime.Composable+import androidx.compose.ui.res.stringResource+import com.vayunmathur.library.ui.ConfirmDialog+import com.vayunmathur.library.ui.R as UiR+import com.vayunmathur.things.R++/** Scale-reset confirmation for `Route.ResetScaleConfirmDialog`, stateless for previews/tests. */+@Composable+fun ResetScaleConfirmDialog(onConfirm: () -> Unit, onDismiss: () -> Unit) {+    ConfirmDialog(+        title = stringResource(R.string.scale_reset_title),+        message = stringResource(R.string.scale_reset_message),+        confirmLabel = stringResource(UiR.string.reset),+        // Without an explicit dismiss label the dialog renders no cancel button, which is a+        // poor way to guard something destructive.+        dismissLabel = stringResource(UiR.string.cancel),+        destructive = true,+        onConfirm = onConfirm,+        onDismiss = onDismiss,+    )+}
things/src/screenshotTest/kotlin/com/vayunmathur/things/ui/MetadataPreviews.kt
diff --git a/things/src/screenshotTest/kotlin/com/vayunmathur/things/ui/MetadataPreviews.kt b/things/src/screenshotTest/kotlin/com/vayunmathur/things/ui/MetadataPreviews.ktindex a1b0ee7cdc..8a5dd43ca8 100644--- a/things/src/screenshotTest/kotlin/com/vayunmathur/things/ui/MetadataPreviews.kt+++ b/things/src/screenshotTest/kotlin/com/vayunmathur/things/ui/MetadataPreviews.kt@@ -6,6 +6,8 @@ import com.android.tools.screenshot.PreviewTest import com.vayunmathur.library.ui.DynamicTheme import com.vayunmathur.things.platform.DeviceController.LinkState import com.vayunmathur.things.platform.Sex+import com.vayunmathur.things.ui.dialogs.DevicePickerDialog+import com.vayunmathur.things.ui.dialogs.ResetScaleConfirmDialog  /** Phone-shaped, roughly 1080x2340 at xxhdpi — comfortably above the F-Droid minimum. */ private const val PHONE = "spec:width=411dp,height=891dp,dpi=420"@@ -125,4 +127,41 @@ class MetadataPreviews {             )         }     }++    @PreviewTest+    @Preview(name = "5-bottle-picker-dialog", device = PHONE, group = "phone")+    @Composable+    fun Preview5BottlePickerDialog() {+        DynamicTheme(darkTheme = true) {+            DevicePickerDialog(+                searching = false,+                entries = listOf("Smart Bottle" to "AA:BB:CC:DD:EE:FF"),+                onPick = {},+                onDismiss = {},+            )+        }+    }++    @PreviewTest+    @Preview(name = "6-scale-picker-dialog", device = PHONE, group = "phone")+    @Composable+    fun Preview6ScalePickerDialog() {+        DynamicTheme(darkTheme = true) {+            DevicePickerDialog(+                searching = true,+                entries = emptyList(),+                onPick = {},+                onDismiss = {},+            )+        }+    }++    @PreviewTest+    @Preview(name = "7-reset-scale-confirm-dialog", device = PHONE, group = "phone")+    @Composable+    fun Preview7ResetScaleConfirmDialog() {+        DynamicTheme(darkTheme = true) {+            ResetScaleConfirmDialog(onConfirm = {}, onDismiss = {})+        }+    } }
things/src/test/java/com/vayunmathur/things/platform/BleManagerGattTest.kt
diff --git a/things/src/test/java/com/vayunmathur/things/platform/BleManagerGattTest.kt b/things/src/test/java/com/vayunmathur/things/platform/BleManagerGattTest.ktindex b18960b411..0c0a53d399 100644--- a/things/src/test/java/com/vayunmathur/things/platform/BleManagerGattTest.kt+++ b/things/src/test/java/com/vayunmathur/things/platform/BleManagerGattTest.kt@@ -222,7 +222,7 @@ class BleManagerGattTest {         flush()         assertEquals(19, DeviceController.waterTempC.value)         // Wrong char and short values are ignored.-        val other = BluetoothGattCharacteristic(UUID.randomUUID(), 0, 0)+        val other = BluetoothGattCharacteristic(UUID.fromString("00000000-0000-0000-0000-000000000001"), 0, 0)         cb.onCharacteristicChanged(gatt, other, rt)         cb.onCharacteristicChanged(gatt, char, byteArrayOf(0x52))         flush()
things/src/test/java/com/vayunmathur/things/platform/DeviceSessionsTest.kt
diff --git a/things/src/test/java/com/vayunmathur/things/platform/DeviceSessionsTest.kt b/things/src/test/java/com/vayunmathur/things/platform/DeviceSessionsTest.ktindex 01cbf51bde..71744b51e5 100644--- a/things/src/test/java/com/vayunmathur/things/platform/DeviceSessionsTest.kt+++ b/things/src/test/java/com/vayunmathur/things/platform/DeviceSessionsTest.kt@@ -1,4 +1,5 @@ package com.vayunmathur.things.platform+// FIXTURE RELATIVE-TIME EXCEPTION: drink/scale readings are clamped and stamped against the real wall clock inside DeviceController, so future/current reading fixtures must be seeded relative to that clock  // FIXTURE RELATIVE-TIME EXCEPTION: timestamps are seeded relative to the wall clock the code under test reads internally, so the fresh/expired/relative-time branch under test keeps its meaning; a fixed past instant would silently change that branch. 
things/src/test/java/com/vayunmathur/things/platform/HealthConnectHelperTest.kt
diff --git a/things/src/test/java/com/vayunmathur/things/platform/HealthConnectHelperTest.kt b/things/src/test/java/com/vayunmathur/things/platform/HealthConnectHelperTest.ktindex 0ccbe855aa..fac387980f 100644--- a/things/src/test/java/com/vayunmathur/things/platform/HealthConnectHelperTest.kt+++ b/things/src/test/java/com/vayunmathur/things/platform/HealthConnectHelperTest.kt@@ -81,7 +81,7 @@ class HealthConnectHelperTest {     @Test     fun `write hydration swallows provider failures`() = runBlocking {         val fake = FakeHealthConnect(insertError = IllegalStateException("provider died"))-        HealthConnectHelper.writeHydration(fake.client, Instant.now(), 0.2)+        HealthConnectHelper.writeHydration(fake.client, Instant.ofEpochMilli(1_788_277_018_000L), 0.2)         // Reaching here without throwing is the assertion the production catch promises.         assertTrue(fake.inserted.isEmpty())     }@@ -121,7 +121,7 @@ class HealthConnectHelperTest {         val fake = FakeHealthConnect()         HealthConnectHelper.writeBodyComposition(             client = fake.client,-            instant = Instant.now(),+            instant = Instant.ofEpochMilli(1_788_277_018_000L),             weightKg = 80.0,             bodyFatPct = 0.0,             leanMassKg = null,@@ -139,7 +139,7 @@ class HealthConnectHelperTest {     fun `write body composition swallows provider failures`() = runBlocking {         val fake = FakeHealthConnect(insertError = RuntimeException("ipc failed"))         HealthConnectHelper.writeBodyComposition(-            fake.client, Instant.now(), 75.0, 20.0, 60.0, 3.0, 40.0, 1700,+            fake.client, Instant.ofEpochMilli(1_788_277_018_000L), 75.0, 20.0, 60.0, 3.0, 40.0, 1700,         )         assertTrue(fake.inserted.isEmpty())     }
things/src/test/java/com/vayunmathur/things/platform/ScaleBleConnectionTest.kt
diff --git a/things/src/test/java/com/vayunmathur/things/platform/ScaleBleConnectionTest.kt b/things/src/test/java/com/vayunmathur/things/platform/ScaleBleConnectionTest.ktindex 2e04073ddf..c93c9b3aa2 100644--- a/things/src/test/java/com/vayunmathur/things/platform/ScaleBleConnectionTest.kt+++ b/things/src/test/java/com/vayunmathur/things/platform/ScaleBleConnectionTest.kt@@ -260,7 +260,7 @@ class ScaleBleConnectionTest {         flush()         assertEquals(75.0, DeviceController.scaleRealtimeWeight.value)         // Other chars and empty values are ignored; the deprecated overload works too.-        val other = BluetoothGattCharacteristic(UUID.randomUUID(), 0, 0)+        val other = BluetoothGattCharacteristic(UUID.fromString("00000000-0000-0000-0000-000000000001"), 0, 0)         cb.onCharacteristicChanged(gatt, other, pkt)         cb.onCharacteristicChanged(gatt, notifyChar, byteArrayOf())         notifyChar.setValue(pkt)
things/src/test/java/com/vayunmathur/things/ui/ThingsPagesExtraTest.kt
diff --git a/things/src/test/java/com/vayunmathur/things/ui/ThingsPagesExtraTest.kt b/things/src/test/java/com/vayunmathur/things/ui/ThingsPagesExtraTest.ktindex ca6ea17fcb..a191f5eb4d 100644--- a/things/src/test/java/com/vayunmathur/things/ui/ThingsPagesExtraTest.kt+++ b/things/src/test/java/com/vayunmathur/things/ui/ThingsPagesExtraTest.kt@@ -45,6 +45,7 @@ class ThingsPagesExtraTest {         onForgetScale: () -> Unit = {},         onResetScale: () -> Unit = {},         onOpenDevices: () -> Unit = {},+        onOpenResetConfirm: () -> Unit = {},     ) {         composeRule.setContent {             HomePage(@@ -65,6 +66,7 @@ class ThingsPagesExtraTest {                 onForgetBottle = onForgetBottle, onForgetScale = onForgetScale,                 onResetScale = onResetScale,                 onOpenDevices = onOpenDevices,+                onOpenResetConfirm = onOpenResetConfirm,             )         }     }@@ -123,77 +125,95 @@ class ThingsPagesExtraTest {      @Test     fun scaleResetDialogConfirmAndDismiss() {-        var resets = 0-        home(scalePaired = true, onResetScale = { resets++ })+        var opened = false+        home(scalePaired = true, onOpenResetConfirm = { opened = true })         composeRule.onNodeWithText(str(R.string.scale_reset_users)).performScrollTo().performClick()+        // The confirmation itself is a dialog route; the card only requests it.+        assertTrue(opened)+    }++    @Test+    fun resetScaleConfirmDialogConfirms() {+        var resets = 0+        composeRule.setContent {+            com.vayunmathur.things.ui.dialogs.ResetScaleConfirmDialog(+                onConfirm = { resets++ },+                onDismiss = {},+            )+        }         composeRule.onNodeWithText(str(R.string.scale_reset_title)).assertIsDisplayed()-        // Dismiss first: no reset.-        composeRule.onNodeWithText(str(com.vayunmathur.library.ui.R.string.cancel)).performClick()-        assertEquals(0, resets)-        // Reopen and confirm.-        composeRule.onNodeWithText(str(R.string.scale_reset_users)).performScrollTo().performClick()         composeRule.onNodeWithText(str(com.vayunmathur.library.ui.R.string.reset)).performClick()         assertEquals(1, resets)     }      @Test     fun devicesPickerListsAndPicksBottle() {-        var picked: BleManager.BleDevice? = null         var scanned = false+        var opened = false         val device = BleManager.BleDevice("WaterH Bottle", "00:11:22:33:44:55")         composeRule.setContent {             DevicesPage(                 scanning = true, discoveredDevices = listOf(device),                 scaleScanning = false, scaleDevices = emptyList(),-                onScanClick = { scanned = true }, onDeviceClick = { picked = it },+                onScanClick = { scanned = true }, onDeviceClick = {},                 onScaleScanClick = {}, onScaleDeviceClick = {},                 onNavigateBack = null,+                onOpenBottlePicker = { opened = true },             )         }         composeRule.onNodeWithText(str(R.string.connect_new_bottle)).performClick()         assertTrue(scanned)+        // The picker itself is a dialog route; the page only requests it.+        assertTrue(opened)+    }++    @Test+    fun devicePickerDialogPicksBottle() {+        var picked: String? = null+        composeRule.setContent {+            com.vayunmathur.things.ui.dialogs.DevicePickerDialog(+                searching = true,+                entries = listOf("WaterH Bottle" to "00:11:22:33:44:55"),+                onPick = { picked = it },+                onDismiss = {},+            )+        }         composeRule.onNodeWithText(str(R.string.select_device)).assertIsDisplayed()         composeRule.onNodeWithText("WaterH Bottle").assertIsDisplayed()         composeRule.onNodeWithText("00:11:22:33:44:55").performClick()-        assertEquals(device, picked)+        assertEquals("00:11:22:33:44:55", picked)     }      @Test     fun devicesPickerListsAndPicksScaleAndDismisses() {-        var picked: ScaleBleManager.ScaleBleDevice? = null+        var opened = false         val device = ScaleBleManager.ScaleBleDevice("QN-Scale", "00:11:22:33:44:66")         composeRule.setContent {             DevicesPage(                 scanning = false, discoveredDevices = emptyList(),                 scaleScanning = false, scaleDevices = listOf(device),                 onScanClick = {}, onDeviceClick = {},-                onScaleScanClick = {}, onScaleDeviceClick = { picked = it },+                onScaleScanClick = {}, onScaleDeviceClick = {},                 onNavigateBack = {},+                onOpenScalePicker = { opened = true },             )         }         composeRule.onNodeWithText(str(R.string.connect_new_scale)).performClick()-        composeRule.onNodeWithText("QN-Scale").performClick()-        assertEquals(device, picked)-        // Reopen and dismiss with Cancel: nothing more is picked.-        composeRule.onNodeWithText(str(R.string.connect_new_scale)).performClick()-        composeRule.onNodeWithText(str(com.vayunmathur.library.ui.R.string.cancel)).performClick()-        composeRule.onNodeWithText(str(R.string.connect_new_scale)).assertIsDisplayed()+        assertTrue(opened)     }      @Test     fun devicesPickerSearchingAndEmptyStates() {         composeRule.setContent {-            DevicesPage(-                scanning = true, discoveredDevices = emptyList(),-                scaleScanning = false, scaleDevices = emptyList(),-                onScanClick = {}, onDeviceClick = {},-                onScaleScanClick = {}, onScaleDeviceClick = {},-                onNavigateBack = {},+            com.vayunmathur.things.ui.dialogs.DevicePickerDialog(+                searching = true,+                entries = emptyList(),+                onPick = {},+                onDismiss = {},             )         }-        composeRule.onNodeWithText(str(R.string.connect_new_bottle)).performClick()         composeRule.onNodeWithText(str(R.string.searching)).assertIsDisplayed()-        composeRule.onNodeWithText(str(com.vayunmathur.library.ui.R.string.cancel)).performClick()+        composeRule.onNodeWithText(str(com.vayunmathur.library.ui.R.string.cancel)).assertIsDisplayed()     }      @Test