forked from eatvio/eatvio_app
feat(diary): added state for future days with diet
This commit is contained in:
@@ -36,19 +36,33 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
|
||||
String comment = '';
|
||||
|
||||
setSelectedDay(DateTime day) async {
|
||||
setSelectedDay(DateTime day, {bool? forDietOnly}) async {
|
||||
selectedDay = day;
|
||||
if (forDietOnly ?? false) {
|
||||
await fetchDiaryForDietOnly();
|
||||
return;
|
||||
}
|
||||
await fetchDiary(true);
|
||||
}
|
||||
|
||||
setSelectedMeal(int meal) {
|
||||
selectedMeal = meal;
|
||||
emit((state as DiaryStateLoaded).copyWith(selectedMeal: selectedMeal));
|
||||
final currentState = state;
|
||||
if (currentState is DiaryStateLoaded) {
|
||||
emit(currentState.copyWith(selectedMeal: selectedMeal));
|
||||
} else if (currentState is DiaryStateLoadedTomorrowWithDiet) {
|
||||
emit(currentState.copyWith(selectedMeal: selectedMeal));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> fetchDiaryForDietOnly() async {
|
||||
final stringDate = selectedDay.format('yyyy-MM-dd');
|
||||
final diaryDay = await diaryRepository.getDiaryByDateForDietOnly(stringDate);
|
||||
emit(DiaryState.loadedTomorrowWithDiet(diary: diaryDay, selectedDay: selectedDay, selectedMeal: selectedMeal));
|
||||
}
|
||||
|
||||
fetchDiary([bool needLoading = false, int? diaryId]) async {
|
||||
this.diaryId ??= diaryId;
|
||||
await profileRepository.getChosenDiets();
|
||||
if (needLoading) {
|
||||
emit(const DiaryState.loading());
|
||||
selectedMeal = 1;
|
||||
@@ -58,7 +72,7 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
if (diaryId != null) {
|
||||
diaryDay = await diaryRepository.getDiaryDay(diaryId);
|
||||
} else {
|
||||
final stringDate = selectedDay.toUtc().format('yyyy-MM-dd');
|
||||
final stringDate = selectedDay.format('yyyy-MM-dd');
|
||||
diaryDay = await diaryRepository.getDiaryByDate(stringDate);
|
||||
}
|
||||
ConsultantReviewModel? consultantReview;
|
||||
|
||||
@@ -21,8 +21,14 @@ class DiaryState with _$DiaryState {
|
||||
required DateTime selectedDay,
|
||||
required int selectedMeal,
|
||||
@Default(false) bool isConsultantView,
|
||||
ConsultantReviewModel? consultantReview,
|
||||
ConsultantReviewModel? consultantReview,
|
||||
@Default(false) bool isDiaryFinished,
|
||||
@Default(false) bool isDiaryCommented,
|
||||
}) = DiaryStateLoaded;
|
||||
|
||||
const factory DiaryState.loadedTomorrowWithDiet({
|
||||
required DiaryFutureDietDayModel diary,
|
||||
required DateTime selectedDay,
|
||||
required int selectedMeal,
|
||||
}) = DiaryStateLoadedTomorrowWithDiet;
|
||||
}
|
||||
|
||||
@@ -96,6 +96,11 @@ class DiaryScreenContent extends StatelessWidget {
|
||||
showMood: state.diary.typeOfDiary.hasChosenPlan,
|
||||
showAddButton: !state.isConsultantView && !state.isDiaryFinished && !state.isDiaryCommented,
|
||||
),
|
||||
if (state is DiaryStateLoadedTomorrowWithDiet)
|
||||
const MealWidget(
|
||||
showMood: false,
|
||||
showAddButton: false,
|
||||
),
|
||||
if (state is DiaryStateLoaded) const CaloriesChartWidget(),
|
||||
const SizedBox(height: 12),
|
||||
Builder(builder: (context) {
|
||||
|
||||
@@ -53,8 +53,30 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
if (!isSameDay(_selectedDay, selectedDay)) {
|
||||
final today = DateTime.now();
|
||||
if (selectedDay.isAfter(today)) {
|
||||
showErrorDialog(context, title: s.want_to_see_tomorrow, description: s.can_not_see_next_day);
|
||||
return;
|
||||
if ((state is DiaryStateLoaded)) {
|
||||
if (!state.diary.typeOfDiary.hasChosenDiet) {
|
||||
showErrorDialog(context, title: s.want_to_see_tomorrow, description: s.can_not_see_next_day);
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_selectedDay = selectedDay;
|
||||
_focusedDay = focusedDay;
|
||||
});
|
||||
context.read<DiaryCubit>().setSelectedDay(_selectedDay, forDietOnly: true);
|
||||
return;
|
||||
}
|
||||
if (state is DiaryStateLoadedTomorrowWithDiet) {
|
||||
if (!state.diary.typeOfDiary.hasChosenDiet) {
|
||||
showErrorDialog(context, title: s.want_to_see_tomorrow, description: s.can_not_see_next_day);
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_selectedDay = selectedDay;
|
||||
_focusedDay = focusedDay;
|
||||
});
|
||||
context.read<DiaryCubit>().setSelectedDay(_selectedDay, forDietOnly: true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
setState(() {
|
||||
_selectedDay = selectedDay;
|
||||
|
||||
@@ -59,6 +59,32 @@ class _MealButtonWidgetState extends State<MealButtonWidget> {
|
||||
),
|
||||
);
|
||||
}
|
||||
if (state is DiaryStateLoadedTomorrowWithDiet) {
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
context.read<DiaryCubit>().setSelectedMeal(widget.mealNumber);
|
||||
},
|
||||
borderRadius: BorderRadius.circular(100),
|
||||
child: Ink(
|
||||
width: MediaQuery.of(context).size.width / 5 - (96 / 5),
|
||||
height: 32,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(100),
|
||||
color: state.selectedMeal == widget.mealNumber ? appTheme.blueA400 : const Color(0xFFF1F2F6),
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'${widget.mealNumber}',
|
||||
style: TextStyle(
|
||||
color: state.selectedMeal == widget.mealNumber ? Colors.white : const Color(0xFF6D6F71),
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
);
|
||||
|
||||
@@ -64,6 +64,18 @@ class _ProductWidgetState extends State<ProductWidget> {
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<DiaryCubit, DiaryState>(
|
||||
builder: (context, state) {
|
||||
if (state is DiaryStateLoadedTomorrowWithDiet) {
|
||||
return ListTile(
|
||||
horizontalTitleGap: 0,
|
||||
contentPadding: widget.portion.fromDiet ? const EdgeInsets.only(right: 24, left: 4) : null,
|
||||
title: Text(widget.portion.productName),
|
||||
subtitle: Text('${widget.portion.quantity} ${widget.portion.portion == 'gr' ? 'гр' : 'шт'}'),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
tileColor: const Color(0xFFF6FAFF),
|
||||
);
|
||||
}
|
||||
if (state is DiaryStateLoaded) {
|
||||
final isDiaryCommented = state.diary.typeOfDiary.diaryState == DiaryStateEnum.commented ||
|
||||
state.diary.typeOfDiary.diaryState == DiaryStateEnum.lastDayCommented ||
|
||||
|
||||
@@ -28,6 +28,18 @@ class _ProductsListWidgetState extends State<ProductsListWidget> {
|
||||
],
|
||||
);
|
||||
}
|
||||
if (state is DiaryStateLoadedTomorrowWithDiet) {
|
||||
int mealIndex = state.selectedMeal - 1;
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
for (int i = 0; i < state.diary.meals[mealIndex].portions.length; i++) ...[
|
||||
ProductWidget(portion: state.diary.meals[mealIndex].portions[i]),
|
||||
i < state.diary.meals[mealIndex].portions.length - 1 ? const SizedBox(height: 8) : const SizedBox.shrink(),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user