plans filters ready

This commit is contained in:
Username1548
2024-12-16 20:31:23 +07:00
parent 067b10ed7f
commit 27241aebac
45 changed files with 1806 additions and 657 deletions

View File

@@ -1,5 +1,8 @@
import 'dart:developer';
import 'package:easy_debounce/easy_debounce.dart';
import 'package:eatvio_app/core/utils/toasts.dart';
import 'package:eatvio_app/features/diets/repositories/diet_repository.dart';
import 'package:eatvio_app/features/plans/presentation/cubit/about_plan_state.dart';
import 'package:eatvio_app/features/plans/repositories/plan_repository.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -22,11 +25,13 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
try {
emit(state.copyWith(isLoading: true));
final plan = await planRepository.getPlan(slug);
emit(state.copyWith(
planModel: plan,
error: null,
isLoading: false,
),);
emit(
state.copyWith(
planModel: plan,
error: null,
isLoading: false,
),
);
} catch (e, stackTrace) {
log(e.toString(), stackTrace: stackTrace);
emit(state.copyWith(error: e.toString(), isLoading: false));
@@ -37,36 +42,73 @@ class AboutPlanCubit extends Cubit<AboutPlanState> {
emit(state.copyWith(chosenOptionIndex: index));
}
Future<void> likePlan(String slug) async {
try {
await planRepository.likePlan(slug);
final plan = await planRepository.getPlan(slug);
emit(state.copyWith(planModel: plan, error: null));
} catch (e, stackTrace) {
log(e.toString(), stackTrace: stackTrace);
emit(state.copyWith(error: e.toString()));
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));
} 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));
}
EasyDebounce.debounce(
'like_plan_$slug',
Duration(seconds: 1),
() async {
try {
await planRepository.likePlan(slug);
} catch (e) {
log(e.toString());
planRepository.emitLikesStateToFeed(LikesState(wasLikedByMe: currentPlan.wasLikedByMe, totalLikes: currentPlan.totalLikes, index: index));
emit(state.copyWith(planModel: currentPlan, error: null));
}
},
);
}
Future<void> addToFavorite(String slug) async {
try {
await planRepository.addToFavorite(slug);
final plan = await planRepository.getPlan(slug);
emit(state.copyWith(planModel: plan, error: null));
} catch (e, stackTrace) {
log(e.toString(), stackTrace: stackTrace);
emit(state.copyWith(error: e.toString()));
}
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));
EasyDebounce.debounce(
'add_or_remove_plan_to_favorites',
Duration(seconds: 1),
() async {
try {
await planRepository.addToFavorite(slug);
} catch (e) {
log(e.toString());
showErrorToast('Произошла ошибка. Попробуйте ещё раз через некоторое время');
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentPlan.isFavorite, index: index));
emit(state.copyWith(planModel: currentPlan, error: null));
}
},
);
}
Future<void> removeFromFavorite(String slug) async {
try {
await planRepository.removeFromFavorite(slug);
final plan = await planRepository.getPlan(slug);
emit(state.copyWith(planModel: plan, error: null));
} catch (e, stackTrace) {
log(e.toString(), stackTrace: stackTrace);
emit(state.copyWith(error: e.toString()));
}
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));
EasyDebounce.debounce(
'add_or_remove_plan_to_favorites',
Duration(seconds: 1),
() async {
try {
await planRepository.removeFromFavorite(slug);
} catch (e) {
log(e.toString());
showErrorToast('Произошла ошибка. Попробуйте ещё раз через некоторое время');
planRepository.emitFavoriteStateToFeed(FavoriteState(isFavorite: currentPlan.isFavorite, index: currentPlan.id));
emit(state.copyWith(planModel: currentPlan, error: null));
}
},
);
}
}

View File

@@ -31,9 +31,10 @@ import 'package:flutter_svg/svg.dart';
import '../widgets/option_widget.dart';
class AboutPlanScreen extends StatefulWidget {
AboutPlanScreen({super.key, required this.plan, required this.navigateToComments});
AboutPlanScreen({super.key, required this.plan, required this.navigateToComments, required this.index});
final PlanModel plan;
final bool navigateToComments;
final int index;
@override
State<AboutPlanScreen> createState() => _AboutPlanScreenState();
}
@@ -90,14 +91,14 @@ class _AboutPlanScreenState extends State<AboutPlanScreen> {
}
},
builder: (BuildContext context, CommentsState commentsState) {
Future.delayed(Duration(milliseconds: 500), () {
if (widget.navigateToComments && !_jumped) {
_scrollToBottom();
}
});
return BlocBuilder<AboutPlanCubit, AboutPlanState>(
builder: (context, state) {
if (state.isLoading) return Scaffold(body: Loader());
WidgetsBinding.instance.addPostFrameCallback((_) {
if (widget.navigateToComments && !_jumped) {
_scrollToBottom();
}
});
return Scaffold(
backgroundColor: appTheme.gray100,
resizeToAvoidBottomInset: true,
@@ -152,9 +153,9 @@ class _AboutPlanScreenState extends State<AboutPlanScreen> {
final planModel = state.planModel;
if (planModel == null) return;
if (planModel.isFavorite) {
context.read<AboutPlanCubit>().removeFromFavorite(planModel.slug);
context.read<AboutPlanCubit>().removeFromFavorite(slug: planModel.slug, index: widget.index);
} else {
context.read<AboutPlanCubit>().addToFavorite(planModel.slug);
context.read<AboutPlanCubit>().addToFavorite(slug: planModel.slug, index: widget.index);
}
},
width: 52,
@@ -352,7 +353,7 @@ class _AboutPlanScreenState extends State<AboutPlanScreen> {
SizedBox(width: 12),
GestureDetector(
onTap: () {
context.read<AboutPlanCubit>().likePlan(widget.plan.slug);
context.read<AboutPlanCubit>().likePlan(slug: widget.plan.slug, index: widget.index);
},
child: InfoBouble(
children: [