diff --git a/lib/helpers/gif_helper.dart b/lib/helpers/gif_helper.dart index a223ffc..8dd187b 100644 --- a/lib/helpers/gif_helper.dart +++ b/lib/helpers/gif_helper.dart @@ -9,7 +9,7 @@ class GifHelper { /// include a trailing slash. /// /// Returns null if text is not a valid GIF format - static String? parseGifId(String text) { + static String? parseGif(String text) { final trimmed = text.trim(); final match = RegExp(r'^g:([A-Za-z0-9_-]+)$').firstMatch(trimmed); if (match != null) { @@ -30,4 +30,9 @@ class GifHelper { ).firstMatch(trimmed); return pageMatch?.group(1); } + + /// Encode a GIF in a format that parseGif() can parse. + static String encodeGif(String gifId) { + return 'g:$gifId'; + } } diff --git a/lib/helpers/reaction_helper.dart b/lib/helpers/reaction_helper.dart index 90733c3..169b1a1 100644 --- a/lib/helpers/reaction_helper.dart +++ b/lib/helpers/reaction_helper.dart @@ -109,4 +109,9 @@ class ReactionHelper { return ReactionInfo(targetHash: match.group(1)!, emoji: emoji); } + + /// Encode a reaction message that parseReaction() can parse. + static String encodeReaction(String hash, String emojiIndex) { + return 'r:$hash:$emojiIndex'; + } } diff --git a/lib/screens/channel_chat_screen.dart b/lib/screens/channel_chat_screen.dart index 131d74c..7beaaf4 100644 --- a/lib/screens/channel_chat_screen.dart +++ b/lib/screens/channel_chat_screen.dart @@ -356,7 +356,7 @@ class _ChannelChatScreenState extends State { final settingsService = context.watch(); final enableTracing = settingsService.settings.enableMessageTracing; final isOutgoing = message.isOutgoing; - final gifId = GifHelper.parseGifId(message.text); + final gifId = GifHelper.parseGif(message.text); final poi = _parsePoiMessage(message.text); final translatedDisplayText = message.translatedText != null && @@ -700,7 +700,7 @@ class _ChannelChatScreenState extends State { final colorScheme = Theme.of(context).colorScheme; final previewTextColor = colorScheme.onSurface.withValues(alpha: 0.7); - final gifId = GifHelper.parseGifId(replyText); + final gifId = GifHelper.parseGif(replyText); final poi = _parsePoiMessage(replyText); Widget contentPreview; @@ -892,7 +892,7 @@ class _ChannelChatScreenState extends State { isScrollControlled: true, builder: (context) => GifPicker( onGifSelected: (gifId) { - _textController.text = 'g:$gifId'; + _textController.text = GifHelper.encodeGif(gifId); }, ), ); @@ -1048,7 +1048,7 @@ class _ChannelChatScreenState extends State { child: ValueListenableBuilder( valueListenable: _textController, builder: (context, value, child) { - final gifId = GifHelper.parseGifId(value.text); + final gifId = GifHelper.parseGif(value.text); if (gifId != null) { return Focus( autofocus: true, @@ -1316,7 +1316,7 @@ class _ChannelChatScreenState extends State { message.senderName, message.text, ); - final reactionText = 'r:$hash:$emojiIndex'; + final reactionText = ReactionHelper.encodeReaction(hash, emojiIndex); connector.sendChannelMessage(widget.channel, reactionText); } diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index daba56b..8057f1f 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -524,7 +524,7 @@ class _ChatScreenState extends State { child: ValueListenableBuilder( valueListenable: _textController, builder: (context, value, child) { - final gifId = GifHelper.parseGifId(value.text); + final gifId = GifHelper.parseGif(value.text); if (gifId != null) { return Focus( autofocus: true, @@ -605,7 +605,7 @@ class _ChatScreenState extends State { isScrollControlled: true, builder: (context) => GifPicker( onGifSelected: (gifId) { - _textController.text = 'g:$gifId'; + _textController.text = GifHelper.encodeGif(gifId); }, ), ); @@ -1538,7 +1538,7 @@ class _ChatScreenState extends State { senderName, message.text, ); - final reactionText = 'r:$hash:$emojiIndex'; + final reactionText = ReactionHelper.encodeReaction(hash, emojiIndex); connector.sendMessage(_resolveContact(connector), reactionText); } } @@ -1568,7 +1568,7 @@ class _MessageBubble extends StatelessWidget { final enableTracing = settingsService.settings.enableMessageTracing; final isOutgoing = message.isOutgoing; final colorScheme = Theme.of(context).colorScheme; - final gifId = GifHelper.parseGifId(message.text); + final gifId = GifHelper.parseGif(message.text); final poi = _parsePoiMessage(message.text); final isFailed = message.status == MessageStatus.failed; final bubbleColor = isFailed