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

@@ -27,9 +27,9 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
Future<void> getPlan({required String slug, int? index}) async {
try {
final plan = await planRepository.getPlan(slug);
if (index != null) {
planRepository.emitCommentsStateToFeed(CommentsState(index: index, totalComments: plan.totalComments));
}
planRepository.emitCommentsStateToFeed(
CommentsState(index: index ?? 0, totalComments: plan.totalComments, slug: slug),
);
emit(state.copyWith(planModel: plan, error: null));
} catch (e, stackTrace) {
await reportFrom(e, stackTrace, this);
@@ -59,14 +59,20 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
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));
}
planRepository.emitLikesStateToFeed(LikesState(
wasLikedByMe: false,
totalLikes: currentPlan.totalLikes - 1,
index: index ?? 0,
slug: slug,
));
} 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));
}
planRepository.emitLikesStateToFeed(LikesState(
wasLikedByMe: true,
totalLikes: currentPlan.totalLikes + 1,
index: index ?? 0,
slug: slug,
));
}
EasyDebounce.debounce('like_plan_$slug', const Duration(seconds: 1), () async {
@@ -75,9 +81,12 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
} catch (e, st) {
await reportFrom(e, st, this);
log(e.toString());
if (index != null) {
planRepository.emitLikesStateToFeed(LikesState(wasLikedByMe: currentPlan.wasLikedByMe, totalLikes: currentPlan.totalLikes, index: index));
}
planRepository.emitLikesStateToFeed(LikesState(
wasLikedByMe: currentPlan.wasLikedByMe,
totalLikes: currentPlan.totalLikes,
index: index ?? 0,
slug: slug,
));
emit(state.copyWith(planModel: currentPlan, error: null));
}
});
@@ -88,9 +97,9 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
if (currentPlan == null) return;
emit(state.copyWith(planModel: currentPlan.copyWith(isFavorite: true)));
if (index != null) {
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: true, index: index));
}
planRepository.emitFavoriteStateToFeed(
FavoriteState(isFavorite: true, index: index ?? 0, slug: slug),
);
EasyDebounce.debounce('add_or_remove_plan_to_favorites', const Duration(seconds: 1), () async {
try {
await planRepository.addToFavorite(slug);
@@ -98,9 +107,9 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
await reportFrom(e, st, this);
log(e.toString());
showErrorToast('Произошла ошибка. Попробуйте ещё раз через некоторое время');
if (index != null) {
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentPlan.isFavorite, index: index));
}
planRepository.emitFavoriteStateToFeed(
FavoriteState(isFavorite: currentPlan.isFavorite, index: index ?? 0, slug: slug),
);
emit(state.copyWith(planModel: currentPlan, error: null));
}
});
@@ -111,9 +120,9 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
if (currentPlan == null) return;
emit(state.copyWith(planModel: currentPlan.copyWith(isFavorite: false)));
if (index != null) {
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: false, index: index));
}
planRepository.emitFavoriteStateToFeed(
FavoriteState(isFavorite: false, index: index ?? 0, slug: slug),
);
EasyDebounce.debounce('add_or_remove_plan_to_favorites', const Duration(seconds: 1), () async {
try {
await planRepository.removeFromFavorite(slug);
@@ -121,9 +130,9 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
await reportFrom(e, st, this);
log(e.toString());
showErrorToast('Произошла ошибка. Попробуйте ещё раз через некоторое время');
if (index != null) {
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentPlan.isFavorite, index: index));
}
planRepository.emitFavoriteStateToFeed(
FavoriteState(isFavorite: currentPlan.isFavorite, index: index ?? 0, slug: slug),
);
emit(state.copyWith(planModel: currentPlan, error: null));
}
});

View File

@@ -12,9 +12,9 @@ class PlanRepository {
final _planLikeStateStreamController = StreamController<LikesState>.broadcast();
final _planFavoriteStateStreamController = StreamController<FavoriteState>.broadcast();
final _planCommentsStateStreamController = StreamController<CommentsState>.broadcast();
Stream get planLikeState => _planLikeStateStreamController.stream;
Stream get planFavoriteState => _planFavoriteStateStreamController.stream;
Stream get planCommentsState => _planCommentsStateStreamController.stream;
Stream<LikesState> get planLikeState => _planLikeStateStreamController.stream;
Stream<FavoriteState> get planFavoriteState => _planFavoriteStateStreamController.stream;
Stream<CommentsState> get planCommentsState => _planCommentsStateStreamController.stream;
Future<PlanModel> getPlan(String slug) async {
return planService.getPlan(slug);