Skip to content

Commit

Permalink
feat: 5855 - no visible product type filter by default (#5878)
Browse files Browse the repository at this point in the history
* feat: 5855 - no visible product type filter by default

Impacted files:
* `background_task_price.dart`: minor unrelated fix due to off-dart 3.17.0
* `dao_product.dart`: we overwrite the products product_type with the server product type only if null
* `product_query.dart`: now retrieving product_type field in products found on a server
* `product_refresher.dart`: unrelated minor refactoring
* `pubspec.lock`: wtf
* `pubspec.yaml`: upgraded off-dart to 3.17.0
* `search_product_helper.dart`: visible/invisible product type filter, by default invisible and working on food
* `user_preferences_dev_mode.dart`: on/off switch for visible product type filter, off by default

* Move the setting to "App settings"

---------

Co-authored-by: Edouard Marquez <moi@edouard-marquez.me>
Co-authored-by: Edouard Marquez <g123k@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 19, 2024
1 parent 3655c3b commit 5a0ba55
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ abstract class BackgroundTaskPrice extends BackgroundTask {
..productCode = barcodes[i];

// create price
final MaybeError<Price> addedPrice =
final MaybeError<Price?> addedPrice =
await OpenPricesAPIClient.createPrice(
price: newPrice,
bearerToken: bearerToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class UserPreferences extends ChangeNotifier {
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';
static const String _TAG_SEARCH_SHOW_PRODUCT_TYPE_FILTER =
'_search_show_product_type_filter';
static const String _TAG_PRODUCT_PAGE_ACTIONS = '_product_page_actions';

/// Camera preferences
Expand Down Expand Up @@ -488,6 +490,15 @@ class UserPreferences extends ChangeNotifier {
),
);

Future<void> setSearchProductTypeFilter(final bool visible) async {
await _sharedPreferences.setBool(
_TAG_SEARCH_SHOW_PRODUCT_TYPE_FILTER, visible);
notifyListeners();
}

bool get searchProductTypeFilterVisible =>
_sharedPreferences.getBool(_TAG_SEARCH_SHOW_PRODUCT_TYPE_FILTER) ?? false;

List<ProductFooterActionBar> get productPageActions {
final List<String>? actions =
_sharedPreferences.getStringList(_TAG_PRODUCT_PAGE_ACTIONS);
Expand Down
4 changes: 3 additions & 1 deletion packages/smooth_app/lib/database/dao_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ class DaoProduct extends AbstractSqlDao implements BulkDeletable {
required final ProductType productType,
}) async {
for (final Product product in products) {
product.productType = productType;
// in case the server product has no product type, which shouldn't happen
// in the future
product.productType ??= productType;
}
await localDatabase.database.transaction(
(final Transaction transaction) async => _bulkReplaceLoop(
Expand Down
5 changes: 5 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -2425,6 +2425,11 @@
"description": "Label for expanding nutrition facts table in application setting"
},
"expand_ingredients_body": "Keep the ingredients panel expanded",
"search_product_filter_visibility_title": "Show a filter in the search",
"search_product_filter_visibility_subtitle": "Select search site: Open Food Facts, Open Beauty Facts, Open Pet Food Facts or Open Products Facts",
"@search_product_filter_visibility_subtitle": {
"description": "Label for showing the product type filter in the search bar"
},
"no_internet_connection": "No internet connection",
"@no_internet_connection": {
"description": "Message when there is no internet connection"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ class UserPreferencesSettings extends AbstractUserPreferences {
subtitle: appLocalizations.expand_ingredients_body,
panelId: KnowledgePanelCard.PANEL_INGREDIENTS_ID,
),
_getDivider(),
UserPreferencesItemSwitch(
title: appLocalizations.search_product_filter_visibility_title,
subtitle: appLocalizations.search_product_filter_visibility_subtitle,
value: userPreferences.searchProductTypeFilterVisible,
onChanged: (final bool visible) async =>
userPreferences.setSearchProductTypeFilter(visible),
),
if (CameraHelper.hasACamera)
_getTitle(
label: appLocalizations.settings_app_miscellaneous,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ class ProductRefresher {
/// Returns the standard configuration for several barcodes product query.
ProductSearchQueryConfiguration getBarcodeListQueryConfiguration(
final List<String> barcodes,
final OpenFoodFactsLanguage language, {
final List<ProductField>? fields,
}) =>
final OpenFoodFactsLanguage language,
) =>
ProductSearchQueryConfiguration(
fields: fields ?? ProductQuery.fields,
fields: ProductQuery.fields,
language: language,
country: ProductQuery.getCountry(),
parametersList: <Parameter>[
Expand Down
10 changes: 8 additions & 2 deletions packages/smooth_app/lib/pages/search/search_product_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class SearchProductHelper extends SearchHelper {
appLocalizations.search;

@override
Widget getAdditionalFilter() => _ProductTypeFilter(this);
Widget? getAdditionalFilter() =>
UserPreferences.getUserPreferencesSync().searchProductTypeFilterVisible
? _ProductTypeFilter(this)
: null;

@override
void search(
Expand Down Expand Up @@ -119,7 +122,10 @@ class SearchProductHelper extends SearchHelper {
localDatabase: localDatabase,
productQuery: KeywordsProductQuery(
value,
productType: _productType,
productType: UserPreferences.getUserPreferencesSync()
.searchProductTypeFilterVisible
? ProductType.food
: _productType,
),
context: context,
editableAppBarTitle: false,
Expand Down
1 change: 1 addition & 0 deletions packages/smooth_app/lib/query/product_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ abstract class ProductQuery {
ProductField.NAME_ALL_LANGUAGES,
ProductField.BRANDS,
ProductField.BARCODE,
ProductField.PRODUCT_TYPE,
ProductField.NUTRISCORE,
ProductField.FRONT_IMAGE,
ProductField.IMAGE_FRONT_URL,
Expand Down
4 changes: 2 additions & 2 deletions packages/smooth_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1076,10 +1076,10 @@ packages:
dependency: "direct main"
description:
name: openfoodfacts
sha256: d35a213d6354246e3b27e0b18fe3def658e00c337346cf11c4f8ba082f92dfaa
sha256: "56df1ab54751a1b4491d99580b90141ea3327cf546724fa6055943e85d64fd51"
url: "https://pub.dev"
source: hosted
version: "3.16.0"
version: "3.17.0"
openfoodfacts_flutter_lints:
dependency: "direct dev"
description:
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ dependencies:
path: ../scanner/zxing


openfoodfacts: 3.16.0
openfoodfacts: 3.17.0
# openfoodfacts:
# path: ../../../openfoodfacts-dart

Expand Down

0 comments on commit 5a0ba55

Please sign in to comment.