Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(talk_app): Implement swipe-to-reply #2612

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/neon_framework/packages/talk_app/lib/src/pages/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:intl/intl.dart';
import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/utils.dart';
import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:talk_app/src/blocs/room.dart';
import 'package:talk_app/src/theme.dart';
import 'package:talk_app/src/utils/helpers.dart';
Expand Down Expand Up @@ -124,6 +123,19 @@ class _TalkRoomPageState extends State<TalkRoomPage> {
previousChatMessage: previousMessage,
);

if (canReplyToMessage(room, message)) {
child = Dismissible(
key: Key(message.id.toString()),
confirmDismiss: (_) async {
bloc.setReplyChatMessage(message);

// We don't use the real dismiss feature as we don't want the widget to be removed from the list
return false;
},
child: child,
);
}

if (previousMessage == null ||
(tz.local.translate(previousMessage.timestamp * 1000) ~/ _millisecondsPerDay) !=
(tz.local.translate(message.timestamp * 1000) ~/ _millisecondsPerDay)) {
Expand Down Expand Up @@ -176,10 +188,7 @@ class _TalkRoomPageState extends State<TalkRoomPage> {
),
);

if (room.readOnly == 0 &&
spreed.ParticipantPermission.values
.byBinary(room.permissions)
.contains(spreed.ParticipantPermission.canSendMessageAndShareAndReact)) {
if (canSendMessageAndShareAndReact(room)) {
body = Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,30 @@ bool hasFeature(BuildContext context, String feature) {

return capabilities.features.contains(feature);
}

/// Checks whether the user is allowed to send chat messages in a [room].
bool canSendMessageAndShareAndReact(spreed.Room room) {
return room.readOnly == 0 &&
spreed.ParticipantPermission.values
.byBinary(room.permissions)
.contains(spreed.ParticipantPermission.canSendMessageAndShareAndReact);
}

/// Checks whether the user is allowed to reply to a [chatMessage] in a [room].
bool canReplyToMessage(spreed.Room room, spreed.$ChatMessageInterface chatMessage) {
return canSendMessageAndShareAndReact(room) &&
chatMessage.messageType != spreed.MessageType.commentDeleted &&
chatMessage.isReplyable;
}

/// Checks whether the user is allowed to edit a [chatMessage] in a [room].
bool canEditMessage(BuildContext context, spreed.Room room, spreed.$ChatMessageInterface chatMessage) {
return chatMessage.messageType != spreed.MessageType.commentDeleted &&
chatMessage.actorId == room.actorId &&
hasFeature(context, 'edit-messages');
}

/// Checks whether the user is allowed to delete a [chatMessage] in a [room].
bool canDeleteMessage(spreed.Room room, spreed.$ChatMessageInterface chatMessage) {
return chatMessage.messageType != spreed.MessageType.commentDeleted && chatMessage.actorId == room.actorId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -517,15 +517,8 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
spreed.Room room,
spreed.$ChatMessageInterface chatMessage,
) {
final readOnly = room.readOnly == 1;
final permissions = spreed.ParticipantPermission.values.byBinary(room.permissions);
final hasChatPermission = permissions.contains(spreed.ParticipantPermission.canSendMessageAndShareAndReact);

return [
if (widget.chatMessage.messageType != spreed.MessageType.commentDeleted &&
chatMessage.isReplyable &&
!readOnly &&
hasChatPermission)
if (canReplyToMessage(room, chatMessage))
(
icon: const Icon(Icons.add_reaction_outlined),
child: Text(TalkLocalizations.of(context).roomMessageReaction),
Expand All @@ -546,18 +539,13 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
NeonProvider.of<TalkRoomBloc>(context).addReaction(chatMessage, reaction);
},
),
if (widget.chatMessage.messageType != spreed.MessageType.commentDeleted &&
chatMessage.isReplyable &&
!readOnly &&
hasChatPermission)
if (canReplyToMessage(room, chatMessage))
(
icon: const Icon(Icons.reply),
child: Text(TalkLocalizations.of(context).roomMessageReply),
onPressed: () => NeonProvider.of<TalkRoomBloc>(context).setReplyChatMessage(chatMessage),
),
if (chatMessage.messageType != spreed.MessageType.commentDeleted &&
chatMessage.actorId == room.actorId &&
hasFeature(context, 'edit-messages'))
if (canEditMessage(context, room, chatMessage))
(
icon: const Icon(Icons.edit),
child: Text(TalkLocalizations.of(context).roomMessageEdit),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:talk_app/l10n/localizations.dart';
import 'package:talk_app/src/blocs/room.dart';
import 'package:talk_app/src/utils/helpers.dart';
import 'package:talk_app/src/widgets/reactions_overview_dialog.dart';

/// Widget for displaying the current reactions on a chat message including the ability to add and remove reactions.
Expand All @@ -26,10 +27,7 @@ class TalkReactions extends StatelessWidget {
Widget build(BuildContext context) {
final bloc = NeonProvider.of<TalkRoomBloc>(context);

final canUpdateReactions = room.readOnly == 0 &&
spreed.ParticipantPermission.values
.byBinary(room.permissions)
.contains(spreed.ParticipantPermission.canSendMessageAndShareAndReact);
final canUpdateReactions = canSendMessageAndShareAndReact(room);

const shape = RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(50)),
Expand Down
104 changes: 104 additions & 0 deletions packages/neon_framework/packages/talk_app/test/room_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,108 @@ void main() {
expect(find.byType(TalkMessageInput), findsNothing);
await expectLater(find.byType(TestApp), matchesGoldenFile('goldens/room_page_no_chat_permission.png'));
});

group('Swipe-to-reply', () {
late spreed.ChatMessageWithParent chatMessage;

setUp(() {
chatMessage = MockChatMessageWithParent();
when(() => chatMessage.timestamp).thenReturn(0);
when(() => chatMessage.actorId).thenReturn('test');
when(() => chatMessage.actorType).thenReturn(spreed.ActorType.users);
when(() => chatMessage.actorDisplayName).thenReturn('test');
when(() => chatMessage.messageType).thenReturn(spreed.MessageType.comment);
when(() => chatMessage.message).thenReturn('abc');
when(() => chatMessage.reactions).thenReturn(BuiltMap());
when(() => chatMessage.messageParameters).thenReturn(BuiltMap());
when(() => chatMessage.id).thenReturn(0);
when(() => chatMessage.isReplyable).thenReturn(true);

when(() => bloc.messages).thenAnswer(
(_) => BehaviorSubject.seeded(
Result.success(
BuiltList<spreed.ChatMessageWithParent>([
chatMessage,
]),
),
),
);

when(() => bloc.reactions).thenAnswer((_) => BehaviorSubject.seeded(BuiltMap()));
});

testWidgets('Allowed', (tester) async {
final account = MockAccount();

await tester.pumpWidgetWithAccessibility(
TestApp(
localizationsDelegates: TalkLocalizations.localizationsDelegates,
supportedLocales: TalkLocalizations.supportedLocales,
appThemes: const [
TalkTheme(),
],
providers: [
Provider<Account>.value(value: account),
NeonProvider<TalkRoomBloc>.value(value: bloc),
NeonProvider<ReferencesBloc>.value(value: referencesBloc),
],
child: const TalkRoomPage(),
),
);

expect(find.byType(Dismissible), findsOne);

await tester.drag(find.byType(TalkCommentMessage), const Offset(1000, 0));
await tester.pumpAndSettle();
verify(() => bloc.setReplyChatMessage(chatMessage)).called(1);
});

testWidgets('Read-only', (tester) async {
when(() => room.readOnly).thenReturn(1);

final account = MockAccount();

await tester.pumpWidgetWithAccessibility(
TestApp(
localizationsDelegates: TalkLocalizations.localizationsDelegates,
supportedLocales: TalkLocalizations.supportedLocales,
appThemes: const [
TalkTheme(),
],
providers: [
Provider<Account>.value(value: account),
NeonProvider<TalkRoomBloc>.value(value: bloc),
NeonProvider<ReferencesBloc>.value(value: referencesBloc),
],
child: const TalkRoomPage(),
),
);

expect(find.byType(Dismissible), findsNothing);
});

testWidgets('No permission', (tester) async {
when(() => room.permissions).thenReturn(0);

final account = MockAccount();

await tester.pumpWidgetWithAccessibility(
TestApp(
localizationsDelegates: TalkLocalizations.localizationsDelegates,
supportedLocales: TalkLocalizations.supportedLocales,
appThemes: const [
TalkTheme(),
],
providers: [
Provider<Account>.value(value: account),
NeonProvider<TalkRoomBloc>.value(value: bloc),
NeonProvider<ReferencesBloc>.value(value: referencesBloc),
],
child: const TalkRoomPage(),
),
);

expect(find.byType(Dismissible), findsNothing);
});
});
}