2024-04-17 15:24:09 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:solian/models/message.dart';
|
2024-04-19 11:36:03 +00:00
|
|
|
import 'package:solian/models/post.dart';
|
2024-04-17 15:24:09 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/utils/service_url.dart';
|
2024-04-29 12:22:06 +00:00
|
|
|
import 'package:solian/widgets/exts.dart';
|
2024-04-19 11:36:03 +00:00
|
|
|
import 'package:solian/widgets/posts/attachment_editor.dart';
|
|
|
|
import 'package:badges/badges.dart' as badge;
|
2024-04-17 15:24:09 +00:00
|
|
|
|
|
|
|
class ChatMessageEditor extends StatefulWidget {
|
|
|
|
final String channel;
|
|
|
|
final Message? editing;
|
2024-04-18 15:39:48 +00:00
|
|
|
final Message? replying;
|
|
|
|
final Function? onReset;
|
2024-04-17 15:24:09 +00:00
|
|
|
|
2024-05-01 11:39:48 +00:00
|
|
|
const ChatMessageEditor({super.key, required this.channel, this.editing, this.replying, this.onReset});
|
2024-04-17 15:24:09 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<ChatMessageEditor> createState() => _ChatMessageEditorState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChatMessageEditorState extends State<ChatMessageEditor> {
|
|
|
|
final _textController = TextEditingController();
|
2024-05-01 11:39:48 +00:00
|
|
|
final _focusNode = FocusNode();
|
2024-04-17 15:24:09 +00:00
|
|
|
|
|
|
|
bool _isSubmitting = false;
|
2024-04-27 12:10:15 +00:00
|
|
|
int? _prevEditingId;
|
2024-04-17 15:24:09 +00:00
|
|
|
|
2024-04-19 11:36:03 +00:00
|
|
|
List<Attachment> _attachments = List.empty(growable: true);
|
|
|
|
|
|
|
|
void viewAttachments(BuildContext context) {
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => AttachmentEditor(
|
|
|
|
provider: 'messaging',
|
|
|
|
current: _attachments,
|
2024-04-24 15:19:26 +00:00
|
|
|
onUpdate: (value) => setState(() => _attachments = value),
|
2024-04-19 11:36:03 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-17 15:24:09 +00:00
|
|
|
Future<void> sendMessage(BuildContext context) async {
|
|
|
|
if (_isSubmitting) return;
|
|
|
|
|
2024-05-01 11:39:48 +00:00
|
|
|
_focusNode.requestFocus();
|
|
|
|
|
2024-04-17 15:24:09 +00:00
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
if (!await auth.isAuthorized()) return;
|
|
|
|
|
|
|
|
final uri = widget.editing == null
|
|
|
|
? getRequestUri('messaging', '/api/channels/${widget.channel}/messages')
|
2024-05-01 11:39:48 +00:00
|
|
|
: getRequestUri('messaging', '/api/channels/${widget.channel}/messages/${widget.editing!.id}');
|
2024-04-17 15:24:09 +00:00
|
|
|
|
|
|
|
final req = Request(widget.editing == null ? "POST" : "PUT", uri);
|
|
|
|
req.headers['Content-Type'] = 'application/json';
|
|
|
|
req.body = jsonEncode(<String, dynamic>{
|
|
|
|
'content': _textController.value.text,
|
2024-04-19 11:36:03 +00:00
|
|
|
'attachments': _attachments,
|
2024-04-18 15:39:48 +00:00
|
|
|
'reply_to': widget.replying?.id,
|
2024-04-17 15:24:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
setState(() => _isSubmitting = true);
|
|
|
|
var res = await Response.fromStream(await auth.client!.send(req));
|
|
|
|
if (res.statusCode != 200) {
|
|
|
|
var message = utf8.decode(res.bodyBytes);
|
2024-04-29 12:22:06 +00:00
|
|
|
context.showErrorDialog(message);
|
2024-04-17 15:24:09 +00:00
|
|
|
} else {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
setState(() => _isSubmitting = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
_textController.clear();
|
2024-04-19 11:36:03 +00:00
|
|
|
_attachments.clear();
|
2024-04-18 15:39:48 +00:00
|
|
|
|
|
|
|
if (widget.onReset != null) widget.onReset!();
|
|
|
|
}
|
|
|
|
|
|
|
|
void syncWidget() {
|
2024-04-27 12:10:15 +00:00
|
|
|
if (widget.editing != null && _prevEditingId != widget.editing!.id) {
|
2024-04-18 15:39:48 +00:00
|
|
|
setState(() {
|
2024-04-27 12:10:15 +00:00
|
|
|
_prevEditingId = widget.editing!.id;
|
2024-04-18 15:39:48 +00:00
|
|
|
_textController.text = widget.editing!.content;
|
2024-05-01 11:39:48 +00:00
|
|
|
_attachments = widget.editing!.attachments ?? List.empty(growable: true);
|
2024-04-18 15:39:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didUpdateWidget(oldWidget) {
|
|
|
|
syncWidget();
|
|
|
|
super.didUpdateWidget(oldWidget);
|
2024-04-17 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-04-18 15:39:48 +00:00
|
|
|
final editingBanner = MaterialBanner(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 20),
|
|
|
|
leading: const Icon(Icons.edit_note),
|
|
|
|
backgroundColor: const Color(0xFFE0E0E0),
|
|
|
|
dividerColor: const Color.fromARGB(1, 0, 0, 0),
|
|
|
|
content: Text(AppLocalizations.of(context)!.chatMessageEditNotify),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child: Text(AppLocalizations.of(context)!.cancel),
|
|
|
|
onPressed: () => reset(),
|
2024-04-17 15:24:09 +00:00
|
|
|
),
|
2024-04-18 15:39:48 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
final replyingBanner = MaterialBanner(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 20),
|
|
|
|
leading: const Icon(Icons.reply),
|
|
|
|
backgroundColor: const Color(0xFFE0E0E0),
|
|
|
|
dividerColor: const Color.fromARGB(1, 0, 0, 0),
|
|
|
|
content: Text(AppLocalizations.of(context)!.chatMessageReplyNotify),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child: Text(AppLocalizations.of(context)!.cancel),
|
|
|
|
onPressed: () => reset(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
widget.editing != null ? editingBanner : Container(),
|
|
|
|
widget.replying != null ? replyingBanner : Container(),
|
|
|
|
Container(
|
|
|
|
height: 56,
|
2024-04-19 11:36:03 +00:00
|
|
|
padding: const EdgeInsets.only(top: 4, bottom: 4, right: 8),
|
2024-04-18 15:39:48 +00:00
|
|
|
decoration: const BoxDecoration(
|
|
|
|
border: Border(
|
|
|
|
top: BorderSide(width: 0.3, color: Color(0xffdedede)),
|
2024-04-17 15:24:09 +00:00
|
|
|
),
|
|
|
|
),
|
2024-04-18 15:39:48 +00:00
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
|
|
children: [
|
2024-04-19 11:36:03 +00:00
|
|
|
badge.Badge(
|
|
|
|
showBadge: _attachments.isNotEmpty,
|
2024-05-01 11:39:48 +00:00
|
|
|
badgeContent: Text(_attachments.length.toString(), style: const TextStyle(color: Colors.white)),
|
2024-04-19 11:36:03 +00:00
|
|
|
position: badge.BadgePosition.custom(top: -2, end: 8),
|
|
|
|
child: TextButton(
|
2024-05-01 11:39:48 +00:00
|
|
|
style: TextButton.styleFrom(shape: const CircleBorder(), padding: const EdgeInsets.all(4)),
|
|
|
|
onPressed: !_isSubmitting ? () => viewAttachments(context) : null,
|
2024-04-19 11:36:03 +00:00
|
|
|
child: const Icon(Icons.attach_file),
|
|
|
|
),
|
|
|
|
),
|
2024-04-18 15:39:48 +00:00
|
|
|
Expanded(
|
|
|
|
child: TextField(
|
2024-05-01 11:39:48 +00:00
|
|
|
focusNode: _focusNode,
|
2024-04-18 15:39:48 +00:00
|
|
|
controller: _textController,
|
|
|
|
maxLines: null,
|
|
|
|
autofocus: true,
|
|
|
|
autocorrect: true,
|
|
|
|
keyboardType: TextInputType.text,
|
|
|
|
decoration: InputDecoration.collapsed(
|
2024-05-01 11:39:48 +00:00
|
|
|
hintText: AppLocalizations.of(context)!.chatMessagePlaceholder,
|
2024-04-18 15:39:48 +00:00
|
|
|
),
|
|
|
|
onSubmitted: (_) => sendMessage(context),
|
2024-05-01 11:39:48 +00:00
|
|
|
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
|
2024-04-18 15:39:48 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
TextButton(
|
2024-05-01 11:39:48 +00:00
|
|
|
style: TextButton.styleFrom(shape: const CircleBorder(), padding: const EdgeInsets.all(4)),
|
2024-04-18 15:39:48 +00:00
|
|
|
onPressed: !_isSubmitting ? () => sendMessage(context) : null,
|
|
|
|
child: const Icon(Icons.send),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2024-04-17 15:24:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|