From ec7a2860747c49efb50f4045f1bbc30ff76adaaa Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 27 May 2025 23:36:19 +0200 Subject: [PATCH] pagination wip --- covas_mobile/lib/pages/ListItemMenu.dart | 53 +++++++++++------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/covas_mobile/lib/pages/ListItemMenu.dart b/covas_mobile/lib/pages/ListItemMenu.dart index 9d726c7..ce5e366 100644 --- a/covas_mobile/lib/pages/ListItemMenu.dart +++ b/covas_mobile/lib/pages/ListItemMenu.dart @@ -50,8 +50,8 @@ class _MyHomePageState extends State { final AuthService _authService = AuthService(); late ScrollController _scrollController; int _fetchCount = 0; - bool _last = false; bool _isLoading = false; + bool _lastData = false; Future> postsFuture = getPosts(); List filteredPosts = []; @@ -149,9 +149,12 @@ class _MyHomePageState extends State { } void _incrementFetchCount() { - setState(() { - _fetchCount++; - }); + if (!_lastData) { + setState(() { + _fetchCount++; + }); + _fetchData(); + } } void _scrollListener() { @@ -159,7 +162,8 @@ class _MyHomePageState extends State { _scrollController.position.maxScrollExtent) { _incrementFetchCount(); } - _fetchData(); + + // Scroll to top } Future _fetchData() async { @@ -467,23 +471,15 @@ class _MyHomePageState extends State { print("results fetch : ${body}"); print("fetch count : ${_fetchCount}"); // Update state after getting the response + if (body.isEmpty) { + _lastData = true; + } setState(() { - if ((body.isNotEmpty) || (_fetchCount > 0)) { - if (body.isEmpty) { - _fetchCount--; - _last = true; - } + if (body.isNotEmpty) { // If we have results, map them to Events - if (!_last) { - filteredPosts.addAll( - body - .map((e) => Events.fromJson(e as Map)) - .toList(), - ); - } - } else { - // If no results, clear filteredPosts - filteredPosts.clear(); + filteredPosts.addAll(body + .map((e) => Events.fromJson(e as Map)) + .toList()); } }); } else { @@ -623,7 +619,7 @@ class _MyHomePageState extends State { labelText: 'Search by item', onChanged: (value) { _fetchCount = 0; - _last = false; + _lastData = false; if (value.isNotEmpty) { setState(() { itemName = value; @@ -642,7 +638,7 @@ class _MyHomePageState extends State { }, onClear: () { _fetchCount = 0; - _last = false; + _lastData = false; setState(() { inputItem.clear(); itemName = ''; @@ -655,7 +651,7 @@ class _MyHomePageState extends State { suggestions: suggestionsItem, onSuggestionTap: (suggestion) async { _fetchCount = 0; - _last = false; + _lastData = false; setState(() { itemName = suggestion['name']; inputItem.text = itemName; @@ -672,7 +668,7 @@ class _MyHomePageState extends State { labelText: 'Search by tags', onChanged: (value) { _fetchCount = 0; - _last = false; + _lastData = false; if (value.isNotEmpty) { setState(() { itemTags = value; @@ -691,6 +687,7 @@ class _MyHomePageState extends State { }, onClear: () { _fetchCount = 0; + _lastData = false; setState(() { inputTags.clear(); }); @@ -699,7 +696,7 @@ class _MyHomePageState extends State { suggestions: suggestionsTags, onSuggestionTap: (suggestion) async { _fetchCount = 0; - _last = false; + _lastData = false; setState(() { itemTags = suggestion['name']; @@ -725,7 +722,7 @@ class _MyHomePageState extends State { labelText: 'Search by geographical zone', onChanged: (value) async { _fetchCount = 0; - _last = false; + _lastData = false; if (value.isNotEmpty) { setState(() { @@ -749,7 +746,7 @@ class _MyHomePageState extends State { }, onClear: () async { _fetchCount = 0; - _last = false; + _lastData = false; final prefs = await SharedPreferences.getInstance(); prefs.remove("city_lat"); prefs.remove("city_long"); @@ -766,7 +763,7 @@ class _MyHomePageState extends State { suggestions: suggestionsGeo, onSuggestionTap: (suggestion) async { _fetchCount = 0; - _last = false; + _lastData = false; final latitude = suggestion['geometry']['coordinates'][1]; final longitude = suggestion['geometry']['coordinates'][0];