forked from eatvio/eatvio_app
changed logic of view requests sending
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:eatvio_app/features/feed/models/feed_blog_post_model.dart';
|
||||
import 'package:eatvio_app/features/feed/models/view_model.dart';
|
||||
import 'package:eatvio_app/features/feed/presentation/bloc/feed_cubit/feed_state.dart';
|
||||
import 'package:eatvio_app/features/feed/presentation/pages/feed_screen.dart';
|
||||
import 'package:eatvio_app/features/feed/repositories/feed_repository.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
@@ -84,9 +82,4 @@ class FeedCubit extends Cubit<FeedState> {
|
||||
Future<void> viewDiet(int id) async => await feedRepository.viewDiet(id);
|
||||
Future<void> viewPlan(String slug) async => await feedRepository.viewPlan(slug);
|
||||
|
||||
Future<void> viewContent({required ViewableContentType type, List<int>? ids, List<String>? slugs}) async {
|
||||
feedRepository.viewContent(
|
||||
viewModel: ViewModel(type: type.getStringifiedVersion(), ids: ids, slugs: slugs),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:easy_debounce/easy_debounce.dart';
|
||||
import 'package:eatvio_app/core/const.dart';
|
||||
import 'package:eatvio_app/core/views/cubit/views_cubit.dart';
|
||||
import 'package:eatvio_app/core/widgets/add_item_button.dart';
|
||||
import 'package:eatvio_app/core/widgets/custom_search_bar.dart';
|
||||
import 'package:eatvio_app/features/create_diet/presentation/create_diet_screen.dart';
|
||||
@@ -62,19 +61,15 @@ class _FeedScreenContentState extends State<FeedScreenContent> {
|
||||
final consultantsController = TextEditingController();
|
||||
|
||||
late final List<String> chipsLabelList;
|
||||
late final AppLifecycleListener _listener;
|
||||
int _selectedChipIndex = 0;
|
||||
final blogPostsScrollController = ScrollController();
|
||||
final recipesScrollController = ScrollController();
|
||||
final dietsScrollController = ScrollController();
|
||||
final plansScrollController = ScrollController();
|
||||
final consultantsScrollController = ScrollController();
|
||||
final _feedCubit = sl<FeedCubit>();
|
||||
@override
|
||||
void initState() {
|
||||
// Initialize chipsLabelList based on hidePlans setting
|
||||
chipsLabelList = hidePlans ? ['Все', 'Рецепты', 'Диеты', 'Консультанты'] : ['Все', 'Рецепты', 'Диеты', 'Планы питания', 'Консультанты'];
|
||||
_listener = AppLifecycleListener(onDetach: _sendViews);
|
||||
consultantsScrollController.addListener(_onScrollConsultants);
|
||||
super.initState();
|
||||
}
|
||||
@@ -83,20 +78,7 @@ class _FeedScreenContentState extends State<FeedScreenContent> {
|
||||
if (consultantsScrollController.offset >= consultantsScrollController.position.maxScrollExtent && !consultantsScrollController.position.outOfRange) {}
|
||||
}
|
||||
|
||||
void _sendViews() {
|
||||
if (postsIds.isNotEmpty) {
|
||||
_feedCubit.viewContent(type: ViewableContentType.posts, ids: postsIds.toList());
|
||||
}
|
||||
if (plansIds.isNotEmpty) {
|
||||
_feedCubit.viewContent(type: ViewableContentType.plans, ids: plansIds.toList());
|
||||
}
|
||||
if (dietsIds.isNotEmpty) {
|
||||
_feedCubit.viewContent(type: ViewableContentType.diets, ids: dietsIds.toList());
|
||||
}
|
||||
if (recipesIds.isNotEmpty) {
|
||||
_feedCubit.viewContent(type: ViewableContentType.recipes, ids: recipesIds.toList());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
@@ -105,63 +87,10 @@ class _FeedScreenContentState extends State<FeedScreenContent> {
|
||||
dietsScrollController.dispose();
|
||||
plansScrollController.dispose();
|
||||
consultantsScrollController.dispose();
|
||||
for (final map in _visibilityTimers.values) {
|
||||
for (final t in map.values) {
|
||||
t.cancel();
|
||||
}
|
||||
map.clear();
|
||||
}
|
||||
_listener.dispose();
|
||||
_sendViews();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
final Set<int> postsIds = <int>{};
|
||||
final Set<int> plansIds = <int>{};
|
||||
final Set<int> dietsIds = <int>{};
|
||||
final Set<int> recipesIds = <int>{};
|
||||
|
||||
final Map<ViewableContentType, Map<int, Timer>> _visibilityTimers = {
|
||||
ViewableContentType.posts: <int, Timer>{},
|
||||
ViewableContentType.plans: <int, Timer>{},
|
||||
ViewableContentType.diets: <int, Timer>{},
|
||||
ViewableContentType.recipes: <int, Timer>{},
|
||||
};
|
||||
|
||||
Set<int> _idsForType(ViewableContentType type) {
|
||||
switch (type) {
|
||||
case ViewableContentType.posts:
|
||||
return postsIds;
|
||||
case ViewableContentType.plans:
|
||||
return plansIds;
|
||||
case ViewableContentType.diets:
|
||||
return dietsIds;
|
||||
case ViewableContentType.recipes:
|
||||
return recipesIds;
|
||||
}
|
||||
}
|
||||
|
||||
void _handleVisibility({required double visibleFraction, required int id, required ViewableContentType type}) {
|
||||
final timers = _visibilityTimers[type]!;
|
||||
final ids = _idsForType(type);
|
||||
|
||||
if (ids.contains(id)) {
|
||||
timers[id]?.cancel();
|
||||
timers.remove(id);
|
||||
return;
|
||||
}
|
||||
|
||||
if (visibleFraction >= 0.5) {
|
||||
if (timers.containsKey(id)) return;
|
||||
timers[id] = Timer(const Duration(seconds: 1), () {
|
||||
ids.add(id);
|
||||
timers.remove(id);
|
||||
});
|
||||
} else {
|
||||
timers[id]?.cancel();
|
||||
timers.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -301,10 +230,10 @@ class _FeedScreenContentState extends State<FeedScreenContent> {
|
||||
];
|
||||
},
|
||||
body: switch (_selectedChipIndex) {
|
||||
0 => BlogPosts(handleVisibility: _handleVisibility),
|
||||
1 => Recipes(handleVisibility: _handleVisibility),
|
||||
2 => Diets(handleVisibility: _handleVisibility),
|
||||
3 when !hidePlans => Plans(handleVisibility: _handleVisibility),
|
||||
0 => BlogPosts(handleVisibility: context.read<ViewsCubit>().handleVisibility),
|
||||
1 => Recipes(handleVisibility: context.read<ViewsCubit>().handleVisibility),
|
||||
2 => Diets(handleVisibility: context.read<ViewsCubit>().handleVisibility),
|
||||
3 when !hidePlans => Plans(handleVisibility: context.read<ViewsCubit>().handleVisibility),
|
||||
3 when hidePlans => const Consultants(),
|
||||
4 when !hidePlans => const Consultants(),
|
||||
_ => Center(child: Text(chipsLabelList[_selectedChipIndex])),
|
||||
|
||||
@@ -42,7 +42,7 @@ class Diets extends StatelessWidget {
|
||||
key: ValueKey(index),
|
||||
onVisibilityChanged: (info) {
|
||||
final id = state.dietsList[index].id;
|
||||
handleVisibility(visibleFraction: info.visibleFraction, id: id, type: ViewableContentType.posts);
|
||||
handleVisibility(visibleFraction: info.visibleFraction, id: id, type: ViewableContentType.diets);
|
||||
},
|
||||
child: DietsWidget(diet: state.dietsList[index], index: index),
|
||||
);
|
||||
|
||||
@@ -42,7 +42,7 @@ class Plans extends StatelessWidget {
|
||||
key: ValueKey(index),
|
||||
onVisibilityChanged: (info) {
|
||||
final id = state.plans[index].id;
|
||||
handleVisibility(visibleFraction: info.visibleFraction, id: id, type: ViewableContentType.posts);
|
||||
handleVisibility(visibleFraction: info.visibleFraction, id: id, type: ViewableContentType.plans);
|
||||
},
|
||||
child: PlanWidget(plan: state.plans[index], index: index),
|
||||
);
|
||||
|
||||
@@ -46,7 +46,7 @@ class Recipes extends StatelessWidget {
|
||||
key: ValueKey(index),
|
||||
onVisibilityChanged: (info) {
|
||||
final id = state.recipesList[index].id;
|
||||
handleVisibility(visibleFraction: info.visibleFraction, id: id, type: ViewableContentType.posts);
|
||||
handleVisibility(visibleFraction: info.visibleFraction, id: id, type: ViewableContentType.recipes);
|
||||
},
|
||||
child: RecipesWidget(
|
||||
recipe: state.recipesList[index],
|
||||
|
||||
Reference in New Issue
Block a user