1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 06:49:22 +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/conversation_message.dart';
import 'package:comunic/models/new_conversation_message.dart'; import 'package:comunic/models/new_conversation_message.dart';
import 'package:comunic/utils/account_utils.dart'; import 'package:comunic/utils/account_utils.dart';
import 'package:meta/meta.dart';
/// Conversation helper /// Conversation helper
/// ///
@ -137,7 +138,8 @@ class ConversationsHelper {
/// ///
/// Set [lastMessageID] to 0 to specify that we do not have any message of the /// Set [lastMessageID] to 0 to specify that we do not have any message of the
/// conversation yet or another value else /// conversation yet or another value else
Future<ConversationMessagesList> downloadNewMessagesSingle(int conversationID, Future<ConversationMessagesList> _downloadNewMessagesSingle(
int conversationID,
{int lastMessageID = 0}) async { {int lastMessageID = 0}) async {
// Execute the request on the server // Execute the request on the server
final response = await APIRequest( final response = await APIRequest(
@ -168,6 +170,17 @@ class ConversationsHelper {
return list; 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 /// Send a new message to the server
Future<SendMessageResult> sendMessage(NewConversationMessage message) async { Future<SendMessageResult> sendMessage(NewConversationMessage message) async {
final request = APIRequest( final request = APIRequest(

View File

@ -32,4 +32,12 @@ class ConversationMessagesList extends ListBase<ConversationMessage> {
return users; 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 /// Load a list of messages
Future<void> _loadMessages() async { Future<void> _loadMessages() async {
//First, get the messages //First, get the messages
final messages = await _conversationsHelper final messages = await _conversationsHelper.getNewMessages(
.downloadNewMessagesSingle(widget.conversationID); conversationID: widget.conversationID,
lastMessageID: _messages == null ? 0 : _messages.lastMessageID);
if (messages == null) return _errorLoading(); if (messages == null) return _errorLoading();
@ -81,7 +82,7 @@ class _ConversationScreenState extends State<ConversationScreen> {
//Reverse the order of the messages //Reverse the order of the messages
_messages.sort(); _messages.sort();
final reverse =_messages.reversed; final reverse = _messages.reversed;
_messages = ConversationMessagesList(); _messages = ConversationMessagesList();
_messages.addAll(reverse); _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 /// Check if a message is the first message of a user or not
bool _isFirstMessage(int index) { bool _isFirstMessage(int index) {
return index == _messages.length - 1 || 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 /// Error handling
@ -176,7 +178,7 @@ class _ConversationScreenState extends State<ConversationScreen> {
Widget _buildMessagesList() { Widget _buildMessagesList() {
return Expanded( return Expanded(
child: ListView.builder( child: ListView.builder(
reverse: true, reverse: true,
itemCount: _messages.length, itemCount: _messages.length,
itemBuilder: (c, i) { itemBuilder: (c, i) {
return ConversationMessageTile( return ConversationMessageTile(