forked from eatvio/eatvio_app
153 lines
5.6 KiB
Dart
153 lines
5.6 KiB
Dart
import 'dart:async';
|
|
import 'dart:developer';
|
|
import 'package:easy_debounce/easy_debounce.dart';
|
|
import 'package:eatvio_app/core/utils/toasts.dart';
|
|
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
|
|
import 'package:eatvio_app/features/recipe/repositories/about_recipe_repository.dart';
|
|
import 'package:eatvio_app/features/profile/repositories/profile_repository.dart';
|
|
import 'package:eatvio_app/features/recipe/presentation/cubit/about_recipe_state.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
class AboutRecipeCubit extends Cubit<AboutRecipeState> {
|
|
AboutRecipeCubit({required this.recipeRepository, required this.profileRepository}) : super(AboutRecipeState.initial());
|
|
final AboutRecipeRepository recipeRepository;
|
|
final ProfileRepository profileRepository;
|
|
bool isCurrentDietStarted = false;
|
|
bool hasStartedDiet = false;
|
|
String? userStartedDietName;
|
|
|
|
Future<void> getRecipe(String id, {int? index}) async {
|
|
try {
|
|
final recipe = await recipeRepository.getRecipe(id);
|
|
if (index != null) {
|
|
recipeRepository.emitCommentsStateToFeed(CommentsState(index: index, totalComments: recipe.commentsCount));
|
|
}
|
|
emit(AboutRecipeState.loaded(recipe: recipe));
|
|
} catch (e) {
|
|
log(e.toString());
|
|
emit(AboutRecipeState.error(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
Future<void> initRecipe(String id, {int? index}) async {
|
|
try {
|
|
emit(AboutRecipeState.loading());
|
|
final recipe = await recipeRepository.getRecipe(id);
|
|
if (index != null) {
|
|
recipeRepository.emitCommentsStateToFeed(CommentsState(index: index, totalComments: recipe.commentsCount));
|
|
}
|
|
emit(AboutRecipeState.loaded(recipe: recipe));
|
|
} catch (e) {
|
|
log(e.toString());
|
|
emit(AboutRecipeState.error(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
Future<void> likeRecipe(String id, int? recipeIndexInFeed) async {
|
|
if (state is! AboutRecipeStateLoaded) return;
|
|
|
|
final currentRecipe = (state as AboutRecipeStateLoaded).recipe;
|
|
|
|
if (currentRecipe.wasLikedByMe) {
|
|
final updatedRecipe = currentRecipe.copyWith(
|
|
likesCount: currentRecipe.likesCount - 1,
|
|
wasLikedByMe: false,
|
|
);
|
|
if (recipeIndexInFeed != null) {
|
|
recipeRepository
|
|
.emitLikesStateToFeed(LikesState(wasLikedByMe: updatedRecipe.wasLikedByMe, totalLikes: updatedRecipe.likesCount, index: recipeIndexInFeed));
|
|
}
|
|
emit(AboutRecipeState.loaded(recipe: updatedRecipe));
|
|
} else {
|
|
final updatedRecipe = currentRecipe.copyWith(
|
|
likesCount: currentRecipe.likesCount + 1,
|
|
wasLikedByMe: true,
|
|
);
|
|
if (recipeIndexInFeed != null) {
|
|
recipeRepository
|
|
.emitLikesStateToFeed(LikesState(wasLikedByMe: updatedRecipe.wasLikedByMe, totalLikes: updatedRecipe.likesCount, index: recipeIndexInFeed));
|
|
}
|
|
emit(AboutRecipeState.loaded(recipe: updatedRecipe));
|
|
}
|
|
|
|
EasyDebounce.debounce(
|
|
'like_recipe',
|
|
Duration(seconds: 1),
|
|
() async {
|
|
try {
|
|
await recipeRepository.likeRecipe(id);
|
|
} catch (e) {
|
|
log(e.toString());
|
|
if (recipeIndexInFeed != null) {
|
|
recipeRepository
|
|
.emitLikesStateToFeed(LikesState(wasLikedByMe: currentRecipe.wasLikedByMe, totalLikes: currentRecipe.likesCount, index: recipeIndexInFeed));
|
|
}
|
|
emit(AboutRecipeState.loaded(recipe: currentRecipe));
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<void> addToFavorite(String id, int? recipeIndex) async {
|
|
if (state is! AboutRecipeStateLoaded) return;
|
|
|
|
final currentRecipe = (state as AboutRecipeStateLoaded).recipe;
|
|
|
|
final updatedRecipe = currentRecipe.copyWith(
|
|
isFavorite: true,
|
|
);
|
|
if (recipeIndex != null) {
|
|
recipeRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: updatedRecipe.isFavorite, index: recipeIndex));
|
|
}
|
|
emit(AboutRecipeState.loaded(recipe: updatedRecipe));
|
|
|
|
EasyDebounce.debounce(
|
|
'add_or_remove_diet_to_favorites',
|
|
Duration(seconds: 1),
|
|
() async {
|
|
try {
|
|
await recipeRepository.addToFavorite(id);
|
|
} catch (e) {
|
|
log(e.toString());
|
|
showErrorToast('Произошла ошибка. Попробуйте ещё раз через некоторое время');
|
|
if (recipeIndex != null) {
|
|
recipeRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentRecipe.isFavorite, index: recipeIndex));
|
|
}
|
|
emit(AboutRecipeState.loaded(recipe: currentRecipe));
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<void> removeFromFavorite(String id, int? recipeIndex) async {
|
|
if (state is! AboutRecipeStateLoaded) return;
|
|
|
|
final currentRecipe = (state as AboutRecipeStateLoaded).recipe;
|
|
|
|
final updatedRecipe = currentRecipe.copyWith(
|
|
isFavorite: false,
|
|
);
|
|
if (recipeIndex != null) {
|
|
recipeRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: updatedRecipe.isFavorite, index: recipeIndex));
|
|
}
|
|
emit(AboutRecipeState.loaded(recipe: updatedRecipe));
|
|
|
|
EasyDebounce.debounce(
|
|
'add_or_remove_diet_to_favorites',
|
|
Duration(seconds: 1),
|
|
() async {
|
|
try {
|
|
await recipeRepository.removeFromFavorite(id);
|
|
} catch (e) {
|
|
log(e.toString());
|
|
showErrorToast('Произошла ошибка. Попробуйте ещё раз через некоторое время');
|
|
if (recipeIndex != null) {
|
|
recipeRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentRecipe.isFavorite, index: recipeIndex));
|
|
}
|
|
emit(AboutRecipeState.loaded(recipe: currentRecipe));
|
|
}
|
|
},
|
|
);
|
|
}
|
|
}
|