Move isOnlyEmoji utility function into String extension

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-01-12 15:30:45 +05:30
parent 85aaf9eab8
commit 0605cbc3b4
5 changed files with 26 additions and 18 deletions
+21
View File
@@ -1,7 +1,28 @@
import 'package:emojis/emoji.dart';
import 'package:characters/characters.dart';
final _emojis = Emoji.all();
extension StringExtension on String {
String capitalize() {
return "${this[0].toUpperCase()}${this.substring(1)}";
}
// Emojis guidelines
// 1 to 3 emojis: big size with no text bubble.
// 4+ emojis or emojis+text: standard size with text bubble.
bool get isOnlyEmoji {
final characters = this.trim().characters;
if (characters.isEmpty) return false;
if (characters.length > 3) return false;
return characters.every((c) {
return _emojis.firstWhere(
(Emoji emoji) => emoji.char.contains(c),
orElse: () => null,
) !=
null;
});
}
}
/// List extension
+3 -2
View File
@@ -17,6 +17,7 @@ import '../stream_chat_flutter.dart';
import 'date_divider.dart';
import 'stream_channel.dart';
import 'swipeable.dart';
import 'extension.dart';
typedef MessageBuilder = Widget Function(
BuildContext,
@@ -708,7 +709,7 @@ class _MessageListViewState extends State<MessageListView> {
Message message,
) {
final isMyMessage = message.user.id == StreamChat.of(context).user.id;
final isOnlyEmoji = textIsOnlyEmoji(message.text);
final isOnlyEmoji = message.text.isOnlyEmoji;
return MessageWidget(
showThreadReplyIndicator: false,
@@ -808,7 +809,7 @@ class _MessageListViewState extends State<MessageListView> {
final showInChannelIndicator = !_isThreadConversation && isThreadMessage;
final showThreadReplyIndicator = !_isThreadConversation && hasReplies;
final isOnlyEmoji = textIsOnlyEmoji(message.text);
final isOnlyEmoji = message.text.isOnlyEmoji;
Widget child = MessageWidget(
key: ValueKey<String>('MESSAGE-${message.id}'),
+1 -1
View File
@@ -938,7 +938,7 @@ class _MessageWidgetState extends State<MessageWidget> {
);
}
bool get isOnlyEmoji => textIsOnlyEmoji(widget.message.text);
bool get isOnlyEmoji => widget.message.text.isOnlyEmoji;
Color _getBackgroundColor() {
if (hasQuotedMessage) {
+1 -1
View File
@@ -125,7 +125,7 @@ class QuotedMessageWidget extends StatelessWidget {
}
Widget _buildMessage(BuildContext context) {
final isOnlyEmoji = textIsOnlyEmoji(message.text);
final isOnlyEmoji = message.text.isOnlyEmoji;
var msg = _hasAttachments && !_containsText
? message.copyWith(text: message.attachments.last?.title ?? '')
: message;
-14
View File
@@ -1,4 +1,3 @@
import 'package:emojis/emoji.dart';
import 'package:flutter/material.dart';
import 'package:stream_chat/stream_chat.dart';
import 'package:url_launcher/url_launcher.dart';
@@ -215,16 +214,3 @@ StreamSvgIcon getFileTypeImage(String type) {
break;
}
}
final _emojis = Emoji.all();
bool textIsOnlyEmoji(String text) {
return text.trim().characters.isNotEmpty &&
text.trim().characters.every((c) =>
_emojis.firstWhere(
(Emoji emoji) => emoji.char.contains(c),
orElse: () => null,
) !=
null) &&
text.characters.length < 4;
}