Skip to content

Commit

Permalink
Ignore existing Robotoff questions (#6130)
Browse files Browse the repository at this point in the history
  • Loading branch information
g123k authored Jan 6, 2025
1 parent eb0a8bf commit 1f8f66c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
25 changes: 25 additions & 0 deletions packages/smooth_app/lib/helpers/robotoff_question_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:collection/collection.dart';
import 'package:openfoodfacts/openfoodfacts.dart';

extension RobotoffQuestionHelper on List<RobotoffQuestion> {
bool areSameAs(List<RobotoffQuestion> questions) {
return equals(questions, RobotoffQuestionsEquality());
}
}

class RobotoffQuestionsEquality implements Equality<RobotoffQuestion> {
@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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ class ProductPageState extends State<ProductPage>
},
child: hasPendingOperations
? const ProductPageLoadingIndicator()
: ProductQuestionsWidget(upToDateProduct),
: KeepQuestionWidgetAlive(
keepWidgetAlive: _keepRobotoffQuestionsAlive,
child: ProductQuestionsWidget(upToDateProduct),
),
),
),
],
Expand Down Expand Up @@ -194,14 +197,11 @@ class ProductPageState extends State<ProductPage>
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,
),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -87,13 +88,20 @@ class _ProductQuestionsWidgetState extends State<ProductQuestionsWidget>
if (context.mounted && answeredQuestions != null && answeredQuestions > 0) {
return _reloadQuestions(
updateInsightAnnotations: true,
ignoreExistingQuestions: true,
);
}
}

Future<void> _reloadQuestions({
bool updateInsightAnnotations = false,
bool ignoreExistingQuestions = false,
}) async {
final List<RobotoffQuestion>? currentQuestions =
_state is _ProductQuestionsWithQuestions
? (_state as _ProductQuestionsWithQuestions).questions
: null;

setState(() => _state = const _ProductQuestionsLoading());

final List<RobotoffQuestion> questions =
Expand All @@ -118,8 +126,14 @@ class _ProductQuestionsWidgetState extends State<ProductQuestionsWidget>
}

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());
}
Expand Down

0 comments on commit 1f8f66c

Please sign in to comment.