fix: plans - like and bookmark in consultant profile (#582)

- decouple PlanWidget from feed PlansCubit via owner-provided callbacks
- add like/favorite actions to profile PlansCubit: slug lookup, optimistic
  update, rollback of the affected plan, per-slug debounce, isClosed guards
- pass slug through like/favorite/comments events; guard out-of-range index
  in feed plans handlers and deep-link about_plan sync
- capture post like intent before await in profile blog post widget
- replace null force-unwraps in profile screen, guard indexOf == -1
- tests: profile plan like/bookmark, slug sync, bounds guards, #700 regression
This commit is contained in:
2026-11-20 16:30:13 +03:00
parent 1e8c1ab2eb
commit 0df474b2b5
22 changed files with 2139 additions and 137 deletions

View File

@@ -1,25 +1,197 @@
import 'dart:async';
import 'dart:developer';
import 'package:easy_debounce/easy_debounce.dart';
import 'package:eatvio_app/core/error_reporting/report_from.dart';
import 'package:eatvio_app/core/utils/toasts.dart';
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
import 'package:eatvio_app/features/plans/repositories/plan_repository.dart';
import 'package:eatvio_app/features/plans/repositories/plans_repository.dart';
import 'package:eatvio_app/features/profile/presentation/bloc/plans_cubit/plans_state.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:eatvio_app/core/error_reporting/report_from.dart';
class PlansCubit extends Cubit<PlansState> {
final PlansRepository plansRepository;
final PlanRepository planRepository;
static const Duration _debounceDuration = Duration(milliseconds: 200);
static const String _favoriteErrorMessage = 'Произошла ошибка. Попробуйте ещё раз через некоторое время';
late final StreamSubscription<LikesState> likesStateSubscription;
late final StreamSubscription<FavoriteState> favoriteStateSubscription;
late final StreamSubscription<CommentsState> commentsStateSubscription;
final Set<String> _pendingDebounceTags = {};
PlansCubit({
required this.plansRepository,
}) : super(const PlansState());
required this.planRepository,
}) : super(const PlansState()) {
likesStateSubscription = planRepository.planLikeState.listen(_changePlanLikeState);
favoriteStateSubscription = planRepository.planFavoriteState.listen(_changePlanFavoriteState);
commentsStateSubscription = planRepository.planCommentsState.listen(_changePlanCommentsState);
}
@override
Future<void> close() async {
for (final tag in _pendingDebounceTags) {
EasyDebounce.cancel(tag);
}
_pendingDebounceTags.clear();
await likesStateSubscription.cancel();
await favoriteStateSubscription.cancel();
await commentsStateSubscription.cancel();
await super.close();
}
Future<void> getPlans(String slug) async {
emit(const PlansState.loading());
try {
emit(PlansState.loaded(plansList: await plansRepository.getPlansBySlug(slug)));
final plansList = await plansRepository.getPlansBySlug(slug);
if (!isClosed) emit(PlansState.loaded(plansList: plansList));
} catch (e, st) {
await reportFrom(e, st, this);
log(e.toString());
emit(PlansState.error(errorMessage: e.toString()));
if (!isClosed) emit(PlansState.error(errorMessage: e.toString()));
}
}
Future<void> likePlan(String slug) async {
final plan = _planBySlug(slug);
if (plan == null) return;
final wasLikedByMe = plan.wasLikedByMe;
final totalLikes = plan.totalLikes;
_updatePlanBySlug(
slug,
(plan) => plan.copyWith(
wasLikedByMe: !plan.wasLikedByMe,
totalLikes: plan.wasLikedByMe ? plan.totalLikes - 1 : plan.totalLikes + 1,
),
);
_debounce('profile_like_plan_$slug', () async {
try {
await planRepository.likePlan(slug);
} catch (e, st) {
await reportFrom(e, st, this);
if (isClosed) return;
_updatePlanBySlug(
slug,
(plan) => plan.copyWith(wasLikedByMe: wasLikedByMe, totalLikes: totalLikes),
);
}
});
}
Future<void> addPlanToFavorite(String slug) => _changeFavorite(slug, true);
Future<void> removePlanFromFavorite(String slug) => _changeFavorite(slug, false);
Future<void> _changeFavorite(String slug, bool isFavorite) async {
final plan = _planBySlug(slug);
if (plan == null) return;
final previousIsFavorite = plan.isFavorite;
_updatePlanBySlug(slug, (plan) => plan.copyWith(isFavorite: isFavorite));
_debounce('profile_favorite_$slug', () async {
try {
if (isFavorite) {
await planRepository.addToFavorite(slug);
} else {
await planRepository.removeFromFavorite(slug);
}
} catch (e, st) {
await reportFrom(e, st, this);
if (isClosed) return;
_updatePlanBySlug(slug, (plan) => plan.copyWith(isFavorite: previousIsFavorite));
showErrorToast(_favoriteErrorMessage);
}
});
}
void _changePlanLikeState(LikesState likesState) {
_updatePlan(
slug: likesState.slug,
index: likesState.index,
transform: (plan) => plan.copyWith(
totalLikes: likesState.totalLikes,
wasLikedByMe: likesState.wasLikedByMe,
),
);
}
void _changePlanFavoriteState(FavoriteState favoriteState) {
_updatePlan(
slug: favoriteState.slug,
index: favoriteState.index,
transform: (plan) => plan.copyWith(isFavorite: favoriteState.isFavorite),
);
}
void _changePlanCommentsState(CommentsState commentsState) {
_updatePlan(
slug: commentsState.slug,
index: commentsState.index,
transform: (plan) => plan.copyWith(totalComments: commentsState.totalComments),
);
}
void _updatePlan({
required String? slug,
required int index,
required PlanModel Function(PlanModel plan) transform,
}) {
if (slug != null) {
_updatePlanBySlug(slug, transform);
} else {
_updatePlanByIndex(index, transform);
}
}
void _updatePlanBySlug(String slug, PlanModel Function(PlanModel plan) transform) {
final current = state;
if (current is! PlansStateLoaded || isClosed) return;
final index = current.plansList.indexWhere((plan) => plan.slug == slug);
if (index == -1) return;
_emitPlanAt(current, index, transform);
}
void _updatePlanByIndex(int index, PlanModel Function(PlanModel plan) transform) {
final current = state;
if (current is! PlansStateLoaded || isClosed) return;
if (index < 0 || index >= current.plansList.length) return;
_emitPlanAt(current, index, transform);
}
void _emitPlanAt(PlansStateLoaded current, int index, PlanModel Function(PlanModel plan) transform) {
final plansList = [...current.plansList];
plansList[index] = transform(plansList[index]);
emit(current.copyWith(plansList: plansList));
}
PlanModel? _planBySlug(String slug) {
final current = state;
if (current is! PlansStateLoaded) return null;
for (final plan in current.plansList) {
if (plan.slug == slug) return plan;
}
return null;
}
void _debounce(String tag, Future<void> Function() action) {
_pendingDebounceTags.add(tag);
EasyDebounce.debounce(tag, _debounceDuration, () async {
_pendingDebounceTags.remove(tag);
await action();
});
}
}

View File

@@ -97,6 +97,7 @@ class ProfileCubit extends Cubit<ProfileState> {
void updateFeedBlogPostList(FeedBlogPostModel feedBlogPostModel, bool wasLikedByMe) {
List<FeedBlogPostModel> feedBlogPostList = [...(state as ProfileStateLoaded).blogPosts];
final int index = feedBlogPostList.indexOf(feedBlogPostModel);
if (index == -1) return;
feedBlogPostList[index] = feedBlogPostModel.copyWith(
wasLikedByMe: wasLikedByMe,
totalLikes: wasLikedByMe == false ? feedBlogPostList[index].totalLikes - 1 : feedBlogPostList[index].totalLikes + 1,