Skip to content

Commit

Permalink
Merge pull request #160 from PDG-NUTRI/151-ajouter-backend-dans-addme…
Browse files Browse the repository at this point in the history
…al-et-modification

151 ajouter backend dans addmeal et modification
  • Loading branch information
LucaCoduriV authored Sep 4, 2022
2 parents 2461d43 + 5522960 commit f3fe0a6
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 101 deletions.
42 changes: 21 additions & 21 deletions lib/api/firebase_meal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ class FirebaseMeal extends FirebaseAPI implements IMeal {
FirebaseMeal(FirebaseFirestore db) : super(db, 'meal');

@override
void createMeal(Meal meal) {
collectionReference
.withConverter(
fromFirestore: Meal.fromFirestore,
toFirestore: (Meal meal, options) => meal.toFirestore())
.doc(meal.uid)
.set(meal)
.then((value) => log("Meal Added"))
.catchError((error) {
log("Failed to add meal: $error");
throw Exception(error);
});
Future<void> createMeal(Meal meal) async {
try {
await collectionReference
.withConverter(
fromFirestore: Meal.fromFirestore,
toFirestore: (Meal meal, options) => meal.toFirestore())
.doc(meal.uid)
.set(meal);
log("Meal Added");
} catch (e) {
log("Failed to add meal: $e");
throw Exception(e);
}
}

@override
Expand All @@ -41,15 +42,14 @@ class FirebaseMeal extends FirebaseAPI implements IMeal {
}

@override
void updateMeal(Meal meal) {
collectionReference
.doc(meal.uid)
.update(meal.toFirestore())
.then((value) => log("Meal Updated"))
.catchError((error) {
log("Failed to update meal: $error");
throw Exception(error);
});
Future<void> updateMeal(Meal meal) async {
try {
await collectionReference.doc(meal.uid).update(meal.toFirestore());
log("Meal Updated");
} catch (e) {
log("Failed to update meal: $e");
throw Exception(e);
}
}

@override
Expand Down
4 changes: 2 additions & 2 deletions lib/api/imeal.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:pdg_app/model/meal.dart';

abstract class IMeal {
void createMeal(Meal meal);
Future<void> createMeal(Meal meal);
Future<Meal> readMeal(String mealId);
void updateMeal(Meal meal);
Future<void> updateMeal(Meal meal);
void deleteMeal(String mealId);
Future<List<Meal>> getUsersMealForDay(String userId, DateTime day);
Future<List<Meal>> getUserMeal(String userId);
Expand Down
7 changes: 6 additions & 1 deletion lib/provider/meal_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ class MealProvider extends ChangeNotifier {
}

Future<void> addMeal(Meal meal) async {
_mealApi.createMeal(meal);
await _mealApi.createMeal(meal);
notifyListeners();
}

Future<void> updateMeal(Meal meal) async {
await _mealApi.updateMeal(meal);
notifyListeners();
}
}
14 changes: 9 additions & 5 deletions lib/router/router.gr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ class AppRouter extends _i17.RootStackRouter {
final args = routeData.argsAs<AddMealScreenRouteArgs>();
return _i17.MaterialPageX<_i23.Meal?>(
routeData: routeData,
child: _i13.AddMealScreen(day: args.day, key: args.key));
child: _i13.AddMealScreen(
day: args.day, meal: args.meal, key: args.key));
},
RegisterFirstPageRoute.name: (routeData) {
return _i17.MaterialPageX<dynamic>(
Expand Down Expand Up @@ -394,23 +395,26 @@ class DiaryScreenRoute extends _i17.PageRouteInfo<void> {
/// generated route for
/// [_i13.AddMealScreen]
class AddMealScreenRoute extends _i17.PageRouteInfo<AddMealScreenRouteArgs> {
AddMealScreenRoute({required DateTime day, _i18.Key? key})
AddMealScreenRoute({required DateTime day, _i23.Meal? meal, _i18.Key? key})
: super(AddMealScreenRoute.name,
path: 'add', args: AddMealScreenRouteArgs(day: day, key: key));
path: 'add',
args: AddMealScreenRouteArgs(day: day, meal: meal, key: key));

static const String name = 'AddMealScreenRoute';
}

class AddMealScreenRouteArgs {
const AddMealScreenRouteArgs({required this.day, this.key});
const AddMealScreenRouteArgs({required this.day, this.meal, this.key});

final DateTime day;

final _i23.Meal? meal;

final _i18.Key? key;

@override
String toString() {
return 'AddMealScreenRouteArgs{day: $day, key: $key}';
return 'AddMealScreenRouteArgs{day: $day, meal: $meal, key: $key}';
}
}

Expand Down
160 changes: 117 additions & 43 deletions lib/screens/add_meal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import '../widgets/slider_with_text.dart';

class AddMealScreen extends StatefulWidget {
final DateTime _day;
const AddMealScreen({required DateTime day, Key? key})
final Meal? _meal;

const AddMealScreen({required DateTime day, Meal? meal, Key? key})
: _day = day,
_meal = meal,
super(key: key);

@override
Expand All @@ -30,7 +33,7 @@ enum _TimeButtonEnum {
}

class _AddMealScreenState extends State<AddMealScreen> {
double _hungerBeforeValue = 3;
double _hungerBeforeValue = 5;
double _hungerAfterValue = 3;
final ImagePicker _picker = ImagePicker();
XFile? _image;
Expand All @@ -39,6 +42,31 @@ class _AddMealScreenState extends State<AddMealScreen> {
TimeOfDay? _startTime;
TimeOfDay? _endTime;
final TextEditingController _nameTextController = TextEditingController();
final TextEditingController _settingsController = TextEditingController();
final TextEditingController _commentController = TextEditingController();

@override
void initState() {
if (widget._meal != null) {
_nameTextController.text = widget._meal!.title;

if (widget._meal!.setting != null) {
_settingsController.text = widget._meal!.setting!;
}

if (widget._meal!.comment != null) {
_commentController.text = widget._meal!.comment!;
}

_hungerBeforeValue = widget._meal!.hunger.toDouble();
_hungerAfterValue = widget._meal!.satiety.toDouble();

_startTime = TimeOfDay.fromDateTime(widget._meal!.startTime);
_endTime = TimeOfDay.fromDateTime(widget._meal!.endTime);
}

super.initState();
}

Future<XFile?> _takePicture() async {
final XFile? image = await _picker.pickImage(source: ImageSource.camera);
Expand All @@ -54,6 +82,8 @@ class _AddMealScreenState extends State<AddMealScreen> {
Widget build(BuildContext context) {
return AddMeal(
nameTextController: _nameTextController,
settingsController: _settingsController,
commentController: _commentController,
hungerBeforeValue: _hungerBeforeValue,
hungerAfterValue: _hungerAfterValue,
onHungerAfterChanged: (value) => setState(() {
Expand Down Expand Up @@ -125,12 +155,28 @@ class _AddMealScreenState extends State<AddMealScreen> {
_endTime?.hour ?? 0,
_endTime?.minute ?? 0,
);
if (widget._meal == null) {
AutoRouter.of(context).pop(Meal(
title: _nameTextController.text,
startTime: selectedStartDate,
endTime: selectedEndDate,
hunger: _hungerBeforeValue.toInt(),
satiety: _hungerAfterValue.toInt(),
setting: _settingsController.text,
comment: _commentController.text,
owner: context.read<AuthProvider>().userUid,
));
return;
}
AutoRouter.of(context).pop(Meal(
uid: widget._meal!.uid,
title: _nameTextController.text,
startTime: selectedStartDate,
endTime: selectedEndDate,
hunger: 0,
satiety: 0,
hunger: _hungerBeforeValue.toInt(),
satiety: _hungerAfterValue.toInt(),
setting: _settingsController.text,
comment: _commentController.text,
owner: context.read<AuthProvider>().userUid,
));
},
Expand All @@ -155,26 +201,30 @@ class AddMeal extends StatelessWidget {
final String? _endTimeText;
final void Function()? _onValidatePressed;
final TextEditingController? _nameTextController;
final TextEditingController? _settingsController;
final TextEditingController? _commentController;

const AddMeal({
Key? key,
required double hungerBeforeValue,
required double hungerAfterValue,
required void Function(double) onHungerBeforeChanged,
required void Function(double) onHungerAfterChanged,
required void Function() onCameraPressed,
required void Function() onGalleryPressed,
required void Function(TimeOfDay) onTimeSelected,
void Function()? onStartTimeSelected,
void Function()? onEndTimeSelected,
void Function()? onTimeSelectCanceled,
void Function()? onValidatePressed,
bool showTimePicker = false,
String? startTimeText,
String? endTimeText,
XFile? image,
TextEditingController? nameTextController,
}) : _hungerBeforeValue = hungerBeforeValue,
const AddMeal(
{Key? key,
required double hungerBeforeValue,
required double hungerAfterValue,
required void Function(double) onHungerBeforeChanged,
required void Function(double) onHungerAfterChanged,
required void Function() onCameraPressed,
required void Function() onGalleryPressed,
required void Function(TimeOfDay) onTimeSelected,
void Function()? onStartTimeSelected,
void Function()? onEndTimeSelected,
void Function()? onTimeSelectCanceled,
void Function()? onValidatePressed,
bool showTimePicker = false,
String? startTimeText,
String? endTimeText,
XFile? image,
TextEditingController? nameTextController,
TextEditingController? settingsController,
TextEditingController? commentController})
: _hungerBeforeValue = hungerBeforeValue,
_hungerAfterValue = hungerAfterValue,
_onHungerAfterChanged = onHungerAfterChanged,
_onHungerBeforeChanged = onHungerBeforeChanged,
Expand All @@ -190,6 +240,8 @@ class AddMeal extends StatelessWidget {
_endTimeText = endTimeText,
_onValidatePressed = onValidatePressed,
_nameTextController = nameTextController,
_settingsController = settingsController,
_commentController = commentController,
super(key: key);

@override
Expand All @@ -207,6 +259,8 @@ class AddMeal extends StatelessWidget {
Expanded(
child: _ListView(
nameTextController: _nameTextController,
settingsController: _settingsController,
commentController: _commentController,
hungerAfterValue: _hungerAfterValue,
hungerBeforeValue: _hungerBeforeValue,
onHungerAfterChanged: _onHungerAfterChanged,
Expand Down Expand Up @@ -331,19 +385,23 @@ class _ListView extends StatelessWidget {
final String? _startTimeText;
final String? _endTimeText;
final TextEditingController? _nameTextController;
final TextEditingController? _settingsController;
final TextEditingController? _commentController;

const _ListView({
Key? key,
required double hungerBeforeValue,
required double hungerAfterValue,
required void Function(double) onHungerBeforeChanged,
required void Function(double) onHungerAfterChanged,
void Function()? onStartTimePress,
void Function()? onEndTimePress,
String? startTimeText,
String? endTimeText,
TextEditingController? nameTextController,
}) : _hungerBeforeValue = hungerBeforeValue,
const _ListView(
{Key? key,
required double hungerBeforeValue,
required double hungerAfterValue,
required void Function(double) onHungerBeforeChanged,
required void Function(double) onHungerAfterChanged,
void Function()? onStartTimePress,
void Function()? onEndTimePress,
String? startTimeText,
String? endTimeText,
TextEditingController? nameTextController,
TextEditingController? settingsController,
TextEditingController? commentController})
: _hungerBeforeValue = hungerBeforeValue,
_hungerAfterValue = hungerAfterValue,
_onHungerAfterChanged = onHungerAfterChanged,
_onHungerBeforeChanged = onHungerBeforeChanged,
Expand All @@ -352,6 +410,8 @@ class _ListView extends StatelessWidget {
_startTimeText = startTimeText,
_endTimeText = endTimeText,
_nameTextController = nameTextController,
_settingsController = settingsController,
_commentController = commentController,
super(key: key);

List<Widget> listViewContent(BuildContext context) => [
Expand Down Expand Up @@ -384,22 +444,36 @@ class _ListView extends StatelessWidget {
value: _hungerBeforeValue,
onChanged: _onHungerBeforeChanged,
labels: const [
"encore faim",
"inconfort",
"léger inconfort",
"confort"
"0 - not hungry",
"1",
"2",
"3",
"4",
"5 - really hungry"
]),
SliderWithText(
context: context,
text: "Rate your satiety after eating",
value: _hungerAfterValue,
onChanged: _onHungerAfterChanged,
labels: const [
"encore faim",
"inconfort",
"léger inconfort",
"confort"
"still hungry",
"discomfort",
"mild discomfort",
"comfort"
]),
MainTextField(
name: "Settings",
icon: const Icon(Icons.people_alt, color: Colors.black),
maxLines: null,
controller: _settingsController,
),
MainTextField(
name: "Comments",
icon: const Icon(Icons.comment, color: Colors.black),
maxLines: null,
controller: _commentController,
),
];

@override
Expand Down
Loading

0 comments on commit f3fe0a6

Please sign in to comment.