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

155 lines
5.1 KiB
Dart
Raw Normal View History

2022-03-18 18:21:08 +00:00
import 'package:comunic/helpers/server_config_helper.dart';
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,
2022-03-18 18:21:08 +00:00
REPORT
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;
2022-03-18 18:21:08 +00:00
final Function(ConversationMessage) onReportMessage;
2019-04-25 06:56:16 +00:00
2019-05-04 06:58:14 +00:00
const ConversationMessageTile({
Key? key,
required this.message,
required this.user,
required this.onRequestMessageStats,
required this.onRequestMessageUpdate,
required this.onRequestMessageDelete,
2022-03-18 18:21:08 +00:00
required this.onReportMessage,
2022-03-11 15:40:56 +00:00
}) : super(key: key);
2019-04-25 06:56:16 +00:00
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(
2022-03-11 15:21:35 +00:00
enabled: (message.message.content ?? "") != "",
2021-03-13 07:17:54 +00:00
value: _MenuChoices.COPY_MESSAGE,
child: Text(tr("Copy message")!),
2021-03-13 07:17:54 +00:00
),
PopupMenuItem(
enabled: message.file != null,
value: _MenuChoices.COPY_URL,
child: Text(tr("Copy URL")!),
2021-03-13 07:17:54 +00:00
),
PopupMenuItem(
value: _MenuChoices.GET_STATS,
child: Text(tr("Statistics")!),
2021-03-13 07:17:54 +00:00
),
// Update message content
PopupMenuItem(
enabled: message.isOwner &&
2022-03-11 15:40:56 +00:00
!message.message.isNull &&
message.message.content!.isNotEmpty,
2021-03-13 07:17:54 +00:00
value: _MenuChoices.REQUEST_UPDATE_CONTENT,
child: Text(tr("Update")!),
2021-03-13 07:17:54 +00:00
),
// Delete the message
PopupMenuItem(
enabled: message.isOwner,
value: _MenuChoices.DELETE,
child: Text(tr("Delete")!),
2021-03-13 07:17:54 +00:00
),
2022-03-18 18:21:08 +00:00
// Report the message
PopupMenuItem(
enabled:
srvConfig!.isReportingEnabled && !message.isOwner,
value: _MenuChoices.REPORT,
child: Text(tr("Report abuse")!),
),
2021-03-13 07:17:54 +00:00
]..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-14 14:28:39 +00:00
linksColor: 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:
copyToClipboard(context, message.message.content!);
2021-03-11 16:04:18 +00:00
break;
case _MenuChoices.COPY_URL:
copyToClipboard(context, message.file!.url!);
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;
2022-03-18 18:21:08 +00:00
case _MenuChoices.REPORT:
onReportMessage(message);
break;
2019-05-04 06:58:14 +00:00
}
}
2019-04-25 06:56:16 +00:00
}