1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59:21 +00:00

Do not retrieve twice the same message

This commit is contained in:
Pierre HUBERT 2019-04-27 08:51:58 +02:00
parent c9eb57586f
commit eaac6b1afa
3 changed files with 29 additions and 6 deletions

View File

@ -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<ConversationMessagesList> downloadNewMessagesSingle(int conversationID,
Future<ConversationMessagesList> _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<ConversationMessagesList> getNewMessages(
{@required int conversationID, int lastMessageID = 0}) async {
return await _downloadNewMessagesSingle(conversationID,
lastMessageID: lastMessageID);
}
/// Send a new message to the server
Future<SendMessageResult> sendMessage(NewConversationMessage message) async {
final request = APIRequest(

View File

@ -32,4 +32,12 @@ class ConversationMessagesList extends ListBase<ConversationMessage> {
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;
}
}

View File

@ -58,8 +58,9 @@ class _ConversationScreenState extends State<ConversationScreen> {
/// Load a list of messages
Future<void> _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<ConversationScreen> {
//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<ConversationScreen> {
/// 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<ConversationScreen> {
Widget _buildMessagesList() {
return Expanded(
child: ListView.builder(
reverse: true,
reverse: true,
itemCount: _messages.length,
itemBuilder: (c, i) {
return ConversationMessageTile(