refactor(diary): Fix

This commit is contained in:
2024-07-12 16:49:50 +03:00
parent ed74fd99f0
commit f3a0df20b7
7 changed files with 155 additions and 163 deletions

View File

@@ -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);
}

View File

@@ -18,23 +18,23 @@ abstract class DiaryState with _$DiaryState {
const factory DiaryState.loaded({
required DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
required List<dynamic> autocompleteList,
required List<int> recommendedChartValues,
required List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
required List<Portion> portions,
required List<Photo> photos,
required Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
TimeOfDay? mealTimeController,
required TextEditingController? commentController,
required TextEditingController? productNameController,
required TextEditingController? productQuantityController,
required TextEditingController? caloriesController,
required TextEditingController? fatsController,
required TextEditingController? proteinsController,
required TextEditingController? carbsController,
required TextEditingController commentController,
required TextEditingController productNameController,
required TextEditingController productQuantityController,
required TextEditingController caloriesController,
required TextEditingController fatsController,
required TextEditingController proteinsController,
required TextEditingController carbsController,
int? selectedMood,
int? selectedSatiety,
required bool wakeupExpanded,

View File

@@ -23,12 +23,12 @@ mixin _$DiaryState {
required TResult Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -58,12 +58,12 @@ mixin _$DiaryState {
TResult? Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -93,12 +93,12 @@ mixin _$DiaryState {
TResult Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -210,12 +210,12 @@ class _$DiaryStateImpl implements _DiaryState {
required TResult Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -248,12 +248,12 @@ class _$DiaryStateImpl implements _DiaryState {
TResult? Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -286,12 +286,12 @@ class _$DiaryStateImpl implements _DiaryState {
TResult Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -405,12 +405,12 @@ class _$DiaryStateLoadingImpl implements DiaryStateLoading {
required TResult Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -443,12 +443,12 @@ class _$DiaryStateLoadingImpl implements DiaryStateLoading {
TResult? Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -481,12 +481,12 @@ class _$DiaryStateLoadingImpl implements DiaryStateLoading {
TResult Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -566,12 +566,12 @@ abstract class _$$DiaryStateLoadedImplCopyWith<$Res> {
$Res call(
{DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -592,7 +592,7 @@ abstract class _$$DiaryStateLoadedImplCopyWith<$Res> {
bool isAdded});
$DiaryModelCopyWith<$Res> get diary;
$MealCopyWith<$Res>? get currentMeal;
$MealCopyWith<$Res> get currentMeal;
}
/// @nodoc
@@ -608,12 +608,12 @@ class __$$DiaryStateLoadedImplCopyWithImpl<$Res>
$Res call({
Object? diary = null,
Object? diaryDate = freezed,
Object? autocompleteList = freezed,
Object? autocompleteList = null,
Object? recommendedChartValues = null,
Object? consumedChartValues = null,
Object? portions = freezed,
Object? photos = freezed,
Object? currentMeal = freezed,
Object? portions = null,
Object? photos = null,
Object? currentMeal = null,
Object? portionController = freezed,
Object? wakeupController = freezed,
Object? retireController = freezed,
@@ -642,10 +642,10 @@ class __$$DiaryStateLoadedImplCopyWithImpl<$Res>
? _value.diaryDate
: diaryDate // ignore: cast_nullable_to_non_nullable
as String?,
autocompleteList: freezed == autocompleteList
autocompleteList: null == autocompleteList
? _value._autocompleteList
: autocompleteList // ignore: cast_nullable_to_non_nullable
as List<dynamic>?,
as List<dynamic>,
recommendedChartValues: null == recommendedChartValues
? _value._recommendedChartValues
: recommendedChartValues // ignore: cast_nullable_to_non_nullable
@@ -654,18 +654,18 @@ class __$$DiaryStateLoadedImplCopyWithImpl<$Res>
? _value._consumedChartValues
: consumedChartValues // ignore: cast_nullable_to_non_nullable
as List<int>,
portions: freezed == portions
portions: null == portions
? _value._portions
: portions // ignore: cast_nullable_to_non_nullable
as List<Portion>?,
photos: freezed == photos
as List<Portion>,
photos: null == photos
? _value._photos
: photos // ignore: cast_nullable_to_non_nullable
as List<Photo>?,
currentMeal: freezed == currentMeal
as List<Photo>,
currentMeal: null == currentMeal
? _value.currentMeal
: currentMeal // ignore: cast_nullable_to_non_nullable
as Meal?,
as Meal,
portionController: freezed == portionController
? _value.portionController
: portionController // ignore: cast_nullable_to_non_nullable
@@ -751,12 +751,8 @@ class __$$DiaryStateLoadedImplCopyWithImpl<$Res>
@override
@pragma('vm:prefer-inline')
$MealCopyWith<$Res>? get currentMeal {
if (_value.currentMeal == null) {
return null;
}
return $MealCopyWith<$Res>(_value.currentMeal!, (value) {
$MealCopyWith<$Res> get currentMeal {
return $MealCopyWith<$Res>(_value.currentMeal, (value) {
return _then(_value.copyWith(currentMeal: value));
});
}
@@ -768,12 +764,12 @@ class _$DiaryStateLoadedImpl implements DiaryStateLoaded {
const _$DiaryStateLoadedImpl(
{required this.diary,
this.diaryDate,
final List<dynamic>? autocompleteList,
required final List<dynamic> autocompleteList,
required final List<int> recommendedChartValues,
required final List<int> consumedChartValues,
final List<Portion>? portions,
final List<Photo>? photos,
this.currentMeal,
required final List<Portion> portions,
required final List<Photo> photos,
required this.currentMeal,
this.portionController,
this.wakeupController,
this.retireController,
@@ -802,15 +798,13 @@ class _$DiaryStateLoadedImpl implements DiaryStateLoaded {
final DiaryModel diary;
@override
final String? diaryDate;
final List<dynamic>? _autocompleteList;
final List<dynamic> _autocompleteList;
@override
List<dynamic>? get autocompleteList {
final value = _autocompleteList;
if (value == null) return null;
List<dynamic> get autocompleteList {
if (_autocompleteList is EqualUnmodifiableListView)
return _autocompleteList;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(value);
return EqualUnmodifiableListView(_autocompleteList);
}
final List<int> _recommendedChartValues;
@@ -831,28 +825,24 @@ class _$DiaryStateLoadedImpl implements DiaryStateLoaded {
return EqualUnmodifiableListView(_consumedChartValues);
}
final List<Portion>? _portions;
final List<Portion> _portions;
@override
List<Portion>? get portions {
final value = _portions;
if (value == null) return null;
List<Portion> get portions {
if (_portions is EqualUnmodifiableListView) return _portions;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(value);
return EqualUnmodifiableListView(_portions);
}
final List<Photo>? _photos;
final List<Photo> _photos;
@override
List<Photo>? get photos {
final value = _photos;
if (value == null) return null;
List<Photo> get photos {
if (_photos is EqualUnmodifiableListView) return _photos;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(value);
return EqualUnmodifiableListView(_photos);
}
@override
final Meal? currentMeal;
final Meal currentMeal;
@override
final SelectionPopupModel? portionController;
@override
@@ -997,12 +987,12 @@ class _$DiaryStateLoadedImpl implements DiaryStateLoaded {
required TResult Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -1061,12 +1051,12 @@ class _$DiaryStateLoadedImpl implements DiaryStateLoaded {
TResult? Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -1125,12 +1115,12 @@ class _$DiaryStateLoadedImpl implements DiaryStateLoaded {
TResult Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -1227,12 +1217,12 @@ abstract class DiaryStateLoaded implements DiaryState {
const factory DiaryStateLoaded(
{required final DiaryModel diary,
final String? diaryDate,
final List<dynamic>? autocompleteList,
required final List<dynamic> autocompleteList,
required final List<int> recommendedChartValues,
required final List<int> consumedChartValues,
final List<Portion>? portions,
final List<Photo>? photos,
final Meal? currentMeal,
required final List<Portion> portions,
required final List<Photo> photos,
required final Meal currentMeal,
final SelectionPopupModel? portionController,
final TimeOfDay? wakeupController,
final TimeOfDay? retireController,
@@ -1254,12 +1244,12 @@ abstract class DiaryStateLoaded implements DiaryState {
DiaryModel get diary;
String? get diaryDate;
List<dynamic>? get autocompleteList;
List<dynamic> get autocompleteList;
List<int> get recommendedChartValues;
List<int> get consumedChartValues;
List<Portion>? get portions;
List<Photo>? get photos;
Meal? get currentMeal;
List<Portion> get portions;
List<Photo> get photos;
Meal get currentMeal;
SelectionPopupModel? get portionController;
TimeOfDay? get wakeupController;
TimeOfDay? get retireController;
@@ -1354,12 +1344,12 @@ class _$DiaryStateErrorImpl implements DiaryStateError {
required TResult Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -1392,12 +1382,12 @@ class _$DiaryStateErrorImpl implements DiaryStateError {
TResult? Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,
@@ -1430,12 +1420,12 @@ class _$DiaryStateErrorImpl implements DiaryStateError {
TResult Function(
DiaryModel diary,
String? diaryDate,
List<dynamic>? autocompleteList,
List<dynamic> autocompleteList,
List<int> recommendedChartValues,
List<int> consumedChartValues,
List<Portion>? portions,
List<Photo>? photos,
Meal? currentMeal,
List<Portion> portions,
List<Photo> photos,
Meal currentMeal,
SelectionPopupModel? portionController,
TimeOfDay? wakeupController,
TimeOfDay? retireController,

View File

@@ -274,8 +274,7 @@ class DiaryScreen extends StatelessWidget {
_buildMeal(BuildContext context, DiaryStateLoaded state) {
final digitsOnly = FilteringTextInputFormatter.digitsOnly;
List<String> _options =
state.autocompleteList!.map((dynamic item) => item as String).toList();
//print(_options);
state.autocompleteList.map((dynamic item) => item as String).toList();
FocusNode focusNodeName = FocusNode();
return Container(
margin: EdgeInsets.symmetric(horizontal: 16.h),
@@ -350,7 +349,6 @@ class DiaryScreen extends StatelessWidget {
horizontal: 10.h,
vertical: 9.v,
),
defaultValue: mealsToDropdown(diary.meals)[0],
onChanged: changeMeal(context, state),
);
},
@@ -448,7 +446,6 @@ class DiaryScreen extends StatelessWidget {
SelectionPopupModel(title: "gr"),
SelectionPopupModel(title: "pcs")
],
defaultValue: SelectionPopupModel(title: "gr"),
onChanged: changePortion(context, state),
);
},
@@ -461,7 +458,7 @@ class DiaryScreen extends StatelessWidget {
SizedBox(height: 30.v),
_buildAddProduct(context, state),
SizedBox(height: 30.v),
...state.portions!
...state.portions
.map((portion) => _buildProduct(context, portion))
.toList(),
SizedBox(height: 30.v),
@@ -673,12 +670,12 @@ class DiaryScreen extends StatelessWidget {
}
_buildCarousel(BuildContext context, DiaryStateLoaded state) {
if (state.currentMeal!.photos.isEmpty) {
if (state.currentMeal.photos.isEmpty) {
return Text("Фото нет");
} else {
return CarouselSlider(
options: CarouselOptions(height: 340.0),
items: state.currentMeal!.photos.map<Widget>((photo) {
items: state.currentMeal.photos.map<Widget>((photo) {
return _buildPhoto(
context, photo); // Assuming photos are stored as strings
}).toList(),
@@ -1314,7 +1311,7 @@ List<SelectionPopupModel> mealsToDropdown(List<Meal>? meals) {
return meals!
.map((meal) => SelectionPopupModel(
id: meal.id,
title: "Прием пищи ${meal.id}",
title: "Прием пищи ${meal.type}",
isSelected: false,
))
.toList();