forked from eatvio/eatvio_app
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:
@@ -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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -331,6 +331,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
}
|
||||
|
||||
Scaffold _myProfileScreen(ProfileStateLoaded state) {
|
||||
final consultantInfo = state.consultantInfo;
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.grey.shade200,
|
||||
appBar: AppBar(
|
||||
@@ -360,7 +361,8 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
_header(state),
|
||||
if (state.profile.roles.contains('consultant'))
|
||||
if (state.profile.roles.contains('consultant') &&
|
||||
consultantInfo != null)
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
@@ -386,7 +388,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Text(
|
||||
'${state.consultantInfo!.ratingPlace}-й в рейтинге',
|
||||
'${consultantInfo.ratingPlace}-й в рейтинге',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
@@ -396,11 +398,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
),
|
||||
],
|
||||
),
|
||||
if (state
|
||||
.consultantInfo!
|
||||
.educationModel
|
||||
.hasMedicalEducation ==
|
||||
1)
|
||||
if (consultantInfo.educationModel.hasMedicalEducation == 1)
|
||||
const Row(
|
||||
children: [
|
||||
SizedBox(width: 16),
|
||||
@@ -451,6 +449,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
if (!state.profile.roles.contains('consultant') || hidePlans) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
final profileSlug = state.profile.slug;
|
||||
return BlocBuilder<PlansCubit, PlansState>(
|
||||
bloc: plansCubit,
|
||||
builder: (BuildContext context, PlansState state) {
|
||||
@@ -486,7 +485,15 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
});
|
||||
},
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return PlanWidget(plan: state.plansList[index], index: index);
|
||||
return PlanWidget(
|
||||
plan: state.plansList[index],
|
||||
index: index,
|
||||
onLikeTap: (slug) => plansCubit.likePlan(slug),
|
||||
onToggleFavorite: (slug, isFavorite) => isFavorite
|
||||
? plansCubit.removePlanFromFavorite(slug)
|
||||
: plansCubit.addPlanToFavorite(slug),
|
||||
onPlanChanged: () => plansCubit.getPlans(profileSlug),
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
@@ -895,10 +902,11 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
}
|
||||
|
||||
SingleChildRenderObjectWidget _socialLinks(ProfileStateLoaded state) {
|
||||
if (!state.profile.roles.contains('consultant') &&
|
||||
state.consultantInfo != null) {
|
||||
final consultantInfo = state.consultantInfo;
|
||||
if (!state.profile.roles.contains('consultant') || consultantInfo == null) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
final educationModel = consultantInfo.educationModel;
|
||||
return Padding(
|
||||
padding:
|
||||
const EdgeInsets.symmetric(horizontal: 4) +
|
||||
@@ -913,47 +921,34 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
_socialLinkButton(
|
||||
() {
|
||||
launchUrl(
|
||||
Uri.parse(
|
||||
state.consultantInfo!.educationModel.vkLink ?? '',
|
||||
),
|
||||
Uri.parse(educationModel.vkLink ?? ''),
|
||||
mode: LaunchMode.externalApplication,
|
||||
);
|
||||
},
|
||||
'assets/icons/vk.svg',
|
||||
state.consultantInfo!.educationModel.vkLink!.isEmpty
|
||||
? true
|
||||
: false,
|
||||
educationModel.vkLink?.isEmpty ?? true,
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
_socialLinkButton(
|
||||
() {
|
||||
launchUrl(
|
||||
Uri.parse(
|
||||
state.consultantInfo!.educationModel.tgLink ?? '',
|
||||
),
|
||||
Uri.parse(educationModel.tgLink ?? ''),
|
||||
mode: LaunchMode.externalApplication,
|
||||
);
|
||||
},
|
||||
'assets/icons/telegram.svg',
|
||||
state.consultantInfo!.educationModel.tgLink!.isEmpty
|
||||
? true
|
||||
: false,
|
||||
educationModel.tgLink?.isEmpty ?? true,
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
_socialLinkButton(
|
||||
() {
|
||||
launchUrl(
|
||||
Uri.parse(
|
||||
state.consultantInfo!.educationModel.personalSiteLink ??
|
||||
'',
|
||||
),
|
||||
Uri.parse(educationModel.personalSiteLink ?? ''),
|
||||
mode: LaunchMode.externalApplication,
|
||||
);
|
||||
},
|
||||
'assets/icons/web.svg',
|
||||
state.consultantInfo!.educationModel.personalSiteLink!.isEmpty
|
||||
? true
|
||||
: false,
|
||||
educationModel.personalSiteLink?.isEmpty ?? true,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -191,21 +191,24 @@ class _ProfileBlogPostWidgetState extends State<ProfileBlogPostWidget> {
|
||||
? Colors.red.withValues(alpha: 0.8)
|
||||
: null,
|
||||
onTap: () async {
|
||||
final nextLiked = !isLiked;
|
||||
setState(() {
|
||||
isLiked = !isLiked;
|
||||
isLiked ? totalLikes++ : totalLikes--;
|
||||
isLiked = nextLiked;
|
||||
nextLiked ? totalLikes++ : totalLikes--;
|
||||
});
|
||||
isLiked
|
||||
? await context.read<FeedCubit>().likePost(
|
||||
post.id,
|
||||
)
|
||||
: await context.read<FeedCubit>().unlikedPost(
|
||||
post.id,
|
||||
);
|
||||
widget.profileCubit.updateFeedBlogPostList(
|
||||
post,
|
||||
isLiked ? false : true,
|
||||
nextLiked,
|
||||
);
|
||||
if (nextLiked) {
|
||||
await context.read<FeedCubit>().likePost(
|
||||
post.id,
|
||||
);
|
||||
} else {
|
||||
await context.read<FeedCubit>().unlikedPost(
|
||||
post.id,
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
|
||||
Reference in New Issue
Block a user