import 'dart:developer'; 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(String slug) async { try { await planRepository.likePlan(slug); 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 addToFavorite(String slug) async { try { await planRepository.addToFavorite(slug); 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 removeFromFavorite(String slug) async { try { await planRepository.removeFromFavorite(slug); 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())); } } }