veilidchat/lib/chat/views/chat_component.dart

294 lines
10 KiB
Dart
Raw Normal View History

2024-05-27 18:04:00 -04:00
import 'dart:typed_data';
2024-02-11 00:29:58 -05:00
import 'package:async_tools/async_tools.dart';
2023-08-08 02:03:26 -04:00
import 'package:awesome_extensions/awesome_extensions.dart';
2024-05-27 18:04:00 -04:00
import 'package:fixnum/fixnum.dart';
2023-07-29 10:55:35 -04:00
import 'package:flutter/material.dart';
2024-01-31 22:29:06 -05:00
import 'package:flutter_bloc/flutter_bloc.dart';
2023-07-29 10:55:35 -04:00
import 'package:flutter_chat_types/flutter_chat_types.dart' as types;
2023-08-06 19:46:40 -04:00
import 'package:flutter_chat_ui/flutter_chat_ui.dart';
2024-02-11 00:29:58 -05:00
import 'package:veilid_support/veilid_support.dart';
2023-07-29 10:55:35 -04:00
2024-02-08 21:05:59 -05:00
import '../../account_manager/account_manager.dart';
2024-02-11 00:29:58 -05:00
import '../../chat_list/chat_list.dart';
2024-02-08 21:05:59 -05:00
import '../../contacts/contacts.dart';
2024-02-08 20:35:59 -05:00
import '../../proto/proto.dart' as proto;
2024-01-31 22:29:06 -05:00
import '../../theme/theme.dart';
import '../chat.dart';
2024-05-27 18:04:00 -04:00
const String metadataKeyExpirationDuration = 'expiration';
const String metadataKeyViewLimit = 'view_limit';
const String metadataKeyAttachments = 'attachments';
2024-02-11 14:17:10 -05:00
class ChatComponent extends StatelessWidget {
const ChatComponent._(
{required TypedKey localUserIdentityKey,
2024-03-24 12:13:27 -04:00
required SingleContactMessagesCubit messagesCubit,
required SingleContactMessagesState messagesState,
2024-02-11 14:17:10 -05:00
required types.User localUser,
required types.User remoteUser,
super.key})
: _localUserIdentityKey = localUserIdentityKey,
2024-02-29 14:37:50 -05:00
_messagesCubit = messagesCubit,
2024-03-01 20:28:02 -05:00
_messagesState = messagesState,
2024-02-11 14:17:10 -05:00
_localUser = localUser,
_remoteUser = remoteUser;
final TypedKey _localUserIdentityKey;
2024-03-24 12:13:27 -04:00
final SingleContactMessagesCubit _messagesCubit;
final SingleContactMessagesState _messagesState;
2024-02-11 14:17:10 -05:00
final types.User _localUser;
final types.User _remoteUser;
// Builder wrapper function that takes care of state management requirements
static Widget builder(
2024-05-27 18:04:00 -04:00
{required TypedKey localConversationRecordKey, Key? key}) =>
2024-02-11 14:17:10 -05:00
Builder(builder: (context) {
// Get all watched dependendies
final activeAccountInfo = context.watch<ActiveAccountInfo>();
final accountRecordInfo =
2024-04-05 22:03:04 -04:00
context.watch<AccountRecordCubit>().state.asData?.value;
2024-02-11 14:17:10 -05:00
if (accountRecordInfo == null) {
return debugPage('should always have an account record here');
}
2024-02-27 12:45:58 -05:00
final contactList =
2024-04-05 22:03:04 -04:00
context.watch<ContactListCubit>().state.state.asData?.value;
2024-02-11 14:17:10 -05:00
if (contactList == null) {
return debugPage('should always have a contact list here');
}
2024-02-20 17:57:05 -05:00
final avconversation = context.select<ActiveConversationsBlocMapCubit,
2024-02-11 14:17:10 -05:00
AsyncValue<ActiveConversationState>?>(
2024-05-27 18:04:00 -04:00
(x) => x.state[localConversationRecordKey]);
2024-02-11 14:17:10 -05:00
if (avconversation == null) {
2024-02-28 20:32:37 -05:00
return waitingPage();
2024-02-11 14:17:10 -05:00
}
2024-04-05 22:03:04 -04:00
final conversation = avconversation.asData?.value;
2024-02-11 14:17:10 -05:00
if (conversation == null) {
return avconversation.buildNotData();
}
2023-07-29 10:55:35 -04:00
2024-02-11 14:17:10 -05:00
// Make flutter_chat_ui 'User's
final localUserIdentityKey = activeAccountInfo
.localAccount.identityMaster
.identityPublicTypedKey();
final localUser = types.User(
id: localUserIdentityKey.toString(),
firstName: accountRecordInfo.profile.name,
);
final editedName = conversation.contact.editedProfile.name;
final remoteUser = types.User(
2024-02-12 09:10:07 -05:00
id: conversation.contact.identityPublicKey.toVeilid().toString(),
2024-02-11 14:17:10 -05:00
firstName: editedName);
2024-02-29 14:37:50 -05:00
// Get the messages cubit
2024-03-24 12:13:27 -04:00
final messages = context.select<ActiveSingleContactChatBlocMapCubit,
(SingleContactMessagesCubit, SingleContactMessagesState)?>(
2024-05-27 18:04:00 -04:00
(x) => x.tryOperate(localConversationRecordKey,
2024-03-01 20:28:02 -05:00
closure: (cubit) => (cubit, cubit.state)));
2024-02-29 14:37:50 -05:00
2024-02-11 14:17:10 -05:00
// Get the messages to display
// and ensure it is safe to operate() on the MessageCubit for this chat
2024-03-01 20:28:02 -05:00
if (messages == null) {
2024-02-11 14:17:10 -05:00
return waitingPage();
}
2023-07-29 10:55:35 -04:00
2024-02-11 14:17:10 -05:00
return ChatComponent._(
localUserIdentityKey: localUserIdentityKey,
2024-03-01 20:28:02 -05:00
messagesCubit: messages.$1,
messagesState: messages.$2,
2024-02-11 14:17:10 -05:00
localUser: localUser,
remoteUser: remoteUser,
key: key);
});
2023-07-29 10:55:35 -04:00
2024-02-11 14:17:10 -05:00
/////////////////////////////////////////////////////////////////////
2023-07-29 10:55:35 -04:00
2024-05-27 18:04:00 -04:00
types.Message? messageToChatMessage(MessageState message) {
final isLocal = message.content.author.toVeilid() == _localUserIdentityKey;
2024-04-17 21:31:26 -04:00
types.Status? status;
if (message.sendState != null) {
assert(isLocal, 'send state should only be on sent messages');
switch (message.sendState!) {
case MessageSendState.sending:
status = types.Status.sending;
case MessageSendState.sent:
status = types.Status.sent;
case MessageSendState.delivered:
status = types.Status.delivered;
}
}
2023-08-07 00:55:57 -04:00
2024-05-27 18:04:00 -04:00
switch (message.content.whichKind()) {
case proto.Message_Kind.text:
final contextText = message.content.text;
final textMessage = types.TextMessage(
author: isLocal ? _localUser : _remoteUser,
createdAt: (message.timestamp.value ~/ BigInt.from(1000)).toInt(),
2024-05-27 20:34:54 -04:00
id: base64UrlNoPadEncode(message.uniqueId),
2024-05-27 18:04:00 -04:00
text: contextText.text,
showStatus: status != null,
status: status);
return textMessage;
case proto.Message_Kind.secret:
case proto.Message_Kind.delete:
case proto.Message_Kind.erase:
case proto.Message_Kind.settings:
case proto.Message_Kind.permissions:
case proto.Message_Kind.membership:
case proto.Message_Kind.moderation:
case proto.Message_Kind.notSet:
return null;
}
2023-08-07 00:55:57 -04:00
}
2024-05-27 18:04:00 -04:00
void _addTextMessage(
{required String text,
String? topic,
Uint8List? replyId,
Timestamp? expiration,
int? viewLimit,
List<proto.Attachment> attachments = const []}) {
final protoMessageText = proto.Message_Text()..text = text;
if (topic != null) {
protoMessageText.topic = topic;
2023-08-07 00:55:57 -04:00
}
2024-05-27 18:04:00 -04:00
if (replyId != null) {
protoMessageText.replyId = replyId;
}
protoMessageText
..expiration = expiration?.toInt64() ?? Int64.ZERO
..viewLimit = viewLimit ?? 0;
protoMessageText.attachments.addAll(attachments);
2024-05-28 22:01:50 -04:00
_messagesCubit.sendTextMessage(messageText: protoMessageText);
2023-08-07 00:55:57 -04:00
}
2024-04-17 21:31:26 -04:00
void _handleSendPressed(types.PartialText message) {
2024-05-27 18:04:00 -04:00
final text = message.text;
final replyId = (message.repliedMessage != null)
2024-05-27 20:34:54 -04:00
? base64UrlNoPadDecode(message.repliedMessage!.id)
2024-05-27 18:04:00 -04:00
: null;
Timestamp? expiration;
int? viewLimit;
List<proto.Attachment>? attachments;
final metadata = message.metadata;
if (metadata != null) {
final expirationValue =
metadata[metadataKeyExpirationDuration] as TimestampDuration?;
if (expirationValue != null) {
expiration = Veilid.instance.now().offset(expirationValue);
}
final viewLimitValue = metadata[metadataKeyViewLimit] as int?;
if (viewLimitValue != null) {
viewLimit = viewLimitValue;
}
final attachmentsValue =
metadata[metadataKeyAttachments] as List<proto.Attachment>?;
if (attachmentsValue != null) {
attachments = attachmentsValue;
}
}
2023-08-07 00:55:57 -04:00
2024-05-27 18:04:00 -04:00
_addTextMessage(
text: text,
replyId: replyId,
expiration: expiration,
viewLimit: viewLimit,
attachments: attachments ?? []);
2023-07-29 10:55:35 -04:00
}
2024-04-17 21:31:26 -04:00
// void _handleAttachmentPressed() async {
// //
// }
2023-08-02 21:09:28 -04:00
2023-07-29 10:55:35 -04:00
@override
Widget build(BuildContext context) {
2023-08-02 21:09:28 -04:00
final theme = Theme.of(context);
final scale = theme.extension<ScaleScheme>()!;
final textTheme = Theme.of(context).textTheme;
2023-09-24 15:30:54 -04:00
final chatTheme = makeChatTheme(scale, textTheme);
2023-08-06 19:46:40 -04:00
2024-04-05 22:03:04 -04:00
final messages = _messagesState.asData?.value;
2024-02-29 14:37:50 -05:00
if (messages == null) {
2024-03-01 20:28:02 -05:00
return _messagesState.buildNotData();
2024-02-29 14:37:50 -05:00
}
2024-02-11 14:17:10 -05:00
// Convert protobuf messages to chat messages
final chatMessages = <types.Message>[];
final tsSet = <String>{};
2024-02-29 14:37:50 -05:00
for (final message in messages) {
2024-02-11 14:17:10 -05:00
final chatMessage = messageToChatMessage(message);
2024-05-27 18:04:00 -04:00
if (chatMessage == null) {
continue;
}
2024-02-11 14:17:10 -05:00
chatMessages.insert(0, chatMessage);
if (!tsSet.add(chatMessage.id)) {
// ignore: avoid_print
print('duplicate id found: ${chatMessage.id}:\n'
'Messages:\n$messages\n'
'ChatMessages:\n$chatMessages');
assert(false, 'should not have duplicate id');
}
2024-02-11 14:17:10 -05:00
}
return DefaultTextStyle(
style: textTheme.bodySmall!,
child: Align(
alignment: AlignmentDirectional.centerEnd,
child: Stack(
children: [
Column(
2024-02-11 00:29:58 -05:00
children: [
2024-02-11 14:17:10 -05:00
Container(
height: 48,
decoration: BoxDecoration(
color: scale.primaryScale.subtleBorder,
),
child: Row(children: [
Align(
alignment: AlignmentDirectional.centerStart,
child: Padding(
padding: const EdgeInsetsDirectional.fromSTEB(
16, 0, 16, 0),
child: Text(_remoteUser.firstName!,
textAlign: TextAlign.start,
style: textTheme.titleMedium!.copyWith(
color: scale.primaryScale.borderText)),
2024-02-11 14:17:10 -05:00
)),
const Spacer(),
IconButton(
icon: Icon(Icons.close,
color: scale.primaryScale.borderText),
2024-02-11 14:17:10 -05:00
onPressed: () async {
context.read<ActiveChatCubit>().setActiveChat(null);
}).paddingLTRB(16, 0, 16, 0)
]),
),
Expanded(
child: DecoratedBox(
decoration: const BoxDecoration(),
child: Chat(
theme: chatTheme,
2024-04-07 23:16:06 -04:00
// emojiEnlargementBehavior:
// EmojiEnlargementBehavior.multi,
2024-02-11 14:17:10 -05:00
messages: chatMessages,
//onAttachmentPressed: _handleAttachmentPressed,
//onMessageTap: _handleMessageTap,
//onPreviewDataFetched: _handlePreviewDataFetched,
2024-04-17 21:31:26 -04:00
onSendPressed: _handleSendPressed,
2024-02-11 14:17:10 -05:00
//showUserAvatars: false,
//showUserNames: true,
user: _localUser,
2024-02-11 00:29:58 -05:00
),
2024-02-11 14:17:10 -05:00
),
2024-02-11 00:29:58 -05:00
),
],
),
2024-02-11 14:17:10 -05:00
],
),
));
2023-07-29 10:55:35 -04:00
}
}