Skip to content

Commit 4f5eb52

Browse files
Implemented review comments
1 parent 1e92580 commit 4f5eb52

8 files changed

+55
-83
lines changed
Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,26 @@
11
import 'package:openfoodfacts/src/model/off_tagged.dart';
22

33
enum NutrientModifier implements OffTagged {
4-
approximately,
5-
superiorTo,
6-
inferiorTo;
4+
approximately(offTag: '~'),
5+
greaterThan(offTag: '>'),
6+
lessThan(offTag: '<');
77

8+
const NutrientModifier({
9+
required this.offTag,
10+
});
811
@override
9-
String get offTag => symbol;
10-
}
11-
12-
extension NutrientModifierExtension on NutrientModifier {
13-
String get symbol {
14-
switch (this) {
15-
case NutrientModifier.approximately:
16-
return '~';
17-
case NutrientModifier.superiorTo:
18-
return '>';
19-
case NutrientModifier.inferiorTo:
20-
return '<';
21-
}
22-
}
12+
final String offTag;
2313

24-
static NutrientModifier? fromString(String? modifier) {
25-
for (final NutrientModifier value in NutrientModifier.values) {
26-
if (value.symbol == modifier) {
27-
return value;
28-
}
29-
}
30-
31-
switch (modifier) {
32-
case '~':
33-
return NutrientModifier.approximately;
34-
case '>':
35-
return NutrientModifier.superiorTo;
36-
case '<':
37-
return NutrientModifier.inferiorTo;
38-
default:
39-
return null;
40-
}
41-
}
14+
/// Returns the first [NutrientModifier] that matches the [offTag].
15+
static NutrientModifier? fromOffTag(final String? offTag) =>
16+
OffTagged.fromOffTag(offTag, NutrientModifier.values)
17+
as NutrientModifier?;
4218

43-
static NutrientModifier? fromValue(String value) {
44-
if (value.trim().isEmpty) {
19+
static NutrientModifier? fromValue(final String? value) {
20+
if (value == null || value.isEmpty) {
4521
return null;
4622
}
4723

48-
String modifier = value.trim().substring(0, 1);
49-
return fromString(modifier);
24+
return fromOffTag(value.substring(0, 1));
5025
}
5126
}

lib/src/model/robotoff_nutrient_extraction.dart

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:json_annotation/json_annotation.dart';
2-
import 'package:openfoodfacts/src/model/per_size.dart';
3-
import 'package:openfoodfacts/src/model/robotoff_nutrient_extraction_annotation.dart';
2+
import 'per_size.dart';
3+
import 'robotoff_nutrient_extraction_annotation.dart';
44
import '../interface/json_object.dart';
55
import 'nutrient.dart';
66
import 'robotoff_nutrient_extraction_insight.dart';
@@ -12,14 +12,21 @@ class RobotoffNutrientExtractionResult extends JsonObject {
1212
final String? status;
1313
final int? count;
1414
final List<RobotoffNutrientExtractionInsight>? insights;
15+
RobotoffNutrientExtractionInsight? _latestInsight;
1516

16-
const RobotoffNutrientExtractionResult({
17+
RobotoffNutrientExtractionResult({
1718
this.status,
1819
this.count,
1920
this.insights,
2021
});
2122

22-
RobotoffNutrientExtractionInsight? get getLatestInsights {
23+
RobotoffNutrientExtractionInsight? getLatestInsights({
24+
bool recompute = false,
25+
}) {
26+
if (_latestInsight != null && !recompute) {
27+
return _latestInsight;
28+
}
29+
2330
insights?.sort((a, b) {
2431
if (a.completedAt == null && b.completedAt == null) {
2532
return 0;
@@ -31,17 +38,30 @@ class RobotoffNutrientExtractionResult extends JsonObject {
3138

3239
return a.completedAt!.compareTo(b.completedAt!);
3340
});
34-
return insights?.last;
41+
42+
_latestInsight = insights?.last;
43+
44+
return _latestInsight;
3545
}
3646

3747
RobotoffNutrientEntity? getNutrientEntity(
38-
Nutrient nutrient, PerSize perSize) {
39-
return getLatestInsights
40-
?.data?.nutrients?[nutrient.getOffTagPerSize(perSize)];
48+
Nutrient nutrient,
49+
PerSize perSize, {
50+
bool recomputeLatest = false,
51+
}) {
52+
return getLatestInsights(recompute: recomputeLatest)
53+
?.data
54+
?.nutrients?[nutrient.getOffTagPerSize(perSize)];
4155
}
4256

43-
RobotoffNutrientAnnotationData? getNutrientAnnotation(Nutrient nutrient) {
44-
return getLatestInsights?.data?.annotation?.nutrients?[nutrient.offTag];
57+
RobotoffNutrientAnnotationData? getNutrientAnnotation(
58+
Nutrient nutrient, {
59+
bool recomputeLatest = false,
60+
}) {
61+
return getLatestInsights(recompute: recomputeLatest)
62+
?.data
63+
?.annotation
64+
?.nutrients?[nutrient.offTag];
4565
}
4666

4767
factory RobotoffNutrientExtractionResult.fromJson(

lib/src/model/robotoff_nutrient_extraction_annotation.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ part 'robotoff_nutrient_extraction_annotation.g.dart';
99
class RobotoffNutrientAnnotationData {
1010
@JsonKey(toJson: UnitHelper.unitToString, fromJson: UnitHelper.stringToUnit)
1111
Unit? unit;
12+
@JsonKey(name: 'value')
1213
String valueWithModifer;
1314

1415
RobotoffNutrientAnnotationData({
1516
this.unit,
1617
required this.valueWithModifer,
1718
});
1819

19-
get modifier => NutrientModifierExtension.fromValue(valueWithModifer);
20+
NutrientModifier? get modifier =>
21+
NutrientModifier.fromValue(valueWithModifer);
2022

21-
get value {
23+
double? get value {
2224
if (valueWithModifer.trim().isEmpty) {
2325
return null;
2426
}

lib/src/model/robotoff_nutrient_extraction_annotation.g.dart

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/model/robotoff_nutrient_extraction_insight.dart

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,10 @@ class RobotoffNutrientExtractionInsight extends JsonObject {
5757
// TODO: Figure out what this field is and what type it should be
5858
// final String? type;
5959
final RobotoffNutrientDataWrapper? data;
60-
@JsonKey(
61-
fromJson: JsonHelper.nullableStringTimestampToDate,
62-
toJson: JsonHelper.nullableDateToStringTimestamp)
60+
@JsonKey(fromJson: JsonHelper.nullableStringTimestampToDate)
6361
final DateTime? timestamp;
6462
@JsonKey(
65-
name: 'completed_at',
66-
fromJson: JsonHelper.nullableStringTimestampToDate,
67-
toJson: JsonHelper.nullableDateToStringTimestamp)
63+
name: 'completed_at', fromJson: JsonHelper.nullableStringTimestampToDate)
6864
final DateTime? completedAt;
6965
final int? annotation;
7066
@JsonKey(name: 'annotated_result')
@@ -103,7 +99,6 @@ class RobotoffNutrientExtractionInsight extends JsonObject {
10399
RobotoffNutrientExtractionInsight({
104100
this.insightId,
105101
this.barcode,
106-
// this.type,
107102
this.data,
108103
this.timestamp,
109104
this.completedAt,
@@ -125,7 +120,6 @@ class RobotoffNutrientExtractionInsight extends JsonObject {
125120
this.predictorVersion,
126121
this.campaign,
127122
this.confidence,
128-
// this.boundingBox,
129123
});
130124

131125
factory RobotoffNutrientExtractionInsight.fromJson(

lib/src/model/robotoff_nutrient_extraction_insight.g.dart

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/utils/json_helper.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,6 @@ class JsonHelper {
408408
static DateTime? nullableStringTimestampToDate(dynamic json) =>
409409
json == null ? null : stringTimestampToDate(json);
410410

411-
static String dateToStringTimestamp(DateTime dateTime) =>
412-
dateTime.toIso8601String();
413-
414-
static String? nullableDateToStringTimestamp(DateTime? dateTime) =>
415-
dateTime == null ? null : dateToStringTimestamp(dateTime);
416-
417411
static const String _checkboxOnValue = 'on';
418412
static const String _checkboxOffValue = '';
419413

test/api_get_robotoff_test.dart

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ void main() {
231231
expect(result.status, 'found');
232232
expect(result.insights!, isNotEmpty);
233233
expect(result.insights![0].barcode, barcode);
234+
234235
for (Nutrient nutrient in [
235236
Nutrient.fat,
236237
Nutrient.salt,
@@ -244,19 +245,6 @@ void main() {
244245
]) {
245246
expect(result.getNutrientEntity(nutrient, PerSize.oneHundredGrams),
246247
isNotNull);
247-
}
248-
249-
for (Nutrient nutrient in [
250-
Nutrient.fat,
251-
Nutrient.salt,
252-
Nutrient.fiber,
253-
Nutrient.sugars,
254-
Nutrient.proteins,
255-
Nutrient.energyKJ,
256-
Nutrient.energyKCal,
257-
Nutrient.carbohydrates,
258-
Nutrient.saturatedFat,
259-
]) {
260248
expect(
261249
result.getNutrientAnnotation(nutrient)?.valueWithModifer, isNotNull);
262250
}

0 commit comments

Comments
 (0)