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/plans/presentation/cubit/about_plan_state.dart'; import 'package:eatvio_app/features/plans/repositories/plan_repository.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; class AboutPlanCubit extends Cubit { final PlanRepository planRepository; AboutPlanCubit({required this.planRepository}) : super(AboutPlanState.initial()); Future getPlan(String slug) async { try { final plan = await planRepository.getPlan(slug); emit(state.copyWith(planModel: plan, error: null)); } catch (e, stackTrace) { log(e.toString(), stackTrace: stackTrace); emit(state.copyWith(error: e.toString())); } } Future getPlanWithLoading(String slug) async { try { emit(state.copyWith(isLoading: true)); final plan = await planRepository.getPlan(slug); emit( state.copyWith( planModel: plan, error: null, isLoading: false, ), ); } catch (e, stackTrace) { log(e.toString(), stackTrace: stackTrace); emit(state.copyWith(error: e.toString(), isLoading: false)); } } void choseOption(int index) { emit(state.copyWith(chosenOptionIndex: index)); } Future likePlan({required int? index, required String slug}) async { final currentPlan = state.planModel; if (currentPlan == null) return; if (currentPlan.wasLikedByMe) { emit(state.copyWith(planModel: currentPlan.copyWith(wasLikedByMe: false, totalLikes: currentPlan.totalLikes - 1), error: null)); if (index != null) { planRepository.emitLikesStateToFeed(LikesState(wasLikedByMe: false, totalLikes: currentPlan.totalLikes - 1, index: index)); } } else { emit(state.copyWith(planModel: currentPlan.copyWith(wasLikedByMe: true, totalLikes: currentPlan.totalLikes + 1), error: null)); if (index != null) { planRepository.emitLikesStateToFeed(LikesState(wasLikedByMe: true, totalLikes: currentPlan.totalLikes + 1, index: index)); } } EasyDebounce.debounce( 'like_plan_$slug', Duration(seconds: 1), () async { try { await planRepository.likePlan(slug); } catch (e) { log(e.toString()); if (index != null) { planRepository.emitLikesStateToFeed(LikesState(wasLikedByMe: currentPlan.wasLikedByMe, totalLikes: currentPlan.totalLikes, index: index)); } emit(state.copyWith(planModel: currentPlan, error: null)); } }, ); } Future addToFavorite({required String slug, required int? index}) async { final currentPlan = state.planModel; if (currentPlan == null) return; emit(state.copyWith(planModel: currentPlan.copyWith(isFavorite: true))); if (index != null) { planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: true, index: index)); } EasyDebounce.debounce( 'add_or_remove_plan_to_favorites', Duration(seconds: 1), () async { try { await planRepository.addToFavorite(slug); } catch (e) { log(e.toString()); showErrorToast('Произошла ошибка. Попробуйте ещё раз через некоторое время'); if (index != null) { planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentPlan.isFavorite, index: index)); } emit(state.copyWith(planModel: currentPlan, error: null)); } }, ); } Future removeFromFavorite({required String slug, required int? index}) async { final currentPlan = state.planModel; if (currentPlan == null) return; emit(state.copyWith(planModel: currentPlan.copyWith(isFavorite: false))); if (index != null) { planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: false, index: index)); } EasyDebounce.debounce( 'add_or_remove_plan_to_favorites', Duration(seconds: 1), () async { try { await planRepository.removeFromFavorite(slug); } catch (e) { log(e.toString()); showErrorToast('Произошла ошибка. Попробуйте ещё раз через некоторое время'); if (index != null) { planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentPlan.isFavorite, index: index)); } emit(state.copyWith(planModel: currentPlan, error: null)); } }, ); } }