Merge remote-tracking branch 'upstream/main' into issue-851

# Conflicts:
#	lib/features/feed/presentation/widgets/plan_widget.dart
#	lib/features/profile/presentation/pages/profile_screen.dart
This commit is contained in:
misha
2026-11-24 12:24:45 +03:00
90 changed files with 6002 additions and 3070 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:eatvio_app/generated/l10n.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);
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(S.current.error_try_again_later);
}
});
}
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,

View File

@@ -205,24 +205,27 @@ class _ProfileScreenState extends State<ProfileScreen> {
),
),
const SizedBox(width: 12),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 8,
),
decoration: BoxDecoration(
color: const Color(0xFFF6FAFF),
borderRadius: BorderRadius.circular(12),
),
child: Text(
'${state.consultantInfo!.ratingPlace}-й в рейтинге',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: appTheme.blueA400,
if (state.consultantInfo!.ratingPlace != null)
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 8,
),
decoration: BoxDecoration(
color: const Color(0xFFF6FAFF),
borderRadius: BorderRadius.circular(12),
),
child: Text(
S.of(context).rating_place(
state.consultantInfo!.ratingPlace!,
),
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: appTheme.blueA400,
),
),
),
),
],
),
if (state
@@ -331,6 +334,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
}
Scaffold _myProfileScreen(ProfileStateLoaded state) {
final consultantInfo = state.consultantInfo;
return Scaffold(
backgroundColor: Colors.grey.shade200,
appBar: AppBar(
@@ -360,7 +364,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: [
@@ -376,31 +381,30 @@ class _ProfileScreenState extends State<ProfileScreen> {
),
),
const SizedBox(width: 12),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 8,
),
decoration: BoxDecoration(
color: const Color(0xFFF6FAFF),
borderRadius: BorderRadius.circular(12),
),
child: Text(
'${state.consultantInfo!.ratingPlace}-й в рейтинге',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: appTheme.blueA400,
if (consultantInfo.ratingPlace != null)
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 8,
),
decoration: BoxDecoration(
color: const Color(0xFFF6FAFF),
borderRadius: BorderRadius.circular(12),
),
child: Text(
S.of(context).rating_place(
consultantInfo.ratingPlace!,
),
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: appTheme.blueA400,
),
),
),
),
],
),
if (state
.consultantInfo!
.educationModel
.hasMedicalEducation ==
1)
if (consultantInfo.educationModel.hasMedicalEducation == 1)
const Row(
children: [
SizedBox(width: 16),
@@ -451,6 +455,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) {
@@ -490,6 +495,11 @@ class _ProfileScreenState extends State<ProfileScreen> {
plan: state.plansList[index],
index: index,
showPriceOnCover: true,
onLikeTap: (slug) => plansCubit.likePlan(slug),
onToggleFavorite: (slug, isFavorite) => isFavorite
? plansCubit.removePlanFromFavorite(slug)
: plansCubit.addPlanToFavorite(slug),
onPlanChanged: () => plansCubit.getPlans(profileSlug),
);
},
),
@@ -899,10 +909,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) +
@@ -917,47 +928,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,
),
],
),

View File

@@ -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),