2019-04-25 06:56:16 +00:00
|
|
|
import 'package:comunic/models/conversation_message.dart';
|
|
|
|
import 'package:comunic/models/user.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';
|
2021-03-13 17:28:56 +00:00
|
|
|
import 'package:comunic/utils/clipboard_utils.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-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 {
|
|
|
|
final ConversationMessage message;
|
2021-03-13 07:17:54 +00:00
|
|
|
final User user;
|
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,
|
|
|
|
@required this.message,
|
2021-03-13 07:17:54 +00:00
|
|
|
@required this.user,
|
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),
|
2021-03-13 07:17:54 +00:00
|
|
|
assert(user != 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-13 07:17:54 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) => Column(
|
2019-04-26 09:04:06 +00:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2019-04-26 06:58:18 +00:00
|
|
|
children: <Widget>[
|
2021-03-13 07:17:54 +00:00
|
|
|
RichText(
|
|
|
|
text: TextSpan(
|
|
|
|
text:
|
|
|
|
"${user.fullName} - ${formatDisplayDate(message.date, date: false)}",
|
|
|
|
style: TextStyle(color: Colors.white, fontSize: 11),
|
|
|
|
children: [
|
|
|
|
WidgetSpan(
|
|
|
|
child: PopupMenuButton<_MenuChoices>(
|
|
|
|
child: Icon(
|
|
|
|
Icons.more_vert,
|
|
|
|
color: Colors.white,
|
2021-03-13 07:33:55 +00:00
|
|
|
size: 14,
|
2021-03-13 07:17:54 +00:00
|
|
|
),
|
2021-03-13 07:37:59 +00:00
|
|
|
onSelected: (v) => _menuOptionSelected(context, v),
|
2021-03-13 07:17:54 +00:00
|
|
|
itemBuilder: (c) => <PopupMenuItem<_MenuChoices>>[
|
|
|
|
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")),
|
|
|
|
),
|
|
|
|
|
|
|
|
PopupMenuItem(
|
|
|
|
value: _MenuChoices.GET_STATS,
|
|
|
|
child: Text(tr("Statistics")),
|
|
|
|
),
|
|
|
|
|
|
|
|
// Update message content
|
|
|
|
PopupMenuItem(
|
|
|
|
enabled: message.isOwner &&
|
|
|
|
message.message != null &&
|
|
|
|
message.message.content.isNotEmpty,
|
|
|
|
value: _MenuChoices.REQUEST_UPDATE_CONTENT,
|
|
|
|
child: Text(tr("Update")),
|
|
|
|
),
|
|
|
|
|
|
|
|
// Delete the message
|
|
|
|
PopupMenuItem(
|
|
|
|
enabled: message.isOwner,
|
|
|
|
value: _MenuChoices.DELETE,
|
|
|
|
child: Text(tr("Delete")),
|
|
|
|
),
|
|
|
|
]..removeWhere((element) => !element.enabled),
|
|
|
|
),
|
2019-04-26 09:04:06 +00:00
|
|
|
)
|
2021-03-13 07:17:54 +00:00
|
|
|
]),
|
2019-04-26 09:04:06 +00:00
|
|
|
),
|
2021-03-13 07:17:54 +00:00
|
|
|
_buildMessageContent(),
|
2019-04-26 06:58:18 +00:00
|
|
|
],
|
2021-03-13 07:17:54 +00:00
|
|
|
);
|
2019-04-26 06:58:18 +00:00
|
|
|
|
2021-03-13 07:17:54 +00:00
|
|
|
Widget _buildMessageContent() {
|
|
|
|
if (!message.hasFile)
|
|
|
|
return TextWidget(
|
|
|
|
content: message.message,
|
|
|
|
textAlign: TextAlign.justify,
|
2021-03-13 07:40:37 +00:00
|
|
|
style: TextStyle(color: Colors.white),
|
2021-03-13 07:17:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return ConversationFileWidget(messageID: message.id, file: message.file);
|
2019-04-25 06:56:16 +00:00
|
|
|
}
|
2019-05-04 06:58:14 +00:00
|
|
|
|
|
|
|
/// Process menu choice
|
2021-03-13 07:37:59 +00:00
|
|
|
void _menuOptionSelected(BuildContext context, _MenuChoices value) {
|
2019-05-04 08:24:38 +00:00
|
|
|
switch (value) {
|
2021-03-11 16:04:18 +00:00
|
|
|
case _MenuChoices.COPY_MESSAGE:
|
2021-03-13 17:28:56 +00:00
|
|
|
copyToClipboard(context, message.message.content);
|
2021-03-11 16:04:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case _MenuChoices.COPY_URL:
|
2021-03-13 17:28:56 +00:00
|
|
|
copyToClipboard(context, message.message.content);
|
2021-03-11 16:04:18 +00:00
|
|
|
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
|
|
|
}
|