Skip to content

[codex] Fix widget refresh staleness#2193

Open
wolph wants to merge 1 commit into
HabitRPG:mainfrom
wolph:codex/fix-widget-refresh-staleness
Open

[codex] Fix widget refresh staleness#2193
wolph wants to merge 1 commit into
HabitRPG:mainfrom
wolph:codex/fix-widget-refresh-staleness

Conversation

@wolph
Copy link
Copy Markdown

@wolph wolph commented May 17, 2026

Summary

Fixes #1233.

  • Adds an injectable WidgetUpdater to centralize task-list notifications and explicit widget update broadcasts.
  • Refreshes widgets from local data immediately after task/user writes, while system widget updates perform the existing server-backed refresh path.
  • Marks app-initiated widget broadcasts so providers skip the network refresh loop.
  • Makes collection-widget factories load data synchronously in onCreate() and onDataSetChanged().
  • Stops HabitButtonWidgetService only after async rendering finishes.

Tests

  • JAVA_HOME=/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home ./gradlew :Habitica:testProdDebugUnitTest --tests com.habitrpg.android.habitica.widget.BaseWidgetProviderTest --tests com.habitrpg.android.habitica.widget.WidgetUpdaterTest --tests com.habitrpg.android.habitica.widget.TaskListFactoryTest --tests com.habitrpg.android.habitica.data.implementation.UserRepositoryImplTest --tests com.habitrpg.android.habitica.data.implementation.TaskRepositoryImplTest
    • Passes: 23 tests, 23 passed.
  • JAVA_HOME=/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home ./gradlew :Habitica:compileProdDebugKotlin
    • Passes.
  • git diff --check
    • Passes.

Known Existing Failures

  • JAVA_HOME=/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home ./gradlew :Habitica:testProdDebugUnitTest
    • Fails 3 existing TaskTest.isDueToday cases at TaskTest.kt:46, TaskTest.kt:53, and TaskTest.kt:60.
  • JAVA_HOME=/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home ./gradlew test
    • Fails compiling existing common tests at common/src/test/java/com/habitrpg/common/habitica/helpers/NumberAbbreviatorTest.kt:44.

@wolph wolph marked this pull request as ready for review May 17, 2026 00:50
Copilot AI review requested due to automatic review settings May 17, 2026 00:50
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to reduce widget staleness by centralizing widget refreshes and making widget providers/factories refresh from local data after repository writes.

Changes:

  • Adds WidgetUpdater and injects it into repositories/MainActivity.
  • Adds network-refresh gating for widget update broadcasts.
  • Adds unit tests for widget update helpers, factories, and repository-triggered refreshes.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
Habitica/src/main/java/com/habitrpg/android/habitica/widget/WidgetUpdater.kt New centralized widget update helper.
Habitica/src/main/java/com/habitrpg/android/habitica/widget/BaseWidgetProvider.kt Adds pre-update network refresh handling.
Habitica/src/main/java/com/habitrpg/android/habitica/widget/TaskListFactory.kt Loads collection data synchronously.
Habitica/src/main/java/com/habitrpg/android/habitica/widget/TaskListWidgetProvider.kt Relies on repository refresh after task scoring.
Habitica/src/main/java/com/habitrpg/android/habitica/widget/HabitButtonWidgetService.kt Stops service after async rendering.
Habitica/src/main/java/com/habitrpg/android/habitica/data/implementation/TaskRepositoryImpl.kt Notifies widgets after selected task writes.
Habitica/src/main/java/com/habitrpg/android/habitica/data/implementation/UserRepositoryImpl.kt Notifies widgets after user retrieval.
Habitica/src/main/java/com/habitrpg/android/habitica/modules/UserRepositoryModule.kt Wires WidgetUpdater into repositories.
Habitica/src/main/java/com/habitrpg/android/habitica/ui/activities/MainActivity.kt Uses WidgetUpdater on pause.
Habitica/src/test/java/com/habitrpg/android/habitica/widget/WidgetUpdaterTest.kt Tests widget updater behavior.
Habitica/src/test/java/com/habitrpg/android/habitica/widget/BaseWidgetProviderTest.kt Tests refresh helper behavior.
Habitica/src/test/java/com/habitrpg/android/habitica/widget/TaskListFactoryTest.kt Tests synchronous task factory loading.
Habitica/src/test/java/com/habitrpg/android/habitica/data/implementation/UserRepositoryImplTest.kt Tests user repository widget updates.
Habitica/src/test/java/com/habitrpg/android/habitica/data/implementation/TaskRepositoryImplTest.kt Extends task repository widget update tests.
gradle/libs.versions.toml Adds coroutine test dependency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +45 to +47
val mirroredTasks =
userRepository.getUser()
.firstOrNull()?.preferences?.tasks?.mirrorGroupTasks?.toTypedArray()
Comment on lines 64 to +65
localRepository.saveTasks(userId, order, tasks)
widgetUpdater.updateTaskListWidgets()
taskRepository.saveTasks(id, tasksOrder, tasks)
}
}
widgetUpdater.updateAllWidgets()
Comment on lines 113 to +117
if (id != null && tasksOrder != null && tasks != null) {
taskRepository.saveTasks(id, tasksOrder, tasks)
}
}
widgetUpdater.updateAllWidgets()
Comment on lines +174 to +175
return intent.action == AppWidgetManager.ACTION_APPWIDGET_UPDATE &&
!intent.getBooleanExtra(WidgetUpdater.EXTRA_SKIP_NETWORK_REFRESH, false)
Comment on lines +58 to +62
val task = taskRepository.getUnmanagedTask(taskid).firstOrNull()
updateData(task)
}
} finally {
stopSelf(startId)
Comment on lines +41 to +45
try {
dispatchWidgetUpdate(context, intent)
} finally {
pendingResult.finish()
}
@wolph wolph force-pushed the codex/fix-widget-refresh-staleness branch from af05e6e to 65ea197 Compare June 1, 2026 00:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Widgets Don't Refresh Until App Opened

2 participants