fluffychat/lib/pages/chat/events/message.dart

360 lines
14 KiB
Dart
Raw Normal View History

2021-10-26 18:50:34 +02:00
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
2022-06-28 16:51:29 +02:00
import 'package:swipe_to_action/swipe_to_action.dart';
2021-10-26 18:50:34 +02:00
2021-01-16 12:46:38 +01:00
import 'package:fluffychat/config/themes.dart';
2020-01-09 12:16:34 +01:00
import 'package:fluffychat/utils/date_time_extension.dart';
2020-01-18 13:22:22 +01:00
import 'package:fluffychat/utils/string_color.dart';
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/widgets/avatar.dart';
import 'package:fluffychat/widgets/matrix.dart';
import '../../../config/app_config.dart';
import 'message_content.dart';
import 'message_reactions.dart';
2021-11-09 21:32:16 +01:00
import 'reply_content.dart';
2020-01-01 19:10:13 +01:00
import 'state_message.dart';
import 'verification_request_content.dart';
2020-01-01 19:10:13 +01:00
class Message extends StatelessWidget {
final Event event;
2022-01-29 12:35:03 +01:00
final Event? nextEvent;
final void Function(Event)? onSelect;
final void Function(Event)? onAvatarTab;
final void Function(Event)? onInfoTab;
final void Function(String)? scrollToEventId;
final void Function(String) unfold;
2022-06-28 16:51:29 +02:00
final void Function(SwipeDirection) onSwipe;
final bool longPressSelect;
final bool selected;
2020-02-11 12:49:39 +01:00
final Timeline timeline;
2020-01-01 19:10:13 +01:00
2020-02-09 15:15:29 +01:00
const Message(this.event,
2020-02-11 12:49:39 +01:00
{this.nextEvent,
this.longPressSelect = false,
2020-02-11 12:49:39 +01:00
this.onSelect,
2021-11-13 13:06:36 +01:00
this.onInfoTab,
2020-04-08 12:38:52 +02:00
this.onAvatarTab,
2020-09-19 19:21:33 +02:00
this.scrollToEventId,
2022-06-28 16:51:29 +02:00
required this.onSwipe,
2022-01-29 12:35:03 +01:00
required this.unfold,
this.selected = false,
2022-01-29 12:35:03 +01:00
required this.timeline,
Key? key})
2021-10-14 18:09:30 +02:00
: super(key: key);
2020-01-01 19:10:13 +01:00
2020-06-20 11:46:12 +02:00
/// Indicates wheither the user may use a mouse instead
/// of touchscreen.
static bool useMouse = false;
2020-01-01 19:10:13 +01:00
@override
Widget build(BuildContext context) {
if (!{
EventTypes.Message,
EventTypes.Sticker,
EventTypes.Encrypted,
EventTypes.CallInvite
}.contains(event.type)) {
if (event.type.startsWith('m.call.')) {
return Container();
}
return StateMessage(event, unfold: unfold);
}
2020-01-01 19:10:13 +01:00
if (event.type == EventTypes.Message &&
event.messageType == EventTypes.KeyVerificationRequest) {
return VerificationRequestContent(event: event, timeline: timeline);
}
2021-04-14 10:37:15 +02:00
final client = Matrix.of(context).client;
2020-05-13 15:58:59 +02:00
final ownMessage = event.senderId == client.userID;
2021-04-14 10:37:15 +02:00
final alignment = ownMessage ? Alignment.topRight : Alignment.topLeft;
2022-05-18 08:54:50 +02:00
var color = Theme.of(context).scaffoldBackgroundColor;
2021-11-13 13:06:36 +01:00
final displayTime = event.type == EventTypes.RoomCreate ||
nextEvent == null ||
2022-01-29 12:35:03 +01:00
!event.originServerTs.sameEnvironment(nextEvent!.originServerTs);
2020-05-13 15:58:59 +02:00
final sameSender = nextEvent != null &&
2021-11-13 13:06:36 +01:00
[
EventTypes.Message,
EventTypes.Sticker,
EventTypes.Encrypted,
2022-01-29 12:35:03 +01:00
].contains(nextEvent!.type)
? nextEvent!.senderId == event.senderId && !displayTime
: false;
2021-11-13 13:06:36 +01:00
final textColor = ownMessage
2022-05-18 08:54:50 +02:00
? Theme.of(context).colorScheme.onPrimary
: Theme.of(context).colorScheme.onBackground;
2021-04-14 10:37:15 +02:00
final rowMainAxisAlignment =
2020-01-01 19:10:13 +01:00
ownMessage ? MainAxisAlignment.end : MainAxisAlignment.start;
final displayEvent = event.getDisplayEvent(timeline);
2021-11-13 13:06:36 +01:00
final borderRadius = BorderRadius.only(
topLeft: !ownMessage
? const Radius.circular(2)
: const Radius.circular(AppConfig.borderRadius),
topRight: ownMessage
? const Radius.circular(2)
: const Radius.circular(AppConfig.borderRadius),
bottomLeft: const Radius.circular(AppConfig.borderRadius),
bottomRight: const Radius.circular(AppConfig.borderRadius),
);
final noBubble = {
2021-12-27 09:35:07 +01:00
MessageTypes.Video,
MessageTypes.Image,
MessageTypes.Sticker,
}.contains(event.messageType);
2021-11-13 13:06:36 +01:00
if (ownMessage) {
2021-10-25 10:46:58 +02:00
color = displayEvent.status.isError
2020-01-03 11:57:00 +01:00
? Colors.redAccent
2022-05-18 08:54:50 +02:00
: Theme.of(context).colorScheme.primary;
2020-01-01 19:10:13 +01:00
}
2021-04-14 10:37:15 +02:00
final rowChildren = <Widget>[
2021-11-13 13:06:36 +01:00
sameSender || ownMessage
? SizedBox(
width: Avatar.defaultSize,
child: Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Center(
child: SizedBox(
width: 16 * AppConfig.bubbleSizeFactor,
height: 16 * AppConfig.bubbleSizeFactor,
child: event.status == EventStatus.sending
? const CircularProgressIndicator.adaptive(
strokeWidth: 2)
: event.status == EventStatus.error
? const Icon(Icons.error, color: Colors.red)
: null,
),
),
))
: FutureBuilder<User?>(
future: event.fetchSenderUser(),
builder: (context, snapshot) {
final user = snapshot.data ?? event.senderFromMemoryOrFallback;
return Avatar(
mxContent: user.avatarUrl,
name: user.calcDisplayname(),
onTap: () => onAvatarTab!(event),
);
}),
2020-01-01 19:10:13 +01:00
Expanded(
2021-11-13 13:06:36 +01:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
2022-01-14 07:47:09 +01:00
if (!sameSender)
2021-11-13 13:06:36 +01:00
Padding(
padding: const EdgeInsets.only(left: 8.0, bottom: 4),
2022-01-14 07:47:09 +01:00
child: ownMessage || event.room.isDirectChat
2021-11-13 13:06:36 +01:00
? const SizedBox(height: 12)
: FutureBuilder<User?>(
future: event.fetchSenderUser(),
builder: (context, snapshot) {
final displayname =
snapshot.data?.calcDisplayname() ??
event.senderFromMemoryOrFallback
.calcDisplayname();
return Text(
displayname,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: displayname.color,
),
);
}),
2021-11-13 13:06:36 +01:00
),
Container(
alignment: alignment,
2021-11-14 11:53:43 +01:00
padding: const EdgeInsets.only(left: 8),
2021-11-13 13:06:36 +01:00
child: Material(
color: noBubble ? Colors.transparent : color,
elevation: event.type == EventTypes.Sticker ? 0 : 6,
2021-11-13 13:06:36 +01:00
shadowColor:
Theme.of(context).secondaryHeaderColor.withAlpha(100),
borderRadius: borderRadius,
clipBehavior: Clip.antiAlias,
2021-11-13 13:06:36 +01:00
child: InkWell(
onHover: (b) => useMouse = true,
onTap: !useMouse && longPressSelect
2022-01-29 12:35:03 +01:00
? () {}
: () => onSelect!(event),
onLongPress: !longPressSelect ? null : () => onSelect!(event),
2021-11-13 13:06:36 +01:00
borderRadius: borderRadius,
child: Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(AppConfig.borderRadius),
),
padding: noBubble
? EdgeInsets.zero
: EdgeInsets.all(16 * AppConfig.bubbleSizeFactor),
2021-11-13 13:06:36 +01:00
constraints: const BoxConstraints(
maxWidth: FluffyThemes.columnWidth * 1.5),
child: Stack(
children: <Widget>[
2021-11-13 13:06:36 +01:00
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
if (event.relationshipType ==
RelationshipTypes.reply)
2022-01-29 12:35:03 +01:00
FutureBuilder<Event?>(
2021-11-13 13:06:36 +01:00
future: event.getReplyEvent(timeline),
builder: (BuildContext context, snapshot) {
final replyEvent = snapshot.hasData
2022-01-29 12:35:03 +01:00
? snapshot.data!
2021-11-13 13:06:36 +01:00
: Event(
2022-01-29 12:35:03 +01:00
eventId: event.relationshipEventId!,
2021-11-13 13:06:36 +01:00
content: {
'msgtype': 'm.text',
'body': '...'
},
senderId: event.senderId,
type: 'm.room.message',
room: event.room,
status: EventStatus.sent,
originServerTs: DateTime.now(),
);
return InkWell(
onTap: () {
if (scrollToEventId != null) {
2022-01-29 12:35:03 +01:00
scrollToEventId!(replyEvent.eventId);
2021-11-13 13:06:36 +01:00
}
},
child: AbsorbPointer(
child: Container(
margin: EdgeInsets.symmetric(
vertical: 4.0 *
AppConfig.bubbleSizeFactor),
2021-11-13 13:06:36 +01:00
child: ReplyContent(replyEvent,
2022-05-27 15:13:24 +02:00
ownMessage: ownMessage,
2021-11-13 13:06:36 +01:00
timeline: timeline),
),
),
);
},
2021-11-13 13:06:36 +01:00
),
MessageContent(
displayEvent,
textColor: textColor,
onInfoTab: onInfoTab,
),
if (event.hasAggregatedEvents(
timeline, RelationshipTypes.edit))
2021-11-22 19:20:09 +01:00
Padding(
padding: EdgeInsets.only(
top: 4.0 * AppConfig.bubbleSizeFactor),
2021-11-22 19:20:09 +01:00
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.edit_outlined,
color: textColor.withAlpha(164),
2021-11-22 19:20:09 +01:00
size: 14,
),
2021-11-22 19:20:09 +01:00
Text(
' - ${displayEvent.originServerTs.localizedTimeShort(context)}',
style: TextStyle(
color: textColor.withAlpha(164),
fontSize: 12,
),
),
],
),
),
2021-11-13 13:06:36 +01:00
],
),
],
2020-04-08 12:38:52 +02:00
),
2021-11-13 13:06:36 +01:00
),
2020-05-15 19:57:53 +02:00
),
),
2020-05-15 19:57:53 +02:00
),
2021-11-13 13:06:36 +01:00
],
2020-01-01 19:10:13 +01:00
),
),
];
final row = Row(
2021-11-13 13:06:36 +01:00
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: rowMainAxisAlignment,
children: rowChildren,
);
Widget container;
2021-11-13 13:06:36 +01:00
if (event.hasAggregatedEvents(timeline, RelationshipTypes.reaction) ||
displayTime ||
selected) {
container = Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment:
ownMessage ? CrossAxisAlignment.end : CrossAxisAlignment.start,
children: <Widget>[
if (displayTime || selected)
2021-11-13 13:06:36 +01:00
Padding(
padding: displayTime
? EdgeInsets.symmetric(
vertical: 8.0 * AppConfig.bubbleSizeFactor)
: EdgeInsets.zero,
2021-11-13 13:06:36 +01:00
child: Center(
child: Material(
color: displayTime
? Theme.of(context).backgroundColor
: Theme.of(context).backgroundColor.withOpacity(0.33),
2021-11-13 13:06:36 +01:00
borderRadius: BorderRadius.circular(AppConfig.borderRadius / 2),
2021-11-13 20:17:11 +01:00
clipBehavior: Clip.antiAlias,
2021-11-13 13:06:36 +01:00
child: Padding(
padding: const EdgeInsets.all(6.0),
child: Text(
event.originServerTs.localizedTime(context),
2021-11-13 19:22:11 +01:00
style: TextStyle(fontSize: 14 * AppConfig.fontSizeFactor),
2021-11-13 13:06:36 +01:00
),
),
)),
),
row,
2022-01-03 17:06:38 +01:00
if (event.hasAggregatedEvents(timeline, RelationshipTypes.reaction))
Padding(
padding: EdgeInsets.only(
top: 4.0 * AppConfig.bubbleSizeFactor,
2022-01-03 17:06:38 +01:00
left: (ownMessage ? 0 : Avatar.defaultSize) + 12.0,
right: 12.0,
),
child: MessageReactions(event, timeline),
),
],
);
} else {
container = row;
}
2020-01-17 10:39:46 +01:00
2022-06-28 16:51:29 +02:00
return Swipeable(
key: ValueKey(event.eventId),
background: const Padding(
padding: EdgeInsets.symmetric(horizontal: 12.0),
child: Center(
child: Icon(Icons.reply_outlined),
),
),
direction: SwipeDirection.endToStart,
onSwipe: onSwipe,
child: Center(
child: Container(
color: selected
? Theme.of(context).primaryColor.withAlpha(100)
: Theme.of(context).primaryColor.withAlpha(0),
constraints:
const BoxConstraints(maxWidth: FluffyThemes.columnWidth * 2.5),
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: 8.0,
vertical: 4.0 * AppConfig.bubbleSizeFactor,
),
child: container,
2021-11-14 11:53:43 +01:00
),
2021-05-24 11:22:03 +02:00
),
2020-01-01 19:10:13 +01:00
),
);
}
}