2024-07-06 09:12:57 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:solian/controllers/chat_events_controller.dart';
|
|
|
|
import 'package:solian/models/channel.dart';
|
|
|
|
import 'package:solian/models/event.dart';
|
2024-09-03 15:07:20 +00:00
|
|
|
import 'package:solian/providers/last_read.dart';
|
2024-07-06 09:12:57 +00:00
|
|
|
import 'package:solian/widgets/chat/chat_event.dart';
|
|
|
|
import 'package:solian/widgets/chat/chat_event_action.dart';
|
|
|
|
|
|
|
|
class ChatEventList extends StatelessWidget {
|
|
|
|
final String scope;
|
|
|
|
final Channel channel;
|
|
|
|
final ChatEventController chatController;
|
|
|
|
|
|
|
|
final Function(Event) onEdit;
|
|
|
|
final Function(Event) onReply;
|
|
|
|
|
|
|
|
const ChatEventList({
|
|
|
|
super.key,
|
|
|
|
this.scope = 'global',
|
|
|
|
required this.channel,
|
|
|
|
required this.chatController,
|
|
|
|
required this.onEdit,
|
|
|
|
required this.onReply,
|
|
|
|
});
|
|
|
|
|
2024-08-02 07:49:32 +00:00
|
|
|
bool _checkMessageMergeable(Event? a, Event? b) {
|
2024-07-06 09:12:57 +00:00
|
|
|
if (a == null || b == null) return false;
|
|
|
|
if (a.sender.account.id != b.sender.account.id) return false;
|
|
|
|
return a.createdAt.difference(b.createdAt).inMinutes <= 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return CustomScrollView(
|
|
|
|
reverse: true,
|
|
|
|
slivers: [
|
|
|
|
Obx(() {
|
|
|
|
return SliverList.builder(
|
|
|
|
key: Key('chat-history#${channel.id}'),
|
|
|
|
itemCount: chatController.currentEvents.length,
|
|
|
|
itemBuilder: (context, index) {
|
2024-09-03 15:07:20 +00:00
|
|
|
Get.find<LastReadProvider>().messagesLastReadAt =
|
|
|
|
chatController.currentEvents[index].id;
|
|
|
|
|
2024-07-06 09:12:57 +00:00
|
|
|
bool isMerged = false, hasMerged = false;
|
|
|
|
if (index > 0) {
|
2024-08-02 07:49:32 +00:00
|
|
|
hasMerged = _checkMessageMergeable(
|
2024-07-06 09:12:57 +00:00
|
|
|
chatController.currentEvents[index - 1].data,
|
|
|
|
chatController.currentEvents[index].data,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (index + 1 < chatController.currentEvents.length) {
|
2024-08-02 07:49:32 +00:00
|
|
|
isMerged = _checkMessageMergeable(
|
2024-07-06 09:12:57 +00:00
|
|
|
chatController.currentEvents[index].data,
|
|
|
|
chatController.currentEvents[index + 1].data,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
final item = chatController.currentEvents[index].data;
|
|
|
|
|
2024-09-06 15:55:44 +00:00
|
|
|
return GestureDetector(
|
|
|
|
behavior: HitTestBehavior.opaque,
|
2024-07-11 16:44:57 +00:00
|
|
|
child: ChatEvent(
|
2024-09-13 16:30:33 +00:00
|
|
|
key: Key('m${item!.uuid}'),
|
2024-07-11 16:44:57 +00:00
|
|
|
item: item,
|
|
|
|
isMerged: isMerged,
|
|
|
|
chatController: chatController,
|
|
|
|
).paddingOnly(
|
|
|
|
top: !isMerged ? 8 : 0,
|
|
|
|
bottom: !hasMerged ? 8 : 0,
|
2024-07-06 09:12:57 +00:00
|
|
|
),
|
|
|
|
onLongPress: () {
|
|
|
|
showModalBottomSheet(
|
|
|
|
useRootNavigator: true,
|
|
|
|
context: context,
|
|
|
|
builder: (context) => ChatEventAction(
|
|
|
|
channel: channel,
|
|
|
|
realm: channel.realm,
|
|
|
|
item: item,
|
|
|
|
onEdit: () {
|
|
|
|
onEdit(item);
|
|
|
|
},
|
|
|
|
onReply: () {
|
|
|
|
onReply(item);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
2024-08-19 14:25:17 +00:00
|
|
|
);
|
2024-07-06 09:12:57 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
Obx(() {
|
|
|
|
final amount =
|
|
|
|
chatController.totalEvents - chatController.currentEvents.length;
|
|
|
|
|
|
|
|
if (amount.value <= 0 || chatController.isLoading.isTrue) {
|
2024-09-07 09:48:07 +00:00
|
|
|
return const SliverToBoxAdapter(child: SizedBox.shrink());
|
2024-07-06 09:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SliverToBoxAdapter(
|
|
|
|
child: ListTile(
|
|
|
|
tileColor: Theme.of(context).colorScheme.surfaceContainerLow,
|
|
|
|
leading: const Icon(Icons.sync_disabled),
|
|
|
|
title: Text('messageUnSync'.tr),
|
|
|
|
subtitle: Text('messageUnSyncCaption'.trParams({
|
|
|
|
'count': amount.string,
|
|
|
|
})),
|
|
|
|
onTap: () {
|
|
|
|
chatController.loadEvents(channel, scope);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|