Skip to content

Commit dfd99c1

Browse files
Copilotteolemon
andauthored
Improve code clarity and SharedPreferences iteration using .any()
Agent-Logs-Url: https://github.com/openfoodfacts/smooth-app/sessions/55b77ca3-eba4-499e-a998-4ce36355f11d Co-authored-by: teolemon <1689815+teolemon@users.noreply.github.com>
1 parent cfa9651 commit dfd99c1

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

packages/smooth_app/lib/data_models/preferences/user_preferences.dart

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -509,16 +509,22 @@ class UserPreferences extends ChangeNotifier {
509509
///
510510
/// This checks both attribute importances (i.e., attributes set to a value
511511
/// other than "not important") and unwanted ingredients.
512+
///
513+
/// The method filters the SharedPreferences key space by the project-specific
514+
/// prefix and exits as soon as one configured preference is found.
512515
bool hasAnyPreferencesForProject(final String projectKey) {
513516
final String prefix = '${_TAG_PREFIX_IMPORTANCE}${projectKey}_';
514-
for (final String key in _sharedPreferences.getKeys()) {
515-
if (key.startsWith(prefix)) {
516-
final String? value = _sharedPreferences.getString(key);
517-
if (value != null &&
518-
value != PreferenceImportance.ID_NOT_IMPORTANT) {
519-
return true;
517+
final bool hasConfiguredAttribute = _sharedPreferences.getKeys().any(
518+
(String key) {
519+
if (!key.startsWith(prefix)) {
520+
return false;
520521
}
521-
}
522+
final String? value = _sharedPreferences.getString(key);
523+
return value != null && value != PreferenceImportance.ID_NOT_IMPORTANT;
524+
},
525+
);
526+
if (hasConfiguredAttribute) {
527+
return true;
522528
}
523529
return getUnwantedIngredientsForProject(projectKey).isNotEmpty;
524530
}

packages/smooth_app/lib/pages/food_preferences/food_preferences_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ class _FoodPreferencesPageState extends State<FoodPreferencesPage> {
292292
? Colors.white.withValues(alpha: 0.3)
293293
: lightTheme
294294
? extension.primaryLight
295+
// Semi-transparent track so the dark header shows through
295296
: extension.primarySemiDark.withValues(alpha: 0.5),
296297
currentValue: _controller.progress,
297298
maxValue: 1,

0 commit comments

Comments
 (0)