diff --git a/lib/helpers/conversations_helper.dart b/lib/helpers/conversations_helper.dart index e30220a..16ae66b 100644 --- a/lib/helpers/conversations_helper.dart +++ b/lib/helpers/conversations_helper.dart @@ -10,6 +10,7 @@ import 'package:comunic/models/conversation.dart'; import 'package:comunic/models/conversation_message.dart'; import 'package:comunic/models/new_conversation_message.dart'; import 'package:comunic/utils/account_utils.dart'; +import 'package:meta/meta.dart'; /// Conversation helper /// @@ -137,7 +138,8 @@ class ConversationsHelper { /// /// Set [lastMessageID] to 0 to specify that we do not have any message of the /// conversation yet or another value else - Future downloadNewMessagesSingle(int conversationID, + Future _downloadNewMessagesSingle( + int conversationID, {int lastMessageID = 0}) async { // Execute the request on the server final response = await APIRequest( @@ -168,6 +170,17 @@ class ConversationsHelper { return list; } + /// Get new messages for a given conversation + /// + /// If [lastMessageID] is set to 0 then we retrieve the last messages of + /// the conversation. + /// Otherwise [lastMessageID] contains the ID of the last known message + Future getNewMessages( + {@required int conversationID, int lastMessageID = 0}) async { + return await _downloadNewMessagesSingle(conversationID, + lastMessageID: lastMessageID); + } + /// Send a new message to the server Future sendMessage(NewConversationMessage message) async { final request = APIRequest( diff --git a/lib/lists/conversation_messages_list.dart b/lib/lists/conversation_messages_list.dart index 527fa58..98616ab 100644 --- a/lib/lists/conversation_messages_list.dart +++ b/lib/lists/conversation_messages_list.dart @@ -32,4 +32,12 @@ class ConversationMessagesList extends ListBase { return users; } + + /// Get the ID of the last message present in this list + int get lastMessageID { + int lastMessageID = 0; + for (ConversationMessage message in this) + if (message.id > lastMessageID) lastMessageID = message.id; + return lastMessageID; + } } diff --git a/lib/ui/screens/conversation_screen.dart b/lib/ui/screens/conversation_screen.dart index c9e2701..8640147 100644 --- a/lib/ui/screens/conversation_screen.dart +++ b/lib/ui/screens/conversation_screen.dart @@ -58,8 +58,9 @@ class _ConversationScreenState extends State { /// Load a list of messages Future _loadMessages() async { //First, get the messages - final messages = await _conversationsHelper - .downloadNewMessagesSingle(widget.conversationID); + final messages = await _conversationsHelper.getNewMessages( + conversationID: widget.conversationID, + lastMessageID: _messages == null ? 0 : _messages.lastMessageID); if (messages == null) return _errorLoading(); @@ -81,7 +82,7 @@ class _ConversationScreenState extends State { //Reverse the order of the messages _messages.sort(); - final reverse =_messages.reversed; + final reverse = _messages.reversed; _messages = ConversationMessagesList(); _messages.addAll(reverse); }); @@ -164,7 +165,8 @@ class _ConversationScreenState extends State { /// Check if a message is the first message of a user or not bool _isFirstMessage(int index) { return index == _messages.length - 1 || - (index < _messages.length - 1 && _messages[index + 1].userID != _messages[index].userID); + (index < _messages.length - 1 && + _messages[index + 1].userID != _messages[index].userID); } /// Error handling @@ -176,7 +178,7 @@ class _ConversationScreenState extends State { Widget _buildMessagesList() { return Expanded( child: ListView.builder( - reverse: true, + reverse: true, itemCount: _messages.length, itemBuilder: (c, i) { return ConversationMessageTile(