forked from eatvio/eatvio_app
new test cases
This commit is contained in:
@@ -1,14 +1,21 @@
|
||||
import 'dart:async';
|
||||
import 'dart:developer';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:easy_debounce/easy_debounce.dart';
|
||||
import 'package:eatvio_app/core/utils/date_time_utils.dart';
|
||||
import 'package:eatvio_app/core/utils/toasts.dart';
|
||||
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
|
||||
import 'package:eatvio_app/features/diets/presentation/cubit/about_diet_state.dart';
|
||||
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class AboutDietCubit extends Cubit<AboutDietState> {
|
||||
AboutDietCubit({required this.dietsRepository}) : super(AboutDietState.initial());
|
||||
AboutDietCubit({required this.dietsRepository, required this.profileRepository}) : super(AboutDietState.initial());
|
||||
final DietRepository dietsRepository;
|
||||
final ProfileRepository profileRepository;
|
||||
bool isCurrentDietStarted = false;
|
||||
bool hasStartedDiet = false;
|
||||
String? userStartedDietName;
|
||||
|
||||
Future<void> getDiet(int id, {int? index}) async {
|
||||
try {
|
||||
@@ -16,17 +23,30 @@ class AboutDietCubit extends Cubit<AboutDietState> {
|
||||
if (index != null) {
|
||||
dietsRepository.emitCommentsStateToFeed(CommentsState(index: index, totalComments: diet.totalComments));
|
||||
}
|
||||
emit(AboutDietState.loaded(diet));
|
||||
emit(AboutDietState.loaded(diet: diet, isStarted: isCurrentDietStarted, hasStartedDiet: hasStartedDiet));
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> getDietWithLoading(int id) async {
|
||||
Future<void> initDietPage(int id) async {
|
||||
try {
|
||||
emit(AboutDietState.loading());
|
||||
final diet = await dietsRepository.getDiet(id);
|
||||
emit(AboutDietState.loaded(diet));
|
||||
final chosenDiets = await profileRepository.getChosenDiets();
|
||||
final lastChosenDiet = chosenDiets.firstOrNull;
|
||||
if (lastChosenDiet != null && ['inited', 'started'].contains(lastChosenDiet.state)) {
|
||||
hasStartedDiet = true;
|
||||
if (diet.id == lastChosenDiet.dietId) {
|
||||
isCurrentDietStarted = true;
|
||||
} else {
|
||||
if (lastChosenDiet.dietId != null) {
|
||||
final chosenDiet = await dietsRepository.getDiet(lastChosenDiet.dietId!);
|
||||
userStartedDietName = chosenDiet.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
emit(AboutDietState.loaded(diet: diet, isStarted: isCurrentDietStarted, hasStartedDiet: hasStartedDiet));
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
}
|
||||
@@ -45,7 +65,7 @@ class AboutDietCubit extends Cubit<AboutDietState> {
|
||||
if (dietIndexInFeed != null) {
|
||||
dietsRepository.emitLikesStateToFeed(LikesState(wasLikedByMe: updatedDiet.wasLikedByMe, totalLikes: updatedDiet.totalLikes, index: dietIndexInFeed));
|
||||
}
|
||||
emit(AboutDietState.loaded(updatedDiet));
|
||||
emit(AboutDietState.loaded(diet: updatedDiet, isStarted: isCurrentDietStarted, hasStartedDiet: hasStartedDiet));
|
||||
} else {
|
||||
final updatedDiet = currentDiet.copyWith(
|
||||
totalLikes: currentDiet.totalLikes + 1,
|
||||
@@ -54,7 +74,7 @@ class AboutDietCubit extends Cubit<AboutDietState> {
|
||||
if (dietIndexInFeed != null) {
|
||||
dietsRepository.emitLikesStateToFeed(LikesState(wasLikedByMe: updatedDiet.wasLikedByMe, totalLikes: updatedDiet.totalLikes, index: dietIndexInFeed));
|
||||
}
|
||||
emit(AboutDietState.loaded(updatedDiet));
|
||||
emit(AboutDietState.loaded(diet: updatedDiet, isStarted: isCurrentDietStarted, hasStartedDiet: hasStartedDiet));
|
||||
}
|
||||
|
||||
EasyDebounce.debounce(
|
||||
@@ -69,7 +89,7 @@ class AboutDietCubit extends Cubit<AboutDietState> {
|
||||
dietsRepository
|
||||
.emitLikesStateToFeed(LikesState(wasLikedByMe: currentDiet.wasLikedByMe, totalLikes: currentDiet.totalLikes, index: dietIndexInFeed));
|
||||
}
|
||||
emit(AboutDietState.loaded(currentDiet));
|
||||
emit(AboutDietState.loaded(diet: currentDiet, isStarted: isCurrentDietStarted, hasStartedDiet: hasStartedDiet));
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -86,7 +106,7 @@ class AboutDietCubit extends Cubit<AboutDietState> {
|
||||
if (dietIndex != null) {
|
||||
dietsRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: updatedDiet.isFavorite, index: dietIndex));
|
||||
}
|
||||
emit(AboutDietState.loaded(updatedDiet));
|
||||
emit(AboutDietState.loaded(diet: updatedDiet, isStarted: isCurrentDietStarted, hasStartedDiet: hasStartedDiet));
|
||||
|
||||
EasyDebounce.debounce(
|
||||
'add_or_remove_diet_to_favorites',
|
||||
@@ -100,7 +120,7 @@ class AboutDietCubit extends Cubit<AboutDietState> {
|
||||
if (dietIndex != null) {
|
||||
dietsRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentDiet.isFavorite, index: dietIndex));
|
||||
}
|
||||
emit(AboutDietState.loaded(currentDiet));
|
||||
emit(AboutDietState.loaded(diet: currentDiet, isStarted: isCurrentDietStarted, hasStartedDiet: hasStartedDiet));
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -117,7 +137,7 @@ class AboutDietCubit extends Cubit<AboutDietState> {
|
||||
if (dietIndex != null) {
|
||||
dietsRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: updatedDiet.isFavorite, index: dietIndex));
|
||||
}
|
||||
emit(AboutDietState.loaded(updatedDiet));
|
||||
emit(AboutDietState.loaded(diet: updatedDiet, isStarted: isCurrentDietStarted, hasStartedDiet: hasStartedDiet));
|
||||
|
||||
EasyDebounce.debounce(
|
||||
'add_or_remove_diet_to_favorites',
|
||||
@@ -131,9 +151,50 @@ class AboutDietCubit extends Cubit<AboutDietState> {
|
||||
if (dietIndex != null) {
|
||||
dietsRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentDiet.isFavorite, index: dietIndex));
|
||||
}
|
||||
emit(AboutDietState.loaded(currentDiet));
|
||||
emit(AboutDietState.loaded(diet: currentDiet, isStarted: isCurrentDietStarted, hasStartedDiet: hasStartedDiet));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> startDiet(DateTime date) async {
|
||||
try {
|
||||
if (state is! DietStateLoaded) return false;
|
||||
|
||||
final id = (state as DietStateLoaded).diet.id;
|
||||
final stringDate = date.format('yyyy-MM-dd');
|
||||
await dietsRepository.startDiet(id, stringDate);
|
||||
return true;
|
||||
} catch (e, st) {
|
||||
log(e.toString(), stackTrace: st);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> cancelDiet() async {
|
||||
try {
|
||||
if (state is! DietStateLoaded) return false;
|
||||
final id = (state as DietStateLoaded).diet.id;
|
||||
await dietsRepository.cancelDiet(id);
|
||||
return true;
|
||||
} on DioException catch (e) {
|
||||
log(e.toString());
|
||||
return false;
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void clearStartedDietsState() {
|
||||
if (state is! DietStateLoaded) return;
|
||||
final diet = (state as DietStateLoaded).diet;
|
||||
emit(AboutDietState.loaded(diet: diet));
|
||||
}
|
||||
|
||||
void emitCurrentStartedDietsState() {
|
||||
if (state is! DietStateLoaded) return;
|
||||
final diet = (state as DietStateLoaded).diet;
|
||||
emit(AboutDietState.loaded(diet: diet, isStarted: true));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,10 @@ class AboutDietState with _$AboutDietState {
|
||||
|
||||
const factory AboutDietState.initial() = DietStateInitial;
|
||||
const factory AboutDietState.loading() = DietStateLoading;
|
||||
const factory AboutDietState.loaded(Diet diet) = DietStateLoaded;
|
||||
const factory AboutDietState.loaded({
|
||||
required Diet diet,
|
||||
@Default(false) bool isStarted,
|
||||
@Default(false) bool hasStartedDiet,
|
||||
}) = DietStateLoaded;
|
||||
const factory AboutDietState.error({required String message}) = DietStateError;
|
||||
}
|
||||
|
||||
@@ -21,7 +21,8 @@ mixin _$AboutDietState {
|
||||
TResult Function() $default, {
|
||||
required TResult Function() initial,
|
||||
required TResult Function() loading,
|
||||
required TResult Function(Diet diet) loaded,
|
||||
required TResult Function(Diet diet, bool isStarted, bool hasStartedDiet)
|
||||
loaded,
|
||||
required TResult Function(String message) error,
|
||||
}) =>
|
||||
throw _privateConstructorUsedError;
|
||||
@@ -30,7 +31,7 @@ mixin _$AboutDietState {
|
||||
TResult? Function()? $default, {
|
||||
TResult? Function()? initial,
|
||||
TResult? Function()? loading,
|
||||
TResult? Function(Diet diet)? loaded,
|
||||
TResult? Function(Diet diet, bool isStarted, bool hasStartedDiet)? loaded,
|
||||
TResult? Function(String message)? error,
|
||||
}) =>
|
||||
throw _privateConstructorUsedError;
|
||||
@@ -39,7 +40,7 @@ mixin _$AboutDietState {
|
||||
TResult Function()? $default, {
|
||||
TResult Function()? initial,
|
||||
TResult Function()? loading,
|
||||
TResult Function(Diet diet)? loaded,
|
||||
TResult Function(Diet diet, bool isStarted, bool hasStartedDiet)? loaded,
|
||||
TResult Function(String message)? error,
|
||||
required TResult orElse(),
|
||||
}) =>
|
||||
@@ -139,7 +140,8 @@ class _$AboutDietStateImpl implements _AboutDietState {
|
||||
TResult Function() $default, {
|
||||
required TResult Function() initial,
|
||||
required TResult Function() loading,
|
||||
required TResult Function(Diet diet) loaded,
|
||||
required TResult Function(Diet diet, bool isStarted, bool hasStartedDiet)
|
||||
loaded,
|
||||
required TResult Function(String message) error,
|
||||
}) {
|
||||
return $default();
|
||||
@@ -151,7 +153,7 @@ class _$AboutDietStateImpl implements _AboutDietState {
|
||||
TResult? Function()? $default, {
|
||||
TResult? Function()? initial,
|
||||
TResult? Function()? loading,
|
||||
TResult? Function(Diet diet)? loaded,
|
||||
TResult? Function(Diet diet, bool isStarted, bool hasStartedDiet)? loaded,
|
||||
TResult? Function(String message)? error,
|
||||
}) {
|
||||
return $default?.call();
|
||||
@@ -163,7 +165,7 @@ class _$AboutDietStateImpl implements _AboutDietState {
|
||||
TResult Function()? $default, {
|
||||
TResult Function()? initial,
|
||||
TResult Function()? loading,
|
||||
TResult Function(Diet diet)? loaded,
|
||||
TResult Function(Diet diet, bool isStarted, bool hasStartedDiet)? loaded,
|
||||
TResult Function(String message)? error,
|
||||
required TResult orElse(),
|
||||
}) {
|
||||
@@ -262,7 +264,8 @@ class _$DietStateInitialImpl implements DietStateInitial {
|
||||
TResult Function() $default, {
|
||||
required TResult Function() initial,
|
||||
required TResult Function() loading,
|
||||
required TResult Function(Diet diet) loaded,
|
||||
required TResult Function(Diet diet, bool isStarted, bool hasStartedDiet)
|
||||
loaded,
|
||||
required TResult Function(String message) error,
|
||||
}) {
|
||||
return initial();
|
||||
@@ -274,7 +277,7 @@ class _$DietStateInitialImpl implements DietStateInitial {
|
||||
TResult? Function()? $default, {
|
||||
TResult? Function()? initial,
|
||||
TResult? Function()? loading,
|
||||
TResult? Function(Diet diet)? loaded,
|
||||
TResult? Function(Diet diet, bool isStarted, bool hasStartedDiet)? loaded,
|
||||
TResult? Function(String message)? error,
|
||||
}) {
|
||||
return initial?.call();
|
||||
@@ -286,7 +289,7 @@ class _$DietStateInitialImpl implements DietStateInitial {
|
||||
TResult Function()? $default, {
|
||||
TResult Function()? initial,
|
||||
TResult Function()? loading,
|
||||
TResult Function(Diet diet)? loaded,
|
||||
TResult Function(Diet diet, bool isStarted, bool hasStartedDiet)? loaded,
|
||||
TResult Function(String message)? error,
|
||||
required TResult orElse(),
|
||||
}) {
|
||||
@@ -385,7 +388,8 @@ class _$DietStateLoadingImpl implements DietStateLoading {
|
||||
TResult Function() $default, {
|
||||
required TResult Function() initial,
|
||||
required TResult Function() loading,
|
||||
required TResult Function(Diet diet) loaded,
|
||||
required TResult Function(Diet diet, bool isStarted, bool hasStartedDiet)
|
||||
loaded,
|
||||
required TResult Function(String message) error,
|
||||
}) {
|
||||
return loading();
|
||||
@@ -397,7 +401,7 @@ class _$DietStateLoadingImpl implements DietStateLoading {
|
||||
TResult? Function()? $default, {
|
||||
TResult? Function()? initial,
|
||||
TResult? Function()? loading,
|
||||
TResult? Function(Diet diet)? loaded,
|
||||
TResult? Function(Diet diet, bool isStarted, bool hasStartedDiet)? loaded,
|
||||
TResult? Function(String message)? error,
|
||||
}) {
|
||||
return loading?.call();
|
||||
@@ -409,7 +413,7 @@ class _$DietStateLoadingImpl implements DietStateLoading {
|
||||
TResult Function()? $default, {
|
||||
TResult Function()? initial,
|
||||
TResult Function()? loading,
|
||||
TResult Function(Diet diet)? loaded,
|
||||
TResult Function(Diet diet, bool isStarted, bool hasStartedDiet)? loaded,
|
||||
TResult Function(String message)? error,
|
||||
required TResult orElse(),
|
||||
}) {
|
||||
@@ -470,7 +474,7 @@ abstract class _$$DietStateLoadedImplCopyWith<$Res> {
|
||||
$Res Function(_$DietStateLoadedImpl) then) =
|
||||
__$$DietStateLoadedImplCopyWithImpl<$Res>;
|
||||
@useResult
|
||||
$Res call({Diet diet});
|
||||
$Res call({Diet diet, bool isStarted, bool hasStartedDiet});
|
||||
|
||||
$DietCopyWith<$Res> get diet;
|
||||
}
|
||||
@@ -489,12 +493,22 @@ class __$$DietStateLoadedImplCopyWithImpl<$Res>
|
||||
@override
|
||||
$Res call({
|
||||
Object? diet = null,
|
||||
Object? isStarted = null,
|
||||
Object? hasStartedDiet = null,
|
||||
}) {
|
||||
return _then(_$DietStateLoadedImpl(
|
||||
null == diet
|
||||
diet: null == diet
|
||||
? _value.diet
|
||||
: diet // ignore: cast_nullable_to_non_nullable
|
||||
as Diet,
|
||||
isStarted: null == isStarted
|
||||
? _value.isStarted
|
||||
: isStarted // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
hasStartedDiet: null == hasStartedDiet
|
||||
? _value.hasStartedDiet
|
||||
: hasStartedDiet // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -512,14 +526,23 @@ class __$$DietStateLoadedImplCopyWithImpl<$Res>
|
||||
/// @nodoc
|
||||
|
||||
class _$DietStateLoadedImpl implements DietStateLoaded {
|
||||
const _$DietStateLoadedImpl(this.diet);
|
||||
const _$DietStateLoadedImpl(
|
||||
{required this.diet,
|
||||
this.isStarted = false,
|
||||
this.hasStartedDiet = false});
|
||||
|
||||
@override
|
||||
final Diet diet;
|
||||
@override
|
||||
@JsonKey()
|
||||
final bool isStarted;
|
||||
@override
|
||||
@JsonKey()
|
||||
final bool hasStartedDiet;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'AboutDietState.loaded(diet: $diet)';
|
||||
return 'AboutDietState.loaded(diet: $diet, isStarted: $isStarted, hasStartedDiet: $hasStartedDiet)';
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -527,11 +550,15 @@ class _$DietStateLoadedImpl implements DietStateLoaded {
|
||||
return identical(this, other) ||
|
||||
(other.runtimeType == runtimeType &&
|
||||
other is _$DietStateLoadedImpl &&
|
||||
(identical(other.diet, diet) || other.diet == diet));
|
||||
(identical(other.diet, diet) || other.diet == diet) &&
|
||||
(identical(other.isStarted, isStarted) ||
|
||||
other.isStarted == isStarted) &&
|
||||
(identical(other.hasStartedDiet, hasStartedDiet) ||
|
||||
other.hasStartedDiet == hasStartedDiet));
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(runtimeType, diet);
|
||||
int get hashCode => Object.hash(runtimeType, diet, isStarted, hasStartedDiet);
|
||||
|
||||
/// Create a copy of AboutDietState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@@ -548,10 +575,11 @@ class _$DietStateLoadedImpl implements DietStateLoaded {
|
||||
TResult Function() $default, {
|
||||
required TResult Function() initial,
|
||||
required TResult Function() loading,
|
||||
required TResult Function(Diet diet) loaded,
|
||||
required TResult Function(Diet diet, bool isStarted, bool hasStartedDiet)
|
||||
loaded,
|
||||
required TResult Function(String message) error,
|
||||
}) {
|
||||
return loaded(diet);
|
||||
return loaded(diet, isStarted, hasStartedDiet);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -560,10 +588,10 @@ class _$DietStateLoadedImpl implements DietStateLoaded {
|
||||
TResult? Function()? $default, {
|
||||
TResult? Function()? initial,
|
||||
TResult? Function()? loading,
|
||||
TResult? Function(Diet diet)? loaded,
|
||||
TResult? Function(Diet diet, bool isStarted, bool hasStartedDiet)? loaded,
|
||||
TResult? Function(String message)? error,
|
||||
}) {
|
||||
return loaded?.call(diet);
|
||||
return loaded?.call(diet, isStarted, hasStartedDiet);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -572,12 +600,12 @@ class _$DietStateLoadedImpl implements DietStateLoaded {
|
||||
TResult Function()? $default, {
|
||||
TResult Function()? initial,
|
||||
TResult Function()? loading,
|
||||
TResult Function(Diet diet)? loaded,
|
||||
TResult Function(Diet diet, bool isStarted, bool hasStartedDiet)? loaded,
|
||||
TResult Function(String message)? error,
|
||||
required TResult orElse(),
|
||||
}) {
|
||||
if (loaded != null) {
|
||||
return loaded(diet);
|
||||
return loaded(diet, isStarted, hasStartedDiet);
|
||||
}
|
||||
return orElse();
|
||||
}
|
||||
@@ -624,9 +652,14 @@ class _$DietStateLoadedImpl implements DietStateLoaded {
|
||||
}
|
||||
|
||||
abstract class DietStateLoaded implements AboutDietState {
|
||||
const factory DietStateLoaded(final Diet diet) = _$DietStateLoadedImpl;
|
||||
const factory DietStateLoaded(
|
||||
{required final Diet diet,
|
||||
final bool isStarted,
|
||||
final bool hasStartedDiet}) = _$DietStateLoadedImpl;
|
||||
|
||||
Diet get diet;
|
||||
bool get isStarted;
|
||||
bool get hasStartedDiet;
|
||||
|
||||
/// Create a copy of AboutDietState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@@ -707,7 +740,8 @@ class _$DietStateErrorImpl implements DietStateError {
|
||||
TResult Function() $default, {
|
||||
required TResult Function() initial,
|
||||
required TResult Function() loading,
|
||||
required TResult Function(Diet diet) loaded,
|
||||
required TResult Function(Diet diet, bool isStarted, bool hasStartedDiet)
|
||||
loaded,
|
||||
required TResult Function(String message) error,
|
||||
}) {
|
||||
return error(message);
|
||||
@@ -719,7 +753,7 @@ class _$DietStateErrorImpl implements DietStateError {
|
||||
TResult? Function()? $default, {
|
||||
TResult? Function()? initial,
|
||||
TResult? Function()? loading,
|
||||
TResult? Function(Diet diet)? loaded,
|
||||
TResult? Function(Diet diet, bool isStarted, bool hasStartedDiet)? loaded,
|
||||
TResult? Function(String message)? error,
|
||||
}) {
|
||||
return error?.call(message);
|
||||
@@ -731,7 +765,7 @@ class _$DietStateErrorImpl implements DietStateError {
|
||||
TResult Function()? $default, {
|
||||
TResult Function()? initial,
|
||||
TResult Function()? loading,
|
||||
TResult Function(Diet diet)? loaded,
|
||||
TResult Function(Diet diet, bool isStarted, bool hasStartedDiet)? loaded,
|
||||
TResult Function(String message)? error,
|
||||
required TResult orElse(),
|
||||
}) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import 'package:eatvio_app/features/diets/presentation/cubit/about_diet_cubit.da
|
||||
import 'package:eatvio_app/features/diets/presentation/cubit/about_diet_state.dart';
|
||||
import 'package:eatvio_app/features/diets/presentation/widgets/author_card.dart';
|
||||
import 'package:eatvio_app/core/widgets/cpfc_container.dart';
|
||||
import 'package:eatvio_app/features/diets/presentation/widgets/cancel_diet_bottom_sheet.dart';
|
||||
import 'package:eatvio_app/features/diets/presentation/widgets/expandable_text.dart';
|
||||
import 'package:eatvio_app/features/diets/presentation/widgets/info_bouble.dart';
|
||||
import 'package:eatvio_app/core/widgets/main_background_bouble.dart';
|
||||
@@ -75,7 +76,7 @@ class _AboutDietScreenState extends State<AboutDietScreen> {
|
||||
Widget build(BuildContext context) {
|
||||
final s = S.of(context);
|
||||
return BlocProvider(
|
||||
create: (context) => dietsCubit..getDietWithLoading(widget.diet.id),
|
||||
create: (context) => dietsCubit..initDietPage(widget.diet.id),
|
||||
child: BlocProvider<CommentsCubit>(
|
||||
create: (BuildContext context) => commentsCubit
|
||||
..getComments(
|
||||
@@ -130,18 +131,37 @@ class _AboutDietScreenState extends State<AboutDietScreen> {
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
backgroundColor: appTheme.greenA700,
|
||||
backgroundColor: dietState.isStarted ? appTheme.transparentRed : appTheme.greenA700,
|
||||
onPressed: () {
|
||||
final cubit = context.read<AboutDietCubit>();
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) => StartDietBottomSheet(
|
||||
aboutDietCubit: cubit,
|
||||
),
|
||||
);
|
||||
|
||||
if (dietState.isStarted) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) => CancelDietBottomSheet(
|
||||
aboutDietCubit: cubit,
|
||||
dietName: dietState.diet.name,
|
||||
),
|
||||
);
|
||||
} else if (dietState.hasStartedDiet) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) => CancelDietBottomSheet(
|
||||
aboutDietCubit: cubit,
|
||||
title: 'Вы собираетесь начать диету ${dietState.diet.name}, завершить диету ${cubit.userStartedDietName}?',
|
||||
),
|
||||
);
|
||||
} else {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) => StartDietBottomSheet(
|
||||
aboutDietCubit: cubit,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
label: Text(
|
||||
s.startDiet,
|
||||
dietState.isStarted ? 'Отменить диету' : s.startDiet,
|
||||
style: TextStyles.textMSemibold.copyWith(color: appTheme.whiteA700),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -5,8 +5,8 @@ import 'package:intl/intl.dart';
|
||||
import 'package:table_calendar/table_calendar.dart';
|
||||
|
||||
class CalendarWidget extends StatefulWidget {
|
||||
const CalendarWidget({super.key});
|
||||
|
||||
const CalendarWidget({super.key, required this.startDate});
|
||||
final List<DateTime> startDate;
|
||||
@override
|
||||
State<CalendarWidget> createState() => _CalendarWidgetState();
|
||||
}
|
||||
@@ -56,6 +56,9 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
_selectedDay = selectedDay;
|
||||
_focusedDay = focusedDay;
|
||||
});
|
||||
if (widget.startDate.isNotEmpty) {
|
||||
widget.startDate[0] = selectedDay;
|
||||
}
|
||||
}
|
||||
},
|
||||
onFormatChanged: (format) {
|
||||
@@ -69,6 +72,7 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
_focusedDay = focusedDay;
|
||||
},
|
||||
calendarStyle: CalendarStyle(
|
||||
outsideDaysVisible: true,
|
||||
weekendTextStyle: TextStyle(
|
||||
color: appTheme.gray,
|
||||
),
|
||||
@@ -90,6 +94,17 @@ class _CalendarWidgetState extends State<CalendarWidget> {
|
||||
color: appTheme.blueA400,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
outsideTextStyle: TextStyle(
|
||||
color: appTheme.gray,
|
||||
),
|
||||
outsideDecoration: BoxDecoration(
|
||||
color: appTheme.dividerColor,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
withinRangeDecoration: BoxDecoration(
|
||||
color: appTheme.blueA400,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
todayTextStyle: TextStyle(
|
||||
color: appTheme.gray,
|
||||
),
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import 'package:eatvio_app/core/utils/toasts.dart';
|
||||
import 'package:eatvio_app/core/widgets/bottom_sheet_handler_bar.dart';
|
||||
import 'package:eatvio_app/features/diets/presentation/cubit/about_diet_cubit.dart';
|
||||
import 'package:eatvio_app/features/diets/presentation/widgets/start_diet_bottom_sheet.dart';
|
||||
import 'package:eatvio_app/core/theme/custom_text_style.dart';
|
||||
import 'package:eatvio_app/core/theme/theme_helper.dart';
|
||||
import 'package:eatvio_app/core/widgets/custom_outlined_button.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class CancelDietBottomSheet extends StatelessWidget {
|
||||
const CancelDietBottomSheet({super.key, required this.aboutDietCubit, this.title, this.dietName});
|
||||
final AboutDietCubit aboutDietCubit;
|
||||
final String? title;
|
||||
final String? dietName;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SizedBox(height: 4),
|
||||
BottomSheetHandlerBar(),
|
||||
SizedBox(height: 12),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 25.0),
|
||||
child: Text(
|
||||
title ?? 'Вы действительно хотите отменить прохождение диеты ${dietName ?? ''}?',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyles.headingXLSemibold.copyWith(
|
||||
color: appTheme.black,
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(left: 16, right: 16, top: 16, bottom: 28),
|
||||
child: CustomOutlinedButton(
|
||||
onPressed: () async {
|
||||
if (title != null) {
|
||||
final result = await aboutDietCubit.cancelDiet();
|
||||
if (result) {
|
||||
Navigator.pop(context);
|
||||
showSuccessToast('предыдущая диета отменена');
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) => StartDietBottomSheet(
|
||||
aboutDietCubit: aboutDietCubit,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
showErrorToast('Не удалось отменить диету');
|
||||
Navigator.pop(context);
|
||||
}
|
||||
} else {
|
||||
final result = await aboutDietCubit.cancelDiet();
|
||||
if (result) {
|
||||
showSuccessToast('Диета отменена');
|
||||
Navigator.pop(context);
|
||||
aboutDietCubit.clearStartedDietsState();
|
||||
} else {
|
||||
showErrorToast('Не удалось отменить диету');
|
||||
Navigator.pop(context);
|
||||
}
|
||||
}
|
||||
},
|
||||
height: 60,
|
||||
text: 'Подтвердить',
|
||||
buttonTextStyle: TextStyles.textLSemibold.copyWith(
|
||||
color: appTheme.whiteA700,
|
||||
),
|
||||
buttonStyle: ButtonStyle(
|
||||
backgroundColor: WidgetStatePropertyAll(appTheme.blueA400),
|
||||
side: WidgetStatePropertyAll(BorderSide.none),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:eatvio_app/core/utils/toasts.dart';
|
||||
import 'package:eatvio_app/core/widgets/bottom_sheet_handler_bar.dart';
|
||||
import 'package:eatvio_app/core/widgets/custom_divider.dart';
|
||||
import 'package:eatvio_app/features/diets/presentation/cubit/about_diet_cubit.dart';
|
||||
@@ -8,9 +9,16 @@ import 'package:eatvio_app/core/theme/theme_helper.dart';
|
||||
import 'package:eatvio_app/core/widgets/custom_outlined_button.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class StartDietBottomSheet extends StatelessWidget {
|
||||
class StartDietBottomSheet extends StatefulWidget {
|
||||
const StartDietBottomSheet({super.key, required this.aboutDietCubit});
|
||||
final AboutDietCubit aboutDietCubit;
|
||||
|
||||
@override
|
||||
State<StartDietBottomSheet> createState() => _StartDietBottomSheetState();
|
||||
}
|
||||
|
||||
class _StartDietBottomSheetState extends State<StartDietBottomSheet> {
|
||||
final startDate = [DateTime.now()];
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final s = S.of(context);
|
||||
@@ -42,11 +50,23 @@ class StartDietBottomSheet extends StatelessWidget {
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||||
child: CustomDivider(),
|
||||
),
|
||||
CalendarWidget(),
|
||||
CalendarWidget(
|
||||
startDate: startDate,
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(left: 16, right: 16, top: 16, bottom: 28),
|
||||
child: CustomOutlinedButton(
|
||||
onPressed: () {},
|
||||
onPressed: () async {
|
||||
final result = await widget.aboutDietCubit.startDiet(startDate[0]);
|
||||
if (result) {
|
||||
showSuccessToast('Диета успешно начата');
|
||||
Navigator.pop(context);
|
||||
widget.aboutDietCubit.emitCurrentStartedDietsState();
|
||||
} else {
|
||||
showErrorToast('Не удалось начать диету');
|
||||
Navigator.pop(context);
|
||||
}
|
||||
},
|
||||
height: 60,
|
||||
text: s.startDiet,
|
||||
buttonTextStyle: TextStyles.textLSemibold.copyWith(
|
||||
|
||||
Reference in New Issue
Block a user