Skip to content

Commit

Permalink
feat: 5335 - owner field icon/info for "categories" (openfoodfacts#5845)
Browse files Browse the repository at this point in the history
Impacted files:
* `add_basic_details_page.dart`: minor refactoring
* `nutrition_page_loaded.dart`: minor refactoring
* `owner_field_info.dart`: new class `OwnerFieldIcon`
* `simple_input_page.dart`: now displays an `OwnerFieldInfo` if there's at least one owner field
* `simple_input_page_helpers.dart`: new method `bool isOwnerField`
* `simple_input_text_field.dart`: minor refactoring
* `simple_input_widget.dart`: now displays an `OwnerFieldIcon` if it's an owner field
  • Loading branch information
monsieurtanuki authored Nov 14, 2024
1 parent ae01e03 commit 8fc4174
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,7 @@ class _AddBasicDetailsPageState extends State<AddBasicDetailsPage> {
final OpenFoodFactsLanguage? language,
}) =>
_isOwnerField(productField, language: language)
? Semantics(
label: AppLocalizations.of(context).owner_field_info_title,
child: const Icon(OwnerFieldInfo.ownerFieldIconData),
)
? const OwnerFieldIcon()
: null;

bool _hasOwnerField() {
Expand Down
10 changes: 2 additions & 8 deletions packages/smooth_app/lib/pages/product/nutrition_page_loaded.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,7 @@ class _NutritionPageLoadedState extends State<NutritionPageLoaded>
) ==
null
? null
: Semantics(
label: appLocalizations.owner_field_info_title,
child: const Icon(OwnerFieldInfo.ownerFieldIconData),
),
: const OwnerFieldIcon(),
),
textInputAction: TextInputAction.next,
onFieldSubmitted: (_) {
Expand Down Expand Up @@ -569,10 +566,7 @@ class _NutrientValueCell extends StatelessWidget {
product.getOwnerFieldTimestamp(OwnerField.nutrient(nutrient)) ==
null
? null
: Semantics(
label: appLocalizations.owner_field_info_title,
child: const Icon(OwnerFieldInfo.ownerFieldIconData),
),
: const OwnerFieldIcon(),
),
keyboardType: const TextInputType.numberWithOptions(
signed: false,
Expand Down
19 changes: 15 additions & 4 deletions packages/smooth_app/lib/pages/product/owner_field_info.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

/// Icon to display when the product field value is "producer provided".
const IconData _ownerFieldIconData = Icons.factory;

/// Standard info tile about "owner fields".
class OwnerFieldInfo extends StatelessWidget {
const OwnerFieldInfo({super.key});

/// Icon to display when the product field value is "producer provided".
static const IconData ownerFieldIconData = Icons.factory;

@override
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context);
Expand All @@ -16,9 +16,20 @@ class OwnerFieldInfo extends StatelessWidget {
final Color? lightGrey = Colors.grey[300];
return ListTile(
tileColor: dark ? darkGrey : lightGrey,
leading: const Icon(ownerFieldIconData),
leading: const Icon(_ownerFieldIconData),
title: Text(appLocalizations.owner_field_info_title),
subtitle: Text(appLocalizations.owner_field_info_message),
);
}
}

/// Standard icon about "owner fields".
class OwnerFieldIcon extends StatelessWidget {
const OwnerFieldIcon();

@override
Widget build(BuildContext context) => Semantics(
label: AppLocalizations.of(context).owner_field_info_title,
child: const Icon(_ownerFieldIconData),
);
}
12 changes: 12 additions & 0 deletions packages/smooth_app/lib/pages/product/simple_input_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/pages/input/unfocus_field_when_tap_outside.dart';
import 'package:smooth_app/pages/product/common/product_buttons.dart';
import 'package:smooth_app/pages/product/may_exit_page_helper.dart';
import 'package:smooth_app/pages/product/owner_field_info.dart';
import 'package:smooth_app/pages/product/simple_input_page_helpers.dart';
import 'package:smooth_app/pages/product/simple_input_widget.dart';
import 'package:smooth_app/widgets/smooth_scaffold.dart';
Expand Down Expand Up @@ -56,6 +57,17 @@ class _SimpleInputPageState extends State<SimpleInputPage> {
final List<Widget> simpleInputs = <Widget>[];
final List<String> titles = <String>[];

bool hasOwnerField = false;
for (final AbstractSimpleInputPageHelper helper in widget.helpers) {
if (helper.isOwnerField(widget.product)) {
hasOwnerField = true;
break;
}
}

if (hasOwnerField) {
simpleInputs.add(const OwnerFieldInfo());
}
for (int i = 0; i < widget.helpers.length; i++) {
titles.add(widget.helpers[i].getTitle(appLocalizations));
simpleInputs.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ abstract class AbstractSimpleInputPageHelper extends ChangeNotifier {

/// Returns the enum to be used for matomo analytics.
AnalyticsEditEvents getAnalyticsEditEvent();

/// Returns true if the field is an owner field.
bool isOwnerField(final Product product) => false;
}

/// Implementation for "Stores" of an [AbstractSimpleInputPageHelper].
Expand Down Expand Up @@ -396,6 +399,16 @@ class SimpleInputPageLabelHelper extends AbstractSimpleInputPageHelper {

/// Implementation for "Categories" of an [AbstractSimpleInputPageHelper].
class SimpleInputPageCategoryHelper extends AbstractSimpleInputPageHelper {
@override
bool isOwnerField(final Product product) =>
product.getOwnerFieldTimestamp(
OwnerField.productField(
ProductField.CATEGORIES,
ProductQuery.getLanguage(),
),
) !=
null;

@override
List<String> initTerms(final Product product) =>
product.categoriesTagsInLanguages?[getLanguage()] ?? <String>[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SimpleInputTextField extends StatefulWidget {
this.shapeProvider,
this.padding,
required this.productType,
this.suffixIcon,
});

final FocusNode focusNode;
Expand All @@ -33,6 +34,7 @@ class SimpleInputTextField extends StatefulWidget {
final String? Function()? shapeProvider;
final EdgeInsetsGeometry? padding;
final ProductType? productType;
final Widget? suffixIcon;

@override
State<SimpleInputTextField> createState() => _SimpleInputTextFieldState();
Expand Down Expand Up @@ -81,6 +83,7 @@ class _SimpleInputTextFieldState extends State<SimpleInputTextField> {
hintText: widget.hintText,
constraints: widget.constraints,
manager: _manager,
suffixIcon: widget.suffixIcon,
),
),
if (widget.withClearButton)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/helpers/collections_helper.dart';
import 'package:smooth_app/helpers/haptic_feedback_helper.dart';
import 'package:smooth_app/pages/product/explanation_widget.dart';
import 'package:smooth_app/pages/product/owner_field_info.dart';
import 'package:smooth_app/pages/product/simple_input_page_helpers.dart';
import 'package:smooth_app/pages/product/simple_input_text_field.dart';

Expand Down Expand Up @@ -61,6 +62,7 @@ class _SimpleInputWidgetState extends State<SimpleInputWidget> {
context,
widget.product,
);
final bool isOwnerField = widget.helper.isOwnerField(widget.product);

return Column(
mainAxisAlignment: MainAxisAlignment.start,
Expand Down Expand Up @@ -96,6 +98,7 @@ class _SimpleInputWidgetState extends State<SimpleInputWidget> {
start: 9.0,
),
productType: widget.product.productType,
suffixIcon: !isOwnerField ? null : const OwnerFieldIcon(),
),
),
Tooltip(
Expand Down

0 comments on commit 8fc4174

Please sign in to comment.