forked from eatvio/eatvio_app
refactor(diary): Fix
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'package:eatvio_app/features/diary/models/diary_model.dart';
|
||||
import 'package:eatvio_app/features/diary/models/meal.dart';
|
||||
import 'package:eatvio_app/features/diary/repositories/diary_repository.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
@@ -55,8 +56,13 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
try {
|
||||
Map<String, int> data = await diaryRepository.getDiaryID();
|
||||
final int? diaryID = data['id'];
|
||||
final DiaryModel diary = await diaryRepository.getDiary(diaryID ?? 45);
|
||||
final Meal currentMeal = diary.meals[0];
|
||||
emit(DiaryState.loaded(
|
||||
diary: await diaryRepository.getDiary(diaryID ?? 45),
|
||||
diary: diary,
|
||||
currentMeal: currentMeal,
|
||||
portions: currentMeal.portions,
|
||||
photos: currentMeal.photos,
|
||||
commentController: TextEditingController(),
|
||||
productNameController: TextEditingController(),
|
||||
productQuantityController: TextEditingController(),
|
||||
@@ -69,8 +75,9 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
moodExpanded: false,
|
||||
isLoading: false,
|
||||
isAdded: false,
|
||||
recommendedChartValues: [0, 0, 0, 0],
|
||||
consumedChartValues: [0, 0, 0, 0]));
|
||||
autocompleteList: [],
|
||||
recommendedChartValues: [1, 1, 1, 1],
|
||||
consumedChartValues: [1, 1, 1, 1]));
|
||||
} catch (e) {
|
||||
emit(DiaryState.error(errorMessage: e.toString()));
|
||||
print(e);
|
||||
@@ -143,13 +150,13 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
// Отправляем данные по HTTP маршруту и ждем ответ
|
||||
var response = await sendSatietyData(
|
||||
event.selectedElement,
|
||||
currentState.currentMeal?.id,
|
||||
currentState.currentMeal.id,
|
||||
currentState.diary.id,
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
print(
|
||||
'Satiety change success! meal_id: ${currentState.currentMeal?.id}, mood: ${event.selectedElement}',
|
||||
'Satiety change success! meal_id: ${currentState.currentMeal.id}, mood: ${event.selectedElement}',
|
||||
);
|
||||
emit(updatedState); // Передаем обновленное состояние в emit
|
||||
}
|
||||
@@ -189,13 +196,13 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
// Отправляем данные по HTTP маршруту и ждем ответ
|
||||
var response = await sendMoodData(
|
||||
event.selectedElement,
|
||||
currentState.currentMeal?.id,
|
||||
currentState.currentMeal.id,
|
||||
currentState.diary.id,
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
print(
|
||||
'Mood change success! meal_id: ${currentState.currentMeal?.id}, mood: ${event.selectedElement}',
|
||||
'Mood change success! meal_id: ${currentState.currentMeal.id}, mood: ${event.selectedElement}',
|
||||
);
|
||||
emit(updatedState); // Передаем обновленное состояние в emit
|
||||
}
|
||||
@@ -359,8 +366,8 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
) async {
|
||||
if (state is DiaryStateLoaded) {
|
||||
var currentState = state as DiaryStateLoaded;
|
||||
Meal? meal = currentState.diary.meals!
|
||||
.firstWhere((meal) => meal.id == event.value);
|
||||
Meal? meal =
|
||||
currentState.diary.meals.firstWhere((meal) => meal.id == event.value);
|
||||
|
||||
DiaryStateLoaded updatedState = currentState.copyWith(
|
||||
currentMeal: event.value,
|
||||
@@ -431,7 +438,7 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
currentState.productNameController?.text,
|
||||
currentState.portionController?.id == 0 ? 'pcs' : 'gr',
|
||||
int.parse(currentState.productQuantityController!.text),
|
||||
currentState.currentMeal!.id,
|
||||
currentState.currentMeal.id,
|
||||
currentState.diary.id,
|
||||
);
|
||||
|
||||
@@ -523,7 +530,7 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
var currentState = state as DiaryStateLoaded;
|
||||
|
||||
int? mealId =
|
||||
currentState.currentMeal?.id; // Rename id to mealId for clarity
|
||||
currentState.currentMeal.id; // Rename id to mealId for clarity
|
||||
|
||||
var response = await sendRemovePortion(
|
||||
event.portionId,
|
||||
@@ -570,7 +577,7 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
var response = await sendRemovePhoto(
|
||||
event.path,
|
||||
event.photoID,
|
||||
currentState.currentMeal?.id,
|
||||
currentState.currentMeal.id,
|
||||
currentState.diary.id,
|
||||
);
|
||||
print(response.statusCode);
|
||||
@@ -668,7 +675,7 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
await uploadImageToServer(
|
||||
event.image.path,
|
||||
currentState.diary.id,
|
||||
currentState.currentMeal!.id,
|
||||
currentState.currentMeal.id,
|
||||
);
|
||||
await reloadScreen(event, emit);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user