forked from eatvio/eatvio_app
170 lines
4.3 KiB
Dart
170 lines
4.3 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> {
|
|
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();
|
|
}
|
|
|
|
setSelectedMeal(int meal) {
|
|
selectedMeal = meal;
|
|
emit((state as DiaryStateLoaded).copyWith(selectedMeal: selectedMeal));
|
|
}
|
|
|
|
fetchDiary() async {
|
|
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,
|
|
required 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,
|
|
}) async {
|
|
await diaryRepository.addPortionToMeal(
|
|
diaryId: diaryId,
|
|
mealId: mealId,
|
|
productName: productName,
|
|
portion: portion,
|
|
quantity: quantity,
|
|
);
|
|
await fetchDiary();
|
|
}
|
|
|
|
updateProductInMeal({
|
|
required int diaryId,
|
|
required int mealId,
|
|
required int portionId,
|
|
required String productName,
|
|
required String portion,
|
|
required int quantity,
|
|
}) async {
|
|
await diaryRepository.updateProductInMeal(
|
|
diaryId: diaryId,
|
|
mealId: mealId,
|
|
portionId: portionId,
|
|
productName: productName,
|
|
portion: portion,
|
|
quantity: quantity,
|
|
);
|
|
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,
|
|
required int mealId,
|
|
}) async {
|
|
await diaryRepository.uploadPhotos(
|
|
files: await ImageCompressUtility.compressFilesList(files),
|
|
diaryId: diaryId,
|
|
mealId: mealId,
|
|
);
|
|
await fetchDiary();
|
|
}
|
|
|
|
deletePhoto({
|
|
required DiaryPhotoModel photo,
|
|
required int diaryId,
|
|
required int mealId,
|
|
}) async {
|
|
await diaryRepository.deletePhoto(
|
|
photo: photo,
|
|
diaryId: diaryId,
|
|
mealId: mealId,
|
|
);
|
|
await fetchDiary();
|
|
}
|
|
|
|
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,
|
|
);
|
|
}
|
|
}
|