Skip to content

Commit

Permalink
Fix the case when the feed as an end (no more data).
Browse files Browse the repository at this point in the history
  • Loading branch information
j-fbriere committed Feb 23, 2024
1 parent ff2ac90 commit 97f2703
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/group/_feed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class SubscriptionGroupFeedState extends State<SubscriptionGroupFeed> with Widge
late ItemPositionsListener _itemPositionsListener;
bool _insertOffset = true;
bool _keepFeedOffset = false;
final List<TweetChain> _lastData = [];
final List<TweetChain> _data = [];
bool _toScroll = false;
Response? _errorResponse;
Expand All @@ -72,7 +73,9 @@ class SubscriptionGroupFeedState extends State<SubscriptionGroupFeed> with Widge
WidgetsBinding.instance.addObserver(this);
_visiblePositionState = VisiblePositionState();
_itemPositionsListener = ItemPositionsListener.create();
_itemPositionsListener.itemPositions.addListener(() { _checkFetchData(); });
_itemPositionsListener.itemPositions.addListener(() {
_checkFetchData();
});
Future.delayed(Duration.zero, () {
_checkFetchData();
});
Expand All @@ -99,9 +102,11 @@ class SubscriptionGroupFeedState extends State<SubscriptionGroupFeed> with Widge
}

Future<void> _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();
}
});
Expand Down Expand Up @@ -342,12 +347,14 @@ class SubscriptionGroupFeedState extends State<SubscriptionGroupFeed> 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);
}
Expand Down Expand Up @@ -379,7 +386,8 @@ class SubscriptionGroupFeedState extends State<SubscriptionGroupFeed> with Widge
_toScroll = true;
}

} catch (e, stackTrace) {
}
catch (e, stackTrace) {
if (e is Exception) {
log.severe(e.toString());
setState(() {
Expand All @@ -391,6 +399,13 @@ class SubscriptionGroupFeedState extends State<SubscriptionGroupFeed> with Widge
// probably something to do
}
}
finally {
if (_isLoading) {
setState(() {
_isLoading = false;
});
}
}
}

void _showOverlay(BuildContext context) {
Expand Down

0 comments on commit 97f2703

Please sign in to comment.