Skip to content

Commit 6dbd159

Browse files
browstudio
andauthored
fix(mobile): keep "+" reactions working when a new message rebuilds the row (#9831)
## Summary A new message shifts the timeline and can unmount the long-pressed message row while its actions sheet or emoji picker is open. Then: - tapping "+" in the actions sheet did nothing, because the picker opened from the row's dead context (after [#9820 emoji picker crash](#9820), it is a silent no-op instead of a crash), and - a pick made after the row went away read through the row's disposed `WidgetRef`, so the reaction was dropped. The actions sheet and the timeline "+" pill now capture the page's `ProviderContainer` and its navigator's context when they open. The picker opens from the navigator, and reaction dispatch reads through the container. `toggleReaction` and `armReactionBurst` take a container, and grouped system rows read through one too. The reactions tray's Add button keeps working after the row is gone, since its callback holds the container and navigator it captured at build time. Reactions are also bound to the community that was active when the surface opened: the actions sheet and the reaction details tray (Flutter sheet or native iOS tray) capture it when they open, and the timeline "+" pill when it is tapped. If a notification switches community meanwhile, a quick tile, picker pick, tray toggle or tray Add is dropped instead of being recorded, animated or published as the newly active account. `ChannelActions.isCommunityValid` now uses the same `RelayConfig.isSameCommunity` comparison. Failure notices for these reactions show from the captured navigator's context, so they still appear when the row is gone. ## Regressions None expected. The picker attaches to the same navigator as before (the row's nearest navigator), and the providers read are the same ones, from the same `ProviderScope`. ## Validation - New regression tests in `message_actions_row_rebuild_tests.dart`: the row unmounts before "+" is tapped, and while the native picker is open. Both failed on main (picker never presented, pick lost) and pass with the fix. Two more switch community while the sheet is open, then tap a quick tile or pick from "+", and check nothing is added, recorded or armed. - New test in the reaction row combined tests: the row unmounts while the native reactions tray is open, and its Add still opens the picker. Another switches community while the tray is open and checks that a tray toggle and Add are both dropped. - Automated simulator app test (iOS 27 sim): opened a channel, long-pressed a message, tapped "+", picked an emoji in the native picker, and saw the reaction land. Then toggled it off from the reactions sheet. Live edits and reactions from another client arrived during the run without issue. --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
1 parent 2b44717 commit 6dbd159

15 files changed

Lines changed: 643 additions & 128 deletions

File tree

‎mobile/lib/features/channels/channel_detail_page/message_bubble.dart‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class _MessageBubble extends HookConsumerWidget {
3939

4040
@override
4141
Widget build(BuildContext context, WidgetRef ref) {
42+
final container = ProviderScope.containerOf(context, listen: false);
4243
final startsThreads = ref.watch(
4344
channelStartsThreadsProvider(currentChannelId),
4445
);
@@ -220,17 +221,15 @@ class _MessageBubble extends HookConsumerWidget {
220221
reactions: message.reactions,
221222
onToggle: (emoji) => toggleReaction(
222223
context,
223-
ref,
224+
container,
224225
message,
225226
emoji,
226227
),
227228
showAddButton: isMember && !isArchived,
228-
onAddReaction: () =>
229-
showAddReactionPicker(
230-
context: context,
231-
ref: ref,
232-
message: message,
233-
),
229+
onAddReaction: addReactionPickerFor(
230+
context: context,
231+
message: message,
232+
),
234233
),
235234
),
236235
),

‎mobile/lib/features/channels/channel_detail_page/system_rows.dart‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,13 @@ class _SystemMessageRow extends HookConsumerWidget {
8787
? message.reactions
8888
: _aggregateSystemMessageReactions(sourceMessages);
8989

90+
// Read through the page's container, and report failures from the page's
91+
// navigator: the actions sheet can toggle after a new message rebuilds
92+
// this row away.
93+
final container = ProviderScope.containerOf(context, listen: false);
94+
final pageContext = Navigator.of(context).context;
9095
void toggleGroupedReaction(String emoji) {
91-
final actions = ref.read(channelActionsProvider);
96+
final actions = container.read(channelActionsProvider);
9297
final reactedMessages = sourceMessages.where(
9398
(source) => source.reactions.any(
9499
(reaction) =>
@@ -99,14 +104,14 @@ class _SystemMessageRow extends HookConsumerWidget {
99104
);
100105
if (reactedMessages.isEmpty) {
101106
reportReactionFailure(
102-
context,
107+
pageContext,
103108
actions.addReaction(message.id, emoji, targetAuthor: message.pubkey),
104109
adding: true,
105110
);
106111
return;
107112
}
108113
reportReactionFailure(
109-
context,
114+
pageContext,
110115
Future.wait([
111116
for (final source in reactedMessages)
112117
actions.removeReaction(
@@ -290,12 +295,15 @@ class _SystemMessageRow extends HookConsumerWidget {
290295
messageId: message.id,
291296
channelId: channelId,
292297
reactions: reactions,
293-
onToggle: (emoji) =>
294-
toggleReaction(context, ref, message, emoji),
298+
onToggle: (emoji) => toggleReaction(
299+
context,
300+
container,
301+
message,
302+
emoji,
303+
),
295304
showAddButton: isMember && !isArchived,
296-
onAddReaction: () => showAddReactionPicker(
305+
onAddReaction: addReactionPickerFor(
297306
context: context,
298-
ref: ref,
299307
message: message,
300308
),
301309
),
@@ -323,7 +331,7 @@ class _SystemMessageRow extends HookConsumerWidget {
323331
reactions: reactions,
324332
onToggle: groupedMessages == null
325333
? (emoji) =>
326-
toggleReaction(context, ref, message, emoji)
334+
toggleReaction(context, container, message, emoji)
327335
: toggleGroupedReaction,
328336
),
329337
),

‎mobile/lib/features/channels/channel_management_actions.dart‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -715,10 +715,7 @@ final channelActionsProvider = Provider<ChannelActions>((ref) {
715715
nsec: relayConfig.nsec,
716716
),
717717
currentPubkey: currentPubkey,
718-
isCommunityValid: () {
719-
final currentConfig = ref.read(relayConfigProvider);
720-
return currentConfig.baseUrl == relayConfig.baseUrl &&
721-
currentConfig.nsec == relayConfig.nsec;
722-
},
718+
isCommunityValid: () =>
719+
ref.read(relayConfigProvider).isSameCommunity(relayConfig),
723720
);
724721
});

‎mobile/lib/features/channels/message_actions.dart‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ Future<void> showMessageActions({
8888
EdgeInsets popoverSpotlightPadding = const EdgeInsets.all(Grid.xxs),
8989
}) async {
9090
if (!context.mounted) return;
91+
// A new message can rebuild the long-pressed row away while the sheet is
92+
// up, so reactions go through the page rather than the row.
93+
final pageContainer = ProviderScope.containerOf(context, listen: false);
94+
final pickerContext = Navigator.of(context).context;
95+
final stillCurrent = captureReactionCommunity(pageContainer);
9196
unawaited(HapticFeedback.mediumImpact());
9297
final restoreFocusOnCancel = composerFocusNode?.hasFocus ?? false;
9398
composerFocusNode?.unfocus();
@@ -125,8 +130,9 @@ Future<void> showMessageActions({
125130
reactions: reactions ?? message.reactions,
126131
onToggleReaction: onToggleReaction,
127132
sheetContext: sheetContext,
128-
pageContext: context,
129-
pageRef: ref,
133+
pickerContext: pickerContext,
134+
pageContainer: pageContainer,
135+
stillCurrent: stillCurrent,
130136
),
131137
SizedBox(height: message.isSystem ? Grid.xxs : Grid.xs),
132138
if (!message.isSystem) ...[

‎mobile/lib/features/channels/message_actions/quick_reaction_row.dart‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,27 @@ class _QuickReactionRow extends ConsumerWidget {
88
/// The sheet's context, popped before the reaction fires.
99
final BuildContext sheetContext;
1010

11-
/// The long-pressed message's page context — survives the sheet pop, so the
12-
/// picker opened from "+" isn't torn down with the sheet.
13-
final BuildContext pageContext;
11+
/// The page navigator's context. The picker opened from "+" outlives both
12+
/// this sheet and the long-pressed row, which a new message can rebuild away.
13+
final BuildContext pickerContext;
1414

15-
/// The long-pressed message's page ref. The picker callback outlives this
16-
/// bottom sheet, so it must not read through the sheet's disposed ref.
17-
final WidgetRef pageRef;
15+
/// The page's container. Reactions outlive this sheet and the long-pressed
16+
/// row, so they must not read through either one's disposed ref.
17+
final ProviderContainer pageContainer;
18+
19+
/// Whether the community active when the sheet opened is still active. A
20+
/// notification can switch community while the sheet or its picker is up,
21+
/// and a reaction must not then go out as the other account.
22+
final bool Function() stillCurrent;
1823

1924
const _QuickReactionRow({
2025
required this.message,
2126
required this.reactions,
2227
this.onToggleReaction,
2328
required this.sheetContext,
24-
required this.pageContext,
25-
required this.pageRef,
29+
required this.pickerContext,
30+
required this.pageContainer,
31+
required this.stillCurrent,
2632
});
2733

2834
@override
@@ -40,27 +46,28 @@ class _QuickReactionRow extends ConsumerWidget {
4046
};
4147

4248
void react(String value) {
49+
if (!stillCurrent()) return;
4350
if (reactions.any((r) => r.emoji == value && r.reactedByCurrentUser)) {
4451
if (onToggleReaction != null) {
4552
onToggleReaction!(value);
4653
} else {
47-
toggleReaction(pageContext, pageRef, message, value);
54+
toggleReaction(pickerContext, pageContainer, message, value);
4855
}
4956
return;
5057
}
5158
// The generic picker is also used for composing and statuses. Record
5259
// recency here, at the reaction call site, so only reactions drive the
5360
// quick-reaction row.
54-
pageRef.read(recentEmojiProvider.notifier).record(value);
61+
pageContainer.read(recentEmojiProvider.notifier).record(value);
5562
// The sheet is on its way out, so the burst can't come from this tile —
5663
// hand it to the pill that's about to appear in the timeline.
57-
armReactionBurst(pageRef, message, value);
64+
armReactionBurst(pageContainer, message, value);
5865
if (onToggleReaction != null) {
5966
onToggleReaction!(value);
6067
} else {
6168
reportReactionFailure(
62-
pageContext,
63-
pageRef
69+
pickerContext,
70+
pageContainer
6471
.read(channelActionsProvider)
6572
.addReaction(message.id, value, targetAuthor: message.pubkey),
6673
adding: true,
@@ -109,7 +116,12 @@ class _QuickReactionRow extends ConsumerWidget {
109116
label: 'Add reaction',
110117
onTap: () {
111118
if (!_selectMessageAction(context)) return;
112-
showEmojiPicker(context: pageContext, onSelect: react);
119+
showEmojiPicker(
120+
context: pickerContext,
121+
onSelect: (value) {
122+
react(value);
123+
},
124+
);
113125
},
114126
child: Icon(
115127
BuzzIcons.plus,

‎mobile/lib/features/channels/reaction_row.dart‎

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,18 @@ const _pillGlyphSize = 16.0;
5555
/// [emoji], removes the reaction; otherwise adds it.
5656
///
5757
/// Used by channel detail, thread detail, and system message rows to avoid
58-
/// duplicating the toggle wiring.
58+
/// duplicating the toggle wiring. Takes the page's [ProviderContainer] rather
59+
/// than a row's ref, so a toggle from a sheet still lands after a new message
60+
/// rebuilds the row away.
5961
void toggleReaction(
6062
BuildContext context,
61-
WidgetRef ref,
63+
ProviderContainer container,
6264
TimelineMessage message,
6365
String emoji,
6466
) {
6567
final reaction = message.reactions.where((r) => r.emoji == emoji).firstOrNull;
6668
if (reaction == null) return;
67-
final actions = ref.read(channelActionsProvider);
69+
final actions = container.read(channelActionsProvider);
6870
if (reaction.reactedByCurrentUser && reaction.currentUserReactionId != null) {
6971
reportReactionFailure(
7072
context,
@@ -74,7 +76,7 @@ void toggleReaction(
7476
} else {
7577
// Only adding counts as a use — removing a reaction shouldn't promote the
7678
// emoji in the frequently-used ranking.
77-
ref.read(recentEmojiProvider.notifier).record(emoji);
79+
container.read(recentEmojiProvider.notifier).record(emoji);
7880
reportReactionFailure(
7981
context,
8082
actions.addReaction(message.id, emoji, targetAuthor: message.pubkey),
@@ -152,38 +154,63 @@ String? _reactionFailureMessage(Object error, {required bool adding}) {
152154
/// Pill taps don't go through here: they burst immediately, because the pill is
153155
/// already on screen. Adding a reaction you already have is a no-op on the
154156
/// relay, so it shouldn't celebrate either.
155-
void armReactionBurst(WidgetRef ref, TimelineMessage message, String emoji) {
157+
void armReactionBurst(
158+
ProviderContainer container,
159+
TimelineMessage message,
160+
String emoji,
161+
) {
156162
final alreadyReacted = message.reactions.any(
157163
(reaction) => reaction.emoji == emoji && reaction.reactedByCurrentUser,
158164
);
159165
if (alreadyReacted) return;
160-
ref.read(pendingReactionBurstProvider.notifier).arm(message.id, emoji);
166+
container.read(pendingReactionBurstProvider.notifier).arm(message.id, emoji);
161167
}
162168

163-
/// Open the emoji picker and add whatever the user chooses as a reaction to
164-
/// [message].
169+
/// Returns a check that a reaction picked later still belongs to the community
170+
/// that was active now.
171+
///
172+
/// A notification can switch the active community while a picker is open. The
173+
/// picked reaction targets a message from the old one, so it is dropped rather
174+
/// than signed by the new account.
175+
bool Function() captureReactionCommunity(ProviderContainer container) {
176+
final opened = container.read(relayConfigProvider);
177+
return () => container.read(relayConfigProvider).isSameCommunity(opened);
178+
}
179+
180+
/// Returns the callback for a row's `+` that opens the emoji picker and adds
181+
/// whatever the user chooses as a reaction to [message].
165182
///
166183
/// Shared by the `+` pill in the channel timeline and in the thread view so the
167184
/// recency bookkeeping and burst arming can't drift apart between them.
168-
void showAddReactionPicker({
185+
///
186+
/// Call it while the row builds. A new message can rebuild the row away before
187+
/// the callback runs or while the picker is open, so it holds the page's
188+
/// container and navigator rather than the row's context.
189+
VoidCallback addReactionPickerFor({
169190
required BuildContext context,
170-
required WidgetRef ref,
171191
required TimelineMessage message,
172192
}) {
173-
showEmojiPicker(
174-
context: context,
175-
onSelect: (emoji) {
176-
ref.read(recentEmojiProvider.notifier).record(emoji);
177-
armReactionBurst(ref, message, emoji);
178-
reportReactionFailure(
179-
context,
180-
ref
181-
.read(channelActionsProvider)
182-
.addReaction(message.id, emoji, targetAuthor: message.pubkey),
183-
adding: true,
184-
);
185-
},
186-
);
193+
final container = ProviderScope.containerOf(context, listen: false);
194+
final navigator = Navigator.of(context);
195+
return () {
196+
if (!navigator.mounted) return;
197+
final stillCurrent = captureReactionCommunity(container);
198+
showEmojiPicker(
199+
context: navigator.context,
200+
onSelect: (emoji) {
201+
if (!stillCurrent()) return;
202+
container.read(recentEmojiProvider.notifier).record(emoji);
203+
armReactionBurst(container, message, emoji);
204+
reportReactionFailure(
205+
navigator.context,
206+
container
207+
.read(channelActionsProvider)
208+
.addReaction(message.id, emoji, targetAuthor: message.pubkey),
209+
adding: true,
210+
);
211+
},
212+
);
213+
};
187214
}
188215

189216
class ReactionRow extends HookWidget {
@@ -585,12 +612,27 @@ Future<void> showReactionDetailSheet({
585612
VoidCallback? onAddReaction,
586613
}) async {
587614
if (reactions.value.isEmpty) return;
615+
// A notification can switch community while the sheet is up (or while the
616+
// native tray dismisses); nothing picked here may go out as the new account.
617+
final stillCurrent = captureReactionCommunity(
618+
ProviderScope.containerOf(context, listen: false),
619+
);
620+
void guardedToggle(String emoji) {
621+
if (stillCurrent()) onToggle(emoji);
622+
}
623+
624+
final add = onAddReaction;
625+
final guardedAdd = add == null
626+
? null
627+
: () {
628+
if (stillCurrent()) add();
629+
};
588630
if (await _showNativeReactionDetails(
589631
context,
590632
channelId,
591633
reactions,
592-
onToggle,
593-
onAddReaction,
634+
guardedToggle,
635+
guardedAdd,
594636
)) {
595637
return;
596638
}
@@ -615,8 +657,8 @@ Future<void> showReactionDetailSheet({
615657
return _ReactionDetailSheet(
616658
channelId: channelId,
617659
reactions: value,
618-
onToggle: onToggle,
619-
onAddReaction: onAddReaction,
660+
onToggle: guardedToggle,
661+
onAddReaction: guardedAdd,
620662
);
621663
},
622664
),
@@ -1027,6 +1069,8 @@ Future<bool> _showNativeReactionDetails(
10271069
paletteSubscription.close();
10281070
}
10291071
});
1030-
if (addRequested && context.mounted) onAddReaction?.call();
1072+
// The row may be gone after the native tray's dismissal animation; the
1073+
// callback holds what it needs, so an Add the user tapped still opens.
1074+
if (addRequested) onAddReaction?.call();
10311075
return handled;
10321076
}

0 commit comments

Comments
 (0)