Skip to content

Commit

Permalink
feat: 4223 - comparison page for 3 random products on dev mode
Browse files Browse the repository at this point in the history
New file:
* `compare_products3_page.dart`: Test page about comparing 3 products. Work in progress.

Impacted files:
* `attributes_card_helper.dart`: refactored introducing two new reusable methods
* `nutrition_container.dart`: refactored introducing one reusable getter
* `product_cards_helper.dart`: refactored introducing one new reusable method
* `user_preferences_dev_mode.dart`: added an item computing 3 random products and showing the product comparison page
  • Loading branch information
monsieurtanuki committed Aug 4, 2023
1 parent 1c3921e commit a039670
Show file tree
Hide file tree
Showing 5 changed files with 474 additions and 16 deletions.
40 changes: 29 additions & 11 deletions packages/smooth_app/lib/helpers/attributes_card_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,35 @@ enum AttributeEvaluation {
}
}

Widget getAttributeDisplayIcon(final Attribute attribute) => Padding(
padding: const EdgeInsetsDirectional.only(end: VERY_SMALL_SPACE),
child: _attributeMatchComparison(
attribute,
const Icon(CupertinoIcons.question, color: RED_COLOR),
const Icon(Icons.lens, color: RED_COLOR),
const Icon(Icons.lens, color: LIGHT_ORANGE_COLOR),
const Icon(Icons.lens, color: LIGHT_ORANGE_COLOR),
const Icon(Icons.lens, color: LIGHT_GREEN_COLOR),
const Icon(Icons.lens, color: LIGHT_GREEN_COLOR),
),
Widget getAttributeDisplayIcon(final Attribute attribute) {
final Color color = getAttributeDisplayBackgroundColor(attribute);
final IconData iconData = getAttributeDisplayIconData(attribute);
return Padding(
padding: const EdgeInsetsDirectional.only(end: VERY_SMALL_SPACE),
child: Icon(iconData, color: color),
);
}

Color getAttributeDisplayBackgroundColor(final Attribute attribute) =>
_attributeMatchComparison<Color>(
attribute,
RED_COLOR,
RED_COLOR,
LIGHT_ORANGE_COLOR,
LIGHT_ORANGE_COLOR,
LIGHT_GREEN_COLOR,
LIGHT_GREEN_COLOR,
);

IconData getAttributeDisplayIconData(final Attribute attribute) =>
_attributeMatchComparison<IconData>(
attribute,
CupertinoIcons.question,
Icons.lens,
Icons.lens,
Icons.lens,
Icons.lens,
Icons.lens,
);

bool isMatchAvailable(Attribute attribute) {
Expand Down
30 changes: 25 additions & 5 deletions packages/smooth_app/lib/helpers/product_cards_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,24 @@ List<Attribute> getMandatoryAttributes(
final List<String> attributeGroupOrder,
final Set<String> attributesToExcludeIfStatusIsUnknown,
final ProductPreferences preferences,
) {
) =>
getSortedAttributes(
product,
attributeGroupOrder,
attributesToExcludeIfStatusIsUnknown,
preferences,
PreferenceImportance.ID_MANDATORY,
);

/// Returns the attributes, ordered by importance desc and attribute group order
List<Attribute> getSortedAttributes(
final Product product,
final List<String> attributeGroupOrder,
final Set<String> attributesToExcludeIfStatusIsUnknown,
final ProductPreferences preferences,
final String importance, {
final bool excludeMainScoreAttributes = true,
}) {
final List<Attribute> result = <Attribute>[];
if (product.attributeGroups == null) {
return result;
Expand All @@ -106,9 +123,10 @@ List<Attribute> getMandatoryAttributes(
for (final AttributeGroup attributeGroup in product.attributeGroups!) {
mandatoryAttributesByGroup[attributeGroup.id!] = getFilteredAttributes(
attributeGroup,
PreferenceImportance.ID_MANDATORY,
importance,
attributesToExcludeIfStatusIsUnknown,
preferences,
excludeMainScoreAttributes: excludeMainScoreAttributes,
);
}

Expand All @@ -131,15 +149,17 @@ List<Attribute> getFilteredAttributes(
final AttributeGroup attributeGroup,
final String importance,
final Set<String> attributesToExcludeIfStatusIsUnknown,
final ProductPreferences preferences,
) {
final ProductPreferences preferences, {
final bool excludeMainScoreAttributes = true,
}) {
final List<Attribute> result = <Attribute>[];
if (attributeGroup.attributes == null) {
return result;
}
for (final Attribute attribute in attributeGroup.attributes!) {
final String attributeId = attribute.id!;
if (SCORE_ATTRIBUTE_IDS.contains(attributeId)) {
if (excludeMainScoreAttributes &&
SCORE_ATTRIBUTE_IDS.contains(attributeId)) {
continue;
}
if (attributeGroup.id == AttributeGroup.ATTRIBUTE_GROUP_LABELS) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand All @@ -8,6 +10,7 @@ import 'package:smooth_app/background/background_task_badge.dart';
import 'package:smooth_app/data_models/continuous_scan_model.dart';
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/dao_product_list.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart';
Expand All @@ -18,6 +21,8 @@ import 'package:smooth_app/pages/offline_tasks_page.dart';
import 'package:smooth_app/pages/preferences/abstract_user_preferences.dart';
import 'package:smooth_app/pages/preferences/user_preferences_dev_debug_info.dart';
import 'package:smooth_app/pages/preferences/user_preferences_page.dart';
import 'package:smooth_app/pages/product/compare_products3_page.dart';
import 'package:smooth_app/pages/product/ordered_nutrients_cache.dart';
import 'package:smooth_app/query/product_query.dart';

/// Full page display of "dev mode" for the preferences page.
Expand Down Expand Up @@ -338,6 +343,67 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
ProductQuery.setLanguage(context, userPreferences);
},
),
ListTile(
title: const Text('Side by side comparison: 3 random products'),
onTap: () async {
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final DaoProduct daoProduct = DaoProduct(localDatabase);
final List<String> allBarcodes = await daoProduct.getAllKeys();
final int length = allBarcodes.length;
if (length < 3) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'We need at least 3 products in the local database!',
),
),
);
}
return;
}
final Random random = Random();
final int index1 = random.nextInt(length);
final int index2 = (index1 + 1) % length;
final int index3 = (index1 + 2) % length;
final Product product1 =
(await daoProduct.get(allBarcodes[index1]))!;
final Product product2 =
(await daoProduct.get(allBarcodes[index2]))!;
final Product product3 =
(await daoProduct.get(allBarcodes[index3]))!;
if (context.mounted) {
final OrderedNutrientsCache? cache =
await OrderedNutrientsCache.getCache(context);
if (context.mounted) {
if (cache == null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(context)
.nutrition_cache_loading_error,
),
),
);
return;
}
await Navigator.push<void>(
context,
MaterialPageRoute<void>(
builder: (BuildContext context) => CompareProducts3Page(
products: <Product>[
product1,
product2,
product3,
],
orderedNutrientsCache: cache,
),
),
);
}
}
},
),
ListTile(
title: const Text('Debugging information'),
onTap: () async => Navigator.of(context).push(MaterialPageRoute<void>(
Expand Down
Loading

0 comments on commit a039670

Please sign in to comment.