forked from eatvio/eatvio_app
refactor(diary): API mor produc nutritional and new Product model
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
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/models/product.dart';
|
||||
import 'package:eatvio_app/features/diary/repositories/diary_repository.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
@@ -18,7 +19,8 @@ part 'diary_event.dart';
|
||||
class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
final DiaryRepository diaryRepository;
|
||||
|
||||
DiaryBloc({required this.diaryRepository}) : super(const DiaryState.loading()) {
|
||||
DiaryBloc({required this.diaryRepository})
|
||||
: super(const DiaryState.loading()) {
|
||||
on<LoadDiary>(loadDiary);
|
||||
on<ChangeSatiety>(_changeSatiety); // пользователь нажимает на шкалу сытости
|
||||
on<ChangeMood>(_changeMood); // пользователь нажимает на шкалу настроения
|
||||
@@ -57,7 +59,10 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
final int? diaryID = data['id'];
|
||||
final DiaryModel diary = await diaryRepository.getDiary(diaryID ?? 45);
|
||||
final Meal currentMeal = diary.meals[0];
|
||||
List<SelectionPopupModel> portionList = [SelectionPopupModel(title: "gr"), SelectionPopupModel(title: "pcs")];
|
||||
List<SelectionPopupModel> portionList = [
|
||||
SelectionPopupModel(title: "gr"),
|
||||
SelectionPopupModel(title: "pcs")
|
||||
];
|
||||
List<SelectionPopupModel> mealList = [
|
||||
SelectionPopupModel(title: "Прием пищи 1", id: 1),
|
||||
SelectionPopupModel(title: "Прием пищи 2", id: 2),
|
||||
@@ -114,14 +119,22 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
if (responseChart.statusCode == 200) {
|
||||
final responseBody = jsonDecode(responseChart.body);
|
||||
|
||||
final recommendedChartValues = responseBody['dailyNutritionRecommendation'].values.map<int>((value) => convertToInt(value)).toList();
|
||||
final recommendedChartValues =
|
||||
responseBody['dailyNutritionRecommendation']
|
||||
.values
|
||||
.map<int>((value) => convertToInt(value))
|
||||
.toList();
|
||||
|
||||
final consumedChartValues = responseBody['dailyNutritionIntake'].values.map<int>((value) => convertToInt(value)).toList();
|
||||
final consumedChartValues = responseBody['dailyNutritionIntake']
|
||||
.values
|
||||
.map<int>((value) => convertToInt(value))
|
||||
.toList();
|
||||
|
||||
print([recommendedChartValues, consumedChartValues]);
|
||||
return [recommendedChartValues, consumedChartValues];
|
||||
} else {
|
||||
emit(DiaryStateError(errorMessage: 'Ошибка при декодировании данных КБЖУ'));
|
||||
emit(DiaryStateError(
|
||||
errorMessage: 'Ошибка при декодировании данных КБЖУ'));
|
||||
print('fail');
|
||||
return [[], []]; // Return empty lists instead of -1
|
||||
}
|
||||
@@ -148,8 +161,10 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
) async {
|
||||
if (state is DiaryStateLoaded) {
|
||||
var currentState = state as DiaryStateLoaded;
|
||||
var updatedState = currentState.copyWith(selectedSatiety: event.selectedElement);
|
||||
diaryRepository.changeSatiety(event.selectedElement, currentState.currentMeal.id, currentState.diary.id);
|
||||
var updatedState =
|
||||
currentState.copyWith(selectedSatiety: event.selectedElement);
|
||||
diaryRepository.changeSatiety(event.selectedElement,
|
||||
currentState.currentMeal.id, currentState.diary.id);
|
||||
emit(updatedState);
|
||||
}
|
||||
}
|
||||
@@ -160,9 +175,11 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
) async {
|
||||
if (state is DiaryStateLoaded) {
|
||||
var currentState = state as DiaryStateLoaded;
|
||||
var updatedState = currentState.copyWith(selectedMood: event.selectedElement);
|
||||
var updatedState =
|
||||
currentState.copyWith(selectedMood: event.selectedElement);
|
||||
|
||||
diaryRepository.changeMood(event.selectedElement, currentState.currentMeal.id, currentState.diary.id);
|
||||
diaryRepository.changeMood(event.selectedElement,
|
||||
currentState.currentMeal.id, currentState.diary.id);
|
||||
|
||||
emit(updatedState); // Передаем обновленное состояние в emit
|
||||
}
|
||||
@@ -174,9 +191,11 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
) async {
|
||||
if (state is DiaryStateLoaded) {
|
||||
var currentState = state as DiaryStateLoaded;
|
||||
var updatedState = currentState.copyWith(wakeupController: event.selectedTime);
|
||||
var updatedState =
|
||||
currentState.copyWith(wakeupController: event.selectedTime);
|
||||
final DateFormat formatter = DateFormat('HH:mm');
|
||||
final String wakeupString = formatter.format(DateTime(2022, 1, 1, event.selectedTime.hour, event.selectedTime.minute));
|
||||
final String wakeupString = formatter.format(DateTime(
|
||||
2022, 1, 1, event.selectedTime.hour, event.selectedTime.minute));
|
||||
await diaryRepository.changeWakeup(
|
||||
currentState.diary.id,
|
||||
wakeupString,
|
||||
@@ -191,9 +210,11 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
) async {
|
||||
if (state is DiaryStateLoaded) {
|
||||
var currentState = state as DiaryStateLoaded;
|
||||
var updatedState = currentState.copyWith(retireController: event.selectedTime);
|
||||
var updatedState =
|
||||
currentState.copyWith(retireController: event.selectedTime);
|
||||
final DateFormat formatter = DateFormat('HH:mm');
|
||||
final String retireString = formatter.format(DateTime(2022, 1, 1, event.selectedTime.hour, event.selectedTime.minute));
|
||||
final String retireString = formatter.format(DateTime(
|
||||
2022, 1, 1, event.selectedTime.hour, event.selectedTime.minute));
|
||||
await diaryRepository.changeRetire(
|
||||
currentState.diary.id,
|
||||
retireString,
|
||||
@@ -219,7 +240,8 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
) async {
|
||||
if (state is DiaryStateLoaded) {
|
||||
final currentState = state as DiaryStateLoaded;
|
||||
final Meal meal = currentState.diary.meals.firstWhere((meal) => meal.type == event.value.id);
|
||||
final Meal meal = currentState.diary.meals
|
||||
.firstWhere((meal) => meal.type == event.value.id);
|
||||
DiaryStateLoaded updatedState = currentState.copyWith(
|
||||
currentMeal: meal,
|
||||
portions: meal.portions,
|
||||
@@ -239,8 +261,9 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
if (state is DiaryStateLoaded) {
|
||||
var currentState = state as DiaryStateLoaded;
|
||||
|
||||
final productNutritionalValues = await diaryRepository.inputProduct(event.input);
|
||||
|
||||
final Map<String, List<Product>> productNutritionalValues =
|
||||
await diaryRepository.inputProduct(event.input);
|
||||
print(productNutritionalValues);
|
||||
// Create a new controller with the input text and update the autocomplete list
|
||||
/*var updatedState = currentState.copyWith(
|
||||
productNameController: TextEditingController(text: event.input),
|
||||
@@ -309,52 +332,18 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
if (state is DiaryStateLoaded) {
|
||||
var currentState = state as DiaryStateLoaded;
|
||||
|
||||
var response = await sendPortionData(
|
||||
currentState.productNameController.text,
|
||||
currentState.portionController.id == 0 ? 'pcs' : 'gr',
|
||||
int.parse(currentState.productQuantityController.text),
|
||||
currentState.currentMeal.id,
|
||||
await diaryRepository.addPortion(
|
||||
currentState.diary.id,
|
||||
currentState.currentMeal.id,
|
||||
currentState.productNameController.text,
|
||||
currentState.portionController.value,
|
||||
int.parse(currentState.productQuantityController.text),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
emit(currentState.copyWith(isAdded: true));
|
||||
await reloadScreen(event, emit);
|
||||
} else {
|
||||
print(
|
||||
'Failed to add portion. Status Code: ${response.statusCode} ${response.body}',
|
||||
);
|
||||
}
|
||||
emit(currentState.copyWith(isAdded: true));
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<http.Response> sendPortionData(
|
||||
String? name,
|
||||
String portion,
|
||||
int? quantity,
|
||||
int? mealID,
|
||||
int diaryID,
|
||||
) async {
|
||||
String url = '${dotenv.env['SERVER_ROOT']!}/diary/$diaryID/meal/$mealID/portions/create';
|
||||
|
||||
final Map<String, dynamic> credentials = {
|
||||
'product_name': name,
|
||||
'portion': portion,
|
||||
'quantity': quantity,
|
||||
};
|
||||
|
||||
var response = await http.post(
|
||||
Uri.parse(url),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: json.encode(credentials),
|
||||
encoding: utf8,
|
||||
);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
Future<void> _removePortion(
|
||||
RemovePortionEvent event,
|
||||
Emitter<DiaryState> emit,
|
||||
@@ -362,7 +351,8 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
if (state is DiaryStateLoaded) {
|
||||
var currentState = state as DiaryStateLoaded;
|
||||
|
||||
int? mealId = currentState.currentMeal.id; // Rename id to mealId for clarity
|
||||
int? mealId =
|
||||
currentState.currentMeal.id; // Rename id to mealId for clarity
|
||||
|
||||
var response = await sendRemovePortion(
|
||||
event.portionId,
|
||||
@@ -385,7 +375,8 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
int? mealID,
|
||||
int diaryID,
|
||||
) async {
|
||||
String url = '${dotenv.env['SERVER_ROOT']!}/diary/$diaryID/meal/$mealID/portions/$portionID/delete';
|
||||
String url =
|
||||
'${dotenv.env['SERVER_ROOT']!}/diary/$diaryID/meal/$mealID/portions/$portionID/delete';
|
||||
|
||||
var response = await http.delete(
|
||||
Uri.parse(url),
|
||||
@@ -428,7 +419,8 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
int? mealID,
|
||||
int diaryID,
|
||||
) async {
|
||||
String url = '${dotenv.env['SERVER_ROOT']!}/diary/$diaryID/meal/$mealID/photo/delete';
|
||||
String url =
|
||||
'${dotenv.env['SERVER_ROOT']!}/diary/$diaryID/meal/$mealID/photo/delete';
|
||||
final Map<String, dynamic> credentials = {
|
||||
'id': photoID,
|
||||
'path': path,
|
||||
@@ -469,8 +461,10 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
String? comment,
|
||||
) async {
|
||||
final formatter = DateFormat('HH:mm');
|
||||
final wakeupString = formatter.format(DateTime(2022, 1, 1, wakeup!.hour, wakeup.minute));
|
||||
final retireString = formatter.format(DateTime(2022, 1, 1, retire!.hour, retire.minute));
|
||||
final wakeupString =
|
||||
formatter.format(DateTime(2022, 1, 1, wakeup!.hour, wakeup.minute));
|
||||
final retireString =
|
||||
formatter.format(DateTime(2022, 1, 1, retire!.hour, retire.minute));
|
||||
|
||||
final Map<String, dynamic> credentials = {
|
||||
'wakeup': wakeupString,
|
||||
@@ -514,7 +508,8 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
|
||||
int diaryID,
|
||||
int mealId,
|
||||
) async {
|
||||
String url = '${dotenv.env['SERVER_ROOT']!}/diary/$diaryID/meal/$mealId/photo/upload';
|
||||
String url =
|
||||
'${dotenv.env['SERVER_ROOT']!}/diary/$diaryID/meal/$mealId/photo/upload';
|
||||
|
||||
var request = http.MultipartRequest('POST', Uri.parse(url));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user