import 'package:flutter/foundation.dart'; import 'package:openfoodfacts/openfoodfacts.dart'; import 'package:smooth_app/database/dao_string_list_map.dart'; import 'package:smooth_app/database/local_database.dart'; import 'package:smooth_app/query/product_questions_query.dart'; class RobotoffInsightHelper { const RobotoffInsightHelper(this._localDatabase); final LocalDatabase _localDatabase; Future cacheInsightAnnotationVoted( String barcode, String insightId, ) async { await DaoStringListMap(_localDatabase).add(barcode, insightId); } Future areQuestionsAlreadyVoted( List questions, ) async { final Map> votedHist = await DaoStringListMap( _localDatabase, ).getAll(); final Set newIdsSet = questions .map((RobotoffQuestion e) => e.insightId) .whereType() .toSet(); final Iterable> dbInsights = votedHist.values; return dbInsights.any((List votedIds) { final Set votedSet = votedIds.toSet(); return setEquals(newIdsSet, votedSet); }); } Future removeInsightAnnotationsSavedForProduct(String barcode) async { await DaoStringListMap(_localDatabase).removeKey(barcode); } Future clearInsightAnnotationsSaved() async { final Map> records = await DaoStringListMap( _localDatabase, ).getAll(); for (final String barcode in records.keys) { final List questions = await ProductQuestionsQuery( barcode, ).getQuestions(_localDatabase, 1); if (questions.isEmpty) { await DaoStringListMap(_localDatabase).removeKey(barcode); } } } }