2024-05-26 05:39:21 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-06-02 15:38:34 +00:00
|
|
|
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';
|
2024-06-27 16:05:43 +00:00
|
|
|
import 'package:solian/models/event.dart';
|
2024-05-26 05:39:21 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-07-30 12:49:01 +00:00
|
|
|
import 'package:solian/widgets/attachments/attachment_editor.dart';
|
2024-06-27 16:59:11 +00:00
|
|
|
import 'package:solian/widgets/chat/chat_event.dart';
|
2024-05-26 05:39:21 +00:00
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
|
|
|
class ChatMessageInput extends StatefulWidget {
|
2024-06-27 16:05:43 +00:00
|
|
|
final Event? edit;
|
|
|
|
final Event? 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;
|
2024-06-27 16:05:43 +00:00
|
|
|
final Function(Event) onSent;
|
2024-05-30 15:14:29 +00:00
|
|
|
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,
|
2024-05-30 15:14:29 +00:00
|
|
|
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);
|
|
|
|
|
2024-06-27 16:05:43 +00:00
|
|
|
Event? _editTo;
|
|
|
|
Event? _replyTo;
|
2024-06-02 15:38:34 +00:00
|
|
|
|
2024-05-26 13:03:25 +00:00
|
|
|
void showAttachments() {
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
2024-07-07 05:38:43 +00:00
|
|
|
isScrollControlled: true,
|
2024-07-30 12:49:01 +00:00
|
|
|
builder: (context) => AttachmentEditorPopup(
|
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
|
|
|
Future<void> sendMessage() async {
|
|
|
|
_focusNode.requestFocus();
|
|
|
|
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
final prof = auth.userProfile.value!;
|
|
|
|
if (auth.isAuthorized.isFalse) return;
|
2024-05-26 05:39:21 +00:00
|
|
|
|
2024-06-22 14:39:32 +00:00
|
|
|
final client = auth.configureClient('messaging');
|
2024-05-26 05:39:21 +00:00
|
|
|
|
2024-06-27 16:59:11 +00:00
|
|
|
// TODO Deal with the @ ping (query uid with username), and then add into related_user and replace the @ with internal link in body
|
|
|
|
|
2024-06-27 20:34:35 +00:00
|
|
|
const uuid = Uuid();
|
2024-05-26 05:39:21 +00:00
|
|
|
final payload = {
|
2024-06-27 20:34:35 +00:00
|
|
|
'uuid': uuid.v4(),
|
2024-06-27 16:59:11 +00:00
|
|
|
'type': _editTo == null ? 'messages.new' : 'messages.edit',
|
|
|
|
'body': {
|
|
|
|
'text': _textController.value.text,
|
|
|
|
'algorithm': 'plain',
|
|
|
|
'attachments': List.from(_attachments),
|
|
|
|
'related_users': [
|
|
|
|
if (_replyTo != null) _replyTo!.sender.accountId,
|
|
|
|
],
|
|
|
|
if (_replyTo != null) 'quote_event': _replyTo!.id,
|
|
|
|
if (_editTo != null) 'related_event': _editTo!.id,
|
|
|
|
}
|
2024-05-26 05:39:21 +00:00
|
|
|
};
|
|
|
|
|
2024-06-27 16:59:11 +00:00
|
|
|
// The local mock data
|
2024-05-26 05:39:21 +00:00
|
|
|
final sender = Sender(
|
|
|
|
id: 0,
|
|
|
|
createdAt: DateTime.now(),
|
|
|
|
updatedAt: DateTime.now(),
|
2024-07-24 17:18:47 +00:00
|
|
|
account: Account.fromJson(prof),
|
2024-05-26 05:39:21 +00:00
|
|
|
channelId: widget.channel.id,
|
2024-07-24 17:18:47 +00:00
|
|
|
accountId: prof['id'],
|
2024-05-26 05:39:21 +00:00
|
|
|
notify: 0,
|
|
|
|
);
|
2024-06-27 16:05:43 +00:00
|
|
|
final message = Event(
|
2024-05-26 05:39:21 +00:00
|
|
|
id: 0,
|
|
|
|
uuid: payload['uuid'] as String,
|
|
|
|
createdAt: DateTime.now(),
|
|
|
|
updatedAt: DateTime.now(),
|
2024-06-27 16:59:11 +00:00
|
|
|
body: payload['body'] as Map<String, dynamic>,
|
2024-05-26 05:39:21 +00:00
|
|
|
type: payload['type'] as String,
|
|
|
|
sender: sender,
|
|
|
|
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) {
|
2024-06-27 16:59:11 +00:00
|
|
|
message.isPending = true;
|
2024-06-08 13:35:50 +00:00
|
|
|
widget.onSent(message);
|
|
|
|
}
|
2024-05-26 05:39:21 +00:00
|
|
|
|
|
|
|
resetInput();
|
|
|
|
|
|
|
|
Response resp;
|
2024-06-02 15:38:34 +00:00
|
|
|
if (_editTo != null) {
|
2024-05-26 05:39:21 +00:00
|
|
|
resp = await client.put(
|
2024-07-16 11:46:53 +00:00
|
|
|
'/channels/${widget.realm}/${widget.channel.alias}/messages/${_editTo!.id}',
|
2024-05-26 05:39:21 +00:00
|
|
|
payload,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
resp = await client.post(
|
2024-07-16 11:46:53 +00:00
|
|
|
'/channels/${widget.realm}/${widget.channel.alias}/messages',
|
2024-05-26 05:39:21 +00:00
|
|
|
payload,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
context.showErrorDialog(resp.bodyString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetInput() {
|
2024-05-30 15:14:29 +00:00
|
|
|
if (widget.onReset != null) widget.onReset!();
|
2024-06-02 15:38:34 +00:00
|
|
|
_editTo = null;
|
|
|
|
_replyTo = null;
|
2024-05-26 05:39:21 +00:00
|
|
|
_textController.clear();
|
2024-06-08 13:35:50 +00:00
|
|
|
_attachments.clear();
|
2024-06-02 15:38:34 +00:00
|
|
|
setState(() {});
|
2024-05-26 05:39:21 +00:00
|
|
|
}
|
|
|
|
|
2024-05-30 15:14:29 +00:00
|
|
|
void syncWidget() {
|
2024-06-27 16:59:11 +00:00
|
|
|
if (widget.edit != null && widget.edit!.type.startsWith('messages')) {
|
|
|
|
final body = EventMessageBody.fromJson(widget.edit!.body);
|
2024-06-02 15:38:34 +00:00
|
|
|
_editTo = widget.edit!;
|
2024-06-27 16:59:11 +00:00
|
|
|
_textController.text = body.text;
|
2024-05-30 15:14:29 +00:00
|
|
|
}
|
2024-06-02 15:38:34 +00:00
|
|
|
if (widget.reply != null) {
|
|
|
|
_replyTo = widget.reply!;
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() {});
|
2024-05-30 15:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didUpdateWidget(covariant ChatMessageInput oldWidget) {
|
|
|
|
syncWidget();
|
|
|
|
super.didUpdateWidget(oldWidget);
|
|
|
|
}
|
|
|
|
|
2024-05-26 05:39:21 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-30 15:14:29 +00:00
|
|
|
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-27 16:59:11 +00:00
|
|
|
content: ChatEvent(
|
2024-06-08 13:35:50 +00:00
|
|
|
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-27 16:59:11 +00:00
|
|
|
content: ChatEvent(
|
2024-06-08 13:35:50 +00:00
|
|
|
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-05-30 15:14:29 +00:00
|
|
|
),
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|