|
|
|
|
@ -1,3 +1,5 @@
|
|
|
|
|
import "dart:async";
|
|
|
|
|
|
|
|
|
|
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
|
|
|
|
import "package:flutter_hooks/flutter_hooks.dart";
|
|
|
|
|
import "package:hooks_riverpod/hooks_riverpod.dart";
|
|
|
|
|
@ -10,6 +12,7 @@ final class ChatScroll({
|
|
|
|
|
required final IList<Event> historyItems,
|
|
|
|
|
required final IList<Event> liveItems,
|
|
|
|
|
required final GlobalKey centerKey,
|
|
|
|
|
required final GlobalKey anchorItemKey,
|
|
|
|
|
required final ListController historyListController,
|
|
|
|
|
required final ListController liveListController,
|
|
|
|
|
required final ScrollController scrollController,
|
|
|
|
|
@ -19,30 +22,85 @@ final class ChatScroll({
|
|
|
|
|
required AsyncValue<RoomChat?> controllerData,
|
|
|
|
|
required Future<void> Function() loadOlder,
|
|
|
|
|
required Future<void> Function() markRead,
|
|
|
|
|
required String? contextualEvent,
|
|
|
|
|
required void Function(String id) setContextualEvent,
|
|
|
|
|
}) {
|
|
|
|
|
final historyListController = useRef(ListController());
|
|
|
|
|
final liveListController = useRef(ListController());
|
|
|
|
|
final anchorId = useState<String?>(null);
|
|
|
|
|
|
|
|
|
|
final anchorItemKey = useMemoized(GlobalKey.new, [anchorId.value]);
|
|
|
|
|
|
|
|
|
|
final historyListController = useMemoized(ListController.new, [
|
|
|
|
|
anchorId.value,
|
|
|
|
|
]);
|
|
|
|
|
final liveListController = useMemoized(ListController.new, [
|
|
|
|
|
anchorId.value,
|
|
|
|
|
]);
|
|
|
|
|
final scrollController = useScrollController();
|
|
|
|
|
final centerKey = useMemoized(GlobalKey.new);
|
|
|
|
|
|
|
|
|
|
final anchorId = useState<String?>(null);
|
|
|
|
|
|
|
|
|
|
final anchorIdValue = anchorId.value;
|
|
|
|
|
final pendingAnchorTarget = useState<String?>(null);
|
|
|
|
|
final anchorMountedCompleter = useRef<Completer<BuildContext>?>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() {
|
|
|
|
|
if (anchorId.value == null) {
|
|
|
|
|
if (controllerData case AsyncData(:final value?)
|
|
|
|
|
when value.timeline.isNotEmpty) {
|
|
|
|
|
anchorId.value = value.timeline.last.eventId;
|
|
|
|
|
final hasContextualEvent = value.timeline.any(
|
|
|
|
|
(event) => event.eventId == contextualEvent,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
anchorId.value = hasContextualEvent
|
|
|
|
|
? contextualEvent
|
|
|
|
|
: value.timeline.last.eventId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}, [controllerData]);
|
|
|
|
|
}, [controllerData, contextualEvent]);
|
|
|
|
|
|
|
|
|
|
useEffect(() {
|
|
|
|
|
final target = pendingAnchorTarget.value;
|
|
|
|
|
if (target == null) return null;
|
|
|
|
|
|
|
|
|
|
final found =
|
|
|
|
|
controllerData.value?.timeline.any(
|
|
|
|
|
(event) => event.eventId == target,
|
|
|
|
|
) ??
|
|
|
|
|
false;
|
|
|
|
|
|
|
|
|
|
if (found || controllerData is AsyncError) {
|
|
|
|
|
if (found) {
|
|
|
|
|
anchorId.value = target;
|
|
|
|
|
} else {
|
|
|
|
|
anchorMountedCompleter.value?.completeError(
|
|
|
|
|
StateError("Failed to load context for $target"),
|
|
|
|
|
);
|
|
|
|
|
anchorMountedCompleter.value = null;
|
|
|
|
|
}
|
|
|
|
|
pendingAnchorTarget.value = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}, [controllerData, pendingAnchorTarget.value]);
|
|
|
|
|
|
|
|
|
|
useEffect(() {
|
|
|
|
|
final completer = anchorMountedCompleter.value;
|
|
|
|
|
if (completer == null) return null;
|
|
|
|
|
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
final context = anchorItemKey.currentContext;
|
|
|
|
|
if (context != null && context.mounted) {
|
|
|
|
|
anchorMountedCompleter.value?.complete(context);
|
|
|
|
|
anchorMountedCompleter.value = null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}, [anchorId.value]);
|
|
|
|
|
|
|
|
|
|
final ({IList<Event> history, IList<Event> live}) split = useMemoized(() {
|
|
|
|
|
final items = controllerData.value?.timeline;
|
|
|
|
|
final anchor = anchorIdValue;
|
|
|
|
|
final anchor = anchorId.value;
|
|
|
|
|
|
|
|
|
|
if (items == null || anchor == null) {
|
|
|
|
|
return (history: const .empty(), live: const .empty());
|
|
|
|
|
@ -58,62 +116,58 @@ final class ChatScroll({
|
|
|
|
|
history: items.take(anchorIndex).toIList().reversed.toIList(),
|
|
|
|
|
live: items.skip(anchorIndex).toIList(),
|
|
|
|
|
);
|
|
|
|
|
}, [controllerData, anchorIdValue]);
|
|
|
|
|
}, [controllerData, anchorId.value]);
|
|
|
|
|
|
|
|
|
|
useEffect(() {
|
|
|
|
|
const topThreshold = 500.0;
|
|
|
|
|
const bottomThreshold = 50.0;
|
|
|
|
|
useEffect(
|
|
|
|
|
() {
|
|
|
|
|
const topThreshold = 500.0;
|
|
|
|
|
const bottomThreshold = 50.0;
|
|
|
|
|
|
|
|
|
|
Future<void> checkPosition() async {
|
|
|
|
|
if (!scrollController.hasClients) return;
|
|
|
|
|
Future<void> checkPosition() async {
|
|
|
|
|
if (!scrollController.hasClients) return;
|
|
|
|
|
if (contextualEvent != null) return;
|
|
|
|
|
|
|
|
|
|
final position = scrollController.position;
|
|
|
|
|
final position = scrollController.position;
|
|
|
|
|
|
|
|
|
|
if (position.extentAfter <= topThreshold) {
|
|
|
|
|
await loadOlder();
|
|
|
|
|
} else if (position.extentBefore <= bottomThreshold) {
|
|
|
|
|
await markRead();
|
|
|
|
|
if (position.extentAfter <= topThreshold) {
|
|
|
|
|
await loadOlder();
|
|
|
|
|
} else if (position.extentBefore <= bottomThreshold) {
|
|
|
|
|
await markRead();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scrollController.addListener(checkPosition);
|
|
|
|
|
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => checkPosition());
|
|
|
|
|
|
|
|
|
|
return () => scrollController.removeListener(checkPosition);
|
|
|
|
|
},
|
|
|
|
|
[scrollController, controllerData, loadOlder, markRead, contextualEvent],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
double? resolveOffset(String itemId) {
|
|
|
|
|
final historyIndex = split.history.indexWhere(
|
|
|
|
|
(item) => item.eventId == itemId,
|
|
|
|
|
);
|
|
|
|
|
if (historyIndex != -1) {
|
|
|
|
|
// TODO: Replace SuperSliverView because of the bug that requires this: #94
|
|
|
|
|
// ignore: invalid_use_of_visible_for_testing_member
|
|
|
|
|
return historyListController.getOffsetToReveal(historyIndex, 0.5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scrollController.addListener(checkPosition);
|
|
|
|
|
final liveIndex = split.live.indexWhere((item) => item.eventId == itemId);
|
|
|
|
|
if (liveIndex != -1) {
|
|
|
|
|
// ignore: invalid_use_of_visible_for_testing_member
|
|
|
|
|
return liveListController.getOffsetToReveal(liveIndex, 0.5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => checkPosition());
|
|
|
|
|
|
|
|
|
|
return () {
|
|
|
|
|
scrollController.removeListener(checkPosition);
|
|
|
|
|
};
|
|
|
|
|
}, [scrollController, controllerData, loadOlder, markRead]);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> jumpToId(String itemId) async {
|
|
|
|
|
if (!scrollController.hasClients) return;
|
|
|
|
|
|
|
|
|
|
final historyIndex = split.history.indexWhere(
|
|
|
|
|
(item) => item.eventId == itemId,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
double? offset;
|
|
|
|
|
if (historyIndex != -1) {
|
|
|
|
|
// TODO: Replace SuperSliverView because of the bug that requires this: #94
|
|
|
|
|
// ignore: invalid_use_of_visible_for_testing_member
|
|
|
|
|
offset = historyListController.value.getOffsetToReveal(
|
|
|
|
|
historyIndex,
|
|
|
|
|
0.5,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
final liveIndex = split.live.indexWhere(
|
|
|
|
|
(item) => item.eventId == itemId,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (liveIndex != -1) {
|
|
|
|
|
// ignore: invalid_use_of_visible_for_testing_member
|
|
|
|
|
offset = liveListController.value.getOffsetToReveal(liveIndex, 0.5);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (offset == null) {
|
|
|
|
|
// not in current timeline
|
|
|
|
|
}
|
|
|
|
|
final offset = resolveOffset(itemId);
|
|
|
|
|
|
|
|
|
|
if (offset != null) {
|
|
|
|
|
await scrollController.animateTo(
|
|
|
|
|
@ -121,6 +175,23 @@ final class ChatScroll({
|
|
|
|
|
duration: const Duration(milliseconds: 700),
|
|
|
|
|
curve: Curves.easeInOut,
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final completer = Completer<BuildContext>();
|
|
|
|
|
anchorMountedCompleter.value = completer;
|
|
|
|
|
pendingAnchorTarget.value = itemId;
|
|
|
|
|
setContextualEvent(itemId);
|
|
|
|
|
|
|
|
|
|
final context = await completer.future;
|
|
|
|
|
|
|
|
|
|
if (context.mounted) {
|
|
|
|
|
await Scrollable.ensureVisible(
|
|
|
|
|
context,
|
|
|
|
|
alignment: 0.5,
|
|
|
|
|
duration: const Duration(milliseconds: 700),
|
|
|
|
|
curve: Curves.easeInOut,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -128,8 +199,9 @@ final class ChatScroll({
|
|
|
|
|
historyItems: split.history,
|
|
|
|
|
liveItems: split.live,
|
|
|
|
|
centerKey: centerKey,
|
|
|
|
|
historyListController: historyListController.value,
|
|
|
|
|
liveListController: liveListController.value,
|
|
|
|
|
anchorItemKey: anchorItemKey,
|
|
|
|
|
historyListController: historyListController,
|
|
|
|
|
liveListController: liveListController,
|
|
|
|
|
scrollController: scrollController,
|
|
|
|
|
jumpToId: jumpToId,
|
|
|
|
|
);
|
|
|
|
|
|