small fixups
This commit is contained in:
parent
856072e0b0
commit
09904536e7
2 changed files with 33 additions and 43 deletions
|
|
@ -66,7 +66,9 @@ class RoomChatController(final String roomId)
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
Future<bool> loadOlder() async {
|
Future<void> loadOlder() async {
|
||||||
|
if (state.isLoading) return;
|
||||||
|
|
||||||
state = .loading();
|
state = .loading();
|
||||||
final timelineKeys = ref
|
final timelineKeys = ref
|
||||||
.read(RoomsController.provider.select((value) => value[roomId]))
|
.read(RoomsController.provider.select((value) => value[roomId]))
|
||||||
|
|
@ -85,7 +87,8 @@ class RoomChatController(final String roomId)
|
||||||
|
|
||||||
if (response.events.isEmpty) {
|
if (response.events.isEmpty) {
|
||||||
state = .data(state.value);
|
state = .data(state.value);
|
||||||
} else {
|
}
|
||||||
|
|
||||||
ref
|
ref
|
||||||
.read(RoomsController.provider.notifier)
|
.read(RoomsController.provider.notifier)
|
||||||
.update(
|
.update(
|
||||||
|
|
@ -108,9 +111,6 @@ class RoomChatController(final String roomId)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.hasMore;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> send(
|
Future<void> send(
|
||||||
String text, {
|
String text, {
|
||||||
bool shouldMention = true,
|
bool shouldMention = true,
|
||||||
|
|
@ -123,7 +123,7 @@ class RoomChatController(final String roomId)
|
||||||
baseContent = relation?.content;
|
baseContent = relation?.content;
|
||||||
} else {
|
} else {
|
||||||
final provider = AttachmentController.provider(roomId);
|
final provider = AttachmentController.provider(roomId);
|
||||||
baseContent = ref.watch(provider)?.$2;
|
baseContent = ref.read(provider)?.$2;
|
||||||
ref.invalidate(provider);
|
ref.invalidate(provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -139,7 +139,7 @@ class RoomChatController(final String roomId)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final client = ref.watch(ClientController.provider.notifier);
|
final client = ref.read(ClientController.provider.notifier);
|
||||||
final event = await client.sendMessage(
|
final event = await client.sendMessage(
|
||||||
SendMessageRequest(
|
SendMessageRequest(
|
||||||
roomId: roomId,
|
roomId: roomId,
|
||||||
|
|
@ -161,7 +161,7 @@ class RoomChatController(final String roomId)
|
||||||
);
|
);
|
||||||
|
|
||||||
ref
|
ref
|
||||||
.watch(RoomsController.provider.notifier)
|
.read(RoomsController.provider.notifier)
|
||||||
.update(
|
.update(
|
||||||
.new({
|
.new({
|
||||||
roomId: .new(
|
roomId: .new(
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ final class ChatScroll<T>({
|
||||||
static ChatScroll<T> use<T>({
|
static ChatScroll<T> use<T>({
|
||||||
required AsyncValue<IList<T>?> controllerData,
|
required AsyncValue<IList<T>?> controllerData,
|
||||||
required String Function(T item) id,
|
required String Function(T item) id,
|
||||||
required Future<bool> Function() loadOlder,
|
required Future<void> Function() loadOlder,
|
||||||
required Future<void> Function() onReachedBottom,
|
required Future<void> Function() onReachedBottom,
|
||||||
}) {
|
}) {
|
||||||
final historyListController = useRef(ListController());
|
final historyListController = useRef(ListController());
|
||||||
|
|
@ -59,35 +59,25 @@ final class ChatScroll<T>({
|
||||||
);
|
);
|
||||||
}, [controllerData, anchorIdValue]);
|
}, [controllerData, anchorIdValue]);
|
||||||
|
|
||||||
Future<void> loadOlderItems() async {
|
|
||||||
if (controllerData.isLoading) return;
|
|
||||||
|
|
||||||
await loadOlder();
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() {
|
useEffect(() {
|
||||||
const loadThreshold = 500.0;
|
const topThreshold = 500.0;
|
||||||
const bottomThreshold = 50.0;
|
const bottomThreshold = 50.0;
|
||||||
|
|
||||||
void checkPosition() {
|
Future<void> checkPosition() async {
|
||||||
if (!scrollController.hasClients) return;
|
if (!scrollController.hasClients) return;
|
||||||
|
|
||||||
final position = scrollController.position;
|
final position = scrollController.position;
|
||||||
|
|
||||||
if (position.extentAfter <= loadThreshold) {
|
if (position.extentAfter <= topThreshold) {
|
||||||
loadOlderItems();
|
await loadOlder();
|
||||||
}
|
} else if (position.extentBefore <= bottomThreshold) {
|
||||||
|
|
||||||
if (position.extentBefore <= bottomThreshold) {
|
|
||||||
onReachedBottom();
|
onReachedBottom();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
scrollController.addListener(checkPosition);
|
scrollController.addListener(checkPosition);
|
||||||
|
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) => checkPosition());
|
||||||
checkPosition();
|
|
||||||
});
|
|
||||||
|
|
||||||
return () {
|
return () {
|
||||||
scrollController.removeListener(checkPosition);
|
scrollController.removeListener(checkPosition);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue