-
Notifications
You must be signed in to change notification settings - Fork 137
Fix siteComponent!! crash on the dashboard
#15138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Fix siteComponent!! crash on the dashboard
#15138
Conversation
The `SiteComponent` is now initialized before updating the state.
| // Create a new site component tied to the lifecycle of the selected site | ||
| siteComponent = siteComponentProvider.get() | ||
| .setSite(siteModel) | ||
| .setCoroutineScope(createSiteCoroutineScope()) | ||
| .build() | ||
|
|
||
| wasReset = false | ||
| state.value = siteModel | ||
| PreferenceUtils.setInt(getPreferences(), SELECTED_SITE_LOCAL_ID, siteModel.id) | ||
|
|
||
| // Notify listeners | ||
| getEventBus().post(SelectedSiteChangedEvent(siteModel)) | ||
|
|
||
| // Create a new site component tied to the lifecycle of the selected site | ||
| siteComponent = siteComponentProvider.get() | ||
| .setSite(get()) | ||
| .setCoroutineScope(createSiteCoroutineScope()) | ||
| .build() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made this change because state.value = siteModel triggers observers, and it was causing them to receive the old siteComponent. Now, I update siteComponent before updating the state so observers get the correct value.
| val widgets: Flow<List<DashboardWidgetDataModel>> = dataStore.data | ||
| .catch { exception -> | ||
| val widgets: Flow<List<DashboardWidgetDataModel>> = dataStoreFlow.flatMapLatest { dataStore -> | ||
| val flow = dataStore?.data ?: flow { throw IOException("Site component is null") } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we pass null site component case to the existing catch block.
📲 You can test the changes from this Pull Request in WooCommerce-Wear Android by scanning the QR code below to install the corresponding build.
|
|
📲 You can test the changes from this Pull Request in WooCommerce Android by scanning the QR code below to install the corresponding build.
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## trunk #15138 +/- ##
============================================
+ Coverage 38.64% 38.67% +0.03%
- Complexity 10456 10466 +10
============================================
Files 2181 2181
Lines 123905 123892 -13
Branches 17100 17102 +2
============================================
+ Hits 47877 47914 +37
+ Misses 71205 71149 -56
- Partials 4823 4829 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Closes WOOMOB-1843
Description
This PR resolves a long-standing
NullPointerExceptioncrash inDashboardDataStoreandDashboardRepositoryoccurring during app initialization or site switching.DashboardDataStoreandDashboardRepositorywere attempting to accessselectedSite.siteComponentusing the non-null assertion operator (!!) during their initialization.If the application started in a state where no site was strictly selected (e.g., fresh install, after logout, or race conditions during startup. I'm not sure because I couldn't reproduce it),
siteComponentwould be null, causing an immediate crash.Instead of assuming a site exists at initialization time, I refactored both classes to be Reactive:
Also I updated
SelectedSiteto support this reactive approach.Test Steps
Since I can't reproduce the crash, it's not possible to test this PR on running app. But I wrote unit tests to reproduce crash cases.
NullPointerExceptioncrash atselectedSite.siteComponent!!,line. Then check out the latest commit and confirm the crash was fixed.NullPointerExceptioncrash atselectedSite.siteComponent!!,line. Then checkout the latest commit and confirm the crash was fixed.Images/gif
RELEASE-NOTES.txtif necessary. Use the "[Internal]" label for non-user-facing changes.