1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 23:09:21 +00:00
comunicmobile/lib/ui/tiles/conversation_message_tile.dart

302 lines
9.3 KiB
Dart
Raw Normal View History

2021-03-11 16:04:18 +00:00
import 'package:clipboard/clipboard.dart';
2021-03-10 23:23:11 +00:00
import 'package:comunic/models/conversation.dart';
2019-04-25 06:56:16 +00:00
import 'package:comunic/models/conversation_message.dart';
import 'package:comunic/models/user.dart';
import 'package:comunic/ui/widgets/account_image_widget.dart';
2021-03-10 23:02:41 +00:00
import 'package:comunic/ui/widgets/conversation_file_tile.dart';
2020-04-16 13:10:47 +00:00
import 'package:comunic/ui/widgets/text_widget.dart';
2019-04-26 06:58:18 +00:00
import 'package:comunic/utils/date_utils.dart';
2019-05-04 06:58:14 +00:00
import 'package:comunic/utils/intl_utils.dart';
2019-11-01 13:17:46 +00:00
import 'package:comunic/utils/ui_utils.dart';
2019-04-25 06:56:16 +00:00
import 'package:flutter/material.dart';
/// Conversation message tile
///
/// @author Pierre HUBERT
2021-03-11 16:04:18 +00:00
enum _MenuChoices {
COPY_URL,
COPY_MESSAGE,
DELETE,
2021-03-11 16:27:20 +00:00
REQUEST_UPDATE_CONTENT,
GET_STATS,
2021-03-11 16:04:18 +00:00
}
2019-05-04 06:58:14 +00:00
2021-03-11 16:27:20 +00:00
typedef OnRequestMessageStats = void Function(ConversationMessage);
2019-05-04 08:24:38 +00:00
typedef OnRequestMessageUpdate = void Function(ConversationMessage);
2019-05-04 06:58:14 +00:00
typedef OnRequestMessageDelete = void Function(ConversationMessage);
2019-04-25 06:56:16 +00:00
class ConversationMessageTile extends StatelessWidget {
2021-03-10 23:23:11 +00:00
final Conversation conversation;
2019-04-25 06:56:16 +00:00
final ConversationMessage message;
final User userInfo;
2019-04-26 06:58:18 +00:00
final bool isLastMessage;
2019-04-26 09:04:06 +00:00
final bool isFirstMessage;
2021-03-11 16:27:20 +00:00
final OnRequestMessageStats onRequestMessageStats;
2019-05-04 08:24:38 +00:00
final OnRequestMessageUpdate onRequestMessageUpdate;
2019-05-04 06:58:14 +00:00
final OnRequestMessageDelete onRequestMessageDelete;
2019-04-25 06:56:16 +00:00
2019-05-04 06:58:14 +00:00
const ConversationMessageTile({
Key key,
2021-03-10 23:23:11 +00:00
@required this.conversation,
2019-05-04 06:58:14 +00:00
@required this.message,
@required this.userInfo,
@required this.isLastMessage,
@required this.isFirstMessage,
2021-03-11 16:27:20 +00:00
@required this.onRequestMessageStats,
2019-05-04 08:24:38 +00:00
@required this.onRequestMessageUpdate,
2019-05-04 06:58:14 +00:00
@required this.onRequestMessageDelete,
}) : assert(message != null),
2019-04-25 06:56:16 +00:00
assert(userInfo != null),
2019-04-26 06:58:18 +00:00
assert(isLastMessage != null),
2019-04-26 09:04:06 +00:00
assert(isFirstMessage != null),
2021-03-11 16:27:20 +00:00
assert(onRequestMessageStats != null),
2019-05-04 08:24:38 +00:00
assert(onRequestMessageUpdate != null),
2019-05-04 06:58:14 +00:00
assert(onRequestMessageDelete != null),
2019-04-25 06:56:16 +00:00
super(key: key);
2021-03-10 23:23:11 +00:00
Color get backgroundColor => message.isOwner
? conversation.color ?? Colors.blueAccent
: darkTheme()
? Colors.white12
: Colors.black12;
2019-04-26 09:04:06 +00:00
/// Build account image
2019-05-04 06:58:14 +00:00
Widget _buildAccountImage(BuildContext context) {
2019-04-26 09:04:06 +00:00
return Container(
2019-04-26 09:28:33 +00:00
margin: EdgeInsets.all(10.0),
2019-05-04 06:58:14 +00:00
child: PopupMenuButton<_MenuChoices>(
child: AccountImageWidget(
user: userInfo,
width: 35.0,
),
itemBuilder: (c) => [
2021-03-11 16:04:18 +00:00
PopupMenuItem(
enabled: (message.message?.content ?? "") != "",
value: _MenuChoices.COPY_MESSAGE,
child: Text(tr("Copy message")),
),
PopupMenuItem(
enabled: message.file != null,
value: _MenuChoices.COPY_URL,
child: Text(tr("Copy URL")),
),
2021-03-11 16:27:20 +00:00
PopupMenuItem(
value: _MenuChoices.GET_STATS,
child: Text(tr("Statistics")),
),
2019-11-01 13:17:46 +00:00
// Update message content
PopupMenuItem(
enabled: message.isOwner,
value: _MenuChoices.REQUEST_UPDATE_CONTENT,
child: Text(tr("Update")),
),
2019-05-04 08:24:38 +00:00
2019-11-01 13:17:46 +00:00
// Delete the message
PopupMenuItem(
enabled: message.isOwner,
value: _MenuChoices.DELETE,
child: Text(tr("Delete")),
),
],
2019-05-04 06:58:14 +00:00
onSelected: _menuOptionSelected,
2019-04-26 09:04:06 +00:00
),
);
}
2019-04-26 09:28:33 +00:00
/// Build widget image
2021-03-10 23:02:41 +00:00
Widget _buildMessageFile(BuildContext context) => ConversationFileWidget(
messageID: message.id,
file: message.file,
2021-03-10 23:23:11 +00:00
defaultBackgroundColor: backgroundColor,
2021-03-10 23:02:41 +00:00
);
2019-04-26 09:28:33 +00:00
2019-04-26 06:58:18 +00:00
/// Build message date
Widget _buildMessageDate() {
return isLastMessage
? Container(
margin: EdgeInsets.only(top: 5.0),
child: Text(
dateTimeToString(message.date),
2021-03-10 23:15:04 +00:00
style: TextStyle(
color: darkTheme() ? Colors.white : Colors.black54,
fontSize: 12.0),
2019-04-26 09:04:06 +00:00
textAlign: TextAlign.center,
2019-04-26 06:58:18 +00:00
),
)
: Container();
}
/// Build a message of the current user
2019-04-26 09:34:24 +00:00
Widget _buildRightMessage(BuildContext context) {
2019-04-26 06:58:18 +00:00
return Container(
margin: EdgeInsets.only(bottom: isLastMessage ? 20.0 : 2.0),
2019-04-26 06:58:18 +00:00
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Column(
children: <Widget>[
2019-04-26 09:04:06 +00:00
Row(
children: <Widget>[
2019-04-26 09:28:33 +00:00
Column(
children: <Widget>[
// Text image
Container(
2021-03-10 23:02:41 +00:00
child:
message.hasFile ? _buildMessageFile(context) : null,
2019-04-26 09:28:33 +00:00
),
// Text message
Container(
child: message.hasMessage
? Container(
width: 200.0,
alignment: Alignment.centerRight,
child: Container(
2020-04-16 13:10:47 +00:00
child: TextWidget(
content: message.message,
textAlign: TextAlign.justify,
style: TextStyle(color: Colors.white),
2020-04-17 09:56:28 +00:00
linksColor: Colors.indigo,
),
padding: EdgeInsets.fromLTRB(
15.0, 10.0, 15.0, 10.0),
decoration: BoxDecoration(
2021-03-10 23:23:11 +00:00
color: backgroundColor,
borderRadius: BorderRadius.circular(8.0),
),
2019-04-26 09:28:33 +00:00
),
)
: null,
),
],
2019-04-26 09:04:06 +00:00
),
// Account image
2019-05-04 06:58:14 +00:00
_buildAccountImage(context)
2019-04-26 09:04:06 +00:00
],
2019-04-26 06:58:18 +00:00
),
// Date
2019-04-26 09:04:06 +00:00
Container(
child: _buildMessageDate(),
margin: EdgeInsets.only(right: 45.0),
)
2019-04-26 06:58:18 +00:00
],
),
],
),
);
// Text
}
/// Build a message of a peer user
2019-04-26 09:34:24 +00:00
Widget _buildLeftMessage(BuildContext context) {
2019-04-26 06:58:18 +00:00
return Container(
2019-04-26 09:28:33 +00:00
margin: EdgeInsets.only(bottom: isLastMessage ? 20.0 : 5.0),
2019-04-26 09:04:06 +00:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
2019-04-26 06:58:18 +00:00
children: <Widget>[
2019-04-26 09:04:06 +00:00
//User name
Container(
2019-04-26 09:28:33 +00:00
margin: EdgeInsets.only(left: 55.0),
2019-04-26 09:04:06 +00:00
child: isFirstMessage
? Text(
userInfo.fullName,
textAlign: TextAlign.left,
style: TextStyle(fontSize: 12.0),
)
: null,
),
Row(
2019-04-26 06:58:18 +00:00
children: <Widget>[
2019-04-26 09:04:06 +00:00
// Account image
2019-05-04 06:58:14 +00:00
_buildAccountImage(context),
2019-04-26 06:58:18 +00:00
2019-04-26 09:04:06 +00:00
Column(
children: <Widget>[
2019-04-26 09:28:33 +00:00
// Text image
Container(
2021-03-10 23:02:41 +00:00
child: message.hasFile ? _buildMessageFile(context) : null,
2019-04-26 09:28:33 +00:00
),
2019-04-26 09:04:06 +00:00
// Text message
Container(
2019-04-26 09:28:33 +00:00
child: message.hasMessage
? Container(
width: 200.0,
alignment: Alignment.centerLeft,
child: Container(
2020-04-16 13:10:47 +00:00
child: TextWidget(
content: message.message,
textAlign: TextAlign.justify,
2019-11-01 13:17:46 +00:00
style: TextStyle(
color: darkTheme()
? Colors.white
: Colors.black),
),
padding:
EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0),
decoration: BoxDecoration(
2021-03-10 23:23:11 +00:00
color: backgroundColor,
borderRadius: BorderRadius.circular(8.0),
),
2019-04-26 09:28:33 +00:00
),
)
: null,
2019-04-26 09:04:06 +00:00
),
],
),
2019-04-26 06:58:18 +00:00
],
),
2019-04-26 09:04:06 +00:00
// Date
Container(
2019-04-26 09:28:33 +00:00
margin: EdgeInsets.only(left: 50.0),
2019-04-26 09:04:06 +00:00
child: Align(
alignment: Alignment.topLeft,
child: _buildMessageDate(),
),
)
2019-04-26 06:58:18 +00:00
],
),
);
}
2019-04-25 06:56:16 +00:00
@override
Widget build(BuildContext context) {
2019-05-04 06:58:14 +00:00
return message.isOwner
2019-04-26 09:34:24 +00:00
? _buildRightMessage(context)
: _buildLeftMessage(context);
2019-04-25 06:56:16 +00:00
}
2019-05-04 06:58:14 +00:00
/// Process menu choice
void _menuOptionSelected(_MenuChoices value) {
2019-05-04 08:24:38 +00:00
switch (value) {
2021-03-11 16:04:18 +00:00
case _MenuChoices.COPY_MESSAGE:
FlutterClipboard.copy(message.message.content);
break;
case _MenuChoices.COPY_URL:
FlutterClipboard.copy(message.file.url);
break;
2021-03-11 16:27:20 +00:00
case _MenuChoices.GET_STATS:
onRequestMessageStats(message);
break;
2019-05-04 08:24:38 +00:00
case _MenuChoices.REQUEST_UPDATE_CONTENT:
onRequestMessageUpdate(message);
break;
2019-05-04 06:58:14 +00:00
case _MenuChoices.DELETE:
onRequestMessageDelete(message);
break;
}
}
2019-04-25 06:56:16 +00:00
}