2023-08-07 00:55:57 -04:00
|
|
|
import 'dart:async';
|
|
|
|
|
2023-08-06 19:46:40 -04:00
|
|
|
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
|
2023-07-29 10:55:35 -04:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
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';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2023-07-29 10:55:35 -04:00
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
2023-08-06 19:46:40 -04:00
|
|
|
import '../../entities/proto.dart' as proto;
|
2023-08-07 00:55:57 -04:00
|
|
|
import '../entities/identity.dart';
|
|
|
|
import '../providers/account.dart';
|
|
|
|
import '../providers/conversation.dart';
|
2023-08-02 21:09:28 -04:00
|
|
|
import '../tools/theme_service.dart';
|
2023-08-07 00:55:57 -04:00
|
|
|
import '../veilid_support/veilid_support.dart';
|
2023-08-02 21:09:28 -04:00
|
|
|
|
2023-07-29 10:55:35 -04:00
|
|
|
class ChatComponent extends ConsumerStatefulWidget {
|
2023-08-07 00:55:57 -04:00
|
|
|
const ChatComponent(
|
|
|
|
{required this.activeAccountInfo,
|
|
|
|
required this.activeChat,
|
|
|
|
required this.activeChatContact,
|
|
|
|
super.key});
|
|
|
|
|
|
|
|
final ActiveAccountInfo activeAccountInfo;
|
|
|
|
final TypedKey activeChat;
|
|
|
|
final proto.Contact activeChatContact;
|
2023-07-29 10:55:35 -04:00
|
|
|
|
|
|
|
@override
|
|
|
|
ChatComponentState createState() => ChatComponentState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChatComponentState extends ConsumerState<ChatComponent> {
|
|
|
|
List<types.Message> _messages = [];
|
|
|
|
final _unfocusNode = FocusNode();
|
2023-08-07 00:55:57 -04:00
|
|
|
late final types.User _localUser;
|
|
|
|
late final types.User _remoteUser;
|
2023-07-29 10:55:35 -04:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2023-08-07 00:55:57 -04:00
|
|
|
|
|
|
|
_localUser = types.User(
|
|
|
|
id: widget.activeAccountInfo.localAccount.identityMaster
|
|
|
|
.identityPublicTypedKey()
|
|
|
|
.toString(),
|
|
|
|
firstName: widget.activeAccountInfo.account.profile.name,
|
|
|
|
);
|
|
|
|
_remoteUser = types.User(
|
|
|
|
id: proto.TypedKeyProto.fromProto(
|
|
|
|
widget.activeChatContact.identityPublicKey)
|
|
|
|
.toString(),
|
|
|
|
firstName: widget.activeChatContact.remoteProfile.name);
|
|
|
|
|
|
|
|
unawaited(_loadMessages());
|
2023-07-29 10:55:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_unfocusNode.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:55:57 -04:00
|
|
|
Future<void> _loadMessages() async {
|
|
|
|
final localConversationOwned = proto.OwnedDHTRecordPointerProto.fromProto(
|
|
|
|
widget.activeChatContact.localConversation);
|
|
|
|
final remoteIdentityPublicKey = proto.TypedKeyProto.fromProto(
|
|
|
|
widget.activeChatContact.identityPublicKey);
|
|
|
|
final protoMessages = await getLocalConversationMessages(
|
|
|
|
activeAccountInfo: widget.activeAccountInfo,
|
|
|
|
localConversationOwned: localConversationOwned,
|
|
|
|
remoteIdentityPublicKey: remoteIdentityPublicKey);
|
|
|
|
if (protoMessages == null) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-29 10:55:35 -04:00
|
|
|
setState(() {
|
2023-08-07 00:55:57 -04:00
|
|
|
_messages = [];
|
|
|
|
for (final protoMessage in protoMessages) {
|
|
|
|
final message = protoMessageToMessage(protoMessage);
|
|
|
|
_messages.insert(0, message);
|
|
|
|
}
|
2023-07-29 10:55:35 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:55:57 -04:00
|
|
|
types.Message protoMessageToMessage(proto.Message message) {
|
|
|
|
final isLocal = message.author ==
|
|
|
|
widget.activeAccountInfo.localAccount.identityMaster
|
|
|
|
.identityPublicTypedKey()
|
|
|
|
.toProto();
|
|
|
|
|
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-07-29 10:55:35 -04:00
|
|
|
createdAt: DateTime.now().millisecondsSinceEpoch,
|
|
|
|
id: const Uuid().v4(),
|
|
|
|
text: message.text,
|
|
|
|
);
|
2023-08-07 00:55:57 -04:00
|
|
|
return textMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _addMessage(proto.Message protoMessage) async {
|
|
|
|
if (protoMessage.text.isEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final message = protoMessageToMessage(protoMessage);
|
2023-07-29 10:55:35 -04:00
|
|
|
|
2023-08-07 00:55:57 -04:00
|
|
|
setState(() {
|
|
|
|
_messages.insert(0, message);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Now add the message to the conversation messages
|
|
|
|
final localConversationOwned = proto.OwnedDHTRecordPointerProto.fromProto(
|
|
|
|
widget.activeChatContact.localConversation);
|
|
|
|
final remoteIdentityPublicKey = proto.TypedKeyProto.fromProto(
|
|
|
|
widget.activeChatContact.identityPublicKey);
|
|
|
|
|
|
|
|
await addLocalConversationMessage(
|
|
|
|
activeAccountInfo: widget.activeAccountInfo,
|
|
|
|
localConversationOwned: localConversationOwned,
|
|
|
|
remoteIdentityPublicKey: remoteIdentityPublicKey,
|
|
|
|
message: protoMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _handleSendPressed(types.PartialText message) async {
|
|
|
|
final protoMessage = proto.Message()
|
|
|
|
..author = widget.activeAccountInfo.localAccount.identityMaster
|
|
|
|
.identityPublicTypedKey()
|
|
|
|
.toProto()
|
|
|
|
..timestamp = (await eventualVeilid.future).now().toInt64()
|
|
|
|
..text = message.text;
|
|
|
|
//..signature = signature;
|
|
|
|
|
|
|
|
await _addMessage(protoMessage);
|
2023-07-29 10:55:35 -04:00
|
|
|
}
|
|
|
|
|
2023-08-02 21:09:28 -04:00
|
|
|
void _handleAttachmentPressed() {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
2023-07-29 10:55:35 -04:00
|
|
|
@override
|
|
|
|
// ignore: prefer_expression_function_bodies
|
|
|
|
Widget build(BuildContext context) {
|
2023-08-02 21:09:28 -04:00
|
|
|
final theme = Theme.of(context);
|
|
|
|
final scale = theme.extension<ScaleScheme>()!;
|
|
|
|
final chatTheme = scale.toChatTheme();
|
|
|
|
final textTheme = Theme.of(context).textTheme;
|
2023-08-07 00:55:57 -04:00
|
|
|
final contactName = widget.activeChatContact.editedProfile.name;
|
2023-08-06 19:46:40 -04:00
|
|
|
|
2023-08-02 21:09:28 -04:00
|
|
|
return DefaultTextStyle(
|
|
|
|
style: textTheme.bodySmall!,
|
|
|
|
child: Align(
|
2023-08-05 01:00:46 -04:00
|
|
|
alignment: AlignmentDirectional.centerEnd,
|
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
Column(
|
2023-07-29 10:55:35 -04:00
|
|
|
children: [
|
2023-08-05 01:00:46 -04:00
|
|
|
Container(
|
|
|
|
height: 48,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: scale.primaryScale.subtleBackground,
|
|
|
|
),
|
|
|
|
child: Align(
|
|
|
|
alignment: AlignmentDirectional.centerStart,
|
|
|
|
child: Padding(
|
|
|
|
padding:
|
|
|
|
const EdgeInsetsDirectional.fromSTEB(16, 0, 16, 0),
|
2023-08-07 00:55:57 -04:00
|
|
|
child: Text(contactName,
|
2023-08-05 01:00:46 -04:00
|
|
|
textAlign: TextAlign.start,
|
|
|
|
style: textTheme.titleMedium),
|
2023-07-29 10:55:35 -04:00
|
|
|
),
|
2023-08-05 01:00:46 -04:00
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Container(
|
|
|
|
decoration: const BoxDecoration(),
|
|
|
|
child: Chat(
|
|
|
|
theme: chatTheme,
|
|
|
|
messages: _messages,
|
|
|
|
//onAttachmentPressed: _handleAttachmentPressed,
|
|
|
|
//onMessageTap: _handleMessageTap,
|
|
|
|
//onPreviewDataFetched: _handlePreviewDataFetched,
|
2023-08-02 21:09:28 -04:00
|
|
|
|
2023-08-07 00:55:57 -04:00
|
|
|
onSendPressed: (message) {
|
|
|
|
unawaited(_handleSendPressed(message));
|
|
|
|
},
|
2023-08-05 01:00:46 -04:00
|
|
|
showUserAvatars: true,
|
|
|
|
showUserNames: true,
|
2023-08-07 00:55:57 -04:00
|
|
|
user: _localUser,
|
2023-07-29 10:55:35 -04:00
|
|
|
),
|
2023-08-05 01:00:46 -04:00
|
|
|
),
|
2023-07-29 10:55:35 -04:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2023-08-05 01:00:46 -04:00
|
|
|
],
|
|
|
|
),
|
|
|
|
));
|
2023-07-29 10:55:35 -04:00
|
|
|
}
|
|
|
|
}
|