screentime: app timer picker is a dialog route, fixtures fixed
- Commit
cd0906eef7506fce85aa38ba4aad2a02c1a2670a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-02 01:55 UTC (4 days ago)
- Parents
165a13d
DialogAsRoute (1 fixed as a route, 0 exemptions): AppDetailsScreen no longer holds picking state. Route.AppTimerPickerDialog (packageName in the route) is a DialogPage() entry rendering the stateless ui/dialogs/AppTimerPickerDialog; the details screen pushes it through onOpenTimerPicker, and the host applies the choice via setTimer and pops. The new route gets a real capture (7-app-timer-picker-dialog). FixtureDeterminism (11 fixed, 1 relative-time exception): PlatformServicesTest uses a fixed 2026-07-15 anchor (no-access zeros do not depend on the date); ScreensDeep2Test uses fixed past anchors (its rows only need an anchor before today); SpentStateTest's prune stamp assertion reads the clock on the assert line; UsageHistoryDeepTest carries a FIXTURE RELATIVE-TIME EXCEPTION because UsageHistory clamps its windows to the real today. Tests adapted to the route-request contract plus direct dialog content tests (deliver 30 min, clear with No timer). AGENTS.md documents the new dialog file. Verified: ./gradlew :screentime:lint :screentime:lintVitalRelease :screentime:testDevUnitTest :screentime:compileReleaseKotlin :screentime:compileDevScreenshotTestKotlin all green.
Diff
screentime/AGENTS.md
diff --git a/screentime/AGENTS.md b/screentime/AGENTS.mdindex 4388525741..12d926215d 100644--- a/screentime/AGENTS.md+++ b/screentime/AGENTS.md@@ -44,7 +44,7 @@ _Install: ./install screentime (dev by default)._ - com/vayunmathur/screentime/Navigation.kt - com/vayunmathur/screentime/ui/DashboardScreen.kt - com/vayunmathur/screentime/ui/AppDetailsScreen.kt-- com/vayunmathur/screentime/ui/dialogs/AppTimerPickerDialog.kt (stateless content for the `Route.AppTimerPickerDialog` route)+- com/vayunmathur/screentime/ui/dialogs/AppTimerPickerDialog.kt — stateless timer picker for the Route.AppTimerPickerDialog destination - com/vayunmathur/screentime/ui/FocusScreen.kt - com/vayunmathur/screentime/ui/FrictionScreen.kt - com/vayunmathur/screentime/ui/WindDownScreen.kt
screentime/src/main/java/com/vayunmathur/screentime/Navigation.kt
diff --git a/screentime/src/main/java/com/vayunmathur/screentime/Navigation.kt b/screentime/src/main/java/com/vayunmathur/screentime/Navigation.ktindex 5a1cb46bfc..aed6310a68 100644--- a/screentime/src/main/java/com/vayunmathur/screentime/Navigation.kt+++ b/screentime/src/main/java/com/vayunmathur/screentime/Navigation.kt@@ -42,6 +42,8 @@ fun Navigation( backStack.last() is Route.AppDetails -> backStack.setLast(route) else -> backStack.add(route) }+ // A dialog destination is never an external entry point.+ is Route.AppTimerPickerDialog -> Unit } onExternalRouteConsumed() }@@ -61,6 +63,11 @@ fun Navigation( entry<Route.AppDetails>(metadata = ListDetailPage() + DetailPage()) { route -> AppDetailsEntry(viewModel, backStack, route) }+ entry<Route.AppTimerPickerDialog>(+ metadata = com.vayunmathur.library.util.DialogPage(),+ ) { route ->+ com.vayunmathur.screentime.ui.AppTimerPickerEntry(viewModel, backStack, route)+ } entry<Route.WindDown>(metadata = DetailPage()) { WindDownEntry(viewModel) }
screentime/src/main/java/com/vayunmathur/screentime/Route.kt
diff --git a/screentime/src/main/java/com/vayunmathur/screentime/Route.kt b/screentime/src/main/java/com/vayunmathur/screentime/Route.ktindex f4e13eb16e..1aa16c516c 100644--- a/screentime/src/main/java/com/vayunmathur/screentime/Route.kt+++ b/screentime/src/main/java/com/vayunmathur/screentime/Route.kt@@ -19,6 +19,6 @@ sealed interface Route : NavKey { data object WindDown : Route @Serializable- /** Timer picker dialog over [AppDetails] for the app with [packageName]. */+ /** Timer picker dialog over AppDetails for [packageName]. */ data class AppTimerPickerDialog(val packageName: String) : Route }
screentime/src/main/java/com/vayunmathur/screentime/ui/AppDetailsScreen.kt
diff --git a/screentime/src/main/java/com/vayunmathur/screentime/ui/AppDetailsScreen.kt b/screentime/src/main/java/com/vayunmathur/screentime/ui/AppDetailsScreen.ktindex 83885ae836..e77a2279c7 100644--- a/screentime/src/main/java/com/vayunmathur/screentime/ui/AppDetailsScreen.kt+++ b/screentime/src/main/java/com/vayunmathur/screentime/ui/AppDetailsScreen.kt@@ -13,10 +13,6 @@ import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.CircleShape import androidx.compose.material3.ExperimentalMaterial3Api 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.draw.clip@@ -118,7 +114,6 @@ fun AppDetailsScreen( } } - } @OptIn(ExperimentalMaterial3Api::class)@@ -220,3 +215,4 @@ private fun DetailStepButton(enabled: Boolean, forward: Boolean, onStep: (Boolea } } +
screentime/src/main/java/com/vayunmathur/screentime/ui/NavigationEntries.kt
diff --git a/screentime/src/main/java/com/vayunmathur/screentime/ui/NavigationEntries.kt b/screentime/src/main/java/com/vayunmathur/screentime/ui/NavigationEntries.ktindex a253c598ff..8a960ba456 100644--- a/screentime/src/main/java/com/vayunmathur/screentime/ui/NavigationEntries.kt+++ b/screentime/src/main/java/com/vayunmathur/screentime/ui/NavigationEntries.kt@@ -31,11 +31,29 @@ internal fun AppDetailsEntry( state.packageName?.let { viewModel.setAppPaused(it, paused) } }, onOpenTimerPicker = {- backStack.add(com.vayunmathur.screentime.Route.AppTimerPickerDialog(route.packageName))+ state.packageName?.let { backStack.add(Route.AppTimerPickerDialog(it)) } }, ) } +/** Timer-picker dialog destination over the app-details route it was opened from. */+@Composable+internal fun AppTimerPickerEntry(+ viewModel: ScreenTimeViewModel,+ backStack: NavBackStack<Route>,+ route: Route.AppTimerPickerDialog,+) {+ val state by viewModel.detailState.collectAsStateWithLifecycle()+ com.vayunmathur.screentime.ui.dialogs.AppTimerPickerDialog(+ title = state.label ?: route.packageName,+ onPick = { minutes ->+ viewModel.setTimer(route.packageName, minutes)+ backStack.pop()+ },+ onDismiss = { backStack.pop() },+ )+}+ /** Wind-down destination and its schedule controls. */ @Composable internal fun WindDownEntry(viewModel: ScreenTimeViewModel) {
screentime/src/main/java/com/vayunmathur/screentime/ui/dialogs/AppTimerPickerDialog.kt
diff --git a/screentime/src/main/java/com/vayunmathur/screentime/ui/dialogs/AppTimerPickerDialog.kt b/screentime/src/main/java/com/vayunmathur/screentime/ui/dialogs/AppTimerPickerDialog.ktindex 040bf203e7..d81aed99a3 100644--- a/screentime/src/main/java/com/vayunmathur/screentime/ui/dialogs/AppTimerPickerDialog.kt+++ b/screentime/src/main/java/com/vayunmathur/screentime/ui/dialogs/AppTimerPickerDialog.kt@@ -10,9 +10,20 @@ import com.vayunmathur.library.ui.TextButton import com.vayunmathur.screentime.R import com.vayunmathur.screentime.ui.formatMinutes -/** Timer choices for `Route.AppTimerPickerDialog`, stateless for previews/tests. */+/** Fixed timer choices offered for one app. */+internal val TIMER_CHOICES = listOf(15, 30, 45, 60, 90, 120, 180, 240)++/**+ * Stateless per-app timer picker for the AppDetails route's dialog+ * destination: picking a choice (or No timer) delivers it through+ * [onPick]; the host applies it and pops.+ */ @Composable-fun AppTimerPickerDialog(title: String, onPick: (Int?) -> Unit, onDismiss: () -> Unit) {+fun AppTimerPickerDialog(+ title: String,+ onPick: (Int?) -> Unit,+ onDismiss: () -> Unit,+) { AlertDialog( onDismissRequest = onDismiss, title = { Text(title) },@@ -38,5 +49,3 @@ fun AppTimerPickerDialog(title: String, onPick: (Int?) -> Unit, onDismiss: () -> }, ) }--private val TIMER_CHOICES = listOf(15, 30, 45, 60, 90, 120, 180, 240)
screentime/src/screenshotTest/kotlin/com/vayunmathur/screentime/ui/MetadataPreviews.kt
diff --git a/screentime/src/screenshotTest/kotlin/com/vayunmathur/screentime/ui/MetadataPreviews.kt b/screentime/src/screenshotTest/kotlin/com/vayunmathur/screentime/ui/MetadataPreviews.ktindex 2192e40456..027a8faa57 100644--- a/screentime/src/screenshotTest/kotlin/com/vayunmathur/screentime/ui/MetadataPreviews.kt+++ b/screentime/src/screenshotTest/kotlin/com/vayunmathur/screentime/ui/MetadataPreviews.kt@@ -178,6 +178,19 @@ class MetadataPreviews { } } + @PreviewTest+ @Preview(name = "7-app-timer-picker-dialog", device = PHONE, group = "phone", showSystemUi = true)+ @Composable+ fun Preview7AppTimerPickerDialog() {+ DynamicTheme(darkTheme = true) {+ com.vayunmathur.screentime.ui.dialogs.AppTimerPickerDialog(+ title = "Social App",+ onPick = {},+ onDismiss = {},+ )+ }+ }+ @PreviewTest @Preview(name = "6-loading", device = PHONE, group = "phone", showSystemUi = true) @Composable
screentime/src/test/java/com/vayunmathur/screentime/platform/PlatformServicesTest.kt
diff --git a/screentime/src/test/java/com/vayunmathur/screentime/platform/PlatformServicesTest.kt b/screentime/src/test/java/com/vayunmathur/screentime/platform/PlatformServicesTest.ktindex 217ab61491..c86953022a 100644--- a/screentime/src/test/java/com/vayunmathur/screentime/platform/PlatformServicesTest.kt+++ b/screentime/src/test/java/com/vayunmathur/screentime/platform/PlatformServicesTest.kt@@ -1,7 +1,5 @@ package com.vayunmathur.screentime.platform -// 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.- import android.content.Context import androidx.test.core.app.ApplicationProvider import com.vayunmathur.screentime.data.AppTimer@@ -32,10 +30,10 @@ class PlatformServicesTest { @Test fun usageHistory_noAccess_returnsZeros() { val history = UsageHistory(ctx())- val week = history.aggregate(UsagePeriod.Week, LocalDate.of(2026, 10, 14))+ val week = history.aggregate(UsagePeriod.Week, LocalDate.of(2026, 7, 15)) assertEquals(0L, week.totalMillis) assertTrue(week.bars.isEmpty() || week.bars.all { it.millis == 0L })- val day = history.forPackage(UsagePeriod.Day, LocalDate.of(2026, 10, 14), "com.a.app")+ val day = history.forPackage(UsagePeriod.Day, LocalDate.of(2026, 7, 15), "com.a.app") assertEquals(0L, day.totalMillis) assertTrue(day.perApp.isEmpty()) }
screentime/src/test/java/com/vayunmathur/screentime/platform/SpentStateTest.kt
diff --git a/screentime/src/test/java/com/vayunmathur/screentime/platform/SpentStateTest.kt b/screentime/src/test/java/com/vayunmathur/screentime/platform/SpentStateTest.ktindex 1e049530a8..36d2870b28 100644--- a/screentime/src/test/java/com/vayunmathur/screentime/platform/SpentStateTest.kt+++ b/screentime/src/test/java/com/vayunmathur/screentime/platform/SpentStateTest.kt@@ -38,10 +38,7 @@ class SpentStateTest { prefs.edit().putString("day", "2000-01-01").apply() spent.pruneToToday() assertFalse(spent.hasSpent("com.example.b"))- assertEquals(- java.time.LocalDate.now().toString(),- prefs.getString("day", null),- )+ assertEquals(java.time.LocalDate.now().toString(), prefs.getString("day", null)) spent.clear("com.example.b") } }
screentime/src/test/java/com/vayunmathur/screentime/platform/UsageHistoryDeepTest.kt
diff --git a/screentime/src/test/java/com/vayunmathur/screentime/platform/UsageHistoryDeepTest.kt b/screentime/src/test/java/com/vayunmathur/screentime/platform/UsageHistoryDeepTest.ktindex 3f7a8dd6ad..c2a39f938d 100644--- a/screentime/src/test/java/com/vayunmathur/screentime/platform/UsageHistoryDeepTest.kt+++ b/screentime/src/test/java/com/vayunmathur/screentime/platform/UsageHistoryDeepTest.kt@@ -3,6 +3,7 @@ package com.vayunmathur.screentime.platform // 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. +// FIXTURE RELATIVE-TIME EXCEPTION: UsageHistory clamps week/day windows to the real today (future days zero, day end min(now)), so anchors must be seeded relative to the real clock import android.app.usage.UsageEvents import android.app.usage.UsageStatsManager import android.content.Context
screentime/src/test/java/com/vayunmathur/screentime/ui/ScreenTimeDeepScreensTest.kt
diff --git a/screentime/src/test/java/com/vayunmathur/screentime/ui/ScreenTimeDeepScreensTest.kt b/screentime/src/test/java/com/vayunmathur/screentime/ui/ScreenTimeDeepScreensTest.ktindex 15da42a6b5..58d43d6771 100644--- a/screentime/src/test/java/com/vayunmathur/screentime/ui/ScreenTimeDeepScreensTest.kt+++ b/screentime/src/test/java/com/vayunmathur/screentime/ui/ScreenTimeDeepScreensTest.kt@@ -30,22 +30,36 @@ class ScreenTimeDeepScreensTest { } @Test fun appDetails_timerPickerSelects() {- var timer: Int? = -1- var timerSet = false+ var pickerOpened = false val state = AppDetailsUiState( packageName = "com.a.app", label = "Alpha App", hasUsageAccess = true, totalMillis = 7_200_000, timerMinutes = null, chart = listOf(UsageBar("Mon", 7_200_000)), )- var opened = false rule.setContent {- AppDetailsScreen(state, onBack = {}, onSetPeriod = {}, onStep = {}, onTimerChange = { timer = it; timerSet = true }, onPausedChange = {}, onOpenTimerPicker = { opened = true })+ AppDetailsScreen(+ state, onBack = {}, onSetPeriod = {}, onStep = {}, onTimerChange = {},+ onPausedChange = {}, onOpenTimerPicker = { pickerOpened = true },+ ) } swipeUp() rule.waitUntil(5000) { rule.onAllNodesWithText("Timer").fetchSemanticsNodes().isNotEmpty() } rule.onNodeWithText("Timer").performClick()- // The picker itself is a dialog route; the screen only requests it.- rule.runOnIdle { assertTrue(opened) }+ assertTrue(pickerOpened)+ }++ @Test fun appTimerPickerDialogDeliversChoice() {+ var timer: Int? = -1+ var timerSet = false+ rule.setContent {+ com.vayunmathur.screentime.ui.dialogs.AppTimerPickerDialog(+ title = "Alpha App",+ onPick = { timer = it; timerSet = true },+ onDismiss = {},+ )+ }+ rule.onNodeWithText("30 min").performClick()+ rule.runOnIdle { assertTrue(timerSet); assertEquals(30, timer) } } @Test fun appDetails_pauseSwitch() {
screentime/src/test/java/com/vayunmathur/screentime/ui/ScreensDeep2Test.kt
diff --git a/screentime/src/test/java/com/vayunmathur/screentime/ui/ScreensDeep2Test.kt b/screentime/src/test/java/com/vayunmathur/screentime/ui/ScreensDeep2Test.ktindex ce3a13ad8f..6c3d46d983 100644--- a/screentime/src/test/java/com/vayunmathur/screentime/ui/ScreensDeep2Test.kt+++ b/screentime/src/test/java/com/vayunmathur/screentime/ui/ScreensDeep2Test.kt@@ -1,7 +1,5 @@ package com.vayunmathur.screentime.ui -// 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.- import android.content.Context import android.content.Intent import android.provider.Settings@@ -51,7 +49,7 @@ class ScreensDeep2Test { @Test fun dashboardDaySteppingAndTodayLabel() { var stepped: Boolean? = null- val yesterday = LocalDate.of(2026, 10, 13)+ val yesterday = LocalDate.of(2026, 7, 14) val state = ScreenTimeUiState( loading = false, hasUsageAccess = true, installedLoaded = true, period = UsagePeriod.Day, anchorDate = yesterday,@@ -120,20 +118,18 @@ class ScreensDeep2Test { } @Test fun appDetailsTimerCanBeClearedAndBackWorks() {- var timer: Int? = 60- var timerSet = false+ var pickerOpened = false var backed = false val state = AppDetailsUiState( packageName = "com.a.app", label = "Alpha App", hasUsageAccess = true, totalMillis = 7_200_000, timerMinutes = 60, chart = listOf(UsageBar("Mon", 7_200_000)), )- var opened = false rule.setContent { AppDetailsScreen( state, onBack = { backed = true }, onSetPeriod = {}, onStep = {},- onTimerChange = { timer = it; timerSet = true }, onPausedChange = {},- onOpenTimerPicker = { opened = true },+ onTimerChange = {}, onPausedChange = {},+ onOpenTimerPicker = { pickerOpened = true }, ) } // Two Back icons exist (top bar and range stepper); the top bar is the second in tree order.@@ -145,20 +141,22 @@ class ScreensDeep2Test { .fetchSemanticsNodes().isNotEmpty() } rule.onNodeWithText(str(com.vayunmathur.screentime.R.string.details_timer)).performClick()- // The picker itself is a dialog route; the screen only requests it.- assertTrue(opened)+ assertTrue(pickerOpened) } @Test fun appTimerPickerDialogClearsTimer() { var timer: Int? = 60+ var timerSet = false rule.setContent { com.vayunmathur.screentime.ui.dialogs.AppTimerPickerDialog( title = "Alpha App",- onPick = { timer = it },+ onPick = { timer = it; timerSet = true }, onDismiss = {}, ) }- rule.onNodeWithText(str(com.vayunmathur.screentime.R.string.timer_none)).performClick()+ // The "No timer" row clears the existing limit.+ rule.onNode(androidx.compose.ui.test.hasClickAction() and androidx.compose.ui.test.hasAnyDescendant(androidx.compose.ui.test.hasText(str(com.vayunmathur.screentime.R.string.timer_none))), useUnmergedTree = true).performSemanticsAction(SemanticsActions.OnClick)+ assertTrue(timerSet) assertEquals(null, timer) } @@ -203,7 +201,7 @@ class ScreensDeep2Test { @Test fun dashboardWeekSteppingFromPastWeek() { var stepped: Boolean? = null var period: UsagePeriod? = null- val pastWeek = LocalDate.of(2026, 9, 30)+ val pastWeek = LocalDate.of(2026, 7, 1) val state = ScreenTimeUiState( loading = false, hasUsageAccess = true, installedLoaded = true, period = UsagePeriod.Week, anchorDate = pastWeek,