Skip to content
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

feat: 5639 - storing the latest selected product type #5752

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:math' as math;

import 'package:flutter/material.dart';
Expand Down Expand Up @@ -54,6 +55,9 @@ class UserPreferences extends ChangeNotifier {
return _instance!;
}

/// Once we initialized with main.dart, we don't need the "async".
static UserPreferences getUserPreferencesSync() => _instance!;

late ValueNotifier<bool> onCrashReportingChanged;
late ValueNotifier<bool> onAnalyticsChanged;

Expand Down Expand Up @@ -82,6 +86,7 @@ class UserPreferences extends ChangeNotifier {
static const String _TAG_USER_GROUP = '_user_group';
static const String _TAG_UNIQUE_RANDOM = '_unique_random';
static const String _TAG_LAZY_COUNT_PREFIX = '_lazy_count_prefix';
static const String _TAG_LATEST_PRODUCT_TYPE = '_latest_product_type';

/// Camera preferences

Expand Down Expand Up @@ -468,4 +473,16 @@ class UserPreferences extends ChangeNotifier {
);
}
}

ProductType get latestProductType =>
ProductType.fromOffTag(
_sharedPreferences.getString(_TAG_LATEST_PRODUCT_TYPE)) ??
ProductType.food;

set latestProductType(final ProductType value) => unawaited(
_sharedPreferences.setString(
_TAG_LATEST_PRODUCT_TYPE,
value.offTag,
),
);
}
11 changes: 7 additions & 4 deletions packages/smooth_app/lib/pages/search/search_product_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/fetched_product.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/database/dao_string_list.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
Expand All @@ -17,10 +18,11 @@ import 'package:smooth_app/query/product_query.dart';

/// Search helper dedicated to product search.
class SearchProductHelper extends SearchHelper {
SearchProductHelper();
SearchProductHelper() {
_productType = UserPreferences.getUserPreferencesSync().latestProductType;
}

// TODO(monsieurtanuki): maybe reinit it with latest value
ProductType _productType = ProductType.food;
late ProductType _productType;

@override
String get historyKey => DaoStringList.keySearchProductHistory;
Expand Down Expand Up @@ -154,7 +156,8 @@ class _ProductTypeFilterState extends State<_ProductTypeFilter> {
segments: segments,
selected: <ProductType>{widget.searchProductHelper._productType},
onSelectionChanged: (Set<ProductType> newSelection) => setState(
() => widget.searchProductHelper._productType = newSelection.first,
() => UserPreferences.getUserPreferencesSync().latestProductType =
widget.searchProductHelper._productType = newSelection.first,
),
);
}
Expand Down
Loading