Files
buzz/mobile/lib/shared/custom_emoji/custom_emoji_render.dart
Tom Brow ac2c5831f3 fix(mobile): show already-loaded custom emoji in the iOS reactions sheet (#9150)
On iOS, the reactions sheet (long-press a reaction pill) could show a
custom emoji as truncated `:shortcode:` text, for example `:eye…`, even
though the same emoji rendered as an image on the message pill a moment
before.

The sheet is a native UIKit view that downloaded each custom emoji again
with its own `URLSession` request and showed the shortcode label until
that download finished, and for good when it failed. A shortcode is
illegible in a chip that size.

Now:

- Flutter keeps the encoded bytes of custom emoji it has downloaded (a
bounded LRU: 512 KiB per emoji, 8 MiB total, rebuilt when the media auth
scope changes so accounts never share bytes) and hands them to the
sheet, which decodes them without a fetch. Emoji the timeline never
loaded still download natively as before.
- A custom emoji in the sheet never shows its shortcode: it stays blank
while loading and shows the sparkles icon, matching the native emoji
picker, if the image cannot load. Reaction pills in Flutter get the same
sparkles fallback. Inline emoji in message text keep the shortcode
fallback, where it reads as text. Avatars in the sheet keep their
initial.

### Before / after

Reactions sheet header and chips right after opening, on a message with
two custom-emoji reactions (synthetic test channel, captured from a 30
fps screen recording).

| Before (main) | After (this branch) |
| --- | --- |
| <img width="300" alt="Before: custom emoji chips show truncated
:qa-... shortcodes"
src="https://github.com/user-attachments/assets/593e555b-9c82-4eec-9049-3d909f987c76">
| <img width="300" alt="After: custom emoji chips show images"
src="https://github.com/user-attachments/assets/a6739997-b39f-4062-9e5c-d22a8a38b368">
|

### Validation

- Added tests: the native sheet's payload carries the bytes the pill
already downloaded; the combined reaction tray shows sparkles, not the
shortcode, when a custom emoji fails to load (the existing alignment
test previously asserted the shortcode).
- Full mobile suite and `just mobile-check` pass; the iOS simulator
build compiles.
- Automated simulator run (iPhone 17 Pro, iOS 27, main vs this branch,
signed in to a test community): channel list, opening a channel, the
message action sheet, the emoji picker, and the reactions sheet on a
message carrying two custom emoji I added plus a thumbs-up. On main the
sheet's custom-emoji chips show truncated `:qa-...` shortcodes for a few
frames before the images arrive; on this branch they render as images
from the first frame (see Before / after). Limitations: the emoji images
load fine over the network in this setup, so the failed-download
sparkles fallback was covered by tests only, not seen in the app; a long
press on the combined reaction tray opened the message menu in the
simulator, a tap opened the sheet.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 06:59:03 -08:00

183 lines
6.7 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:gpt_markdown/gpt_markdown.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '../relay/relay.dart';
import '../theme/buzz_icons.dart';
import 'custom_emoji.dart';
/// Default rendered height of an inline custom emoji, in logical pixels.
/// Roughly matches a line of body text so emoji sit on the baseline cleanly.
const double kCustomEmojiInlineSize = 20.0;
/// Encoded bytes of custom emoji that [CustomEmojiImage] has downloaded, so a
/// native surface such as the iOS reactions sheet shows an emoji the timeline
/// already displays without fetching it again. Least recently used entries
/// are evicted past [maxTotalBytes]; an emoji over [maxEntryBytes] is not kept.
class CustomEmojiBytesCache {
static const maxEntryBytes = 512 * 1024;
static const maxTotalBytes = 8 * 1024 * 1024;
final _entries = <String, Uint8List>{};
var _totalBytes = 0;
void remember(String url, Uint8List bytes) {
if (bytes.lengthInBytes > maxEntryBytes) return;
final previous = _entries.remove(url);
if (previous != null) _totalBytes -= previous.lengthInBytes;
_entries[url] = bytes;
_totalBytes += bytes.lengthInBytes;
while (_totalBytes > maxTotalBytes) {
final oldest = _entries.keys.first;
_totalBytes -= _entries.remove(oldest)!.lengthInBytes;
}
}
Uint8List? lookup(String url) {
final bytes = _entries.remove(url);
if (bytes != null) _entries[url] = bytes;
return bytes;
}
}
/// Rebuilt with the media auth scope, like [MediaImageProvider]'s cache key,
/// so one account's downloads never reach another account's surfaces.
final customEmojiBytesCacheProvider = Provider<CustomEmojiBytesCache>((ref) {
ref.watch(mediaGetAuthServiceProvider);
return CustomEmojiBytesCache();
});
/// A single custom-emoji image rendered as a square, network-loaded glyph.
///
/// Reused everywhere a `:shortcode:` resolves to an image: inline in message
/// bodies, reaction pills, status, and the picker. If the image fails to load,
/// it falls back to the literal `:shortcode:` text, so a broken URL never
/// leaves a blank gap in prose; a [compact] glyph, such as a reaction pill,
/// shows a sparkles icon instead, because a truncated shortcode is illegible.
class CustomEmojiImage extends ConsumerWidget {
final String shortcode;
final String url;
final double size;
final bool compact;
const CustomEmojiImage({
super.key,
required this.shortcode,
required this.url,
this.size = kCustomEmojiInlineSize,
this.compact = false,
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final fallbackStyle = DefaultTextStyle.of(context).style;
final bytesCache = ref.watch(customEmojiBytesCacheProvider);
return MediaImage(
url: url,
width: size,
height: size,
decodeWidth: size,
fit: BoxFit.contain,
filterQuality: FilterQuality.medium,
semanticLabel: ':$shortcode:',
onBytesLoaded: (bytes) => bytesCache.remember(url, bytes),
errorBuilder: (_, _, _) => compact
? Icon(
BuzzIcons.sparkles,
size: size,
color: fallbackStyle.color?.withValues(alpha: 0.6),
semanticLabel: ':$shortcode:',
)
: Text(':$shortcode:', style: fallbackStyle),
);
}
}
/// gpt_markdown inline component that replaces `:shortcode:` with an inline
/// [CustomEmojiImage] for *known* shortcodes only. Unknown `:foo:` is left as
/// plain text. Matched case-insensitively; resolved via the lowercase palette.
///
/// Parallel to the `_MentionMd` / `_ChannelLinkMd` components in
/// message_content.dart — add an instance to a `GptMarkdown.inlineComponents`
/// list (before the default components) to enable custom emoji in any markdown
/// surface.
class CustomEmojiMd extends InlineMd {
final Map<String, String> _urlByShortcode;
final double size;
late final RegExp _exp = _buildPattern(_urlByShortcode.keys);
/// Only include shortcodes present in the rendered [content]. gpt_markdown
/// embeds this pattern in a combined regex for every parsed text segment, so
/// unrelated community emoji must not make every message expensive to parse.
CustomEmojiMd(
List<CustomEmoji> palette, {
required String content,
this.size = kCustomEmojiInlineSize,
}) : _urlByShortcode = _referencedUrls(palette, content);
/// Excluded from link labels: this component renders a [WidgetSpan], and a
/// placeholder nested inside the link's own placeholder does not paint on
/// iOS — an authored `[:emoji:](url)` renders as nothing. Link resolution
/// wins over token detection inside a label.
@override
Set<MarkdownScope> get scopes => MarkdownComponent.allScopesExceptLinkLabel;
// Look ahead so adjacent tokens sharing a colon are both considered:
// :unknown:known: must still allow the known token to match.
static final _shortcodeScan = RegExp(
r'(?=:([a-z0-9_-]+):)',
caseSensitive: false,
);
static Map<String, String> _referencedUrls(
List<CustomEmoji> palette,
String content,
) {
final referenced = {
for (final match in _shortcodeScan.allMatches(content))
match.group(1)!.toLowerCase(),
};
if (referenced.isEmpty) return const {};
return {
for (final emoji in palette)
if (referenced.contains(emoji.shortcode)) emoji.shortcode: emoji.url,
};
}
@override
RegExp get exp => _exp;
@override
InlineSpan span(BuildContext context, String text, GptMarkdownConfig config) {
final raw = exp.firstMatch(text.trim())?.group(0);
if (raw == null) {
return TextSpan(text: text, style: config.style);
}
final shortcode = raw.substring(1, raw.length - 1).toLowerCase();
final url = _urlByShortcode[shortcode];
if (url == null) {
return TextSpan(text: text, style: config.style);
}
return WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: CustomEmojiImage(shortcode: shortcode, url: url, size: size),
);
}
/// Build a regex matching `:shortcode:` for any known shortcode, longest
/// first so a longer name isn't shadowed by a shorter prefix. Matches nothing
/// when the palette is empty (a regex that can never match).
static RegExp _buildPattern(Iterable<String> shortcodes) {
final sorted = shortcodes.where((s) => s.trim().isNotEmpty).toSet().toList()
..sort((a, b) => b.length.compareTo(a.length));
if (sorted.isEmpty) {
// Never matches — gpt_markdown skips this component entirely.
return RegExp(r'(?!x)x');
}
final alternatives = sorted.map(RegExp.escape).join('|');
return RegExp(':(?:$alternatives):', caseSensitive: false);
}
}