Solian/lib/widgets/chat/chat_message_input.dart

241 lines
6.6 KiB
Dart
Raw Normal View History

2024-05-26 05:39:21 +00:00
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
2024-05-26 05:39:21 +00:00
import 'package:get/get.dart';
import 'package:solian/exts.dart';
import 'package:solian/models/account.dart';
import 'package:solian/models/channel.dart';
import 'package:solian/models/message.dart';
import 'package:solian/providers/auth.dart';
2024-05-26 13:03:25 +00:00
import 'package:solian/widgets/attachments/attachment_publish.dart';
import 'package:solian/widgets/chat/chat_message.dart';
2024-05-26 05:39:21 +00:00
import 'package:uuid/uuid.dart';
class ChatMessageInput extends StatefulWidget {
final Message? edit;
final Message? reply;
2024-05-30 14:02:54 +00:00
final String? placeholder;
2024-05-26 05:39:21 +00:00
final Channel channel;
final String realm;
final Function(Message) onSent;
final Function()? onReset;
2024-05-26 05:39:21 +00:00
const ChatMessageInput({
super.key,
this.edit,
this.reply,
2024-05-30 14:02:54 +00:00
this.placeholder,
2024-05-26 05:39:21 +00:00
required this.channel,
required this.realm,
required this.onSent,
this.onReset,
2024-05-26 05:39:21 +00:00
});
@override
State<ChatMessageInput> createState() => _ChatMessageInputState();
}
class _ChatMessageInputState extends State<ChatMessageInput> {
final TextEditingController _textController = TextEditingController();
final FocusNode _focusNode = FocusNode();
List<int> _attachments = List.empty(growable: true);
Message? _editTo;
Message? _replyTo;
2024-05-26 13:03:25 +00:00
void showAttachments() {
showModalBottomSheet(
context: context,
2024-06-27 03:05:15 +00:00
builder: (context) => AttachmentPublishPopup(
2024-05-26 13:03:25 +00:00
usage: 'm.attachment',
current: _attachments,
onUpdate: (value) => _attachments = value,
),
);
}
2024-05-26 05:39:21 +00:00
Map<String, dynamic> encodeMessage(String content) {
return {
2024-06-08 13:35:50 +00:00
'value': content.trim(),
2024-05-26 05:39:21 +00:00
'keypair_id': null,
'algorithm': 'plain',
};
}
Future<void> sendMessage() async {
_focusNode.requestFocus();
final AuthProvider auth = Get.find();
final prof = await auth.getProfile();
if (!await auth.isAuthorized) return;
2024-06-22 14:39:32 +00:00
final client = auth.configureClient('messaging');
2024-05-26 05:39:21 +00:00
final payload = {
'uuid': const Uuid().v4(),
'type': 'm.text',
'content': encodeMessage(_textController.value.text),
2024-06-09 15:00:11 +00:00
'attachments': List.from(_attachments),
2024-06-04 15:29:05 +00:00
'reply_to': _replyTo?.id,
2024-05-26 05:39:21 +00:00
};
// The mock data
final sender = Sender(
id: 0,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
account: Account.fromJson(prof.body),
channelId: widget.channel.id,
accountId: prof.body['id'],
notify: 0,
);
final message = Message(
id: 0,
uuid: payload['uuid'] as String,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
content: payload['content'] as Map<String, dynamic>,
type: payload['type'] as String,
2024-06-09 15:00:11 +00:00
attachments: _attachments,
2024-05-26 05:39:21 +00:00
sender: sender,
replyId: _replyTo?.id,
replyTo: _replyTo,
2024-05-26 05:39:21 +00:00
channelId: widget.channel.id,
senderId: sender.id,
);
2024-06-04 15:29:05 +00:00
2024-06-08 13:35:50 +00:00
if (_editTo == null) {
message.isSending = true;
widget.onSent(message);
}
2024-05-26 05:39:21 +00:00
resetInput();
Response resp;
if (_editTo != null) {
2024-05-26 05:39:21 +00:00
resp = await client.put(
2024-06-08 16:09:01 +00:00
'/api/channels/${widget.realm}/${widget.channel.alias}/messages/${_editTo!.id}',
2024-05-26 05:39:21 +00:00
payload,
);
} else {
resp = await client.post(
'/api/channels/${widget.realm}/${widget.channel.alias}/messages',
payload,
);
}
if (resp.statusCode != 200) {
context.showErrorDialog(resp.bodyString);
}
}
void resetInput() {
if (widget.onReset != null) widget.onReset!();
_editTo = null;
_replyTo = null;
2024-05-26 05:39:21 +00:00
_textController.clear();
2024-06-08 13:35:50 +00:00
_attachments.clear();
setState(() {});
2024-05-26 05:39:21 +00:00
}
void syncWidget() {
if (widget.edit != null) {
_editTo = widget.edit!;
_textController.text = widget.edit!.content['value'];
}
if (widget.reply != null) {
_replyTo = widget.reply!;
}
setState(() {});
}
@override
void didUpdateWidget(covariant ChatMessageInput oldWidget) {
syncWidget();
super.didUpdateWidget(oldWidget);
}
2024-05-26 05:39:21 +00:00
@override
Widget build(BuildContext context) {
final notifyBannerActions = [
TextButton(
onPressed: resetInput,
child: Text('cancel'.tr),
)
];
2024-05-26 05:39:21 +00:00
2024-06-08 13:35:50 +00:00
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_replyTo != null)
MaterialBanner(
leading: const FaIcon(FontAwesomeIcons.reply, size: 18),
dividerColor: Colors.transparent,
2024-06-08 16:09:01 +00:00
padding: const EdgeInsets.only(left: 20),
backgroundColor: Theme.of(context)
.colorScheme
.surfaceContainerHighest
.withOpacity(0.5),
2024-06-08 13:35:50 +00:00
content: ChatMessage(
item: _replyTo!,
isContentPreviewing: true,
2024-06-05 15:55:21 +00:00
),
2024-06-08 13:35:50 +00:00
actions: notifyBannerActions,
),
if (_editTo != null)
MaterialBanner(
leading: const Icon(Icons.edit),
dividerColor: Colors.transparent,
2024-06-08 16:09:01 +00:00
padding: const EdgeInsets.only(left: 20),
backgroundColor: Theme.of(context)
.colorScheme
.surfaceContainerHighest
.withOpacity(0.5),
2024-06-08 13:35:50 +00:00
content: ChatMessage(
item: _editTo!,
isContentPreviewing: true,
2024-06-05 15:55:21 +00:00
),
2024-06-08 13:35:50 +00:00
actions: notifyBannerActions,
),
SizedBox(
height: 56,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
child: TextField(
controller: _textController,
focusNode: _focusNode,
maxLines: null,
autocorrect: true,
keyboardType: TextInputType.text,
decoration: InputDecoration.collapsed(
hintText: widget.placeholder ??
'messageInputPlaceholder'.trParams(
{'channel': '#${widget.channel.alias}'},
),
),
2024-06-08 13:35:50 +00:00
onSubmitted: (_) => sendMessage(),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
2024-06-05 15:55:21 +00:00
),
2024-06-08 13:35:50 +00:00
),
IconButton(
icon: const Icon(Icons.attach_file),
color: Colors.teal,
onPressed: () => showAttachments(),
),
IconButton(
icon: const Icon(Icons.send),
color: Theme.of(context).colorScheme.primary,
onPressed: () => sendMessage(),
)
],
).paddingOnly(left: 20, right: 16),
),
],
2024-05-26 05:39:21 +00:00
);
}
}