Skip to content
PidebugMobile9 files · +247 −170

Fix stale OTP not updating in Compose Multiplatform app

Session from pi-coding-sessions on Hugging Face by championswimmer · MIT · condensed by Coders Talk

17msession
0interventions
GOAL

Fix a bug where OTP codes on the Home screen stop updating after navigating to Accounts and back, even after the countdown resets, and add a regression test for it.

  1. Prompt · +0
    Home screen OTPs stop updating after navigating to Accounts and back, even manual regenerate returns stale OTP. Fix in the compose app.
  2. Agent did · +0m
    Explored the Compose app's OTP flow (ViewModel, HomeScreen, OTPCard, TOTP lib), then reworked OTP refresh to a screen-level timer in HomeScreen, updated OTPCard/HomeOtpListSection/AccountsViewModel/AccountDetailScreen, and verified builds and tests passed.
  3. Agent did · +4m
    Built and installed the app on the running Android emulator to verify the fix, then added a regression test in AccountsViewModelTest covering fresh OTP fetch after stale cache.
  4. Fail · +6m
    Developer reproduced the bug again with exact repro steps: after opening an account and returning to Home, the OTP card still doesn't update on rollover.
  5. Agent did · +7m
    Re-investigated and moved the OTP auto-refresh into a persistent loop owned by AccountsViewModel instead of the screen, rebuilt, re-tested, and reinstalled on the emulator.
  6. Fail · +12m
    Developer found the bug persists specifically when staying on Home and just switching tabs, without opening an account; only regenerating via account detail fixed it.
  7. Agent did · +13m
    Traced the real root cause: AccountsViewModel was a Koin singleton but screens fetched it via koinViewModel(), so navigation could call onCleared() on the shared instance and kill its refresh coroutine. Switched all screens to koinInject() to keep it truly singleton-scoped, rebuilt and reinstalled.
  8. Prompt · +15m
    This fix works. Commit and push it.
  9. Agent did · +15m
    Committed the 9 changed files as 'Fix home OTP refresh across tab navigation' on branch arnav/code-simplify (commit ad0bbb3) and pushed to origin.
  10. Outcome · +17m
    OTP bug fixed and pushed (commit ad0bbb3); a follow-up watch-emulator pairing task via adb/companiondevice commands was attempted but left unresolved, ending in setup-wizard activity resolution errors.
VERDICT

Next time I'd start with the exact repro steps (stay-on-Home vs navigate-into-account) up front, since the vague first description led to two wrong fixes before the real bug surfaced. What actually worked was pushing back with a more precise repro each time the fix failed — that's what forced the agent to find the real root cause: a singleton ViewModel being fetched with koinViewModel() instead of koinInject(), which let navigation kill its background refresh coroutine.

I tried this · 0

Discussion · 0

How Builds work