diff --git a/lib/helpers/conversations_helper.dart b/lib/helpers/conversations_helper.dart index 29176d8..56b66c8 100644 --- a/lib/helpers/conversations_helper.dart +++ b/lib/helpers/conversations_helper.dart @@ -4,6 +4,7 @@ import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/helpers/websocket_helper.dart'; import 'package:comunic/lists/conversation_messages_list.dart'; import 'package:comunic/lists/conversations_list.dart'; +import 'package:comunic/lists/unread_conversations_list.dart'; import 'package:comunic/lists/users_list.dart'; import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/api_response.dart'; @@ -11,6 +12,7 @@ import 'package:comunic/models/conversation.dart'; import 'package:comunic/models/conversation_message.dart'; import 'package:comunic/models/displayed_content.dart'; import 'package:comunic/models/new_conversation_message.dart'; +import 'package:comunic/models/unread_conversation.dart'; import 'package:comunic/utils/account_utils.dart'; import 'package:meta/meta.dart'; @@ -385,6 +387,24 @@ class ConversationsHelper { return true; } + /// Get the list of unread conversations + /// + /// Throws in case of failure + static Future getListUnread() async { + final list = (await APIRequest.withLogin("conversations/get_list_unread") + .execWithThrow()) + .getArray(); + + return UnreadConversationsList() + ..addAll(list.map((f) => UnreadConversation( + id: f["id"], + convName: f["conv_name"], + lastActive: f["last_active"], + userID: f["userID"], + message: f["message"], + ))); + } + /// Register a conversation : ask the server to notify about updates to the /// conversation through WebSocket Future registerConversationEvents(int id) async { diff --git a/lib/lists/unread_conversations_list.dart b/lib/lists/unread_conversations_list.dart new file mode 100644 index 0000000..6ae604d --- /dev/null +++ b/lib/lists/unread_conversations_list.dart @@ -0,0 +1,11 @@ +import 'package:comunic/lists/abstract_list.dart'; +import 'package:comunic/models/unread_conversation.dart'; + +/// List of unread conversations +/// +/// @author Pierre Hubert + +class UnreadConversationsList extends AbstractList { + /// Get the ID of the users included in this list + Set get usersID => new Set()..addAll(map((f) => f.userID)); +} diff --git a/lib/models/unread_conversation.dart b/lib/models/unread_conversation.dart new file mode 100644 index 0000000..2088e2e --- /dev/null +++ b/lib/models/unread_conversation.dart @@ -0,0 +1,25 @@ +import 'package:flutter/material.dart'; + +/// Unread conversation information +/// +/// @author Pierre Hubert + +class UnreadConversation { + final int id; + final String convName; + final int lastActive; + final int userID; + final String message; + + const UnreadConversation({ + @required this.id, + @required this.convName, + @required this.lastActive, + @required this.userID, + @required this.message, + }) : assert(id != null), + assert(convName != null), + assert(lastActive != null), + assert(userID != null), + assert(message != null); +} diff --git a/lib/ui/screens/unread_conversations_screen.dart b/lib/ui/screens/unread_conversations_screen.dart new file mode 100644 index 0000000..e0ce2ab --- /dev/null +++ b/lib/ui/screens/unread_conversations_screen.dart @@ -0,0 +1,76 @@ +import 'package:comunic/helpers/conversations_helper.dart'; +import 'package:comunic/helpers/users_helper.dart'; +import 'package:comunic/lists/unread_conversations_list.dart'; +import 'package:comunic/lists/users_list.dart'; +import 'package:comunic/ui/widgets/account_image_widget.dart'; +import 'package:comunic/ui/widgets/async_screen_widget.dart'; +import 'package:comunic/utils/date_utils.dart'; +import 'package:comunic/utils/intl_utils.dart'; +import 'package:flutter/material.dart'; + +/// Unread conversations screen +/// +/// @author Pierre Hubert + +class UnreadConversationsScreen extends StatefulWidget { + @override + _UnreadConversationsScreenState createState() => + _UnreadConversationsScreenState(); +} + +class _UnreadConversationsScreenState extends State { + UnreadConversationsList _list; + UsersList _users; + + Future _refresh() async { + _list = await ConversationsHelper.getListUnread(); + _users = await UsersHelper().getListWithThrow(_list.usersID); + } + + @override + Widget build(BuildContext context) { + return AsyncScreenWidget( + onReload: _refresh, + onBuild: _buildList, + errorMessage: tr("Could not load the list of unread conversations!"), + ); + } + + Widget _buildList() { + // Check for no unread conversation + if (_list.isEmpty) + return Center( + child: Padding( + padding: const EdgeInsets.all(8.0), + child: Text( + tr("You do not have any unread conversation yet..."), + textAlign: TextAlign.center, + ), + ), + ); + + return ListView.builder( + itemBuilder: _tileBuilder, + itemCount: _list.length, + ); + } + + Widget _tileBuilder(BuildContext context, int index) { + final conv = _list[index]; + final user = _users.getUser(conv.userID); + return ListTile( + leading: AccountImageWidget(user: user), + title: Text(user.displayName), + subtitle: RichText( + text: TextSpan(style: Theme.of(context).textTheme.body1, children: [ + TextSpan(text: conv.convName.isNotEmpty ? conv.convName + "\n" : ""), + TextSpan( + text: conv.message, + style: TextStyle(fontStyle: FontStyle.italic), + ), + ]), + ), + trailing: Text(diffTimeFromNowToStr(conv.lastActive)), + ); + } +} diff --git a/lib/ui/widgets/tablet_mode/tablet_appbar_widget.dart b/lib/ui/widgets/tablet_mode/tablet_appbar_widget.dart index d1b837e..c946660 100644 --- a/lib/ui/widgets/tablet_mode/tablet_appbar_widget.dart +++ b/lib/ui/widgets/tablet_mode/tablet_appbar_widget.dart @@ -2,6 +2,7 @@ import 'package:comunic/helpers/events_helper.dart'; import 'package:comunic/helpers/notifications_helper.dart'; import 'package:comunic/models/count_unread_notifications.dart'; import 'package:comunic/ui/screens/notifications_screen.dart'; +import 'package:comunic/ui/screens/unread_conversations_screen.dart'; import 'package:comunic/ui/widgets/safe_state.dart'; import 'package:comunic/ui/widgets/tablet_mode/appbar_custom_dropdown_widget.dart'; import 'package:flutter/material.dart'; @@ -71,13 +72,7 @@ class _ComunicTabletAppBarWidgetState key: conversationsDropdownKey, icon: Icon(Icons.message), notificationsBadge: _unreadNotifications.conversations, - onBuildOverlay: (c) => Center( - child: RaisedButton( - child: Text("Close"), - onPressed: () => - conversationsDropdownKey.currentState.toggleOverlay(), - ), - ), + onBuildOverlay: (c) => UnreadConversationsScreen(), ), PopupMenuButton(itemBuilder: (c) => []), ],