fix(calendar): Finished days fix

This commit is contained in:
2024-07-25 16:53:49 +03:00
parent 102154ddb2
commit 56e5cdbe16
8 changed files with 132 additions and 483 deletions

View File

@@ -9,9 +9,6 @@ import 'package:flutter/material.dart';
import '/core/app_export.dart';
import 'package:image_picker/image_picker.dart';
import 'package:eatvio_app/features/diary/presentation/bloc/diary_state.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter_dotenv/flutter_dotenv.dart';
part 'diary_event.dart';
@@ -19,8 +16,7 @@ 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,29 +53,15 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
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")];
final Map<String, dynamic> chartData =
await diaryRepository.getChartData(diaryID ?? 45);
final Map<String, dynamic> dailyNutritionIntake =
chartData['dailyNutritionIntake']!;
final Map<String, dynamic> dailyNutritionRecommendations =
chartData['dailyNutritionRecommendation']!;
final double ccal = dailyNutritionIntake['energy']! /
dailyNutritionRecommendations['energy']! *
100;
final double proteins = dailyNutritionIntake['proteins']! /
dailyNutritionRecommendations['proteins']! *
100;
final double fats = dailyNutritionIntake['fats']! /
dailyNutritionRecommendations['fats']! *
100;
final double carbs = dailyNutritionIntake['carbohydrates']! /
dailyNutritionRecommendations['carbohydrates']! *
100;
final Map<String, dynamic> chartData = await diaryRepository.getChartData(diaryID ?? 45);
final Map<String, dynamic> dailyNutritionIntake = chartData['dailyNutritionIntake']!;
final Map<String, dynamic> dailyNutritionRecommendations = chartData['dailyNutritionRecommendation']!;
final double ccal = dailyNutritionIntake['energy']! / dailyNutritionRecommendations['energy']! * 100;
final double proteins = dailyNutritionIntake['proteins']! / dailyNutritionRecommendations['proteins']! * 100;
final double fats = dailyNutritionIntake['fats']! / dailyNutritionRecommendations['fats']! * 100;
final double carbs = dailyNutritionIntake['carbohydrates']! / dailyNutritionRecommendations['carbohydrates']! * 100;
Color getColor(double value) {
if (value >= 99.1) {
@@ -105,8 +87,6 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
SelectionPopupModel(title: "Прием пищи 6", id: 6),
];
print(dailyNutritionRecommendations);
print(dailyNutritionIntake);
emit(DiaryState.loaded(
diary: diary,
currentMeal: currentMeal,
@@ -117,6 +97,8 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
chartData: chartDataCounted,
selectedMood: currentMeal.mood,
selectedSatiety: currentMeal.satiety,
wakeupController: TimeOfDay.now(),
retireController: TimeOfDay.now(),
commentController: TextEditingController(),
product_nameController: TextEditingController(),
productQuantityController: TextEditingController(),
@@ -137,54 +119,6 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
}
}
fetchChartValues(
String token,
int diaryID,
Emitter<DiaryState> emit,
) async {
final responseChart = await http.get(
Uri.parse(
dotenv.env['SERVER_ROOT']! + '/diary/$diaryID/dailyNutritionData/',
),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token',
},
);
if (responseChart.statusCode == 200) {
final responseBody = jsonDecode(responseChart.body);
print(responseBody);
final recommendedChartValues =
responseBody['dailyNutritionRecommendation']
.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: 'Ошибка при декодировании данных КБЖУ'));
print('fail');
return [
[],
[],
];
}
}
Future<void> _changeSatiety(
ChangeSatiety event,
Emitter<DiaryState> emit,
@@ -229,11 +163,9 @@ 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,
@@ -248,11 +180,9 @@ 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,
@@ -278,8 +208,7 @@ 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,
@@ -298,12 +227,10 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
if (state is DiaryStateLoaded) {
var currentState = state as DiaryStateLoaded;
final Map<String, List<Product>> productNutritionalValues =
await diaryRepository.inputProduct(event.input);
final Map<String, List<Product>> productNutritionalValues = await diaryRepository.inputProduct(event.input);
var updatedState = currentState.copyWith(
product_nameController: TextEditingController(text: event.input),
autocompleteList:
productNutritionalValues['product_nutritional_value'] ?? [],
autocompleteList: productNutritionalValues['product_nutritional_value'] ?? [],
);
if (event.input.length > 3) {
updatedState = updatedState.copyWith(moderationExpanded: true);
@@ -449,9 +376,9 @@ class DiaryBloc extends Bloc<DiaryEvent, DiaryState> {
await diaryRepository.finishDay(
currentState.diary.id,
currentState.wakeupController,
currentState.retireController,
currentState.commentController.text,
// currentState.wakeupController,
// currentState.retireController,
// currentState.commentController.text,
);
add(LoadDiary());
}

View File

@@ -26,8 +26,8 @@ abstract class DiaryState with _$DiaryState {
required List<Product> autocompleteList,
required Meal currentMeal,
required SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
required TimeOfDay wakeupController,
required TimeOfDay retireController,
TimeOfDay? mealTimeController,
required TextEditingController commentController,
required TextEditingController product_nameController,

View File

@@ -31,8 +31,8 @@ mixin _$DiaryState {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -68,8 +68,8 @@ mixin _$DiaryState {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -105,8 +105,8 @@ mixin _$DiaryState {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -224,8 +224,8 @@ class _$DiaryStateImpl implements _DiaryState {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -264,8 +264,8 @@ class _$DiaryStateImpl implements _DiaryState {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -304,8 +304,8 @@ class _$DiaryStateImpl implements _DiaryState {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -425,8 +425,8 @@ class _$DiaryStateLoadingImpl implements DiaryStateLoading {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -465,8 +465,8 @@ class _$DiaryStateLoadingImpl implements DiaryStateLoading {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -505,8 +505,8 @@ class _$DiaryStateLoadingImpl implements DiaryStateLoading {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -592,8 +592,8 @@ abstract class _$$DiaryStateLoadedImplCopyWith<$Res> {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -636,8 +636,8 @@ class __$$DiaryStateLoadedImplCopyWithImpl<$Res>
Object? autocompleteList = null,
Object? currentMeal = null,
Object? portionController = null,
Object? wakeupController = freezed,
Object? retireController = freezed,
Object? wakeupController = null,
Object? retireController = null,
Object? mealTimeController = freezed,
Object? commentController = null,
Object? product_nameController = null,
@@ -696,14 +696,14 @@ class __$$DiaryStateLoadedImplCopyWithImpl<$Res>
? _value.portionController
: portionController // ignore: cast_nullable_to_non_nullable
as SelectionPopupModel,
wakeupController: freezed == wakeupController
wakeupController: null == wakeupController
? _value.wakeupController
: wakeupController // ignore: cast_nullable_to_non_nullable
as TimeOfDay?,
retireController: freezed == retireController
as TimeOfDay,
retireController: null == retireController
? _value.retireController
: retireController // ignore: cast_nullable_to_non_nullable
as TimeOfDay?,
as TimeOfDay,
mealTimeController: freezed == mealTimeController
? _value.mealTimeController
: mealTimeController // ignore: cast_nullable_to_non_nullable
@@ -802,8 +802,8 @@ class _$DiaryStateLoadedImpl implements DiaryStateLoaded {
required final List<Product> autocompleteList,
required this.currentMeal,
required this.portionController,
this.wakeupController,
this.retireController,
required this.wakeupController,
required this.retireController,
this.mealTimeController,
required this.commentController,
required this.product_nameController,
@@ -888,9 +888,9 @@ class _$DiaryStateLoadedImpl implements DiaryStateLoaded {
@override
final SelectionPopupModel portionController;
@override
final TimeOfDay? wakeupController;
final TimeOfDay wakeupController;
@override
final TimeOfDay? retireController;
final TimeOfDay retireController;
@override
final TimeOfDay? mealTimeController;
@override
@@ -1052,8 +1052,8 @@ class _$DiaryStateLoadedImpl implements DiaryStateLoaded {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -1120,8 +1120,8 @@ class _$DiaryStateLoadedImpl implements DiaryStateLoaded {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -1188,8 +1188,8 @@ class _$DiaryStateLoadedImpl implements DiaryStateLoaded {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -1294,8 +1294,8 @@ abstract class DiaryStateLoaded implements DiaryState {
required final List<Product> autocompleteList,
required final Meal currentMeal,
required final SelectionPopupModel portionController,
final TimeOfDay? wakeupController,
final TimeOfDay? retireController,
required final TimeOfDay wakeupController,
required final TimeOfDay retireController,
final TimeOfDay? mealTimeController,
required final TextEditingController commentController,
required final TextEditingController product_nameController,
@@ -1323,8 +1323,8 @@ abstract class DiaryStateLoaded implements DiaryState {
List<Product> get autocompleteList;
Meal get currentMeal;
SelectionPopupModel get portionController;
TimeOfDay? get wakeupController;
TimeOfDay? get retireController;
TimeOfDay get wakeupController;
TimeOfDay get retireController;
TimeOfDay? get mealTimeController;
TextEditingController get commentController;
TextEditingController get product_nameController;
@@ -1425,8 +1425,8 @@ class _$DiaryStateErrorImpl implements DiaryStateError {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -1465,8 +1465,8 @@ class _$DiaryStateErrorImpl implements DiaryStateError {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,
@@ -1505,8 +1505,8 @@ class _$DiaryStateErrorImpl implements DiaryStateError {
List<Product> autocompleteList,
Meal currentMeal,
SelectionPopupModel portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay wakeupController,
TimeOfDay retireController,
TimeOfDay? mealTimeController,
TextEditingController commentController,
TextEditingController product_nameController,