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

130 lines
3.7 KiB
Dart
Raw Normal View History

import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/lists/users_list.dart';
2019-04-23 12:35:41 +00:00
import 'package:comunic/models/conversation.dart';
2021-03-11 17:00:06 +00:00
import 'package:comunic/ui/widgets/conversation_image_widget.dart';
import 'package:comunic/ui/widgets/custom_list_tile.dart';
2019-04-23 14:38:41 +00:00
import 'package:comunic/utils/date_utils.dart';
2019-04-23 12:35:41 +00:00
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart';
/// Single conversation tile
///
/// @author Pierre HUBERT
2019-04-24 15:46:25 +00:00
typedef OpenConversationCallback = void Function(Conversation);
typedef RequestDeleteConversationCallback = void Function(Conversation);
typedef RequestUpdateConversationCallback = void Function(Conversation);
enum _PopupMenuChoices { UPDATE, DELETE }
2019-04-24 15:46:25 +00:00
2019-04-23 12:35:41 +00:00
class ConversationTile extends StatelessWidget {
final Conversation conversation;
final UsersList usersList;
2019-04-24 15:46:25 +00:00
final OpenConversationCallback onOpen;
final RequestUpdateConversationCallback onRequestUpdate;
final RequestDeleteConversationCallback onRequestDelete;
2019-04-23 12:35:41 +00:00
const ConversationTile({
Key key,
@required this.conversation,
@required this.usersList,
@required this.onOpen,
@required this.onRequestUpdate,
@required this.onRequestDelete,
}) : assert(conversation != null),
assert(usersList != null),
2019-04-24 15:46:25 +00:00
assert(onOpen != null),
assert(onRequestUpdate != null),
assert(onRequestDelete != null),
super(key: key);
2019-04-23 12:35:41 +00:00
_buildSubInformation(IconData icon, String content) {
return Row(
children: <Widget>[
Icon(
icon,
size: 15.0,
color: Colors.grey,
),
Text(" " + content),
],
);
}
@override
Widget build(BuildContext context) {
return CustomListTile(
2019-04-24 15:46:25 +00:00
onTap: () => onOpen(conversation),
2019-04-23 16:23:34 +00:00
// Conversation name
title: Text(
ConversationsHelper.getConversationName(
conversation,
usersList,
),
style: TextStyle(
fontWeight: conversation.sawLastMessage ? null : FontWeight.bold,
),
),
2021-03-10 17:12:56 +00:00
// Tile color
tileColor: conversation.sawLastMessage
? null
: (conversation.color ?? Colors.blue).withOpacity(0.2),
2019-04-23 16:23:34 +00:00
// Leading icon
2021-03-11 17:00:06 +00:00
leading:
ConversationImageWidget(conversation: conversation, users: usersList),
2019-04-23 16:23:34 +00:00
// Conversation information
2019-04-23 12:35:41 +00:00
isThreeLine: true,
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
2021-03-10 17:04:29 +00:00
_buildSubInformation(Icons.access_time,
diffTimeFromNowToStr(conversation.lastActivity)),
2019-04-23 12:35:41 +00:00
_buildSubInformation(
Icons.group,
conversation.members.length == 1
? tr("1 member")
: tr(
"%num% members",
args: {
"num": conversation.members.length.toString(),
},
),
),
],
),
onLongPressOpenMenu: (position) {
showMenu<_PopupMenuChoices>(
context: context,
position: position,
items: [
PopupMenuItem(
child: Text(tr("Update")),
value: _PopupMenuChoices.UPDATE,
),
PopupMenuItem(
child: Text(tr("Delete")),
value: _PopupMenuChoices.DELETE,
)
]).then(_conversationMenuCallback);
},
2019-04-23 12:35:41 +00:00
);
}
/// Method called each time an option of the menu is selected
void _conversationMenuCallback(_PopupMenuChoices c) {
switch (c) {
case _PopupMenuChoices.UPDATE:
onRequestUpdate(conversation);
break;
case _PopupMenuChoices.DELETE:
onRequestDelete(conversation);
break;
}
}
2019-04-23 12:35:41 +00:00
}