Skip to content

Commit

Permalink
Merge pull request #29 from openfoodfacts/smooth-app
Browse files Browse the repository at this point in the history
Fixed servingQuantity issue raised by 0.3.4
  • Loading branch information
PrimaelQuemerais committed Jun 28, 2020
2 parents d5ad7d6 + 71d07f8 commit 21df57e
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [0.3.5] - 28.06.2020
- fixed servingQuantity issue introduced in 0.3.4

## [0.3.4] - 25.06.2020
- selectedImages toJson implementation
- codebase improvements
Expand Down
4 changes: 2 additions & 2 deletions lib/model/Product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class Product extends JsonObject {
String imgSmallUrl;
@JsonKey(name: 'serving_size')
String servingSize;
@JsonKey(name: 'serving_quantity')
String servingQuantity;
@JsonKey(name: 'serving_quantity', fromJson: JsonHelper.servingQuantityFromJson)
double servingQuantity;
@JsonKey(name: 'product_quantity')
dynamic packagingQuantity;

Expand Down
3 changes: 2 additions & 1 deletion lib/model/Product.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions lib/utils/JsonHelper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,19 @@ class JsonHelper {
// not implemented and needed, yet.
return Map<String, dynamic>();
}

static double servingQuantityFromJson(dynamic data) {
if(data is double) {
return data;
}
if(data is int) {
return data.toDouble();
}
try {
return double.parse(data);
} catch(e) {
print("Unable to parse data to double : $e");
return null;
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: openfoodfacts
description: Dart package for the Open Food Facts API, a food products database made by everyone, for everyone.
version: 0.3.4
version: 0.3.5
#authors:
#- Alexander Schacht <grumpf@gmx.de>
#- Primaël Quémerais <primael@reefind.com>
Expand Down
5 changes: 0 additions & 5 deletions test/api_test_getProduct.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ void main() {
result.product.ingredients.forEach((element) {
print(element.toData().toString());});
expect(result.product.ingredients.length, 13);
print("Serving Size: ");
print(result.product.servingSize);

expect(result.product.ingredients.any((i) => i.text == "Wasser"), true);
expect(
Expand Down Expand Up @@ -87,8 +85,6 @@ void main() {
expect(result.product.additives.ids[4], "en:e950");
expect(result.product.additives.names[4], "E950");

expect(result.product.servingSize == null, true);

expect(
result.product.nutrientLevels.levels[NutrientLevels.NUTRIENT_SUGARS],
Level.LOW);
Expand Down Expand Up @@ -122,7 +118,6 @@ void main() {
expect(result.product != null, true);
expect(result.product.barcode, barcode);

expect(result.product.servingSize != null, true);
expect(result.product.nutriments.carbohydratesServing != null, true);
expect(result.product.nutriments.proteinsServing != null, true);
expect(result.product.nutriments.saltServing != null, true);
Expand Down
2 changes: 0 additions & 2 deletions test/api_test_getProductRaw.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ void main() {
expect(result.product != null, true);
expect(result.product.barcode, barcode);

expect(result.product.servingSize != null, true);
expect(result.product.nutriments.carbohydratesServing != null, true);
expect(result.product.nutriments.proteinsServing != null, true);
expect(result.product.nutriments.saltServing != null, true);
Expand Down Expand Up @@ -135,7 +134,6 @@ void main() {
expect(result.product.nutriments.proteins, null);
expect(result.product.nutriments.novaGroup, 1);
expect(result.product.nutriments.fatServing == null, true);
expect(result.product.servingSize == null, true);
});
});
}
10 changes: 5 additions & 5 deletions test/api_test_saveProduct.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import 'test_constants.dart';
void main() {
group('$OpenFoodAPIClient add new products', () {
setUpAll(() async {
new HttpHelper().isTestMode = true;
HttpHelper().isTestMode = true;
});

test('add new product test 1', () async {
Product product = new Product(
Product product = Product(
barcode: "0048151623426",
productName: "Maryland Choc Chip",
quantity: "230g",
Expand All @@ -35,7 +35,7 @@ void main() {
});

test('add new product test 2', () async {
Product product = new Product(
Product product = Product(
barcode: "8008698011065",
productName: "Meisterbäckers Vital",
quantity: "350g",
Expand All @@ -51,7 +51,7 @@ void main() {
});

test('add new product test 3', () async {
Product product = new Product(
Product product = Product(
barcode: "4250752200784",
productName: "Johanneskraut-Rotöl Kapseln",
quantity: "30 Kapseln",
Expand All @@ -69,7 +69,7 @@ void main() {
});

test('add new product test 4', () async {
Product product = new Product(
Product product = Product(
barcode: "4052700676180",
productName: "Calcium + Vitamin D3 + C",
quantity: "14 Tabletten",
Expand Down

0 comments on commit 21df57e

Please sign in to comment.