-
Type:
Story
-
Resolution: Unresolved
-
Priority:
High
-
Affects Version/s: None
-
STOP TB Sprint 13
Format
Scale target: no change in behaviour.
Background / Context
The same logic is copied in many places in the STOP-TB Android app: server calls and token refresh, TB sync loops, logout, activity setup, GPS/household/camp-hub UI, TB list screens and form validation. Each copy has slightly different rules, so every fix has to be made several times and the copies behave differently.
Problem Statement
- The server call → token refresh → retry block is copied about 40 times. Some copies retry with no limit, and workers can refresh the token at the same time.
- TB push/pull loops are copied 5+ times. Logout is written 3 times, and only one version clears local data.
- Language setup is copied in 7 activities, and tapjacking protection is missing on 3 of them.
- UI components, TB list screens and form validation are duplicated across screens.
Current Behaviour
Duplicated code behaves differently from place to place. Bugs get fixed in one copy and stay in the others.
Expected Behaviour / Requirement
Move duplicated piece of logic into one shared, tested component and use it everywhere. Behaviour stays the same unless a task says otherwise (e.g. retry limit, tapjacking).
Tasks
- Common Amrit API call helper (High). Build one helper that calls the API, checks for 401/5002, refreshes the token once and retries once. Add a Mutex on refreshTokenTmc and migrate about 15 repositories, one repository per PR. Remove the SocketTimeoutException("Refreshed Token!") signal.
- Shared TB push/pull sync loops (Medium). Replace the 5 copied push loops in TbPushRepo and the paged pull loops in TbPullRepo/DiagnosticOrderRepo with generic helpers. Depends on task 1.
- Unified logout (Medium). Build one logout used by every entry point (SignInViewModel, VolunteerViewModel). Blocked by the decision on whether logout clears the DB.
- BaseLocalizedActivity (Medium). Create one parent Activity that sets the language in attachBaseContext and applies TapjackingProtectionHelper, and make all 8 activities extend it. This fixes the tapjacking gap on Counselling, Contact Tracing and Splash.
- Reusable GPS, household-link and camp-hub components (Low). Extract LocationCaptureHelper, LinkHouseholdDialog and CampHubConnector, and replace the copies. Can be split into 3 sub-tasks.
- One parameterised TB list screen (Low). Replace the Screening/Suspected/Confirmed list fragments and ViewModels with one list that takes a type parameter. Share ID state between BenListAdapter and BenPagingAdapter using Set/Map lookups. Layouts stay the same.
- Shared form validation helper (Low). Build one tested FormValidator for the Contact Tracing and Counselling forms, and delete the unused ValidationEngine copy.
User Story
As a developer, I want each piece of shared logic to exist in one place so that fixes are made once and the app behaves the same everywhere.
Acceptance Criteria
- Given any server call gets a 401/5002, when the token is refreshed, then the call retries once and is never retried without limit.
- Given several sync workers run at once, when the token expires, then only one token refresh happens.
- Given TB push/pull sync runs, when it finishes, then the records synced are the same as before the refactor.
- Given the user logs out from any screen, when logout completes, then the same cleanup happens every time.
- Given any activity is opened, when the app language changes or an overlay is shown, then the language is applied and tapjacking is blocked.
- Given the TB Screening, Suspected or Confirmed list is opened, when the user scrolls, filters or pages, then results match current behaviour.
- Given a 100k to 1M record dataset, when sync and list screens are used, then performance is equal to or better than before (threshold [TBD/CLARIFICATION REQUIRED]).
Business Rules / Validations
- No change to UI layouts, screen flows or server payloads.
- Each task ships as its own PR. Repositories migrate one per PR.
- Retry is limited to one retry after a token refresh.
Dependencies
- Room migration and WorkManager sync tests (#13) should be in place before tasks 1–2.
- Task 2 depends on task 1.
- Task 3 needs a product/tech decision on whether logout should clear the DB.
- Coordinate task 1 with the thread-safe token store (#28).
Impact / Affected Areas
Repositories and sync (TbPushRepo, TbPullRepo, DiagnosticOrderRepo, BenRepo and others), login/logout, all activities, beneficiary and household registration forms, camp mode, TB list screens, Contact Tracing and Counselling forms.
Out of Scope
- UI redesign or layout changes.
- Changes to server APIs or payload formats.
- Splitting large classes/ViewModels (covered by #15 and #22).
QA / Testing Considerations
- Run regression on full TB push/pull sync.
- Test language switching and screen-overlay blocking on every activity.
- Test the TB lists with paging and filters on a large dataset.