From 1f8f66cd8a07129f84413adaed8eaf4213da5a07 Mon Sep 17 00:00:00 2001 From: Edouard Marquez Date: Mon, 6 Jan 2025 20:21:26 +0100 Subject: [PATCH] Ignore existing Robotoff questions (#6130) --- .../lib/helpers/robotoff_question_helper.dart | 25 +++++++++++++++++++ .../product_page/new_product_page.dart | 18 ++++++------- .../product/product_questions_widget.dart | 18 +++++++++++-- 3 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 packages/smooth_app/lib/helpers/robotoff_question_helper.dart diff --git a/packages/smooth_app/lib/helpers/robotoff_question_helper.dart b/packages/smooth_app/lib/helpers/robotoff_question_helper.dart new file mode 100644 index 000000000000..fe1ea83774a7 --- /dev/null +++ b/packages/smooth_app/lib/helpers/robotoff_question_helper.dart @@ -0,0 +1,25 @@ +import 'package:collection/collection.dart'; +import 'package:openfoodfacts/openfoodfacts.dart'; + +extension RobotoffQuestionHelper on List { + bool areSameAs(List questions) { + return equals(questions, RobotoffQuestionsEquality()); + } +} + +class RobotoffQuestionsEquality implements Equality { + @override + bool equals(RobotoffQuestion e1, RobotoffQuestion e2) => + e1.insightId == e2.insightId; + + @override + int hash(RobotoffQuestion e) => e.insightId.hashCode; + + @override + bool isValidKey(Object? o) { + if (o is RobotoffQuestion) { + return true; + } + throw UnimplementedError(); + } +} diff --git a/packages/smooth_app/lib/pages/product/product_page/new_product_page.dart b/packages/smooth_app/lib/pages/product/product_page/new_product_page.dart index 4fcff02ae5fa..5275bc1023f7 100644 --- a/packages/smooth_app/lib/pages/product/product_page/new_product_page.dart +++ b/packages/smooth_app/lib/pages/product/product_page/new_product_page.dart @@ -142,7 +142,10 @@ class ProductPageState extends State }, child: hasPendingOperations ? const ProductPageLoadingIndicator() - : ProductQuestionsWidget(upToDateProduct), + : KeepQuestionWidgetAlive( + keepWidgetAlive: _keepRobotoffQuestionsAlive, + child: ProductQuestionsWidget(upToDateProduct), + ), ), ), ], @@ -194,14 +197,11 @@ class ProductPageState extends State child: HeroMode( enabled: widget.withHeroAnimation && widget.heroTag?.isNotEmpty == true, - child: KeepQuestionWidgetAlive( - keepWidgetAlive: _keepRobotoffQuestionsAlive, - child: SummaryCard( - upToDateProduct, - _productPreferences, - heroTag: widget.heroTag, - isFullVersion: true, - ), + child: SummaryCard( + upToDateProduct, + _productPreferences, + heroTag: widget.heroTag, + isFullVersion: true, ), ), ), diff --git a/packages/smooth_app/lib/pages/product/product_questions_widget.dart b/packages/smooth_app/lib/pages/product/product_questions_widget.dart index 24109dc0a384..072668aee26d 100644 --- a/packages/smooth_app/lib/pages/product/product_questions_widget.dart +++ b/packages/smooth_app/lib/pages/product/product_questions_widget.dart @@ -9,6 +9,7 @@ import 'package:smooth_app/generic_lib/design_constants.dart'; import 'package:smooth_app/generic_lib/duration_constants.dart'; import 'package:smooth_app/helpers/analytics_helper.dart'; import 'package:smooth_app/helpers/robotoff_insight_helper.dart'; +import 'package:smooth_app/helpers/robotoff_question_helper.dart'; import 'package:smooth_app/pages/hunger_games/question_page.dart'; import 'package:smooth_app/query/product_questions_query.dart'; import 'package:smooth_app/themes/theme_provider.dart'; @@ -87,13 +88,20 @@ class _ProductQuestionsWidgetState extends State if (context.mounted && answeredQuestions != null && answeredQuestions > 0) { return _reloadQuestions( updateInsightAnnotations: true, + ignoreExistingQuestions: true, ); } } Future _reloadQuestions({ bool updateInsightAnnotations = false, + bool ignoreExistingQuestions = false, }) async { + final List? currentQuestions = + _state is _ProductQuestionsWithQuestions + ? (_state as _ProductQuestionsWithQuestions).questions + : null; + setState(() => _state = const _ProductQuestionsLoading()); final List questions = @@ -118,8 +126,14 @@ class _ProductQuestionsWidgetState extends State } if (questions.isNotEmpty == true && !_annotationVoted) { - setState(() => _state = _ProductQuestionsWithQuestions(questions)); - _trackEvent(AnalyticsEvent.questionVisible); + if (ignoreExistingQuestions && + currentQuestions != null && + questions.areSameAs(currentQuestions)) { + setState(() => _state = const _ProductQuestionsWithoutQuestions()); + } else { + setState(() => _state = _ProductQuestionsWithQuestions(questions)); + _trackEvent(AnalyticsEvent.questionVisible); + } } else { setState(() => _state = const _ProductQuestionsWithoutQuestions()); }