2019-04-23 15:29:58 +00:00
|
|
|
import 'package:comunic/helpers/conversations_helper.dart';
|
2022-03-18 18:11:06 +00:00
|
|
|
import 'package:comunic/helpers/server_config_helper.dart';
|
2021-04-06 16:04:16 +00:00
|
|
|
import 'package:comunic/lists/groups_list.dart';
|
2019-04-23 15:29:58 +00:00
|
|
|
import 'package:comunic/lists/users_list.dart';
|
2021-04-25 14:24:42 +00:00
|
|
|
import 'package:comunic/models/config.dart';
|
2019-04-23 12:35:41 +00:00
|
|
|
import 'package:comunic/models/conversation.dart';
|
2021-03-13 11:02:24 +00:00
|
|
|
import 'package:comunic/ui/routes/main_route/main_route.dart';
|
2021-03-11 17:00:06 +00:00
|
|
|
import 'package:comunic/ui/widgets/conversation_image_widget.dart';
|
2020-05-09 05:14:38 +00:00
|
|
|
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);
|
2021-03-13 11:07:16 +00:00
|
|
|
typedef RequestLeaveConversationCallback = void Function(Conversation);
|
2019-05-01 08:58:45 +00:00
|
|
|
typedef RequestUpdateConversationCallback = void Function(Conversation);
|
2019-05-01 08:52:10 +00:00
|
|
|
|
2022-03-18 18:11:06 +00:00
|
|
|
enum _PopupMenuChoices { UPDATE, LEAVE, REPORT }
|
2019-04-24 15:46:25 +00:00
|
|
|
|
2019-04-23 12:35:41 +00:00
|
|
|
class ConversationTile extends StatelessWidget {
|
|
|
|
final Conversation conversation;
|
2019-04-23 15:29:58 +00:00
|
|
|
final UsersList usersList;
|
2022-03-10 18:39:57 +00:00
|
|
|
final GroupsList? groupsList;
|
2019-04-24 15:46:25 +00:00
|
|
|
final OpenConversationCallback onOpen;
|
2019-05-01 08:58:45 +00:00
|
|
|
final RequestUpdateConversationCallback onRequestUpdate;
|
2021-03-13 11:07:16 +00:00
|
|
|
final RequestLeaveConversationCallback onRequestLeave;
|
2022-03-18 18:11:06 +00:00
|
|
|
final Function(Conversation) onReport;
|
2019-04-23 12:35:41 +00:00
|
|
|
|
2019-05-01 08:58:45 +00:00
|
|
|
const ConversationTile({
|
2022-03-10 18:39:57 +00:00
|
|
|
Key? key,
|
|
|
|
required this.conversation,
|
|
|
|
required this.usersList,
|
|
|
|
required this.groupsList,
|
|
|
|
required this.onOpen,
|
|
|
|
required this.onRequestUpdate,
|
|
|
|
required this.onRequestLeave,
|
2022-03-18 18:11:06 +00:00
|
|
|
required this.onReport,
|
|
|
|
}) : 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
|
2021-03-13 11:02:24 +00:00
|
|
|
Widget build(BuildContext context) => Column(
|
|
|
|
children: [_buildMainTile(context), _buildCallTile(context)],
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget _buildMainTile(BuildContext context) => CustomListTile(
|
|
|
|
onTap: () => onOpen(conversation),
|
|
|
|
// Conversation name
|
|
|
|
title: Text(
|
|
|
|
ConversationsHelper.getConversationName(
|
|
|
|
conversation,
|
|
|
|
usersList,
|
2022-03-11 15:21:35 +00:00
|
|
|
),
|
2021-03-13 11:02:24 +00:00
|
|
|
style: TextStyle(
|
|
|
|
fontWeight: conversation.sawLastMessage ? null : FontWeight.bold,
|
|
|
|
),
|
2019-04-23 16:23:34 +00:00
|
|
|
),
|
2021-03-13 11:02:24 +00:00
|
|
|
|
|
|
|
// Tile color
|
|
|
|
tileColor: conversation.sawLastMessage
|
|
|
|
? null
|
2021-04-25 14:24:42 +00:00
|
|
|
: (conversation.color ??
|
|
|
|
config().unreadConversationColor ??
|
|
|
|
Colors.blue)
|
|
|
|
.withOpacity(0.2),
|
2021-03-13 11:02:24 +00:00
|
|
|
|
|
|
|
// Leading icon
|
|
|
|
leading: ConversationImageWidget(
|
2021-04-25 14:03:40 +00:00
|
|
|
conversation: conversation,
|
|
|
|
users: usersList,
|
|
|
|
group: conversation.isGroupConversation
|
2022-03-10 18:39:57 +00:00
|
|
|
? groupsList!.getGroup(conversation.groupID)
|
2021-04-25 14:03:40 +00:00
|
|
|
: null,
|
|
|
|
),
|
2021-03-13 11:02:24 +00:00
|
|
|
|
|
|
|
// Conversation information
|
|
|
|
isThreeLine: true,
|
|
|
|
subtitle: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
children: <Widget>[
|
|
|
|
_buildSubInformation(Icons.access_time,
|
2022-03-10 18:39:57 +00:00
|
|
|
diffTimeFromNowToStr(conversation.lastActivity!)!),
|
2021-04-06 16:04:16 +00:00
|
|
|
conversation.isGroupConversation
|
|
|
|
? _buildSubInformation(
|
|
|
|
Icons.link,
|
|
|
|
tr("Group: %group_name%", args: {
|
|
|
|
"group_name":
|
2022-03-10 18:39:57 +00:00
|
|
|
groupsList!.getGroup(conversation.groupID)!.name
|
|
|
|
})!)
|
2021-04-06 16:04:16 +00:00
|
|
|
: _buildSubInformation(
|
|
|
|
Icons.group,
|
2022-03-10 18:39:57 +00:00
|
|
|
conversation.members!.length == 1
|
|
|
|
? tr("1 member")!
|
2021-04-06 16:04:16 +00:00
|
|
|
: tr(
|
|
|
|
"%num% members",
|
|
|
|
args: {
|
2022-03-10 18:39:57 +00:00
|
|
|
"num": conversation.members!.length.toString(),
|
2021-04-06 16:04:16 +00:00
|
|
|
},
|
2022-03-10 18:39:57 +00:00
|
|
|
)!,
|
2021-04-06 16:04:16 +00:00
|
|
|
),
|
2021-03-13 11:02:24 +00:00
|
|
|
],
|
2019-04-23 16:23:34 +00:00
|
|
|
),
|
|
|
|
|
2021-03-13 11:02:24 +00:00
|
|
|
onLongPressOpenMenu: (position) {
|
|
|
|
showMenu<_PopupMenuChoices>(
|
2022-03-18 18:11:06 +00:00
|
|
|
context: context,
|
|
|
|
position: position,
|
|
|
|
items: [
|
|
|
|
PopupMenuItem(
|
|
|
|
child: Text(tr("Update")!),
|
|
|
|
value: _PopupMenuChoices.UPDATE,
|
|
|
|
),
|
|
|
|
PopupMenuItem(
|
|
|
|
child: Text(tr("Leave")!),
|
|
|
|
value: _PopupMenuChoices.LEAVE,
|
|
|
|
)
|
|
|
|
]..addAll(srvConfig!.isReportingEnabled
|
|
|
|
? [
|
|
|
|
PopupMenuItem(
|
2022-03-18 18:21:08 +00:00
|
|
|
child: Text(tr("Report abuse")!),
|
2022-03-18 18:11:06 +00:00
|
|
|
value: _PopupMenuChoices.REPORT,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
: []))
|
|
|
|
.then(_conversationMenuCallback);
|
2021-03-13 11:02:24 +00:00
|
|
|
},
|
|
|
|
);
|
2019-05-01 08:52:10 +00:00
|
|
|
|
2021-03-13 11:02:24 +00:00
|
|
|
/// Build call tile, in case of ongoing call
|
|
|
|
Widget _buildCallTile(BuildContext context) {
|
|
|
|
if (!conversation.isHavingCall) return Container();
|
|
|
|
|
|
|
|
return Padding(
|
|
|
|
padding: EdgeInsets.only(bottom: 20),
|
|
|
|
child: ListTile(
|
2022-03-10 18:39:57 +00:00
|
|
|
onTap: () => MainController.of(context)!.startCall(conversation.id!),
|
2021-03-13 11:02:24 +00:00
|
|
|
dense: true,
|
2022-03-10 18:39:57 +00:00
|
|
|
title: Text(tr("Ongoing call")!),
|
2021-03-13 11:02:24 +00:00
|
|
|
leading: Icon(Icons.call),
|
|
|
|
tileColor: Colors.yellow.withOpacity(0.2),
|
|
|
|
),
|
2019-04-23 12:35:41 +00:00
|
|
|
);
|
|
|
|
}
|
2019-05-01 08:52:10 +00:00
|
|
|
|
|
|
|
/// Method called each time an option of the menu is selected
|
2022-03-10 18:39:57 +00:00
|
|
|
void _conversationMenuCallback(_PopupMenuChoices? c) {
|
2019-05-01 08:52:10 +00:00
|
|
|
switch (c) {
|
2019-05-01 08:58:45 +00:00
|
|
|
case _PopupMenuChoices.UPDATE:
|
|
|
|
onRequestUpdate(conversation);
|
|
|
|
break;
|
|
|
|
|
2021-03-13 11:07:16 +00:00
|
|
|
case _PopupMenuChoices.LEAVE:
|
|
|
|
onRequestLeave(conversation);
|
2019-05-01 08:52:10 +00:00
|
|
|
break;
|
2022-03-11 15:21:35 +00:00
|
|
|
|
2022-03-18 18:11:06 +00:00
|
|
|
case _PopupMenuChoices.REPORT:
|
|
|
|
onReport(conversation);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case null:
|
2022-03-11 15:21:35 +00:00
|
|
|
break;
|
2019-05-01 08:52:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-23 12:35:41 +00:00
|
|
|
}
|