forked from eatvio/eatvio_app
170 lines
4.6 KiB
Dart
170 lines
4.6 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:eatvio_app/core/image_compressing/image_compress_utility.dart';
|
|
import 'package:eatvio_app/features/diary/models/diary_day_model.dart';
|
|
import 'package:eatvio_app/features/diary/models/nutrition_model.dart';
|
|
import 'package:eatvio_app/features/diary/presentation/bloc/diary_state.dart';
|
|
import 'package:eatvio_app/features/diary/repositories/diary_repository.dart';
|
|
import 'package:eatvio_app/features/glossary/models/glossary_model.dart';
|
|
import 'package:eatvio_app/features/glossary/repositories/glossary_repository.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
class DiaryCubit extends Cubit<DiaryState> {
|
|
final DiaryRepository diaryRepository;
|
|
final GlossaryRepository glossaryRepository;
|
|
|
|
DiaryCubit({
|
|
required this.diaryRepository,
|
|
required this.glossaryRepository,
|
|
}) : super(const DiaryState.loading());
|
|
|
|
getDiaryDay(String date, [bool needLoading = true]) async {
|
|
if (needLoading) emit(const DiaryState.loading());
|
|
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);
|
|
}
|
|
emit(
|
|
DiaryState.loaded(
|
|
diary: diaryDay,
|
|
nutrition: await getNutrition((await diaryRepository.getDiaryByDate(date)).id),
|
|
currentSelectedSatietyRating: satiety,
|
|
),
|
|
);
|
|
} catch (e) {
|
|
emit(DiaryState.error(errorMessage: e.toString()));
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
finishDiaryDay({
|
|
required String date,
|
|
required int diaryId,
|
|
}) async {
|
|
await diaryRepository.finishDiaryDay(
|
|
diaryId: diaryId,
|
|
wakeUp: '',
|
|
retire: '',
|
|
comment: '',
|
|
);
|
|
await getDiaryDay(date, false);
|
|
}
|
|
|
|
Future<NutritionDataModel> getNutrition(int index) async {
|
|
return (await diaryRepository.getNutritionData(index));
|
|
}
|
|
|
|
addProductToMeal({
|
|
required int diaryId,
|
|
required int mealId,
|
|
required String productName,
|
|
required String portion,
|
|
required int quantity,
|
|
required String date,
|
|
}) async {
|
|
await diaryRepository.addPortionToMeal(
|
|
diaryId: diaryId,
|
|
mealId: mealId,
|
|
productName: productName,
|
|
portion: portion,
|
|
quantity: quantity,
|
|
);
|
|
await getDiaryDay(date, false);
|
|
}
|
|
|
|
updateProductInMeal({
|
|
required int diaryId,
|
|
required int mealId,
|
|
required int portionId,
|
|
required String productName,
|
|
required String portion,
|
|
required int quantity,
|
|
required String date,
|
|
}) async {
|
|
await diaryRepository.updateProductInMeal(
|
|
diaryId: diaryId,
|
|
mealId: mealId,
|
|
portionId: portionId,
|
|
productName: productName,
|
|
portion: portion,
|
|
quantity: quantity,
|
|
);
|
|
await getDiaryDay(date, false);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
Future<List<GlossaryModel>> getGlossaryListByQuery(String query) async {
|
|
return await glossaryRepository.getGlossaryListByQuery(query);
|
|
}
|
|
|
|
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,
|
|
);
|
|
await getDiaryDay(date, false);
|
|
}
|
|
|
|
deletePhoto({
|
|
required DiaryPhotoModel photo,
|
|
required int diaryId,
|
|
required int mealId,
|
|
required String date,
|
|
}) async {
|
|
await diaryRepository.deletePhoto(
|
|
photo: photo,
|
|
diaryId: diaryId,
|
|
mealId: mealId,
|
|
);
|
|
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));
|
|
}
|
|
|
|
sendProductToModeration({
|
|
required String name,
|
|
required double energy,
|
|
required double proteins,
|
|
required double fats,
|
|
required double carbohydrates,
|
|
}) async {
|
|
return await diaryRepository.sendProductToModeration(
|
|
name: name,
|
|
energy: energy,
|
|
proteins: proteins,
|
|
fats: fats,
|
|
carbohydrates: carbohydrates,
|
|
);
|
|
}
|
|
}
|