forked from eatvio/eatvio_app
refatcor(diary): Refactored (doesn't work yet though)
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
// ignore_for_file: unused_local_variable
|
||||
|
||||
/*import 'bloc/diary_edit_bloc.dart';
|
||||
import 'models/diary_edit_model.dart';
|
||||
import 'package:eatvio_app/features/diary/models/diary_model.dart';
|
||||
import 'package:eatvio_app/features/diary/presentation/bloc/diary_bloc.dart';
|
||||
import 'package:eatvio_app/features/diary/presentation/bloc/diary_state.dart';
|
||||
import 'package:eatvio_app/locator_service.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_form_builder/flutter_form_builder.dart';
|
||||
@@ -16,42 +20,45 @@ import 'package:eatvio_app/widgets/custom_scale_element.dart';
|
||||
import 'package:carousel_slider/carousel_slider.dart';
|
||||
|
||||
// ignore: must_be_immutable
|
||||
class DiaryEditScreen extends StatelessWidget {
|
||||
DiaryEditScreen({Key? key}) : super(key: key);
|
||||
|
||||
static Widget builder(BuildContext context) {
|
||||
return BlocProvider<DiaryEditBloc>(
|
||||
create: (context) =>
|
||||
DiaryEditBloc(DiaryEditState(diaryEditModelObj: DiaryEditModel()))
|
||||
..add(DiaryEditInitialEvent()),
|
||||
child: DiaryEditScreen(),
|
||||
);
|
||||
}
|
||||
|
||||
class DiaryScreen extends StatelessWidget {
|
||||
DiaryScreen({Key? key}) : super(key: key);
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<DiaryEditBloc, DiaryEditState>(
|
||||
builder: (context, state) {
|
||||
if (state is DiaryEditInitialState) {
|
||||
// Это начальное состояние, данные ещё не загружены.
|
||||
return Container(
|
||||
color: Colors.white,
|
||||
);
|
||||
} else if (state is DiaryEditLoadedState) {
|
||||
// Данные загружены, можно строить экран.
|
||||
return _buildDiaryEditScreen(context, state);
|
||||
} else if (state is DiaryEditErrorState) {
|
||||
// Произошла ошибка при загрузке данных.
|
||||
return Center(child: Text(state.errorMessage));
|
||||
} else {
|
||||
// Другие состояния, если они есть.
|
||||
return Container(); // Пустой контейнер для необработанных состояний.
|
||||
}
|
||||
},
|
||||
return BlocProvider<DiaryBloc>(
|
||||
create: (context) => sl<DiaryBloc>()..add(LoadDiary()),
|
||||
child: BlocBuilder<DiaryBloc, DiaryState>(
|
||||
builder: (context, state) {
|
||||
if (state is DiaryStateLoaded) {
|
||||
return _buildDiary(context, state);
|
||||
} else if (state is DiaryStateLoading) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: CupertinoActivityIndicator(),
|
||||
),
|
||||
);
|
||||
} else if (state is DiaryStateError) {
|
||||
return Scaffold(
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Center(
|
||||
child: Text(state.errorMessage),
|
||||
),
|
||||
),
|
||||
);
|
||||
} else
|
||||
return Scaffold(
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Center(
|
||||
child: Text("Неопределенная ошибка"),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDiaryEditScreen(
|
||||
BuildContext context, DiaryEditLoadedState state) {
|
||||
Widget _buildDiary(BuildContext context, DiaryStateLoaded state) {
|
||||
return RefreshIndicator(
|
||||
onRefresh: refreshPage(context),
|
||||
child: Stack(
|
||||
@@ -121,13 +128,13 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
|
||||
refreshPage(BuildContext context) {
|
||||
return () async {
|
||||
context.read<DiaryEditBloc>().add(RefreshEvent());
|
||||
context.read<DiaryBloc>().add(RefreshEvent());
|
||||
};
|
||||
}
|
||||
|
||||
/// Section Widget
|
||||
_buildHeaderRow(BuildContext context, DiaryEditLoadedState state) {
|
||||
final day = state.diaryId;
|
||||
_buildHeaderRow(BuildContext context, DiaryStateLoaded state) {
|
||||
final day = state.diary.id;
|
||||
return Container(
|
||||
margin: EdgeInsets.symmetric(horizontal: 16.h, vertical: 16.h),
|
||||
padding: EdgeInsets.symmetric(
|
||||
@@ -162,7 +169,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
),
|
||||
SizedBox(height: 2.v),
|
||||
Text(
|
||||
state.diaryDate.toString(),
|
||||
state.diary.date.toString(),
|
||||
style: CustomTextStyles.bodyMediumBluegray800,
|
||||
),
|
||||
],
|
||||
@@ -174,7 +181,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
}
|
||||
|
||||
/// Section Widget
|
||||
_buildAddProduct(BuildContext context, DiaryEditLoadedState state) {
|
||||
_buildAddProduct(BuildContext context, DiaryState state) {
|
||||
return CustomOutlinedButton(
|
||||
text: "Добавить продукт",
|
||||
margin: EdgeInsets.symmetric(horizontal: 10.h),
|
||||
@@ -185,12 +192,12 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
|
||||
addProduct(BuildContext context) {
|
||||
return () {
|
||||
context.read<DiaryEditBloc>().add(AddPortionEvent());
|
||||
context.read<DiaryBloc>().add(AddPortionEvent());
|
||||
};
|
||||
}
|
||||
|
||||
/// Section Widget
|
||||
_buildAddPhoto(BuildContext context, DiaryEditLoadedState state) {
|
||||
_buildAddPhoto(BuildContext context, DiaryStateLoaded state) {
|
||||
bool isLoading = state.isLoading;
|
||||
return GestureDetector(
|
||||
onTap: addImage(context, isLoading),
|
||||
@@ -258,14 +265,14 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
List<XFile>? images = await picker.pickMultiImage();
|
||||
if (images.isNotEmpty) {
|
||||
for (var image in images) {
|
||||
context.read<DiaryEditBloc>().add(UploadImages(image));
|
||||
context.read<DiaryBloc>().add(UploadImages(image));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
_buildMeal(BuildContext context, DiaryEditLoadedState state) {
|
||||
_buildMeal(BuildContext context, DiaryStateLoaded state) {
|
||||
final digitsOnly = FilteringTextInputFormatter.digitsOnly;
|
||||
List<String> _options =
|
||||
state.autocompleteList!.map((dynamic item) => item as String).toList();
|
||||
@@ -329,17 +336,22 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
SizedBox(height: 20.v),
|
||||
Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 10.h),
|
||||
child: BlocSelector<DiaryEditBloc, DiaryEditState, DiaryEditModel?>(
|
||||
selector: (state) => state.diaryEditModelObj,
|
||||
builder: (context, diaryEditModelObj) {
|
||||
child: BlocSelector<DiaryBloc, DiaryState, DiaryModel?>(
|
||||
selector: (state) {
|
||||
if (state is DiaryStateLoaded)
|
||||
return state.diary;
|
||||
else
|
||||
return null;
|
||||
},
|
||||
builder: (context, diary) {
|
||||
return CustomDropDown(
|
||||
hintText: "Прием пищи",
|
||||
items: diaryEditModelObj?.mealList,
|
||||
items: mealsToDropdown(diary!.meals),
|
||||
contentPadding: EdgeInsets.symmetric(
|
||||
horizontal: 10.h,
|
||||
vertical: 9.v,
|
||||
),
|
||||
defaultValue: diaryEditModelObj?.mealList[0],
|
||||
defaultValue: mealsToDropdown(diary.meals)[0],
|
||||
onChanged: changeMeal(context, state),
|
||||
);
|
||||
},
|
||||
@@ -424,13 +436,20 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(left: 30.h),
|
||||
child: BlocSelector<DiaryEditBloc, DiaryEditState,
|
||||
DiaryEditModel?>(
|
||||
selector: (state) => state.diaryEditModelObj,
|
||||
builder: (context, diaryEditModelObj) {
|
||||
child: BlocSelector<DiaryBloc, DiaryState, DiaryModel?>(
|
||||
selector: (state) {
|
||||
if (state is DiaryStateLoaded) {
|
||||
return state.diary;
|
||||
} else
|
||||
return null;
|
||||
},
|
||||
builder: (context, diary) {
|
||||
return CustomDropDown(
|
||||
items: diaryEditModelObj?.portionList,
|
||||
defaultValue: diaryEditModelObj?.portionList?[0],
|
||||
items: [
|
||||
SelectionPopupModel(title: "gr"),
|
||||
SelectionPopupModel(title: "pcs")
|
||||
],
|
||||
defaultValue: SelectionPopupModel(title: "gr"),
|
||||
onChanged: changePortion(context, state),
|
||||
);
|
||||
},
|
||||
@@ -463,9 +482,9 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
style: CustomTextStyles.bodyMediumBlack900,
|
||||
),
|
||||
SizedBox(height: 15.v),
|
||||
BlocBuilder<DiaryEditBloc, DiaryEditState>(
|
||||
BlocBuilder<DiaryBloc, DiaryState>(
|
||||
builder: (context, state) {
|
||||
if (state is DiaryEditLoadedState) {
|
||||
if (state is DiaryStateLoaded) {
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
@@ -505,7 +524,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
],
|
||||
);
|
||||
} else {
|
||||
return Container(); // Возвращаем пустой контейнер, если состояние не является DiaryEditLoadedState
|
||||
return Container(); // Возвращаем пустой контейнер, если состояние не является DiaryState
|
||||
}
|
||||
},
|
||||
),
|
||||
@@ -514,7 +533,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
openCaloriesDialog(BuildContext context, DiaryEditLoadedState state) {
|
||||
openCaloriesDialog(BuildContext context, DiaryStateLoaded state) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext dialogContext) {
|
||||
@@ -587,7 +606,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
) {
|
||||
return () {
|
||||
// Call function to send moderation with the entered values
|
||||
context.read<DiaryEditBloc>().add(
|
||||
context.read<DiaryBloc>().add(
|
||||
SendModeration(
|
||||
name: name,
|
||||
energyOn100gr: energyOn100gr,
|
||||
@@ -609,12 +628,12 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
|
||||
changeSatiety(BuildContext context, int satiety) {
|
||||
return () {
|
||||
context.read<DiaryEditBloc>().add(ChangeSatiety(satiety));
|
||||
context.read<DiaryBloc>().add(ChangeSatiety(satiety));
|
||||
};
|
||||
}
|
||||
|
||||
selectProduct(AutocompleteOnSelected<String> onSelected, String option,
|
||||
DiaryEditLoadedState state) {
|
||||
DiaryStateLoaded state) {
|
||||
state.productNameController?.text = option;
|
||||
return () {
|
||||
onSelected(option);
|
||||
@@ -627,7 +646,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
?.map((dynamic item) => item as String)
|
||||
.toList());*/
|
||||
if (value.length > 2) {
|
||||
context.read<DiaryEditBloc>().add(ProductInput(value));
|
||||
context.read<DiaryBloc>().add(ProductInput(value));
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -640,27 +659,27 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
};
|
||||
}
|
||||
|
||||
changeMeal(BuildContext context, DiaryEditLoadedState state) {
|
||||
changeMeal(BuildContext context, DiaryStateLoaded state) {
|
||||
return (value) {
|
||||
context
|
||||
.read<DiaryEditBloc>()
|
||||
.add(ChangeMeal(meals: state.meals, value: value));
|
||||
.read<DiaryBloc>()
|
||||
.add(ChangeMeal(meals: state.diary.meals, value: value));
|
||||
};
|
||||
}
|
||||
|
||||
changePortion(BuildContext context, DiaryEditLoadedState state) {
|
||||
changePortion(BuildContext context, DiaryState state) {
|
||||
return (value) {
|
||||
context.read<DiaryEditBloc>().add(ChangePortion(value: value));
|
||||
context.read<DiaryBloc>().add(ChangePortion(value: value));
|
||||
};
|
||||
}
|
||||
|
||||
_buildCarousel(BuildContext context, DiaryEditLoadedState state) {
|
||||
if (state.photos!.isEmpty) {
|
||||
_buildCarousel(BuildContext context, DiaryStateLoaded state) {
|
||||
if (state.currentMeal!.photos!.isEmpty) {
|
||||
return Text("Фото нет");
|
||||
} else {
|
||||
return CarouselSlider(
|
||||
options: CarouselOptions(height: 340.0),
|
||||
items: state.photos!.map<Widget>((photo) {
|
||||
items: state.currentMeal!.photos!.map<Widget>((photo) {
|
||||
return _buildPhoto(
|
||||
context, photo); // Assuming photos are stored as strings
|
||||
}).toList(),
|
||||
@@ -668,7 +687,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
_buildProduct(BuildContext context, Map<String, dynamic> portion) {
|
||||
_buildProduct(BuildContext context, Portion portion) {
|
||||
return Container(
|
||||
margin: EdgeInsets.symmetric(horizontal: 10.h),
|
||||
padding: EdgeInsets.symmetric(
|
||||
@@ -688,9 +707,9 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
top: 5.v,
|
||||
),
|
||||
child: Text(
|
||||
portion['product_name'].length > 8
|
||||
? portion['product_name'].substring(0, 8)
|
||||
: portion['product_name'],
|
||||
portion.productName.length > 8
|
||||
? portion.productName.substring(0, 8)
|
||||
: portion.productName,
|
||||
style: CustomTextStyles.titleMedium16,
|
||||
),
|
||||
),
|
||||
@@ -701,9 +720,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
bottom: 3.v,
|
||||
),
|
||||
child: Text(
|
||||
portion['quantity'].toString() +
|
||||
' ' +
|
||||
portion['portion'].toString().tr,
|
||||
portion.quantity.toString() + ' ' + portion.portion.toString().tr,
|
||||
),
|
||||
),
|
||||
Spacer(),
|
||||
@@ -722,7 +739,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
_buildPhoto(BuildContext context, Map<String, dynamic> photo) {
|
||||
_buildPhoto(BuildContext context, Photo photo) {
|
||||
return Stack(
|
||||
children: [
|
||||
Container(
|
||||
@@ -731,7 +748,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
borderRadius: BorderRadiusStyle.roundedBorder5,
|
||||
),
|
||||
child: Image.network(
|
||||
dotenv.env['STORAGE_URL']! + photo['path'],
|
||||
dotenv.env['STORAGE_URL']! + photo.path,
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
@@ -756,21 +773,21 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
removePortion(BuildContext context, Map<String, dynamic> portion) {
|
||||
removePortion(BuildContext context, Portion portion) {
|
||||
return () {
|
||||
context.read<DiaryEditBloc>().add(RemovePortionEvent(portion['id']));
|
||||
context.read<DiaryBloc>().add(RemovePortionEvent(portion.id));
|
||||
};
|
||||
}
|
||||
|
||||
removePhoto(BuildContext context, Map<String, dynamic> photo) {
|
||||
removePhoto(BuildContext context, Photo photo) {
|
||||
return () {
|
||||
context
|
||||
.read<DiaryEditBloc>()
|
||||
.add(RemoveImageEvent(photo['id'].toString(), photo['path']));
|
||||
.read<DiaryBloc>()
|
||||
.add(RemoveImageEvent(photo.id.toString(), photo.path));
|
||||
};
|
||||
}
|
||||
|
||||
_buildNutritionChart(BuildContext context, DiaryEditLoadedState state) {
|
||||
_buildNutritionChart(BuildContext context, DiaryStateLoaded state) {
|
||||
final List<int> recommendedValues = state
|
||||
.recommendedChartValues; // Рекомендуемые значения: калории, жиры, белки, углеводы
|
||||
final List<int> consumedValues = state.consumedChartValues; //
|
||||
@@ -992,7 +1009,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
}
|
||||
|
||||
/// Section Widget
|
||||
_buildComment(BuildContext context, DiaryEditLoadedState state) {
|
||||
_buildComment(BuildContext context, DiaryStateLoaded state) {
|
||||
return Container(
|
||||
margin: EdgeInsets.symmetric(horizontal: 16.h),
|
||||
padding: EdgeInsets.symmetric(
|
||||
@@ -1036,7 +1053,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
_buildWakupRetire(BuildContext context, DiaryEditLoadedState state) {
|
||||
_buildWakupRetire(BuildContext context, DiaryStateLoaded state) {
|
||||
return Container(
|
||||
margin: EdgeInsets.symmetric(horizontal: 16.h),
|
||||
padding: EdgeInsets.symmetric(
|
||||
@@ -1112,7 +1129,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
|
||||
toggleWakeup(BuildContext context) {
|
||||
return () {
|
||||
BlocProvider.of<DiaryEditBloc>(context).add(ToggleWakeupExpandedEvent());
|
||||
BlocProvider.of<DiaryBloc>(context).add(ToggleWakeupExpandedEvent());
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1146,7 +1163,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
BuildContext dialogContext, BuildContext context) {
|
||||
return () {
|
||||
closeDialog(dialogContext);
|
||||
context.read<DiaryEditBloc>().add(FinishDay());
|
||||
context.read<DiaryBloc>().add(FinishDay());
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1156,7 +1173,7 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
};
|
||||
}
|
||||
|
||||
_buildMood(BuildContext context, DiaryEditLoadedState state) {
|
||||
_buildMood(BuildContext context, DiaryStateLoaded state) {
|
||||
return Container(
|
||||
margin: EdgeInsets.symmetric(horizontal: 16.h),
|
||||
padding: EdgeInsets.symmetric(
|
||||
@@ -1238,49 +1255,46 @@ class DiaryEditScreen extends StatelessWidget {
|
||||
|
||||
changeMood(BuildContext context, int satiety) {
|
||||
return () {
|
||||
context.read<DiaryEditBloc>().add(ChangeMood(satiety));
|
||||
context.read<DiaryBloc>().add(ChangeMood(satiety));
|
||||
};
|
||||
}
|
||||
|
||||
toggleMood(BuildContext context) {
|
||||
return () {
|
||||
BlocProvider.of<DiaryEditBloc>(context).add(ToggleMoodExpandedEvent());
|
||||
BlocProvider.of<DiaryBloc>(context).add(ToggleMoodExpandedEvent());
|
||||
};
|
||||
}
|
||||
|
||||
Future<void> _selectWakeupTime(
|
||||
BuildContext context, DiaryEditLoadedState state) async {
|
||||
BuildContext context, DiaryStateLoaded state) async {
|
||||
final TimeOfDay? pickedTime = await showTimePicker(
|
||||
context: context,
|
||||
initialTime: state.wakeupController ?? TimeOfDay.now(),
|
||||
);
|
||||
if (pickedTime != null && pickedTime != state.wakeupController) {
|
||||
state.wakeupController = pickedTime;
|
||||
context.read<DiaryEditBloc>().add(WakeupEvent(pickedTime));
|
||||
context.read<DiaryBloc>().add(WakeupEvent(pickedTime));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _selectRetireTime(
|
||||
BuildContext context, DiaryEditLoadedState state) async {
|
||||
BuildContext context, DiaryStateLoaded state) async {
|
||||
final TimeOfDay? pickedTime = await showTimePicker(
|
||||
context: context,
|
||||
initialTime: state.retireController ?? TimeOfDay.now(),
|
||||
);
|
||||
if (pickedTime != null && pickedTime != state.retireController) {
|
||||
state.retireController = pickedTime;
|
||||
context.read<DiaryEditBloc>().add(RetireEvent(pickedTime));
|
||||
context.read<DiaryBloc>().add(RetireEvent(pickedTime));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _selectMealTime(
|
||||
BuildContext context, DiaryEditLoadedState state) async {
|
||||
BuildContext context, DiaryStateLoaded state) async {
|
||||
final TimeOfDay? pickedTime = await showTimePicker(
|
||||
context: context,
|
||||
initialTime: state.mealTimeController ?? TimeOfDay.now(),
|
||||
);
|
||||
if (pickedTime != null && pickedTime != state.mealTimeController) {
|
||||
state.mealTimeController = pickedTime;
|
||||
context.read<DiaryEditBloc>().add(ChangeMealTime(pickedTime));
|
||||
context.read<DiaryBloc>().add(ChangeMealTime(pickedTime));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1296,4 +1310,13 @@ bool checkConsumedValues(
|
||||
// Если ни одно потребленное значение не превышает рекомендованное с соответствующим индексом
|
||||
return false; // Возвращаем true
|
||||
}
|
||||
*/
|
||||
|
||||
List<SelectionPopupModel> mealsToDropdown(List<Meal> meals) {
|
||||
return meals
|
||||
.map((meal) => SelectionPopupModel(
|
||||
id: meal.id,
|
||||
title: "Прием пищи ${meal.id}",
|
||||
isSelected: false,
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user