forked from eatvio/eatvio_app
feat(diary): fixed bugs fod diary with v2 methods
This commit is contained in:
@@ -60,24 +60,17 @@ abstract class DiaryDayModel with _$DiaryDayModel {
|
||||
meals: meals,
|
||||
chosenPlanId: chosenPlanId,
|
||||
dayOfChosenDiet: dayOfChosenDiet,
|
||||
wakeup: wakeup,
|
||||
retire: retire,
|
||||
diet: diet,
|
||||
);
|
||||
}
|
||||
|
||||
DiaryFutureDietDayEntity toDiaryFutureDayEntity() {
|
||||
|
||||
return DiaryFutureDietDayEntity(
|
||||
date: date,
|
||||
typeOfDiary: typeOfDiary,
|
||||
meals: meals,
|
||||
dayOfChosenDiet: dayOfChosenDiet,
|
||||
diet: diet,
|
||||
);
|
||||
return DiaryFutureDietDayEntity(date: date, typeOfDiary: typeOfDiary, meals: meals, dayOfChosenDiet: dayOfChosenDiet, diet: diet);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
DiaryDietModel? parseDiet(dynamic json) {
|
||||
if (json == null) return null;
|
||||
return DiaryDietModel.fromJson(json['diet']);
|
||||
|
||||
@@ -23,7 +23,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
class DiaryCubit extends Cubit<DiaryState> {
|
||||
late final DateTime today;
|
||||
late DateTime selectedDay;
|
||||
late int selectedMeal;
|
||||
int selectedMeal = 1;
|
||||
final DiaryRepository diaryRepository;
|
||||
final GlossaryRepository glossaryRepository;
|
||||
final ProfileRepository profileRepository;
|
||||
@@ -48,44 +48,70 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
|
||||
String comment = '';
|
||||
|
||||
Future<bool> setSelectedDay(DateTime day, {bool? forDietOnly}) async {
|
||||
selectedDay = day;
|
||||
if (forDietOnly ?? false) {
|
||||
final result = await fetchDiaryV2();
|
||||
return result;
|
||||
Future<bool> setSelectedDay(DateTime day) async {
|
||||
final canSelect = await fetchDiaryV2(needLoading: true);
|
||||
if (canSelect) {
|
||||
selectedDay = day;
|
||||
}
|
||||
await fetchDiaryV2(needLoading: true);
|
||||
return true;
|
||||
return canSelect;
|
||||
}
|
||||
|
||||
Future<bool> fetchDiaryV2({bool needLoading = false, int? diaryId, int? currentDay}) async {
|
||||
final (diaryDay, error) = await qwe();
|
||||
if (error != null) {
|
||||
if (error == DiaryFetchingErrors.canNotCreateFutureDays) {
|
||||
return false;
|
||||
}
|
||||
emit(DiaryState.error(errorMessage: error.toString()));
|
||||
Future<bool> fetchDiaryV2({bool needLoading = false, int? diaryId}) async {
|
||||
final currentState = state;
|
||||
if (needLoading) {
|
||||
emit(const DiaryState.loading());
|
||||
}
|
||||
|
||||
if (diaryDay != null) {
|
||||
final diaryEntity = diaryDay.toEntity();
|
||||
if (diaryId != null) {
|
||||
final diaryDayById = await diaryRepository.getDiaryDay(diaryId);
|
||||
final diaryEntity = diaryDayById.toEntity();
|
||||
if (diaryEntity != null) {
|
||||
setDiary(diaryDay: diaryEntity);
|
||||
setDiary(diaryDay: diaryEntity, diaryId: diaryId);
|
||||
return true;
|
||||
}
|
||||
setDiaryForDietOnly(diaryDay.toDiaryFutureDayEntity());
|
||||
}
|
||||
return true;
|
||||
final diaryDay = await getDiary();
|
||||
if (diaryDay == null) {
|
||||
emit(const DiaryState.error(errorMessage: 'Diary not found'));
|
||||
return true;
|
||||
}
|
||||
|
||||
final diaryEntity = diaryDay.toEntity();
|
||||
if (diaryEntity != null) {
|
||||
setDiary(diaryDay: diaryEntity, diaryId: diaryId);
|
||||
return true;
|
||||
}
|
||||
if (diaryDay.typeOfDiary.hasChosenDiet == true && diaryDay.typeOfDiary.diaryState == null) {
|
||||
setDiaryForDietOnly(diaryDay.toDiaryFutureDayEntity());
|
||||
return true;
|
||||
}
|
||||
emit(currentState);
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<(DiaryDayModel?, DiaryFetchingErrors?)> qwe() async {
|
||||
Future<void> updateCurrentDiary() async {
|
||||
try {
|
||||
final diaryDay = await diaryRepository.getDiaryDayV2(day: selectedDay.toISO8601WithTimeZoneOffset());
|
||||
} on DioException catch (e) {
|
||||
|
||||
final diaryEntity = diaryDay.toEntity();
|
||||
if (diaryEntity != null) {
|
||||
setDiary(diaryDay: diaryEntity, diaryId: diaryId);
|
||||
}
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
return (null, null);
|
||||
Future<DiaryDayModel?> getDiary() async {
|
||||
try {
|
||||
await diaryRepository.createDiaryDay(day: selectedDay.toISO8601WithTimeZoneOffset());
|
||||
final diaryDay = await diaryRepository.getDiaryDayV2(day: selectedDay.toISO8601WithTimeZoneOffset());
|
||||
return diaryDay;
|
||||
} on DioException catch (e) {
|
||||
if (e.response?.statusCode == 422) {
|
||||
final diaryDay = await diaryRepository.getDiaryDayV2(day: selectedDay.toISO8601WithTimeZoneOffset());
|
||||
return diaryDay;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void setSelectedMeal(int meal) {
|
||||
@@ -177,18 +203,14 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> setDiary({int? diaryId, int? currentDay, required DiaryDayEntity diaryDay}) async {
|
||||
Future<void> setDiary({int? diaryId, required DiaryDayEntity diaryDay}) async {
|
||||
this.diaryId ??= diaryId;
|
||||
final isConsultantView = this.diaryId != null;
|
||||
try {
|
||||
final DiaryDayModel diaryDay;
|
||||
bool isMoodEnabled = false;
|
||||
bool isWakeUpAndRetireTimeEnabled = false;
|
||||
if (diaryId != null) {
|
||||
diaryDay = await diaryRepository.getDiaryDay(diaryId);
|
||||
} else {
|
||||
final stringDate = selectedDay.format('yyyy-MM-dd');
|
||||
diaryDay = await diaryRepository.getDiaryByDate(stringDate);
|
||||
final profileSettings = await settingsRepository.getProfileSettings();
|
||||
isMoodEnabled = profileSettings.isMoodFillable;
|
||||
isWakeUpAndRetireTimeEnabled = profileSettings.isBedtimeFillable;
|
||||
@@ -201,14 +223,14 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
final chosenPlanModel = await chatRepository.getChosenPlan(id: diaryDay.chosenPlanId!);
|
||||
if (!isConsultantView &&
|
||||
(diaryDay.typeOfDiary.diaryState == DiaryStateEnum.lastDayCommented || diaryDay.typeOfDiary.diaryState == DiaryStateEnum.commented)) {
|
||||
await diaryRepository.viewCommentOfDiaryDayByConsultant(diaryId: diaryDay.id!);
|
||||
await diaryRepository.viewCommentOfDiaryDayByConsultant(diaryId: diaryDay.id);
|
||||
}
|
||||
consultantReview = await diaryRepository.getDiaryComments(planId: diaryDay.chosenPlanId!, diaryId: diaryDay.id!);
|
||||
consultantReview = await diaryRepository.getDiaryComments(planId: diaryDay.chosenPlanId!, diaryId: diaryDay.id);
|
||||
planDuration = chosenPlanModel.plan.duration;
|
||||
planName = chosenPlanModel.plan.name;
|
||||
currentPlanDay = consultantReview.currentPlanDay;
|
||||
}
|
||||
final nutrition = await getNutrition(diaryDay.id!);
|
||||
final nutrition = await getNutrition(diaryDay.id);
|
||||
|
||||
final isDiaryFinished = diaryDay.typeOfDiary.diaryState == DiaryStateEnum.finished || diaryDay.typeOfDiary.diaryState == DiaryStateEnum.lastDayFinished;
|
||||
|
||||
@@ -218,7 +240,7 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
diaryDay.typeOfDiary.diaryState == DiaryStateEnum.reviewedByUser;
|
||||
emit(
|
||||
DiaryState.loaded(
|
||||
diary: diaryDay.toEntity()!,
|
||||
diary: diaryDay,
|
||||
nutrition: nutrition,
|
||||
selectedDay: selectedDay,
|
||||
selectedMeal: selectedMeal,
|
||||
@@ -241,7 +263,7 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
|
||||
Future<void> finishDiaryDay({required int diaryId, required String wakeUp, required String retire}) async {
|
||||
await diaryRepository.finishDiaryDay(diaryId: diaryId, wakeUp: wakeUp, retire: retire, comment: comment.isEmpty ? null : comment);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<NutritionDataModel> getNutrition(int index) async {
|
||||
@@ -265,7 +287,7 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
quantity: quantity,
|
||||
productNutritionalValueId: productNutritionalValueId,
|
||||
);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<void> updateProductInMeal({
|
||||
@@ -299,27 +321,27 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
portionState: portionState,
|
||||
);
|
||||
}
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<void> markEaten({required int diaryId, required int mealId, required int portionId}) async {
|
||||
await diaryRepository.markEaten(diaryId: diaryId, mealId: mealId, portionId: portionId);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<void> unmarkEaten({required int diaryId, required int mealId, required int portionId}) async {
|
||||
await diaryRepository.unmarkEaten(diaryId: diaryId, mealId: mealId, portionId: portionId);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<void> restore({required int diaryId, required int mealId, required int portionId}) async {
|
||||
await diaryRepository.restore(diaryId: diaryId, mealId: mealId, portionId: portionId);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<void> deleteProductFromMeal({required int diaryId, required int mealId, required int portionId}) async {
|
||||
await diaryRepository.deleteProductFromMeal(diaryId: diaryId, mealId: mealId, portionId: portionId);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<List<GlossaryModel>> getGlossaryListByQuery(String query) async {
|
||||
@@ -328,12 +350,12 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
|
||||
Future<void> uploadPhotos({required List<File> files, required int diaryId}) async {
|
||||
await diaryRepository.uploadPhotos(files: await ImageCompressUtility.compressFilesList(files), diaryId: diaryId, mealId: selectedMeal);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<void> deletePhoto({required DiaryPhotoModel photo, required int diaryId}) async {
|
||||
await diaryRepository.deletePhoto(photo: photo, diaryId: diaryId, mealId: selectedMeal);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<GlossaryModerationModel?> sendProductToModeration({
|
||||
@@ -353,34 +375,32 @@ class DiaryCubit extends Cubit<DiaryState> {
|
||||
|
||||
Future<void> fillTime({required int diaryId, required String time}) async {
|
||||
await diaryRepository.fillTime(diaryId: diaryId, mealId: selectedMeal, time: time);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<void> fillWakeup({required int diaryId, required String time}) async {
|
||||
await diaryRepository.fillWakeup(diaryId: diaryId, time: time);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<void> fillRetire({required int diaryId, required String time}) async {
|
||||
await diaryRepository.fillRetire(diaryId: diaryId, time: time);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<void> fillSatiety({required int diaryId, required int satiety}) async {
|
||||
await diaryRepository.fillSatiety(diaryId: diaryId, mealId: selectedMeal, satiety: satiety);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
Future<void> fillMood({required int diaryId, required int mood}) async {
|
||||
await diaryRepository.fillMood(diaryId: diaryId, mealId: selectedMeal, mood: mood);
|
||||
await fetchDiaryV2();
|
||||
await updateCurrentDiary();
|
||||
}
|
||||
|
||||
void fillComment(String comment) {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
enum DiaryFetchingErrors { canNotCreateFutureDays, dayAlreadyExists }
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import 'package:eatvio_app/features/diary/entities/diary_day_entity.dart';
|
||||
import 'package:eatvio_app/features/diary/entities/diary_future_diet_day_entity.dart';
|
||||
import 'package:eatvio_app/features/diary/models/consultant_review_model.dart';
|
||||
import 'package:eatvio_app/features/diary/models/diary_day_model.dart';
|
||||
import 'package:eatvio_app/features/diary/models/nutrition_model.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ class DiaryScreenContent extends StatelessWidget {
|
||||
final s = S.of(context);
|
||||
return MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider<DiaryCubit>(create: (BuildContext context) => sl<DiaryCubit>()..fetchDiaryV2(needLoading: true, diaryId: diaryId, currentDay: currentPlanDay)),
|
||||
BlocProvider<DiaryCubit>(create: (BuildContext context) => sl<DiaryCubit>()..fetchDiaryV2(needLoading: true, diaryId: diaryId)),
|
||||
BlocProvider(create: (context) => sl<ConsultantCommentsCubit>()),
|
||||
],
|
||||
child: BlocBuilder<DiaryCubit, DiaryState>(
|
||||
|
||||
@@ -43,46 +43,24 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
locale: 'ru_RU',
|
||||
startingDayOfWeek: StartingDayOfWeek.monday,
|
||||
calendarFormat: _calendarFormat,
|
||||
headerStyle: const HeaderStyle(
|
||||
titleCentered: true,
|
||||
formatButtonVisible: false,
|
||||
leftChevronVisible: false,
|
||||
rightChevronVisible: false,
|
||||
),
|
||||
headerStyle: const HeaderStyle(titleCentered: true, formatButtonVisible: false, leftChevronVisible: false, rightChevronVisible: false),
|
||||
onDaySelected: (selectedDay, focusedDay) async {
|
||||
if (!isSameDay(_selectedDay, selectedDay)) {
|
||||
final today = DateTime.now();
|
||||
if (selectedDay.isAfter(today)) {
|
||||
if ((state is DiaryStateLoaded)) {
|
||||
final result = await context.read<DiaryCubit>().setSelectedDay(_selectedDay, forDietOnly: true);
|
||||
if (result == false && context.mounted) {
|
||||
showErrorDialog(context, title: s.want_to_see_tomorrow, description: s.can_not_see_next_day);
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_selectedDay = selectedDay;
|
||||
_focusedDay = focusedDay;
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (state is DiaryStateLoadedTomorrowWithDiet) {
|
||||
final result = await context.read<DiaryCubit>().setSelectedDay(_selectedDay, forDietOnly: true);
|
||||
if (result == false && context.mounted) {
|
||||
showErrorDialog(context, title: s.want_to_see_tomorrow, description: s.can_not_see_next_day);
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_selectedDay = selectedDay;
|
||||
_focusedDay = focusedDay;
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
final previousSelectedDay = _selectedDay;
|
||||
final previousFocusedDay = _selectedDay;
|
||||
setState(() {
|
||||
_selectedDay = selectedDay;
|
||||
_focusedDay = focusedDay;
|
||||
});
|
||||
context.read<DiaryCubit>().setSelectedDay(_selectedDay);
|
||||
final canOpenNextDay = await context.read<DiaryCubit>().setSelectedDay(selectedDay);
|
||||
if (!canOpenNextDay && context.mounted) {
|
||||
showErrorDialog(context, title: s.want_to_see_tomorrow, description: s.can_not_see_next_day);
|
||||
setState(() {
|
||||
_selectedDay = previousSelectedDay;
|
||||
_focusedDay = previousFocusedDay;
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
},
|
||||
selectedDayPredicate: (day) {
|
||||
@@ -101,26 +79,12 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
}
|
||||
},
|
||||
calendarStyle: CalendarStyle(
|
||||
weekendDecoration: const BoxDecoration(
|
||||
color: Colors.white,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
defaultDecoration: const BoxDecoration(
|
||||
color: Colors.white,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
selectedDecoration: BoxDecoration(
|
||||
color: appTheme.blueA400,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
todayTextStyle: const TextStyle(
|
||||
color: Colors.black,
|
||||
),
|
||||
weekendDecoration: const BoxDecoration(color: Colors.white, shape: BoxShape.circle),
|
||||
defaultDecoration: const BoxDecoration(color: Colors.white, shape: BoxShape.circle),
|
||||
selectedDecoration: BoxDecoration(color: appTheme.blueA400, shape: BoxShape.circle),
|
||||
todayTextStyle: const TextStyle(color: Colors.black),
|
||||
todayDecoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: appTheme.blueA400,
|
||||
width: 2,
|
||||
),
|
||||
border: Border.all(color: appTheme.blueA400, width: 2),
|
||||
color: Colors.white,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
@@ -152,10 +116,7 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
child: Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.only(top: 8, bottom: 16),
|
||||
child: Icon(
|
||||
_calendarFormat == CalendarFormat.month ? Icons.keyboard_arrow_up_rounded : Icons.keyboard_arrow_down_rounded,
|
||||
color: Colors.grey.shade600,
|
||||
),
|
||||
child: Icon(_calendarFormat == CalendarFormat.month ? Icons.keyboard_arrow_up_rounded : Icons.keyboard_arrow_down_rounded, color: Colors.grey.shade600),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import 'dart:developer';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
|
||||
Reference in New Issue
Block a user