forked from eatvio/eatvio_app
194 lines
6.5 KiB
Dart
194 lines
6.5 KiB
Dart
import 'dart:developer';
|
|
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> {
|
|
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()) {
|
|
today = DateTime.now();
|
|
selectedDay = today;
|
|
}
|
|
|
|
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('$selectedDay');
|
|
emit(
|
|
DiaryState.loaded(
|
|
diary: diaryDay,
|
|
nutrition: await getNutrition((await diaryRepository.getDiaryByDate('$selectedDay')).id),
|
|
selectedDay: selectedDay,
|
|
selectedMeal: selectedMeal,
|
|
),
|
|
);
|
|
} catch (e) {
|
|
emit(DiaryState.error(errorMessage: e.toString()));
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
finishDiaryDay({required int diaryId, required String wakeUp, required String retire, String? comment}) async {
|
|
await diaryRepository.finishDiaryDay(diaryId: diaryId, wakeUp: wakeUp, retire: retire, comment: comment);
|
|
await fetchDiary();
|
|
}
|
|
|
|
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,
|
|
int? productNutritionalValueId,
|
|
}) async {
|
|
await diaryRepository.addPortionToMeal(
|
|
diaryId: diaryId,
|
|
mealId: mealId,
|
|
productName: productName,
|
|
portion: portion,
|
|
quantity: quantity,
|
|
productNutritionalValueId: productNutritionalValueId,
|
|
);
|
|
await fetchDiary();
|
|
}
|
|
|
|
updateProductInMeal({
|
|
required int diaryId,
|
|
required int mealId,
|
|
required int portionId,
|
|
required String productName,
|
|
required String portion,
|
|
required int quantity,
|
|
int? productNutritionalValueId,
|
|
required PortionState portionState,
|
|
}) async {
|
|
if (portionState == PortionState.filled_diet || portionState == PortionState.eaten_from_diet) {
|
|
await diaryRepository.addPortionToMeal(
|
|
diaryId: diaryId,
|
|
mealId: mealId,
|
|
productName: productName,
|
|
portion: portion,
|
|
quantity: quantity,
|
|
productNutritionalValueId: productNutritionalValueId,
|
|
);
|
|
} else {
|
|
await diaryRepository.updateProductInMeal(
|
|
diaryId: diaryId,
|
|
mealId: mealId,
|
|
portionId: portionId,
|
|
productName: productName,
|
|
portion: portion,
|
|
quantity: quantity,
|
|
productNutritionalValueId: productNutritionalValueId,
|
|
portionState: portionState,
|
|
);
|
|
}
|
|
await fetchDiary();
|
|
}
|
|
|
|
markEaten({required int diaryId, required int mealId, required int portionId}) async {
|
|
await diaryRepository.markEaten(diaryId: diaryId, mealId: mealId, portionId: portionId);
|
|
await fetchDiary();
|
|
}
|
|
|
|
unmarkEaten({required int diaryId, required int mealId, required int portionId}) async {
|
|
await diaryRepository.unmarkEaten(diaryId: diaryId, mealId: mealId, portionId: portionId);
|
|
await fetchDiary();
|
|
}
|
|
|
|
restore({required int diaryId, required int mealId, required int portionId}) async {
|
|
await diaryRepository.restore(diaryId: diaryId, mealId: mealId, portionId: portionId);
|
|
await fetchDiary();
|
|
}
|
|
|
|
deleteProductFromMeal({required int diaryId, required int mealId, required int portionId}) async {
|
|
await diaryRepository.deleteProductFromMeal(diaryId: diaryId, mealId: mealId, portionId: portionId);
|
|
await fetchDiary();
|
|
}
|
|
|
|
Future<List<GlossaryModel>> getGlossaryListByQuery(String query) async {
|
|
return await glossaryRepository.getGlossaryListByQuery(query);
|
|
}
|
|
|
|
uploadPhotos({required List<File> files, required int diaryId}) async {
|
|
await diaryRepository.uploadPhotos(files: await ImageCompressUtility.compressFilesList(files), diaryId: diaryId, mealId: selectedMeal);
|
|
await fetchDiary();
|
|
}
|
|
|
|
deletePhoto({required DiaryPhotoModel photo, required int diaryId}) async {
|
|
await diaryRepository.deletePhoto(photo: photo, diaryId: diaryId, mealId: selectedMeal);
|
|
await fetchDiary();
|
|
}
|
|
|
|
Future<GlossaryModerationModel?> sendProductToModeration({
|
|
required String name,
|
|
required double energy,
|
|
required double proteins,
|
|
required double fats,
|
|
required double carbohydrates,
|
|
}) async {
|
|
try {
|
|
return await diaryRepository.sendProductToModeration(name: name, energy: energy, proteins: proteins, fats: fats, carbohydrates: carbohydrates);
|
|
} catch (e, stackTrace) {
|
|
log(e.toString(), stackTrace: stackTrace);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
fillTime({required int diaryId, required String time}) async {
|
|
await diaryRepository.fillTime(diaryId: diaryId, mealId: selectedMeal, time: time);
|
|
await fetchDiary();
|
|
}
|
|
|
|
fillWakeup({required int diaryId, required String time}) async {
|
|
await diaryRepository.fillWakeup(diaryId: diaryId, time: time);
|
|
await fetchDiary();
|
|
}
|
|
|
|
fillRetire({required int diaryId, required String time}) async {
|
|
await diaryRepository.fillRetire(diaryId: diaryId, time: time);
|
|
await fetchDiary();
|
|
}
|
|
|
|
fillSatiety({required int diaryId, required int satiety}) async {
|
|
await diaryRepository.fillSatiety(diaryId: diaryId, mealId: selectedMeal, satiety: satiety);
|
|
}
|
|
|
|
fillMood({required int diaryId, required int mood}) async {
|
|
await diaryRepository.fillMood(diaryId: diaryId, mealId: selectedMeal, mood: mood);
|
|
}
|
|
|
|
fillComment(String comment) {
|
|
emit((state as DiaryStateLoaded).copyWith(diary: (state as DiaryStateLoaded).diary.copyWith(comment: comment)));
|
|
}
|
|
}
|