Skip to content

Commit 51b0ea6

Browse files
browstudio
andauthored
fix(mobile): skip the emoji picker when its opener is gone (#9820)
On iOS, opening the emoji picker from a context that had already been unmounted threw `StateError('No ProviderScope found')` from `ProviderScope.containerOf`. It threw after the picker had taken its one-sheet-at-a-time guard, so every later emoji picker open in that session was silently refused until the app restarted. The message actions sheet hits this: its "+" tile opens the picker on the long-pressed message row's context, which the timeline can rebuild away while the sheet is open (the reported crash came from a phone in landscape, tapping "+" in the quick reaction row). `showEmojiPicker` now returns early, completing the caller's `onDismiss`, when its context is no longer mounted, and the guard is never taken. That tap still opens no picker (the message usually stays on screen, but its row was rebuilt), so the user has to retry, but later opens keep working. ### Validation - Added [an iOS picker test](https://github.com/block/buzz/blob/fix-ios-emoji-picker-dead-context/mobile/test/features/channels/emoji_picker_test.dart) that opens the picker from a rebuilt-away context, then checks the caller's dismissal runs and a later open from a live context still presents the native sheet. - Ran the fixed build in an iPhone 17 Pro simulator against a live community: long-pressed a message, tapped "+" in the quick reaction row, picked an emoji from the native sheet and saw the reaction land; then repeated on a second message to confirm the picker opens again (with the first pick under Frequently used). The simulator can't reproduce the rebuilt-row race itself; the added test covers that. ### Regressions None expected. The only behavior change is for an opener whose widget is already gone, which previously threw and blocked the picker. Mounted callers take exactly the same path as before. Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
1 parent eae6948 commit 51b0ea6

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎mobile/lib/shared/emoji/emoji_picker.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ void showEmojiPicker({
4343
VoidCallback? onDismiss,
4444
bool nativeEmojiOnly = false,
4545
}) {
46+
// A caller can open the picker from a context that outlived its widget, such
47+
// as the message row behind the actions sheet after the timeline rebuilt it.
48+
// There is nothing left to present on or react for, so complete the caller's
49+
// lifecycle instead of throwing from the ancestor lookups below.
50+
if (!context.mounted) {
51+
onDismiss?.call();
52+
return;
53+
}
4654
if (defaultTargetPlatform == TargetPlatform.iOS) {
4755
unawaited(
4856
_presentIosEmojiPicker(

‎mobile/test/features/channels/emoji_picker_test.dart‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,90 @@ void main() {
11861186
},
11871187
);
11881188

1189+
testWidgets(
1190+
'an open from an unmounted context does not block later opens',
1191+
(tester) async {
1192+
final previousPlatform = debugDefaultTargetPlatformOverride;
1193+
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
1194+
final prefs = await _prefs();
1195+
var presents = 0;
1196+
_setMockNativeEmojiPickerHandler((call) async {
1197+
if (call.method == 'present') presents += 1;
1198+
return true;
1199+
});
1200+
1201+
final showStaleRow = ValueNotifier(true);
1202+
late BuildContext staleContext;
1203+
var staleDismissals = 0;
1204+
1205+
try {
1206+
await tester.pumpWidget(
1207+
ProviderScope(
1208+
overrides: [
1209+
savedPrefsProvider.overrideWithValue(prefs),
1210+
myPubkeyProvider.overrideWithValue('self'),
1211+
customEmojiPaletteProvider.overrideWith(
1212+
() => _FakeCustomEmojiPaletteNotifier(
1213+
Future.value(_customEmoji),
1214+
),
1215+
),
1216+
],
1217+
child: MaterialApp(
1218+
theme: AppTheme.light(),
1219+
home: Scaffold(
1220+
body: ValueListenableBuilder<bool>(
1221+
valueListenable: showStaleRow,
1222+
builder: (context, show, _) => Column(
1223+
children: [
1224+
if (show)
1225+
Builder(
1226+
builder: (rowContext) {
1227+
staleContext = rowContext;
1228+
return const SizedBox.shrink();
1229+
},
1230+
),
1231+
FilledButton(
1232+
onPressed: () => showEmojiPicker(
1233+
context: context,
1234+
onSelect: (_) {},
1235+
),
1236+
child: const Text('Open live'),
1237+
),
1238+
],
1239+
),
1240+
),
1241+
),
1242+
),
1243+
),
1244+
);
1245+
1246+
// The message row an actions sheet captured is rebuilt away before
1247+
// its "+" opens the picker.
1248+
showStaleRow.value = false;
1249+
await tester.pumpAndSettle();
1250+
expect(staleContext.mounted, isFalse);
1251+
1252+
showEmojiPicker(
1253+
context: staleContext,
1254+
onSelect: (_) {},
1255+
onDismiss: () => staleDismissals += 1,
1256+
);
1257+
await tester.pumpAndSettle();
1258+
expect(staleDismissals, 1);
1259+
expect(presents, 0);
1260+
1261+
await tester.tap(find.text('Open live'));
1262+
await tester.pumpAndSettle();
1263+
expect(presents, 1);
1264+
await _sendNativeEmojiPickerCall(tester, 'dismissed');
1265+
} finally {
1266+
_setMockNativeEmojiPickerHandler(null);
1267+
debugDefaultTargetPlatformOverride = previousPlatform;
1268+
showStaleRow.dispose();
1269+
}
1270+
},
1271+
);
1272+
11891273
testWidgets('a reentrant open cannot steal the live sheet callbacks', (
11901274
tester,
11911275
) async {

0 commit comments

Comments
 (0)