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

330 lines
13 KiB
Dart
Raw Normal View History

2021-10-26 18:50:34 +02:00
import 'package:flutter/material.dart';
import 'package:matrix/matrix.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;
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-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;
2021-11-15 13:23:47 +01:00
var color = Theme.of(context).appBarTheme.backgroundColor;
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!.sender.id == event.sender.id && !displayTime
: false;
2021-11-13 13:06:36 +01:00
final textColor = ownMessage
? Colors.white
: Theme.of(context).brightness == Brightness.dark
? Colors.white
: Colors.black;
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
: Theme.of(context).primaryColor;
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,
),
),
))
2021-11-13 13:06:36 +01:00
: Avatar(
2021-11-20 10:42:23 +01:00
mxContent: event.sender.avatarUrl,
name: event.sender.calcDisplayname(),
2022-01-29 12:35:03 +01:00
onTap: () => onAvatarTab!(event),
2021-11-13 13:06:36 +01:00
),
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)
: Text(
event.sender.calcDisplayname(),
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: event.sender.calcDisplayname().color,
),
),
),
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,
lightText: ownMessage,
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) {
container = Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment:
ownMessage ? CrossAxisAlignment.end : CrossAxisAlignment.start,
children: <Widget>[
2021-11-13 13:06:36 +01:00
if (displayTime)
Padding(
padding: EdgeInsets.symmetric(
vertical: 8.0 * AppConfig.bubbleSizeFactor),
2021-11-13 13:06:36 +01:00
child: Center(
child: Material(
color: Theme.of(context).backgroundColor,
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
2021-05-24 11:22:03 +02:00
return Center(
child: Container(
color: selected
2021-05-24 11:22:03 +02:00
? Theme.of(context).primaryColor.withAlpha(100)
: Theme.of(context).primaryColor.withAlpha(0),
2021-10-14 18:09:30 +02:00
constraints:
const BoxConstraints(maxWidth: FluffyThemes.columnWidth * 2.5),
2021-05-24 11:22:03 +02:00
child: Padding(
padding: EdgeInsets.symmetric(
2021-11-14 11:53:43 +01:00
horizontal: 8.0,
vertical: 4.0 * AppConfig.bubbleSizeFactor,
2021-11-14 11:53:43 +01:00
),
2021-05-24 11:22:03 +02:00
child: container,
),
2020-01-01 19:10:13 +01:00
),
);
}
}