2023-08-07 00:55:57 -04:00
|
|
|
import 'dart:async';
|
|
|
|
|
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';
|
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';
|
2024-02-08 21:05:59 -05:00
|
|
|
import '../../tools/tools.dart';
|
2024-01-31 22:29:06 -05:00
|
|
|
import '../chat.dart';
|
|
|
|
|
2024-02-11 14:17:10 -05:00
|
|
|
class ChatComponent extends StatelessWidget {
|
|
|
|
const ChatComponent._(
|
|
|
|
{required TypedKey localUserIdentityKey,
|
2024-02-29 14:37:50 -05:00
|
|
|
required MessagesCubit messagesCubit,
|
2024-03-01 20:28:02 -05:00
|
|
|
required MessagesState 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-02-29 14:37:50 -05:00
|
|
|
final MessagesCubit _messagesCubit;
|
2024-03-01 20:28:02 -05:00
|
|
|
final MessagesState _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(
|
|
|
|
{required TypedKey remoteConversationRecordKey, Key? key}) =>
|
|
|
|
Builder(builder: (context) {
|
|
|
|
// Get all watched dependendies
|
|
|
|
final activeAccountInfo = context.watch<ActiveAccountInfo>();
|
|
|
|
final accountRecordInfo =
|
|
|
|
context.watch<AccountRecordCubit>().state.data?.value;
|
|
|
|
if (accountRecordInfo == null) {
|
|
|
|
return debugPage('should always have an account record here');
|
|
|
|
}
|
2024-02-27 12:45:58 -05:00
|
|
|
final contactList =
|
|
|
|
context.watch<ContactListCubit>().state.state.data?.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>?>(
|
|
|
|
(x) => x.state[remoteConversationRecordKey]);
|
|
|
|
if (avconversation == null) {
|
2024-02-28 20:32:37 -05:00
|
|
|
return waitingPage();
|
2024-02-11 14:17:10 -05:00
|
|
|
}
|
|
|
|
final conversation = avconversation.data?.value;
|
|
|
|
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-01 20:28:02 -05:00
|
|
|
final messages = context.select<ActiveConversationMessagesBlocMapCubit,
|
|
|
|
(MessagesCubit, MessagesState)?>(
|
|
|
|
(x) => x.tryOperate(remoteConversationRecordKey,
|
|
|
|
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-02-11 14:17:10 -05:00
|
|
|
types.Message messageToChatMessage(proto.Message message) {
|
|
|
|
final isLocal = message.author == _localUserIdentityKey.toProto();
|
2023-08-07 00:55:57 -04:00
|
|
|
|
2023-07-29 10:55:35 -04:00
|
|
|
final textMessage = types.TextMessage(
|
2023-08-07 00:55:57 -04:00
|
|
|
author: isLocal ? _localUser : _remoteUser,
|
2023-08-08 02:03:26 -04:00
|
|
|
createdAt: (message.timestamp ~/ 1000).toInt(),
|
|
|
|
id: message.timestamp.toString(),
|
2023-07-29 10:55:35 -04:00
|
|
|
text: message.text,
|
|
|
|
);
|
2023-08-07 00:55:57 -04:00
|
|
|
return textMessage;
|
|
|
|
}
|
|
|
|
|
2024-02-29 14:37:50 -05:00
|
|
|
Future<void> _addMessage(proto.Message message) async {
|
2024-02-11 14:17:10 -05:00
|
|
|
if (message.text.isEmpty) {
|
2023-08-07 00:55:57 -04:00
|
|
|
return;
|
|
|
|
}
|
2024-02-29 14:37:50 -05:00
|
|
|
await _messagesCubit.addMessage(message: message);
|
2023-08-07 00:55:57 -04:00
|
|
|
}
|
|
|
|
|
2024-02-29 14:37:50 -05:00
|
|
|
Future<void> _handleSendPressed(types.PartialText message) async {
|
2023-08-07 00:55:57 -04:00
|
|
|
final protoMessage = proto.Message()
|
2024-02-11 14:17:10 -05:00
|
|
|
..author = _localUserIdentityKey.toProto()
|
|
|
|
..timestamp = Veilid.instance.now().toInt64()
|
2023-08-07 00:55:57 -04:00
|
|
|
..text = message.text;
|
|
|
|
//..signature = signature;
|
|
|
|
|
2024-02-29 14:37:50 -05:00
|
|
|
await _addMessage(protoMessage);
|
2023-07-29 10:55:35 -04:00
|
|
|
}
|
|
|
|
|
2024-02-11 14:17:10 -05:00
|
|
|
Future<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-03-01 20:28:02 -05:00
|
|
|
final messages = _messagesState.data?.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>[];
|
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);
|
|
|
|
chatMessages.insert(0, chatMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
)),
|
|
|
|
const Spacer(),
|
|
|
|
IconButton(
|
|
|
|
icon: const Icon(Icons.close),
|
|
|
|
onPressed: () async {
|
|
|
|
context.read<ActiveChatCubit>().setActiveChat(null);
|
|
|
|
}).paddingLTRB(16, 0, 16, 0)
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: DecoratedBox(
|
|
|
|
decoration: const BoxDecoration(),
|
|
|
|
child: Chat(
|
|
|
|
theme: chatTheme,
|
|
|
|
messages: chatMessages,
|
|
|
|
//onAttachmentPressed: _handleAttachmentPressed,
|
|
|
|
//onMessageTap: _handleMessageTap,
|
|
|
|
//onPreviewDataFetched: _handlePreviewDataFetched,
|
|
|
|
onSendPressed: (message) {
|
2024-02-29 14:37:50 -05:00
|
|
|
singleFuture(
|
|
|
|
this, () async => _handleSendPressed(message));
|
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
|
|
|
}
|
|
|
|
}
|