Repository navigation
Sync page added ux enhancements - #3885
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces significant UX enhancements to the sync page by refactoring the local and cloud storage settings into dedicated pages. This improves modularity and user experience. While the changes are generally positive, I've identified a critical issue in the new LocalStoragePage concerning improper state management within an asynchronous method, which could lead to application crashes. A detailed comment with a suggested fix is provided to address this.
| setState(() => _isSaving = true); | ||
| try { | ||
| SharedPreferencesUtil().unlimitedLocalStorageEnabled = value; | ||
| context.read<SyncProvider>().refreshWals(); | ||
| setState(() => _isSaving = false); | ||
| if (mounted) { | ||
| ScaffoldMessenger.of(context).showSnackBar( | ||
| SnackBar( | ||
| content: Text( | ||
| value ? 'Local storage enabled' : 'Local storage disabled', | ||
| ), | ||
| backgroundColor: Colors.green, | ||
| ), | ||
| ); | ||
| } | ||
| } catch (e) { | ||
| setState(() => _isSaving = false); | ||
| if (mounted) { | ||
| ScaffoldMessenger.of(context).showSnackBar( | ||
| SnackBar( | ||
| content: Text('Failed to update settings: ${e.toString()}'), | ||
| backgroundColor: Colors.red, | ||
| ), | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
There are a couple of issues in the _toggleLocalStorage method that could lead to bugs:
- Potential crash:
setStateis called after anasyncoperation without checking if the widget is still mounted. If the user navigates away from the page before the operation completes, this will throw an exception and crash the app. - Race condition: The
SharedPreferencesUtil().unlimitedLocalStorageEnabled = value;setter returns aFuturewhich is notawaited. This meanscontext.read<SyncProvider>().refreshWals()might be called before the preference is actually saved, leading to a state inconsistency.
To resolve these issues, you should await the preference saving operation and use a finally block to safely update the state only when the widget is mounted.
setState(() => _isSaving = true);
try {
await SharedPreferencesUtil().saveBool('unlimitedLocalStorageEnabled', value);
context.read<SyncProvider>().refreshWals();
if (mounted) {
ScaredPreferences.of(context).showSnackBar(
SnackBar(
content: Text(
value ? 'Local storage enabled' : 'Local storage disabled',
),
backgroundColor: Colors.green,
),
);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Failed to update settings: ${e.toString()}'),
backgroundColor: Colors.red,
),
);
}
} finally {
if (mounted) {
setState(() => _isSaving = false);
}
}
No description provided.