2021-10-26 18:50:34 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
2021-06-18 10:29:48 +02:00
|
|
|
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';
|
2021-05-22 09:24:39 +02:00
|
|
|
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';
|
2021-08-07 16:35:38 +02:00
|
|
|
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;
|
2022-06-28 16:51:29 +02:00
|
|
|
final void Function(SwipeDirection) onSwipe;
|
2022-02-14 18:44:37 +01:00
|
|
|
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,
|
2022-02-14 18:44:37 +01:00
|
|
|
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-02-14 18:44:37 +01:00
|
|
|
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) {
|
2022-02-19 11:05:21 +01:00
|
|
|
if (!{
|
|
|
|
EventTypes.Message,
|
|
|
|
EventTypes.Sticker,
|
|
|
|
EventTypes.Encrypted,
|
|
|
|
EventTypes.CallInvite
|
|
|
|
}.contains(event.type)) {
|
|
|
|
if (event.type.startsWith('m.call.')) {
|
|
|
|
return Container();
|
|
|
|
}
|
2022-10-15 10:38:06 +02:00
|
|
|
return StateMessage(event);
|
2020-01-04 13:53:49 +01:00
|
|
|
}
|
2020-01-01 19:10:13 +01:00
|
|
|
|
2021-08-07 16:35:38 +02: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-07-07 18:50:13 +02:00
|
|
|
var color = Theme.of(context).brightness == Brightness.light
|
|
|
|
? Colors.white
|
2022-08-31 16:06:53 +02:00
|
|
|
: Theme.of(context).colorScheme.surfaceVariant;
|
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)
|
2022-05-30 13:44:05 +02:00
|
|
|
? nextEvent!.senderId == event.senderId && !displayTime
|
2020-01-17 11:08:12 +01:00
|
|
|
: 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;
|
|
|
|
|
2020-08-12 11:30:31 +02:00
|
|
|
final displayEvent = event.getDisplayEvent(timeline);
|
2021-11-13 13:06:36 +01:00
|
|
|
final borderRadius = BorderRadius.only(
|
|
|
|
topLeft: !ownMessage
|
2022-11-04 11:17:22 +01:00
|
|
|
? const Radius.circular(4)
|
2021-11-13 13:06:36 +01:00
|
|
|
: const Radius.circular(AppConfig.borderRadius),
|
2022-11-04 11:17:22 +01:00
|
|
|
topRight: const Radius.circular(AppConfig.borderRadius),
|
2021-11-13 13:06:36 +01:00
|
|
|
bottomLeft: const Radius.circular(AppConfig.borderRadius),
|
2022-11-04 11:17:22 +01:00
|
|
|
bottomRight: ownMessage
|
|
|
|
? const Radius.circular(4)
|
|
|
|
: const Radius.circular(AppConfig.borderRadius),
|
2021-11-13 13:06:36 +01:00
|
|
|
);
|
2021-11-13 18:55:05 +01:00
|
|
|
final noBubble = {
|
2022-07-29 18:24:59 +02:00
|
|
|
MessageTypes.Video,
|
|
|
|
MessageTypes.Image,
|
2022-12-30 10:36:54 +01:00
|
|
|
MessageTypes.Sticker
|
2022-07-29 18:24:59 +02:00
|
|
|
}.contains(event.messageType) &&
|
|
|
|
!event.redacted;
|
2022-12-30 10:36:54 +01:00
|
|
|
final noPadding = {
|
|
|
|
MessageTypes.File,
|
|
|
|
MessageTypes.Audio,
|
|
|
|
}.contains(event.messageType);
|
2020-08-12 11:30:31 +02:00
|
|
|
|
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,
|
2022-04-08 07:42:36 +02:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
))
|
2022-05-30 13:44:05 +02:00
|
|
|
: 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)
|
2022-05-30 13:44:05 +02:00
|
|
|
: 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,
|
2023-01-29 15:57:59 +01:00
|
|
|
color: (Theme.of(context).brightness ==
|
|
|
|
Brightness.light
|
|
|
|
? displayname.color
|
|
|
|
: displayname.lightColor),
|
2022-05-30 13:44:05 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}),
|
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(
|
2021-11-14 18:57:48 +01:00
|
|
|
color: noBubble ? Colors.transparent : color,
|
2022-07-12 21:37:04 +02:00
|
|
|
elevation: event.type == EventTypes.Sticker ? 0 : 4,
|
2022-08-31 16:06:53 +02:00
|
|
|
shadowColor: Colors.black.withAlpha(64),
|
2021-11-13 13:06:36 +01:00
|
|
|
borderRadius: borderRadius,
|
2021-11-13 18:55:05 +01:00
|
|
|
clipBehavior: Clip.antiAlias,
|
2021-11-13 13:06:36 +01:00
|
|
|
child: InkWell(
|
|
|
|
onHover: (b) => useMouse = true,
|
2022-02-14 18:44:37 +01:00
|
|
|
onTap: !useMouse && longPressSelect
|
2022-01-29 12:35:03 +01:00
|
|
|
? () {}
|
|
|
|
: () => onSelect!(event),
|
2022-02-14 18:44:37 +01:00
|
|
|
onLongPress: !longPressSelect ? null : () => onSelect!(event),
|
2021-11-13 13:06:36 +01:00
|
|
|
borderRadius: borderRadius,
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius:
|
|
|
|
BorderRadius.circular(AppConfig.borderRadius),
|
|
|
|
),
|
2022-12-30 10:36:54 +01:00
|
|
|
padding: noBubble || noPadding
|
2022-01-03 23:19:51 +01:00
|
|
|
? 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(
|
2021-05-16 11:06:46 +02:00
|
|
|
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(
|
2022-01-03 23:19:51 +01:00
|
|
|
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-05-16 11:06:46 +02:00
|
|
|
},
|
2021-11-13 13:06:36 +01:00
|
|
|
),
|
|
|
|
MessageContent(
|
|
|
|
displayEvent,
|
|
|
|
textColor: textColor,
|
|
|
|
onInfoTab: onInfoTab,
|
|
|
|
),
|
2021-11-21 14:26:40 +01:00
|
|
|
if (event.hasAggregatedEvents(
|
|
|
|
timeline, RelationshipTypes.edit))
|
2021-11-22 19:20:09 +01:00
|
|
|
Padding(
|
2022-01-03 23:19:51 +01:00
|
|
|
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,
|
2021-11-21 14:26:40 +01:00
|
|
|
color: textColor.withAlpha(164),
|
2021-11-22 19:20:09 +01:00
|
|
|
size: 14,
|
2021-11-21 14:26:40 +01:00
|
|
|
),
|
2021-11-22 19:20:09 +01:00
|
|
|
Text(
|
|
|
|
' - ${displayEvent.originServerTs.localizedTimeShort(context)}',
|
|
|
|
style: TextStyle(
|
|
|
|
color: textColor.withAlpha(164),
|
|
|
|
fontSize: 12,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2021-11-21 14:26:40 +01:00
|
|
|
),
|
2021-11-13 13:06:36 +01:00
|
|
|
],
|
2021-05-16 11:06:46 +02: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
|
|
|
),
|
2021-05-16 11:06:46 +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
|
|
|
),
|
|
|
|
),
|
|
|
|
];
|
2020-08-12 11:30:31 +02:00
|
|
|
final row = Row(
|
2021-11-13 13:06:36 +01:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2020-08-12 11:30:31 +02:00
|
|
|
mainAxisAlignment: rowMainAxisAlignment,
|
|
|
|
children: rowChildren,
|
|
|
|
);
|
|
|
|
Widget container;
|
2021-11-13 13:06:36 +01:00
|
|
|
if (event.hasAggregatedEvents(timeline, RelationshipTypes.reaction) ||
|
2022-04-08 09:59:08 +02:00
|
|
|
displayTime ||
|
|
|
|
selected) {
|
2020-08-12 11:30:31 +02:00
|
|
|
container = Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
crossAxisAlignment:
|
|
|
|
ownMessage ? CrossAxisAlignment.end : CrossAxisAlignment.start,
|
|
|
|
children: <Widget>[
|
2022-04-08 09:59:08 +02:00
|
|
|
if (displayTime || selected)
|
2021-11-13 13:06:36 +01:00
|
|
|
Padding(
|
2022-04-08 09:59:08 +02:00
|
|
|
padding: displayTime
|
|
|
|
? EdgeInsets.symmetric(
|
|
|
|
vertical: 8.0 * AppConfig.bubbleSizeFactor)
|
|
|
|
: EdgeInsets.zero,
|
2021-11-13 13:06:36 +01:00
|
|
|
child: Center(
|
|
|
|
child: Material(
|
2022-04-08 09:59:08 +02:00
|
|
|
color: displayTime
|
2023-01-26 09:47:30 +01:00
|
|
|
? Theme.of(context).colorScheme.background
|
|
|
|
: Theme.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.background
|
|
|
|
.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
|
|
|
),
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
),
|
2020-08-12 11:30:31 +02:00
|
|
|
row,
|
2022-01-03 17:06:38 +01:00
|
|
|
if (event.hasAggregatedEvents(timeline, RelationshipTypes.reaction))
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.only(
|
2022-01-03 23:19:51 +01:00
|
|
|
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),
|
2020-08-12 11:30:31 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
} 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
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|