mirror of
https://github.com/wger-project/flutter.git
synced 2026-02-18 00:17:48 +01:00
Allow to log meals all at once
This commit is contained in:
@@ -16,26 +16,58 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:wger/models/nutrition/ingredient.dart';
|
||||
import 'package:wger/models/nutrition/ingredient_weight_unit.dart';
|
||||
import 'package:wger/models/nutrition/meal_item.dart';
|
||||
|
||||
part 'log.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class Log {
|
||||
final int id;
|
||||
final int nutritionalPlanId;
|
||||
final DateTime date;
|
||||
final String comment;
|
||||
final Ingredient ingredient;
|
||||
final IngredientWeightUnit weightUnit;
|
||||
final double amount;
|
||||
@JsonKey(required: true)
|
||||
int id;
|
||||
|
||||
@JsonKey(required: true, name: 'plan')
|
||||
int planId;
|
||||
|
||||
@JsonKey(required: true)
|
||||
DateTime datetime;
|
||||
|
||||
String comment;
|
||||
|
||||
@JsonKey(required: true, name: 'ingredient')
|
||||
int ingredientId;
|
||||
|
||||
Ingredient ingredientObj;
|
||||
|
||||
@JsonKey(required: true)
|
||||
int weightUnit;
|
||||
|
||||
IngredientWeightUnit weightUnitObj;
|
||||
|
||||
@JsonKey(required: true)
|
||||
double amount;
|
||||
|
||||
Log({
|
||||
@required this.id,
|
||||
@required this.ingredient,
|
||||
@required this.weightUnit,
|
||||
@required this.amount,
|
||||
@required this.nutritionalPlanId,
|
||||
@required this.date,
|
||||
@required this.comment,
|
||||
this.id,
|
||||
this.ingredientId,
|
||||
this.ingredientObj,
|
||||
this.weightUnit,
|
||||
this.weightUnitObj,
|
||||
this.amount,
|
||||
this.planId,
|
||||
this.datetime,
|
||||
this.comment,
|
||||
});
|
||||
|
||||
Log.fromMealItem(MealItem mealItem) {
|
||||
this.ingredientId = mealItem.ingredientId;
|
||||
this.weightUnit = null;
|
||||
this.amount = mealItem.amount;
|
||||
}
|
||||
|
||||
// Boilerplate
|
||||
factory Log.fromJson(Map<String, dynamic> json) => _$LogFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$LogToJson(this);
|
||||
}
|
||||
|
||||
48
lib/models/nutrition/log.g.dart
Normal file
48
lib/models/nutrition/log.g.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'log.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
Log _$LogFromJson(Map<String, dynamic> json) {
|
||||
$checkKeys(json, requiredKeys: const [
|
||||
'id',
|
||||
'plan',
|
||||
'datetime',
|
||||
'ingredient',
|
||||
'weightUnit',
|
||||
'amount'
|
||||
]);
|
||||
return Log(
|
||||
id: json['id'] as int,
|
||||
ingredientId: json['ingredient'] as int,
|
||||
ingredientObj: json['ingredientObj'] == null
|
||||
? null
|
||||
: Ingredient.fromJson(json['ingredientObj'] as Map<String, dynamic>),
|
||||
weightUnit: json['weightUnit'] as int,
|
||||
weightUnitObj: json['weightUnitObj'] == null
|
||||
? null
|
||||
: IngredientWeightUnit.fromJson(
|
||||
json['weightUnitObj'] as Map<String, dynamic>),
|
||||
amount: (json['amount'] as num)?.toDouble(),
|
||||
planId: json['plan'] as int,
|
||||
datetime: json['datetime'] == null
|
||||
? null
|
||||
: DateTime.parse(json['datetime'] as String),
|
||||
comment: json['comment'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$LogToJson(Log instance) => <String, dynamic>{
|
||||
'id': instance.id,
|
||||
'plan': instance.planId,
|
||||
'datetime': instance.datetime?.toIso8601String(),
|
||||
'comment': instance.comment,
|
||||
'ingredient': instance.ingredientId,
|
||||
'ingredientObj': instance.ingredientObj,
|
||||
'weightUnit': instance.weightUnit,
|
||||
'weightUnitObj': instance.weightUnitObj,
|
||||
'amount': instance.amount,
|
||||
};
|
||||
@@ -22,6 +22,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:wger/models/http_exception.dart';
|
||||
import 'package:wger/models/nutrition/ingredient.dart';
|
||||
import 'package:wger/models/nutrition/log.dart';
|
||||
import 'package:wger/models/nutrition/meal.dart';
|
||||
import 'package:wger/models/nutrition/meal_item.dart';
|
||||
import 'package:wger/models/nutrition/nutritional_plan.dart';
|
||||
@@ -35,6 +36,7 @@ class Nutrition extends WgerBaseProvider with ChangeNotifier {
|
||||
static const mealItemUrl = 'mealitem';
|
||||
static const ingredientUrl = 'ingredient';
|
||||
static const ingredientSearchUrl = 'ingredient/search';
|
||||
static const nutritionDiaryUrl = 'nutritiondiary';
|
||||
|
||||
String _url;
|
||||
Auth _auth;
|
||||
@@ -244,4 +246,21 @@ class Nutrition extends WgerBaseProvider with ChangeNotifier {
|
||||
// Process the response
|
||||
return json.decode(utf8.decode(response.bodyBytes))['suggestions'] as List<dynamic>;
|
||||
}
|
||||
|
||||
/// Log meal to nutrition diary
|
||||
Future<void> addMealToDiary(Meal meal, {http.Client client}) async {
|
||||
if (client == null) {
|
||||
client = http.Client();
|
||||
}
|
||||
|
||||
//var meal = findMealById(mealId);
|
||||
for (var item in meal.mealItems) {
|
||||
Log log = Log.fromMealItem(item);
|
||||
log.planId = findById(meal.plan).id;
|
||||
log.datetime = DateTime.now();
|
||||
|
||||
await add(log.toJson(), client, nutritionDiaryUrl);
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,18 +185,7 @@ class DismissibleMealHeader extends StatelessWidget {
|
||||
|
||||
// Log meal
|
||||
} else {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
content: Text('Would log this meal'),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: Text("Close"),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
Provider.of<Nutrition>(context, listen: false).addMealToDiary(_meal);
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user