forked from eatvio/eatvio_app
Merge remote-tracking branch 'upstream/main'
This commit is contained in:
@@ -10,27 +10,43 @@ import 'package:eatvio_app/features/glossary/repositories/glossary_repository.da
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class DiaryCubit extends Cubit<DiaryState> {
|
||||
late final DateTime today;
|
||||
late DateTime selectedDay;
|
||||
late int selectedMeal;
|
||||
final DiaryRepository diaryRepository;
|
||||
final GlossaryRepository glossaryRepository;
|
||||
|
||||
DiaryCubit({
|
||||
required this.diaryRepository,
|
||||
required this.glossaryRepository,
|
||||
}) : super(const DiaryState.loading());
|
||||
}) : super(const DiaryState.loading()) {
|
||||
today = DateTime.now();
|
||||
selectedDay = today;
|
||||
}
|
||||
|
||||
getDiaryDay(String date, [bool needLoading = true]) async {
|
||||
if (needLoading) emit(const DiaryState.loading());
|
||||
setSelectedDay(DateTime day) async {
|
||||
selectedDay = day;
|
||||
await fetchDiary(true);
|
||||
}
|
||||
|
||||
setSelectedMeal(int meal) {
|
||||
selectedMeal = meal;
|
||||
emit((state as DiaryStateLoaded).copyWith(selectedMeal: selectedMeal));
|
||||
}
|
||||
|
||||
fetchDiary([bool needLoading = false]) async {
|
||||
if (needLoading) {
|
||||
emit(const DiaryState.loading());
|
||||
selectedMeal = 1;
|
||||
}
|
||||
try {
|
||||
final diaryDay = await diaryRepository.getDiaryByDate(date);
|
||||
final List<int> satiety = [];
|
||||
for (int i = 0; i < diaryDay.meals.length; i++) {
|
||||
satiety.add(diaryDay.meals[i].satiety);
|
||||
}
|
||||
final diaryDay = await diaryRepository.getDiaryByDate('$selectedDay');
|
||||
emit(
|
||||
DiaryState.loaded(
|
||||
diary: diaryDay,
|
||||
nutrition: await getNutrition((await diaryRepository.getDiaryByDate(date)).id),
|
||||
currentSelectedSatietyRating: satiety,
|
||||
nutrition: await getNutrition((await diaryRepository.getDiaryByDate('$selectedDay')).id),
|
||||
selectedDay: selectedDay,
|
||||
selectedMeal: selectedMeal,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
@@ -40,16 +56,18 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
}
|
||||
|
||||
finishDiaryDay({
|
||||
required String date,
|
||||
required int diaryId,
|
||||
required String wakeUp,
|
||||
required String retire,
|
||||
required String comment,
|
||||
}) async {
|
||||
await diaryRepository.finishDiaryDay(
|
||||
diaryId: diaryId,
|
||||
wakeUp: '',
|
||||
retire: '',
|
||||
comment: '',
|
||||
wakeUp: wakeUp,
|
||||
retire: retire,
|
||||
comment: comment,
|
||||
);
|
||||
await getDiaryDay(date, false);
|
||||
await fetchDiary();
|
||||
}
|
||||
|
||||
Future<NutritionDataModel> getNutrition(int index) async {
|
||||
@@ -62,7 +80,7 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
required String productName,
|
||||
required String portion,
|
||||
required int quantity,
|
||||
required String date,
|
||||
int? productNutritionalValueId,
|
||||
}) async {
|
||||
await diaryRepository.addPortionToMeal(
|
||||
diaryId: diaryId,
|
||||
@@ -70,8 +88,9 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
productName: productName,
|
||||
portion: portion,
|
||||
quantity: quantity,
|
||||
productNutritionalValueId: productNutritionalValueId,
|
||||
);
|
||||
await getDiaryDay(date, false);
|
||||
await fetchDiary();
|
||||
}
|
||||
|
||||
updateProductInMeal({
|
||||
@@ -81,7 +100,7 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
required String productName,
|
||||
required String portion,
|
||||
required int quantity,
|
||||
required String date,
|
||||
int? productNutritionalValueId,
|
||||
}) async {
|
||||
await diaryRepository.updateProductInMeal(
|
||||
diaryId: diaryId,
|
||||
@@ -90,22 +109,22 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
productName: productName,
|
||||
portion: portion,
|
||||
quantity: quantity,
|
||||
productNutritionalValueId: productNutritionalValueId,
|
||||
);
|
||||
await getDiaryDay(date, false);
|
||||
await fetchDiary();
|
||||
}
|
||||
|
||||
deleteProductFromMeal({
|
||||
required int diaryId,
|
||||
required int mealId,
|
||||
required int portionId,
|
||||
required String date,
|
||||
}) async {
|
||||
await diaryRepository.deleteProductFromMeal(
|
||||
diaryId: diaryId,
|
||||
mealId: mealId,
|
||||
portionId: portionId,
|
||||
);
|
||||
await getDiaryDay(date, false);
|
||||
await fetchDiary();
|
||||
}
|
||||
|
||||
Future<List<GlossaryModel>> getGlossaryListByQuery(String query) async {
|
||||
@@ -115,40 +134,25 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
uploadPhotos({
|
||||
required List<File> files,
|
||||
required int diaryId,
|
||||
required int mealId,
|
||||
required String date,
|
||||
}) async {
|
||||
await diaryRepository.uploadPhotos(
|
||||
files: await ImageCompressUtility.compressFilesList(files),
|
||||
diaryId: diaryId,
|
||||
mealId: mealId,
|
||||
mealId: selectedMeal,
|
||||
);
|
||||
await getDiaryDay(date, false);
|
||||
await fetchDiary();
|
||||
}
|
||||
|
||||
deletePhoto({
|
||||
required DiaryPhotoModel photo,
|
||||
required int diaryId,
|
||||
required int mealId,
|
||||
required String date,
|
||||
}) async {
|
||||
await diaryRepository.deletePhoto(
|
||||
photo: photo,
|
||||
diaryId: diaryId,
|
||||
mealId: mealId,
|
||||
mealId: selectedMeal,
|
||||
);
|
||||
await getDiaryDay(date, false);
|
||||
}
|
||||
|
||||
updateCurrentSelectedSatietyRating({
|
||||
required int diaryId,
|
||||
required int meal,
|
||||
required int value,
|
||||
}) {
|
||||
List<int> satiety = [...(state as DiaryStateLoaded).currentSelectedSatietyRating];
|
||||
satiety[meal] = value;
|
||||
diaryRepository.fillSatiety(diaryId: diaryId, meal: meal, satiety: value.toInt() - 1);
|
||||
emit((state as DiaryStateLoaded).copyWith(currentSelectedSatietyRating: satiety));
|
||||
await fetchDiary();
|
||||
}
|
||||
|
||||
Future<GlossaryModerationModel> sendProductToModeration({
|
||||
@@ -166,4 +170,27 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
carbohydrates: carbohydrates,
|
||||
);
|
||||
}
|
||||
|
||||
fillTime({
|
||||
required int diaryId,
|
||||
required String time,
|
||||
}) async {
|
||||
await diaryRepository.fillTime(
|
||||
diaryId: diaryId,
|
||||
mealId: selectedMeal,
|
||||
time: time,
|
||||
);
|
||||
await fetchDiary();
|
||||
}
|
||||
|
||||
fillSatiety({
|
||||
required int diaryId,
|
||||
required int satiety,
|
||||
}) async {
|
||||
await diaryRepository.fillSatiety(
|
||||
diaryId: diaryId,
|
||||
mealId: selectedMeal,
|
||||
satiety: satiety,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user