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

146 lines
4.9 KiB
Dart

import 'package:clipboard/clipboard.dart';
import 'package:comunic/models/conversation_message.dart';
import 'package:comunic/models/user.dart';
import 'package:comunic/ui/widgets/conversation_file_tile.dart';
import 'package:comunic/ui/widgets/text_widget.dart';
import 'package:comunic/utils/date_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
/// Conversation message tile
///
/// @author Pierre HUBERT
enum _MenuChoices {
COPY_URL,
COPY_MESSAGE,
DELETE,
REQUEST_UPDATE_CONTENT,
GET_STATS,
}
typedef OnRequestMessageStats = void Function(ConversationMessage);
typedef OnRequestMessageUpdate = void Function(ConversationMessage);
typedef OnRequestMessageDelete = void Function(ConversationMessage);
class ConversationMessageTile extends StatelessWidget {
final ConversationMessage message;
final User user;
final OnRequestMessageStats onRequestMessageStats;
final OnRequestMessageUpdate onRequestMessageUpdate;
final OnRequestMessageDelete onRequestMessageDelete;
const ConversationMessageTile({
Key key,
@required this.message,
@required this.user,
@required this.onRequestMessageStats,
@required this.onRequestMessageUpdate,
@required this.onRequestMessageDelete,
}) : assert(message != null),
assert(user != null),
assert(onRequestMessageStats != null),
assert(onRequestMessageUpdate != null),
assert(onRequestMessageDelete != null),
super(key: key);
@override
Widget build(BuildContext context) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
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,
size: 14,
),
onSelected: (v) => _menuOptionSelected(context, v),
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),
),
)
]),
),
_buildMessageContent(),
],
);
Widget _buildMessageContent() {
if (!message.hasFile)
return TextWidget(
content: message.message,
textAlign: TextAlign.justify,
style: TextStyle(color: Colors.white),
);
return ConversationFileWidget(messageID: message.id, file: message.file);
}
/// Process menu choice
void _menuOptionSelected(BuildContext context, _MenuChoices value) {
switch (value) {
case _MenuChoices.COPY_MESSAGE:
FlutterClipboard.copy(message.message.content);
snack(context, tr("'${message.message.content}' copied to clipboard!"));
break;
case _MenuChoices.COPY_URL:
FlutterClipboard.copy(message.file.url);
snack(context, tr("'${message.file.url}' copied to clipboard!"));
break;
case _MenuChoices.GET_STATS:
onRequestMessageStats(message);
break;
case _MenuChoices.REQUEST_UPDATE_CONTENT:
onRequestMessageUpdate(message);
break;
case _MenuChoices.DELETE:
onRequestMessageDelete(message);
break;
}
}
}