Skip to content

Commit fb910db

Browse files
authored
feat(predict): refresh eligibility on app focus (#22500)
## **Description** This PR implements automatic eligibility refresh for the Predict feature when the app comes to foreground. Previously, eligibility was only checked on initial load or manual refresh. Now, when a user backgrounds the app and then returns to it, the eligibility status is automatically refreshed. **What is the reason for the change?** Users may change their network configuration (e.g., switching between different network connections) while the app is backgrounded. When they return to the app, the Predict feature should automatically check eligibility again to reflect any potential changes in their connection status. **What is the improvement/solution?** - Added an `AppState` listener to the `usePredictEligibility` hook that detects when the app transitions from `background`/`inactive` to `active` state - Implemented 1-minute debouncing to prevent excessive API calls if the user switches in and out of the app multiple times - Manual refresh functionality remains unchanged and bypasses the debounce - Added comprehensive error handling and logging ## **Changelog** CHANGELOG entry: null ## **Related issues** Fixes: N/A ## **Manual testing steps** ```gherkin Feature: Auto-refresh Predict eligibility on app focus Scenario: user returns to app and eligibility is refreshed Given user has opened the Predict feature And user is eligible to use Predict When user backgrounds the app (switches to another app) And user returns to the app Then eligibility is automatically refreshed And user sees updated eligibility status if it changed Scenario: user rapidly switches in and out of app Given user has opened the Predict feature And eligibility was just refreshed When user backgrounds the app And user returns to the app within 1 minute Then eligibility refresh is skipped due to debouncing And user still has access to Predict feature Scenario: user returns after debounce period Given user has opened the Predict feature And eligibility was refreshed more than 1 minute ago When user backgrounds the app And user returns to the app Then eligibility is automatically refreshed ``` ## **Screenshots/Recordings** N/A - This is an internal behavior change with no visual UI impact. ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I've included tests if applicable - [x] I've documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I've applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Introduce a singleton-managed AppState listener to auto-refresh Predict eligibility on app foreground with 1-minute debounce, logging, and race-condition prevention, plus comprehensive tests. > > - **Predict Hook (`usePredictEligibility.ts`)**: > - **Singleton manager (`EligibilityRefreshManager`)**: > - Manages a single `AppState` `change` listener across hook instances. > - Refreshes `Engine.context.PredictController.refreshEligibility()` on transition to `active`. > - Debounces auto-refreshes by 60s; manual `refreshEligibility` bypasses debounce. > - Prevents concurrent calls by reusing in-flight promise; tracks last refresh time. > - Registers/unregisters per hook mount/unmount; adds detailed `DevLogger` logs and error handling. > - **Tests (`usePredictEligibility.test.ts`)**: > - Cover eligibility state selection, singleton registration/unregistration, app focus auto-refresh, debounce behavior, manual refresh bypass, error logging/continuation, and concurrency/race-condition scenarios. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 8c94340. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 84bd50b commit fb910db

File tree

2 files changed

+812
-40
lines changed

2 files changed

+812
-40
lines changed

0 commit comments

Comments
 (0)