Summary
The timer implementation in the alarm challenge performs a very large async loop that is expensive on the UI isolate.
Problematic Code
File: lib/app/modules/alarmChallenge/controllers/alarm_challenge_controller.dart
- Around line 202:
totalIterations = 1500000
- Around line 204: loop iterates up to 1,500,000 times
- Around line 217:
await Future.delayed(duration ~/ i) runs in each iteration
Current pattern:
- Performs 1.5M loop iterations
- Schedules/delays every iteration
- Continuously mutates reactive state (
progress.value -= decrement)
Impact
- High CPU usage during challenge timer
- Potential UI jank/frame drops on lower-end devices
- Unnecessary battery drain
- Risk of degraded app responsiveness while alarm challenge is active
Expected Behavior
Timer/progress updates should be time-based and efficient (constant or low-frequency updates), without massive iteration counts on the UI isolate.
Suggested Fix
Replace the loop with a time-driven approach, for example:
- Use
Timer.periodic with a reasonable tick interval (e.g. 16ms/50ms/100ms), or
- Use an
AnimationController for smooth progress over 15 seconds, or
- Compute progress from elapsed time using a
Stopwatch and a low-frequency update stream.
Also:
- Cancel/dispose timer cleanly in
onClose
- Guard against updates after controller disposal
Repro
- Enable timer challenge in alarm challenge flow
- Start challenge
- Observe increased CPU usage and possible UI stutter while timer runs
Acceptance Criteria
- No large iteration async loop for timer progression
- Timer remains functionally equivalent (15-second countdown)
- Reduced CPU utilization and no noticeable UI jank during challenge
Summary
The timer implementation in the alarm challenge performs a very large async loop that is expensive on the UI isolate.
Problematic Code
File:
lib/app/modules/alarmChallenge/controllers/alarm_challenge_controller.darttotalIterations = 1500000await Future.delayed(duration ~/ i)runs in each iterationCurrent pattern:
progress.value -= decrement)Impact
Expected Behavior
Timer/progress updates should be time-based and efficient (constant or low-frequency updates), without massive iteration counts on the UI isolate.
Suggested Fix
Replace the loop with a time-driven approach, for example:
Timer.periodicwith a reasonable tick interval (e.g. 16ms/50ms/100ms), orAnimationControllerfor smooth progress over 15 seconds, orStopwatchand a low-frequency update stream.Also:
onCloseRepro
Acceptance Criteria