From c0da82f1c94316cd9cb43e2d9fd973f10b21aec2 Mon Sep 17 00:00:00 2001 From: Krille Date: Tue, 13 Jun 2023 08:41:49 +0200 Subject: [PATCH] refactor: More reliable request history/future timeline mechanism --- lib/pages/chat/chat.dart | 8 ++----- lib/pages/chat/chat_event_list.dart | 34 ++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/pages/chat/chat.dart b/lib/pages/chat/chat.dart index 37e127b3..2a791461 100644 --- a/lib/pages/chat/chat.dart +++ b/lib/pages/chat/chat.dart @@ -204,6 +204,7 @@ class ChatController extends State { void requestHistory() async { if (!timeline!.canRequestHistory) return; + Logs().v('Requesting history...'); try { await timeline!.requestHistory(historyCount: _loadHistoryCount); } catch (err) { @@ -222,6 +223,7 @@ class ChatController extends State { final timeline = this.timeline; if (timeline == null) return; if (!timeline.canRequestFuture) return; + Logs().v('Requesting future...'); try { final mostRecentEventId = timeline.events.first.eventId; await timeline.requestFuture(historyCount: _loadHistoryCount); @@ -244,12 +246,6 @@ class ChatController extends State { } setReadMarker(); if (!scrollController.hasClients) return; - if (scrollController.position.pixels == - scrollController.position.maxScrollExtent) { - requestHistory(); - } else if (scrollController.position.pixels == 0) { - requestFuture(); - } if (timeline?.allowNewEvent == false || scrollController.position.pixels > 0 && _scrolledUp == false) { setState(() => _scrolledUp = true); diff --git a/lib/pages/chat/chat_event_list.dart b/lib/pages/chat/chat_event_list.dart index 59df801a..d3f25952 100644 --- a/lib/pages/chat/chat_event_list.dart +++ b/lib/pages/chat/chat_event_list.dart @@ -53,11 +53,18 @@ class ChatEventList extends StatelessWidget { ); } if (controller.timeline!.canRequestFuture) { - return Center( - child: IconButton( - onPressed: controller.requestFuture, - icon: const Icon(Icons.refresh_outlined), - ), + return Builder( + builder: (context) { + WidgetsBinding.instance.addPostFrameCallback( + (_) => controller.requestFuture(), + ); + return Center( + child: IconButton( + onPressed: controller.requestFuture, + icon: const Icon(Icons.refresh_outlined), + ), + ); + }, ); } return Column( @@ -77,11 +84,18 @@ class ChatEventList extends StatelessWidget { ); } if (controller.timeline!.canRequestHistory) { - return Center( - child: IconButton( - onPressed: controller.requestHistory, - icon: const Icon(Icons.refresh_outlined), - ), + return Builder( + builder: (context) { + WidgetsBinding.instance.addPostFrameCallback( + (_) => controller.requestHistory(), + ); + return Center( + child: IconButton( + onPressed: controller.requestHistory, + icon: const Icon(Icons.refresh_outlined), + ), + ); + }, ); } return const SizedBox.shrink();