Modern Apps git

← Modern-Apps.git

screentime: clear observer and spent state on timer delete

Commit
80bdcd9faad0b9984b6f4fbd1427f540dda9913b
Author
Vayun Mathur <[email protected]>
When
2026-11-08 02:20 UTC (28 days ago)
Parents
7aa34b0
Fix stale enforcement after an app timer is deleted.

Root cause: deleting the Room timer row left the UsageStats observer registered and left today's spent mark set. Coordinator.reconcile only registers observers, so the orphaned observer stayed armed; re-adding a timer later the same day could pause the app immediately from stale spent state.

Add SpentState.clear and call it with AppTimers.unregister on the timer-delete path, off the main thread, before reconciling. This keeps observer registration and spent state consistent with the stored timer rows.

User-visible effect: deleting a timer fully disarms it; a later re-added timer starts from current usage, not from a stale spent flag.

Verified with: ~/code/ma-cov/gslot ./gradlew :screentime:testDevUnitTest; BUILD SUCCESSFUL.

Diff

screentime/src/main/java/com/vayunmathur/screentime/platform/Coordinator.kt
diff --git a/screentime/src/main/java/com/vayunmathur/screentime/platform/Coordinator.kt b/screentime/src/main/java/com/vayunmathur/screentime/platform/Coordinator.ktindex a69978fa2d..cdc6744333 100644--- a/screentime/src/main/java/com/vayunmathur/screentime/platform/Coordinator.kt+++ b/screentime/src/main/java/com/vayunmathur/screentime/platform/Coordinator.kt@@ -88,6 +88,12 @@ class SpentState(context: Context) {      fun hasSpent(packageName: String): Boolean = packageName in spent() +    /** Forget one package's spent mark, e.g. when its timer is deleted. */+    fun clear(packageName: String) {+        pruneToToday()+        prefs.edit().putStringSet(KEY_SPENT, spent() - packageName).apply()+    }+     /** Drop yesterday's trips. This is the daily reset; there is no other. */     fun pruneToToday() {         if (prefs.getString(KEY_DAY, null) == today()) return
screentime/src/main/java/com/vayunmathur/screentime/platform/ScreenTimeViewModel.kt
diff --git a/screentime/src/main/java/com/vayunmathur/screentime/platform/ScreenTimeViewModel.kt b/screentime/src/main/java/com/vayunmathur/screentime/platform/ScreenTimeViewModel.ktindex 9e97b9f328..7c59d867d8 100644--- a/screentime/src/main/java/com/vayunmathur/screentime/platform/ScreenTimeViewModel.kt+++ b/screentime/src/main/java/com/vayunmathur/screentime/platform/ScreenTimeViewModel.kt@@ -222,6 +222,14 @@ class ScreenTimeViewModel(app: Application) : AndroidViewModel(app) {         viewModelScope.launch {             if (minutes == null) {                 rules.timer(packageName)?.let { rules.delete(it) }+                // Deleting the row is not enough: the platform observer stays armed and+                // the spent mark stays set, so re-adding a timer later the same day can+                // pause the app instantly on stale state. Clean up both off the main+                // thread before reconciling.+                withContext(Dispatchers.IO) {+                    AppTimers(getApplication()).unregister(packageName)+                    SpentState(getApplication()).clear(packageName)+                }             } else {                 rules.upsert(AppTimer(packageName, minutes))             }