forked from eatvio/eatvio_app
73 lines
2.3 KiB
Dart
73 lines
2.3 KiB
Dart
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<AboutPlanState> {
|
|
final PlanRepository planRepository;
|
|
AboutPlanCubit({required this.planRepository}) : super(AboutPlanState.initial());
|
|
|
|
Future<void> 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<void> 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<void> 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<void> 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<void> 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()));
|
|
}
|
|
}
|
|
}
|