feat(ui, core): minor fixes and improvements

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2021-12-21 14:11:23 +05:30
committed by xsahil03x
parent 56e030f40c
commit 8b6c732844
2 changed files with 68 additions and 53 deletions
@@ -895,7 +895,7 @@ class MessageInputState extends State<MessageInput>
_actionsShrunk = value.isNotEmpty && actionsLength > 1;
});
_checkContainsUrlDebounced.call([value, context]);
_checkContainsUrl(value, context);
_checkCommands(value, context);
_checkMentions(value, context);
_checkEmoji(value, context);
@@ -920,9 +920,11 @@ class MessageInputState extends State<MessageInput>
String? _lastSearchedContainsUrlText;
CancelableOperation? _enrichUrlOperation;
final _urlRegex = RegExp(
r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+',
);
late final _checkContainsUrlDebounced = debounce(
(String value, BuildContext context) async {
void _checkContainsUrl(String value, BuildContext context) async {
// Cancel the previous operation if it's still running
_enrichUrlOperation?.cancel();
@@ -930,9 +932,7 @@ class MessageInputState extends State<MessageInput>
if (_lastSearchedContainsUrlText == value) return;
_lastSearchedContainsUrlText = value;
final matchedUrls =
RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+')
.allMatches(value);
final matchedUrls = _urlRegex.allMatches(value);
// Reset the og attachment if the text doesn't contain any url
if (matchedUrls.isEmpty) {
@@ -950,7 +950,7 @@ class MessageInputState extends State<MessageInput>
final client = StreamChat.of(context).client;
_enrichUrlOperation = CancelableOperation.fromFuture(
client.enrichUrl(firstMatchedUrl),
_enrichUrl(firstMatchedUrl, client),
).then(
(ogAttachment) {
final attachment = Attachment.fromOGAttachment(ogAttachment);
@@ -962,9 +962,22 @@ class MessageInputState extends State<MessageInput>
widget.onError?.call(error, stackTrace);
},
);
},
const Duration(milliseconds: 650),
);
}
final _ogAttachmentCache = <String, OGAttachmentResponse>{};
Future<OGAttachmentResponse> _enrichUrl(
String url,
StreamChatClient client,
) async {
var response = _ogAttachmentCache[url];
if (response == null) {
final client = StreamChat.of(context).client;
response = await client.enrichUrl(url);
_ogAttachmentCache[url] = response;
}
return response;
}
void _checkEmoji(String value, BuildContext context) {
if (value.isNotEmpty &&
@@ -1169,11 +1182,14 @@ class MessageInputState extends State<MessageInput>
}
Widget _buildAttachments() {
if (_effectiveController.attachments.isEmpty) return const Offstage();
final fileAttachments = _effectiveController.attachments
final nonOGAttachments = _effectiveController.attachments.where(
(it) => it.titleLink == null,
);
if (nonOGAttachments.isEmpty) return const Offstage();
final fileAttachments = nonOGAttachments
.where((it) => it.type == 'file')
.toList(growable: false);
final remainingAttachments = _effectiveController.attachments
final remainingAttachments = nonOGAttachments
.where((it) => it.type != 'file')
.toList(growable: false);
return Column(
@@ -66,10 +66,7 @@ class MessageInputController extends ValueNotifier<Message> {
void _textEditingSyncer() {
final cleanText = value.command == null
? value.text
: value.text?.replaceFirst(
'/${value.command} ',
'',
);
: value.text?.replaceFirst('/${value.command} ', '');
if (cleanText != _textEditingController.text) {
final previousOffset = _textEditingController.value.selection.start;
@@ -207,7 +204,9 @@ class MessageInputController extends ValueNotifier<Message> {
/// Removes the og attachment.
void clearOGAttachment() {
attachments = [...attachments]..remove(_ogAttachment);
if (_ogAttachment != null) {
removeAttachment(_ogAttachment!);
}
_ogAttachment = null;
}