Skip to content

Commit dd00e9c

Browse files
committed
Fix message sheet reaction toggles and dismissal guards
Signed-off-by: kenny lopez <[email protected]>
1 parent 3ae2829 commit dd00e9c

6 files changed

Lines changed: 229 additions & 29 deletions

File tree

‎mobile/ios/Runner/NativeReactionDetails.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ final class NativeReactionDetailsViewController: UIViewController, UITableViewDa
244244
}
245245

246246
func updateReactions(_ reactions: [[String: Any]]) {
247+
guard !reactions.isEmpty else { onClose?(); return }
247248
self.reactions = reactions
248249
rebuildRows()
249250
guard isViewLoaded else { return }

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

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import '../../shared/custom_emoji/custom_emoji.dart';
1919
import '../../shared/custom_emoji/custom_emoji_provider.dart';
2020
import '../../shared/custom_emoji/custom_emoji_render.dart';
2121
import '../../shared/emoji/native_emoji_glyph.dart';
22+
import '../../shared/emoji/emoji_data_provider.dart';
2223
import '../../shared/widgets/sheet_action_section.dart';
2324
import '../../shared/widgets/modal_presentation.dart';
2425
import '../../shared/reminders/remind_me_later_sheet.dart';
@@ -42,6 +43,22 @@ part 'message_actions/quick_reaction_row.dart';
4243
/// `msg.body.slice(0, 100)`.
4344
const _reminderPreviewLength = 100;
4445

46+
// A weak, route-owned latch shared by every action, including pointers that
47+
// were already down when another selection started dismissing the sheet.
48+
final _selectedMessageActionRoutes = Expando<bool>();
49+
50+
bool _selectMessageAction(BuildContext context) {
51+
final route = ModalRoute.of(context);
52+
if (route == null ||
53+
!route.isCurrent ||
54+
_selectedMessageActionRoutes[route] == true) {
55+
return false;
56+
}
57+
_selectedMessageActionRoutes[route] = true;
58+
Navigator.of(context).pop(true);
59+
return true;
60+
}
61+
4562
/// Presents message reactions and grouped actions in the shared bottom sheet.
4663
/// Snapshot and preview callbacks are not used by the sheet presentation.
4764
Future<void> showMessageActions({
@@ -133,7 +150,7 @@ Future<void> showMessageActions({
133150
leading: const Icon(BuzzIcons.copy),
134151
title: const Text('Copy text'),
135152
onTap: () {
136-
Navigator.of(sheetContext).pop(true);
153+
if (!_selectMessageAction(sheetContext)) return;
137154
// Copy to clipboard
138155
final data = ClipboardData(text: message.content);
139156
Clipboard.setData(data);
@@ -149,7 +166,7 @@ Future<void> showMessageActions({
149166
leading: const Icon(BuzzIcons.pencil),
150167
title: const Text('Edit message'),
151168
onTap: () {
152-
Navigator.of(sheetContext).pop(true);
169+
if (!_selectMessageAction(sheetContext)) return;
153170
_showEditSheet(
154171
context: context,
155172
ref: ref,
@@ -168,7 +185,7 @@ Future<void> showMessageActions({
168185
style: TextStyle(color: sheetContext.colors.error),
169186
),
170187
onTap: () {
171-
Navigator.of(sheetContext).pop(true);
188+
if (!_selectMessageAction(sheetContext)) return;
172189
_confirmDelete(
173190
context: context,
174191
ref: ref,
@@ -495,7 +512,7 @@ class _MarkReadUnreadTile extends ConsumerWidget {
495512
leading: Icon(unread ? BuzzIcons.mailCheck : BuzzIcons.mailOpen),
496513
title: Text(unread ? 'Mark read' : 'Mark unread'),
497514
onTap: () {
498-
Navigator.of(context).pop(true);
515+
if (!_selectMessageAction(context)) return;
499516
final notifier = ref.read(readStateProvider.notifier);
500517
if (unread) {
501518
// Advances the `msg:` marker and clears this message's own forced
@@ -536,7 +553,7 @@ class _FollowThreadTile extends ConsumerWidget {
536553
leading: Icon(following ? BuzzIcons.bellOff : BuzzIcons.bellRing),
537554
title: Text(following ? 'Unfollow thread' : 'Follow thread'),
538555
onTap: () {
539-
Navigator.of(context).pop(true);
556+
if (!_selectMessageAction(context)) return;
540557
final notifier = ref.read(threadFollowsProvider.notifier);
541558
if (following) {
542559
notifier.unfollowThread(rootId);
@@ -581,7 +598,7 @@ class _MessageQuickActionsRow extends ConsumerWidget {
581598
icon: BuzzIcons.messageSquareReply,
582599
label: 'Reply',
583600
onTap: () {
584-
Navigator.of(context).pop(true);
601+
if (!_selectMessageAction(context)) return;
585602
Navigator.of(pageContext).push(
586603
MaterialPageRoute<void>(
587604
builder: (_) => ThreadDetailPage(
@@ -600,7 +617,7 @@ class _MessageQuickActionsRow extends ConsumerWidget {
600617
icon: BuzzIcons.link2,
601618
label: 'Copy link',
602619
onTap: () {
603-
Navigator.of(context).pop(true);
620+
if (!_selectMessageAction(context)) return;
604621
copyToClipboard(
605622
pageContext,
606623
messageLinkFor(message: message, channelId: channelId),
@@ -617,7 +634,7 @@ class _MessageQuickActionsRow extends ConsumerWidget {
617634
context,
618635
rootNavigator: true,
619636
).context;
620-
Navigator.of(context).pop(true);
637+
if (!_selectMessageAction(context)) return;
621638
showRemindMeLaterSheet(
622639
context: rootContext,
623640
ref: ref,
@@ -731,29 +748,48 @@ class _QuickReactionCircle extends StatelessWidget {
731748
final VoidCallback onTap;
732749
final Widget child;
733750
final double size;
751+
final String label;
752+
final bool? selected;
734753

735754
const _QuickReactionCircle({
736755
required this.onTap,
737756
required this.child,
738757
required this.size,
758+
required this.label,
759+
this.selected,
739760
});
740761

741762
@override
742763
Widget build(BuildContext context) {
743-
return GestureDetector(
744-
onTap: () {
745-
unawaited(HapticFeedback.lightImpact());
746-
onTap();
747-
},
748-
child: Container(
749-
width: size,
750-
height: size,
751-
alignment: Alignment.center,
752-
decoration: BoxDecoration(
753-
color: context.colors.surfaceContainerHighest,
754-
shape: BoxShape.circle,
764+
return Semantics(
765+
button: true,
766+
label: label,
767+
selected: selected,
768+
excludeSemantics: true,
769+
onTap: onTap,
770+
child: GestureDetector(
771+
excludeFromSemantics: true,
772+
onTap: () {
773+
unawaited(HapticFeedback.lightImpact());
774+
onTap();
775+
},
776+
child: Container(
777+
width: size,
778+
height: size,
779+
alignment: Alignment.center,
780+
decoration: BoxDecoration(
781+
color: selected == true
782+
? context.colors.primary.withValues(alpha: 0.10)
783+
: context.colors.surfaceContainerHighest,
784+
border: selected == true
785+
? Border.all(
786+
color: context.colors.primary.withValues(alpha: 0.40),
787+
)
788+
: null,
789+
shape: BoxShape.circle,
790+
),
791+
child: child,
755792
),
756-
child: child,
757793
),
758794
);
759795
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class _QuickReactionRow extends ConsumerWidget {
2323

2424
@override
2525
Widget build(BuildContext context, WidgetRef ref) {
26+
final dataset = ref.watch(emojiDatasetOrEmptyProvider);
2627
final customEmoji = ref.watch(customEmojiListProvider);
2728
final emoji = quickReactionEmoji(
2829
ref.watch(recentEmojiProvider),
@@ -35,6 +36,12 @@ class _QuickReactionRow extends ConsumerWidget {
3536
};
3637

3738
void react(String value) {
39+
if (message.reactions.any(
40+
(r) => r.emoji == value && r.reactedByCurrentUser,
41+
)) {
42+
toggleReaction(pageRef, message, value);
43+
return;
44+
}
3845
// The generic picker is also used for composing and statuses. Record
3946
// recency here, at the reaction call site, so only reactions drive the
4047
// quick-reaction row.
@@ -65,8 +72,12 @@ class _QuickReactionRow extends ConsumerWidget {
6572
key: ValueKey('quick-reaction-${emoji[index]}'),
6673
child: _QuickReactionCircle(
6774
size: circleSize,
75+
label: dataset.displayName(emoji[index]),
76+
selected: message.reactions.any(
77+
(r) => r.emoji == emoji[index] && r.reactedByCurrentUser,
78+
),
6879
onTap: () {
69-
Navigator.of(sheetContext).pop(true);
80+
if (!_selectMessageAction(context)) return;
7081
react(emoji[index]);
7182
},
7283
child: _QuickReactionGlyph(
@@ -79,8 +90,9 @@ class _QuickReactionRow extends ConsumerWidget {
7990
key: const ValueKey('quick-reaction-more'),
8091
child: _QuickReactionCircle(
8192
size: circleSize,
93+
label: 'Add reaction',
8294
onTap: () {
83-
Navigator.of(sheetContext).pop(true);
95+
if (!_selectMessageAction(context)) return;
8496
showEmojiPicker(context: pageContext, onSelect: react);
8597
},
8698
child: Icon(

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ const _pillGlyphSize = 16.0;
4949
/// Used by channel detail, thread detail, and system message rows to avoid
5050
/// duplicating the toggle wiring.
5151
void toggleReaction(WidgetRef ref, TimelineMessage message, String emoji) {
52+
final reaction = message.reactions.where((r) => r.emoji == emoji).firstOrNull;
53+
if (reaction == null) return;
5254
final actions = ref.read(channelActionsProvider);
53-
final reaction = message.reactions.firstWhere((r) => r.emoji == emoji);
5455
if (reaction.reactedByCurrentUser && reaction.currentUserReactionId != null) {
5556
actions.removeReaction(reaction.currentUserReactionId!, emoji);
5657
} else {
@@ -383,11 +384,22 @@ Future<void> showReactionDetailSheet({
383384
showDragHandle: true,
384385
builder: (_) => ValueListenableBuilder<List<TimelineReaction>>(
385386
valueListenable: reactions,
386-
builder: (_, value, _) => _ReactionDetailSheet(
387-
channelId: channelId,
388-
reactions: value,
389-
onToggle: onToggle,
390-
),
387+
builder: (sheetContext, value, _) {
388+
if (value.isEmpty) {
389+
WidgetsBinding.instance.addPostFrameCallback((_) {
390+
if (sheetContext.mounted &&
391+
ModalRoute.of(sheetContext)?.isCurrent == true) {
392+
Navigator.of(sheetContext).pop();
393+
}
394+
});
395+
return const SizedBox.shrink();
396+
}
397+
return _ReactionDetailSheet(
398+
channelId: channelId,
399+
reactions: value,
400+
onToggle: onToggle,
401+
);
402+
},
391403
),
392404
);
393405
}

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ TimelineMessage _message({
3030
bool isSystem = false,
3131
String? rootId,
3232
List<List<String>> tags = const [],
33+
List<TimelineReaction> reactions = const [],
3334
}) => TimelineMessage(
3435
id: id,
3536
pubkey: pubkey,
@@ -38,8 +39,22 @@ TimelineMessage _message({
3839
isSystem: isSystem,
3940
rootId: rootId,
4041
tags: tags,
42+
reactions: reactions,
4143
);
4244

45+
class _RecordingReactionActions implements ChannelActions {
46+
final added = <String>[];
47+
final removed = <String>[];
48+
@override
49+
Future<void> addReaction(String eventId, String emoji) async =>
50+
added.add(emoji);
51+
@override
52+
Future<void> removeReaction(String eventId, String emoji) async =>
53+
removed.add(eventId);
54+
@override
55+
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
56+
}
57+
4358
final _listedChannel = Channel(
4459
id: _channelId,
4560
name: 'general',
@@ -379,6 +394,73 @@ Future<_MessageActionsPopoverHarness> _pumpMessageActionsPopover(
379394
}
380395

381396
void main() {
397+
testWidgets(
398+
'quick reactions show selection, label controls, and remove our reaction',
399+
(tester) async {
400+
final semantics = tester.ensureSemantics();
401+
final actions = _RecordingReactionActions();
402+
await _pumpMessageActionsPopover(
403+
tester,
404+
message: _message(
405+
reactions: [
406+
const TimelineReaction(
407+
emoji: '👍',
408+
count: 1,
409+
reactedByCurrentUser: true,
410+
userPubkeys: ['self'],
411+
currentUserReactionId: 'our-reaction',
412+
),
413+
],
414+
),
415+
prefs: await _mockPrefs(),
416+
createChannelActions: (_) => actions,
417+
);
418+
final emoji = find.byKey(const ValueKey('quick-reaction-👍'));
419+
final owner = tester.widget<Semantics>(
420+
find.descendant(of: emoji, matching: find.byType(Semantics)).first,
421+
);
422+
expect(owner.properties.button, isTrue);
423+
expect(owner.properties.selected, isTrue);
424+
expect(owner.properties.label, isNotEmpty);
425+
expect(owner.properties.onTap, isNotNull);
426+
expect(find.bySemanticsLabel('Add reaction'), findsOneWidget);
427+
await tester.tap(emoji);
428+
await tester.pumpAndSettle();
429+
expect(actions.removed, ['our-reaction']);
430+
expect(actions.added, isEmpty);
431+
semantics.dispose();
432+
},
433+
);
434+
435+
testWidgets('two active pointers select only one sheet action', (
436+
tester,
437+
) async {
438+
final actions = _RecordingReactionActions();
439+
await _pumpMessageActionsPopover(
440+
tester,
441+
message: _message(),
442+
prefs: await _mockPrefs(),
443+
launcherOnNestedRoute: true,
444+
createChannelActions: (_) => actions,
445+
);
446+
final first = await tester.startGesture(
447+
tester.getCenter(find.byKey(const ValueKey('quick-reaction-👍'))),
448+
pointer: 1,
449+
);
450+
final second = await tester.startGesture(
451+
tester.getCenter(find.text('Copy text')),
452+
pointer: 2,
453+
);
454+
await first.up();
455+
await second.up();
456+
await tester.pumpAndSettle();
457+
expect(actions.added, ['👍']);
458+
expect(
459+
find.byKey(const ValueKey('message-actions-underlying-page')),
460+
findsOneWidget,
461+
);
462+
});
463+
382464
// Some sheets build the app lifecycle, which listens for network changes.
383465
// That listen is asynchronous, so a missing plugin fails whichever test
384466
// happens to be running. Flutter documents mock handlers as cleared after

0 commit comments

Comments
 (0)