Fix stale OTP not updating in Compose Multiplatform app
Session from pi-coding-sessions on Hugging Face by championswimmer · MIT · condensed by Coders Talk
Click a segment to jump to that moment
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.
- +0Prompt · +0Home screen OTPs stop updating after navigating to Accounts and back, even manual regenerate returns stale OTP. Fix in the compose app.
- +0mAgent did · +0mExplored 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.
- +4mAgent did · +4mBuilt 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.
- +6mFail · +6mDeveloper 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.
- +7mAgent did · +7mRe-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.
- +12mFail · +12mDeveloper found the bug persists specifically when staying on Home and just switching tabs, without opening an account; only regenerating via account detail fixed it.
- +13mAgent did · +13mTraced 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.
- +15mPrompt · +15mThis fix works. Commit and push it.
- +15mAgent did · +15mCommitted the 9 changed files as 'Fix home OTP refresh across tab navigation' on branch arnav/code-simplify (commit ad0bbb3) and pushed to origin.
- +17mOutcome · +17mOTP 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.