small fixups

This commit is contained in:
Henry Hiles 2026-09-24 20:31:22 -04:00
commit 09904536e7
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
2 changed files with 33 additions and 43 deletions

View file

@ -16,7 +16,7 @@ final class ChatScroll<T>({
static ChatScroll<T> use<T>({
required AsyncValue<IList<T>?> controllerData,
required String Function(T item) id,
required Future<bool> Function() loadOlder,
required Future<void> Function() loadOlder,
required Future<void> Function() onReachedBottom,
}) {
final historyListController = useRef(ListController());
@ -59,35 +59,25 @@ final class ChatScroll<T>({
);
}, [controllerData, anchorIdValue]);
Future<void> loadOlderItems() async {
if (controllerData.isLoading) return;
await loadOlder();
}
useEffect(() {
const loadThreshold = 500.0;
const topThreshold = 500.0;
const bottomThreshold = 50.0;
void checkPosition() {
Future<void> checkPosition() async {
if (!scrollController.hasClients) return;
final position = scrollController.position;
if (position.extentAfter <= loadThreshold) {
loadOlderItems();
}
if (position.extentBefore <= bottomThreshold) {
if (position.extentAfter <= topThreshold) {
await loadOlder();
} else if (position.extentBefore <= bottomThreshold) {
onReachedBottom();
}
}
scrollController.addListener(checkPosition);
WidgetsBinding.instance.addPostFrameCallback((_) {
checkPosition();
});
WidgetsBinding.instance.addPostFrameCallback((_) => checkPosition());
return () {
scrollController.removeListener(checkPosition);