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(),
|
||||
}) {
|
||||
|
||||
Reference in New Issue
Block a user