Skip to content

Commit

Permalink
Fix calories calculations for entries - 1.0.23 πŸš€
Browse files Browse the repository at this point in the history
I am clinically insane! Hahahahahahahahah
  • Loading branch information
brandonp2412 committed May 31, 2024
1 parent bdd3c60 commit 3e74c8c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 35 deletions.
2 changes: 2 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/243.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fix calories calculations for entries
- Fix entering amounts other than serving
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 21 additions & 34 deletions lib/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,36 @@ EntriesCompanion calculateEntry({
unit: Value(unit),
);

double quantity100G;
if (unit == 'kilojoules') {
final grams = quantity / 4.184;
quantity100G = grams / 100;
} else if (unit == 'serving') {
quantity100G = quantity;
} else {
quantity100G = convertToGrams(quantity, unit) / 100;
}

double servingWeight;
if (food.servingWeight1G != null) {
servingWeight = food.servingWeight1G!;
if (food.servingUnit != 'grams') {
servingWeight =
convertToGrams(servingWeight, food.servingUnit ?? 'grams');
}
} else {
servingWeight = 100;
}
final servingG = convertToGrams(
food.servingWeight1G ?? 100,
food.servingUnit ?? 'grams',
);

final servingQuantity = quantity100G * servingWeight;
var quantityG = 0.0;
if (unit == 'serving')
quantityG = quantity * (food.servingWeight1G ?? 100);
else
quantityG = convertToGrams(quantity, unit);

final kCalories = servingQuantity * (food.calories ?? 100) / servingWeight;
final proteinG = servingQuantity * (food.proteinG ?? 0) / servingWeight;
final fatG = servingQuantity * (food.fatG ?? 0) / servingWeight;
final carbG = servingQuantity * (food.carbohydrateG ?? 0) / servingWeight;
final caloriesG = (food.calories ?? 0) / servingG;
final proteinG = (food.proteinG ?? 0) / servingG;
final fatG = (food.fatG ?? 0) / servingG;
final carbG = (food.carbohydrateG ?? 0) / servingG;

entry = entry.copyWith(
kCalories: Value(kCalories),
fatG: Value(fatG),
carbG: Value(carbG),
proteinG: Value(proteinG),
return entry.copyWith(
kCalories: Value(quantityG * caloriesG),
fatG: Value(quantityG * fatG),
carbG: Value(quantityG * carbG),
proteinG: Value(quantityG * proteinG),
);

return entry;
}

double convertToGrams(double quantity, String unit) {
double quantityInGrams;

switch (unit) {
case 'grams':
case 'milliliters':
quantityInGrams = quantity;
break;
case 'milligrams':
Expand All @@ -85,8 +72,8 @@ double convertToGrams(double quantity, String unit) {
case 'liters':
quantityInGrams = quantity * 1000; // Approximate conversion for water
break;
case 'milliliters':
quantityInGrams = quantity; // Approximate conversion for water
case 'kilojoules':
quantityInGrams = quantity / 4.184;
break;
default:
throw Exception('Unit not recognized');
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: fit_book
description: Track your calories - Completely offline!
publish_to: none
version: 1.0.22+23
version: 1.0.23+24
environment:
sdk: '>=3.3.3 <4.0.0'
dependencies:
Expand Down

0 comments on commit 3e74c8c

Please sign in to comment.