forked from eatvio/eatvio_app
144 lines
3.9 KiB
Dart
144 lines
3.9 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;
|
|
double currentSelectedSatietyRating = 3;
|
|
|
|
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 {
|
|
emit(
|
|
DiaryState.loaded(
|
|
diary: await diaryRepository.getDiaryByDate(date),
|
|
nutrition: await getNutrition((await diaryRepository.getDiaryByDate(date)).id),
|
|
currentSelectedSatietyRating: currentSelectedSatietyRating,
|
|
),
|
|
);
|
|
} 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(double value) {
|
|
currentSelectedSatietyRating = value;
|
|
emit((state as DiaryStateLoaded).copyWith(currentSelectedSatietyRating: currentSelectedSatietyRating));
|
|
}
|
|
}
|