From 97f2703f94f41d91d6aff26e10df188176f2df95 Mon Sep 17 00:00:00 2001 From: jfbriere Date: Fri, 23 Feb 2024 01:40:09 -0500 Subject: [PATCH] Fix the case when the feed as an end (no more data). --- lib/group/_feed.dart | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/group/_feed.dart b/lib/group/_feed.dart index cd495ff1..a1d7ad97 100644 --- a/lib/group/_feed.dart +++ b/lib/group/_feed.dart @@ -57,6 +57,7 @@ class SubscriptionGroupFeedState extends State with Widge late ItemPositionsListener _itemPositionsListener; bool _insertOffset = true; bool _keepFeedOffset = false; + final List _lastData = []; final List _data = []; bool _toScroll = false; Response? _errorResponse; @@ -72,7 +73,9 @@ class SubscriptionGroupFeedState extends State with Widge WidgetsBinding.instance.addObserver(this); _visiblePositionState = VisiblePositionState(); _itemPositionsListener = ItemPositionsListener.create(); - _itemPositionsListener.itemPositions.addListener(() { _checkFetchData(); }); + _itemPositionsListener.itemPositions.addListener(() { + _checkFetchData(); + }); Future.delayed(Duration.zero, () { _checkFetchData(); }); @@ -99,9 +102,11 @@ class SubscriptionGroupFeedState extends State with Widge } Future _checkFetchData() async { - if (_data.isEmpty || (_data.length - _itemPositionsListener.itemPositions.value.first.index) < 20) { + if (_data.isEmpty || (_data.length > _lastData.length && (_data.length - _itemPositionsListener.itemPositions.value.first.index) < 20)) { await _lock.synchronized(() async { - if (_data.isEmpty || (_data.length - _itemPositionsListener.itemPositions.value.first.index) < 20) { + if (_data.isEmpty || (_data.length > _lastData.length && (_data.length - _itemPositionsListener.itemPositions.value.first.index) < 20)) { + _lastData.clear(); + _lastData.addAll(_data); await _listTweets(); } }); @@ -342,12 +347,14 @@ class SubscriptionGroupFeedState extends State with Widge if (positionedChainIdx > -1 && positionedTweetId != null) { positionedTweetIdx = threads[positionedChainIdx].tweets.indexWhere((e) => e.idStr == positionedTweetId); } - if (positionedChainIdx == -1) { + if (positionedChainIdx == -1 && threads.isNotEmpty) { // find the nearest conversation int refId = int.parse(positionedChainId); TweetChain tc = threads.lastWhere((e) { int id = int.parse(e.id); return id > refId; + }, orElse: () { + return threads[threads.length - 1]; }); positionedChainIdx = threads.indexWhere((e) => e.id == tc.id); } @@ -379,7 +386,8 @@ class SubscriptionGroupFeedState extends State with Widge _toScroll = true; } - } catch (e, stackTrace) { + } + catch (e, stackTrace) { if (e is Exception) { log.severe(e.toString()); setState(() { @@ -391,6 +399,13 @@ class SubscriptionGroupFeedState extends State with Widge // probably something to do } } + finally { + if (_isLoading) { + setState(() { + _isLoading = false; + }); + } + } } void _showOverlay(BuildContext context) {