Files
flutter/lib/features/nutrition/models/ingredient.dart
2026-07-01 20:28:06 +02:00

181 lines
4.9 KiB
Dart

/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 wger Team
*
* wger Workout Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:json_annotation/json_annotation.dart';
import 'package:wger/core/json.dart';
import 'package:wger/features/nutrition/models/ingredient_image.dart';
import 'package:wger/features/nutrition/models/ingredient_weight_unit.dart';
import 'package:wger/features/nutrition/models/nutritional_values.dart';
part 'ingredient.g.dart';
enum NutriScore {
@JsonValue('a')
a,
@JsonValue('b')
b,
@JsonValue('c')
c,
@JsonValue('d')
d,
@JsonValue('e')
e,
}
@JsonSerializable()
class Ingredient {
// fields returned by django api that we ignore here:
// uuid, last_updated, last_imported, language
// most license fields
@JsonKey(required: true)
final int id;
// some ingredients don't have these 3 fields set. E.g. USDA entries that
// have been removed upstream, or manually added ingredients.
@JsonKey(required: true, name: 'remote_id')
final String? remoteId;
final int? languageId;
@JsonKey(required: true, name: 'source_name')
final String? sourceName;
@JsonKey(required: true, name: 'source_url')
final String? sourceUrl;
@JsonKey(required: true, name: 'license_object_url')
final String? licenseObjectURl;
/// Barcode of the product
@JsonKey(required: true)
final String? code;
/// Name of the product
@JsonKey(required: true)
final String name;
/// Brand of the product
@JsonKey(name: 'brand')
final String? brand;
@JsonKey(required: true, name: 'created')
final DateTime created;
/// Energy in kCal per 100g of product
@JsonKey(required: true)
final int energy;
/// g per 100g of product
@JsonKey(required: true, fromJson: stringToNum, toJson: numToString)
final num carbohydrates;
/// g per 100g of product. Nullable per Django (`null=True`).
@JsonKey(
required: true,
fromJson: stringToNumNull,
toJson: numToString,
name: 'carbohydrates_sugar',
)
final num? carbohydratesSugar;
/// g per 100g of product
@JsonKey(required: true, fromJson: stringToNum, toJson: numToString)
final num protein;
/// g per 100g of product
@JsonKey(required: true, fromJson: stringToNum, toJson: numToString)
final num fat;
/// g per 100g of product. Nullable per Django (`null=True`).
@JsonKey(
required: true,
fromJson: stringToNumNull,
toJson: numToString,
name: 'fat_saturated',
)
final num? fatSaturated;
/// g per 100g of product. Nullable per Django (`null=True`).
@JsonKey(required: true, fromJson: stringToNumNull, toJson: numToString)
final num? fiber;
/// g per 100g of product. Nullable per Django (`null=True`).
@JsonKey(required: true, fromJson: stringToNumNull, toJson: numToString)
final num? sodium;
@JsonKey(name: 'is_vegan')
final bool? isVegan;
@JsonKey(name: 'is_vegetarian')
final bool? isVegetarian;
@JsonKey(name: 'nutriscore')
final NutriScore? nutriscore;
IngredientImage? image;
@JsonKey(name: 'weight_units', defaultValue: <IngredientWeightUnit>[])
List<IngredientWeightUnit> weightUnits;
Ingredient({
required this.remoteId,
required this.sourceName,
required this.sourceUrl,
this.languageId,
this.licenseObjectURl,
required this.id,
required this.code,
required this.name,
this.brand,
required this.created,
required this.energy,
required this.carbohydrates,
this.carbohydratesSugar,
required this.protein,
required this.fat,
this.fatSaturated,
this.fiber,
this.sodium,
this.isVegan,
this.isVegetarian,
this.nutriscore,
this.image,
this.weightUnits = const [],
});
// Boilerplate
factory Ingredient.fromJson(Map<String, dynamic> json) => _$IngredientFromJson(json);
Map<String, dynamic> toJson() => _$IngredientToJson(this);
NutritionalValues get nutritionalValues {
// Treat missing macronutrient data as zero, better to show "0 g
// fiber" than to omit/explode for ingredients with sparse data.
return NutritionalValues.values(
energy * 1,
protein * 1,
carbohydrates * 1,
(carbohydratesSugar ?? 0) * 1,
fat * 1,
(fatSaturated ?? 0) * 1,
(fiber ?? 0) * 1,
(sodium ?? 0) * 1,
);
}
}