forked from eatvio/eatvio_app
my plans and my diets
This commit is contained in:
@@ -42,15 +42,19 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
|
||||
emit(state.copyWith(chosenOptionIndex: index));
|
||||
}
|
||||
|
||||
Future<void> likePlan({required int index, required String slug}) async {
|
||||
Future<void> 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));
|
||||
planRepository.emitLikesStateToFeed(LikesState(wasLikedByMe: false, totalLikes: currentPlan.totalLikes - 1, index: index));
|
||||
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));
|
||||
planRepository.emitLikesStateToFeed(LikesState(wasLikedByMe: true, totalLikes: currentPlan.totalLikes + 1, index: index));
|
||||
if (index != null) {
|
||||
planRepository.emitLikesStateToFeed(LikesState(wasLikedByMe: true, totalLikes: currentPlan.totalLikes + 1, index: index));
|
||||
}
|
||||
}
|
||||
|
||||
EasyDebounce.debounce(
|
||||
@@ -61,19 +65,23 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
|
||||
await planRepository.likePlan(slug);
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
planRepository.emitLikesStateToFeed(LikesState(wasLikedByMe: currentPlan.wasLikedByMe, totalLikes: currentPlan.totalLikes, index: index));
|
||||
if (index != null) {
|
||||
planRepository.emitLikesStateToFeed(LikesState(wasLikedByMe: currentPlan.wasLikedByMe, totalLikes: currentPlan.totalLikes, index: index));
|
||||
}
|
||||
emit(state.copyWith(planModel: currentPlan, error: null));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> addToFavorite({required String slug, required int index}) async {
|
||||
Future<void> addToFavorite({required String slug, required int? index}) async {
|
||||
final currentPlan = state.planModel;
|
||||
if (currentPlan == null) return;
|
||||
|
||||
emit(state.copyWith(planModel: currentPlan.copyWith(isFavorite: true)));
|
||||
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: true, index: index));
|
||||
if (index != null) {
|
||||
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: true, index: index));
|
||||
}
|
||||
EasyDebounce.debounce(
|
||||
'add_or_remove_plan_to_favorites',
|
||||
Duration(seconds: 1),
|
||||
@@ -83,19 +91,23 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
showErrorToast('Произошла ошибка. Попробуйте ещё раз через некоторое время');
|
||||
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentPlan.isFavorite, index: index));
|
||||
if (index != null) {
|
||||
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentPlan.isFavorite, index: index));
|
||||
}
|
||||
emit(state.copyWith(planModel: currentPlan, error: null));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> removeFromFavorite({required String slug, required int index}) async {
|
||||
Future<void> removeFromFavorite({required String slug, required int? index}) async {
|
||||
final currentPlan = state.planModel;
|
||||
if (currentPlan == null) return;
|
||||
|
||||
emit(state.copyWith(planModel: currentPlan.copyWith(isFavorite: false)));
|
||||
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: false, index: index));
|
||||
if (index != null) {
|
||||
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: false, index: index));
|
||||
}
|
||||
EasyDebounce.debounce(
|
||||
'add_or_remove_plan_to_favorites',
|
||||
Duration(seconds: 1),
|
||||
@@ -105,7 +117,9 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
showErrorToast('Произошла ошибка. Попробуйте ещё раз через некоторое время');
|
||||
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentPlan.isFavorite, index: currentPlan.id));
|
||||
if (index != null) {
|
||||
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentPlan.isFavorite, index: index));
|
||||
}
|
||||
emit(state.copyWith(planModel: currentPlan, error: null));
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user