From 0579397ba7d45be6d8561dbc174cfcf1859aa44a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 27 Apr 2019 09:24:29 +0200 Subject: [PATCH] Use the conversation messages cache before going online --- lib/helpers/conversations_helper.dart | 13 +++++++-- ...conversation_messages_database_helper.dart | 22 +++++++++++++-- .../database/model_database_helper.dart | 28 ++++++++++++++++++- lib/ui/screens/conversation_screen.dart | 21 ++++++++++++-- 4 files changed, 74 insertions(+), 10 deletions(-) diff --git a/lib/helpers/conversations_helper.dart b/lib/helpers/conversations_helper.dart index 16ae66b..34828e1 100644 --- a/lib/helpers/conversations_helper.dart +++ b/lib/helpers/conversations_helper.dart @@ -176,9 +176,16 @@ class ConversationsHelper { /// 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); + {@required int conversationID, + int lastMessageID = 0, + bool online = true}) async { + if (online) + return await _downloadNewMessagesSingle(conversationID, + lastMessageID: lastMessageID); + else + return await _conversationMessagesDatabaseHelper + .getAllMessagesConversations(conversationID, + lastMessageID: lastMessageID); } /// Send a new message to the server diff --git a/lib/helpers/database/conversation_messages_database_helper.dart b/lib/helpers/database/conversation_messages_database_helper.dart index 58fe2a4..7a30f95 100644 --- a/lib/helpers/database/conversation_messages_database_helper.dart +++ b/lib/helpers/database/conversation_messages_database_helper.dart @@ -1,13 +1,14 @@ import 'package:comunic/helpers/database/database_contract.dart'; import 'package:comunic/helpers/database/model_database_helper.dart'; +import 'package:comunic/lists/conversation_messages_list.dart'; import 'package:comunic/models/conversation_message.dart'; /// Conversation messages database helper /// /// @author Pierre HUBERT -class ConversationMessagesDatabaseHelper extends ModelDatabaseHelper { - +class ConversationMessagesDatabaseHelper + extends ModelDatabaseHelper { @override ConversationMessage initializeFromMap(Map map) { return ConversationMessage.fromMap(map); @@ -18,4 +19,19 @@ class ConversationMessagesDatabaseHelper extends ModelDatabaseHelper getAllMessagesConversations( + int conversationID, + {int lastMessageID = 0}) async { + final list = await getMultiple( + where: "${ConversationsMessagesTableContract.C_CONVERSATION_ID} = ? " + "AND ${BaseTableContract.C_ID} > ?", + whereArgs: [conversationID, lastMessageID], + ); + + // Turn the list into a conversation messages list + ConversationMessagesList finalList = ConversationMessagesList(); + finalList.addAll(list); + return finalList; + } +} diff --git a/lib/helpers/database/model_database_helper.dart b/lib/helpers/database/model_database_helper.dart index d3ac1ef..b20ad01 100644 --- a/lib/helpers/database/model_database_helper.dart +++ b/lib/helpers/database/model_database_helper.dart @@ -48,7 +48,33 @@ abstract class ModelDatabaseHelper { /// Get all the entries from the table Future> getAll() async { - List maps = await(await DatabaseHelper.get()).query(tableName()); + List maps = await (await DatabaseHelper.get()).query(tableName()); + return maps.map((f) => initializeFromMap(f)).toList(); + } + + /// Get some entries from the table based on some conditions + Future> getMultiple( + {bool distinct, + List columns, + String where, + List whereArgs, + String groupBy, + String having, + String orderBy, + int limit, + int offset}) async { + List maps = await (await DatabaseHelper.get()).query( + tableName(), + distinct: distinct, + columns: columns, + where: where, + whereArgs: whereArgs, + groupBy: groupBy, + having: having, + orderBy: orderBy, + limit: limit, + offset: offset, + ); return maps.map((f) => initializeFromMap(f)).toList(); } diff --git a/lib/ui/screens/conversation_screen.dart b/lib/ui/screens/conversation_screen.dart index 8640147..cfa80f5 100644 --- a/lib/ui/screens/conversation_screen.dart +++ b/lib/ui/screens/conversation_screen.dart @@ -43,7 +43,7 @@ class _ConversationScreenState extends State { @override void didChangeDependencies() { super.didChangeDependencies(); - _loadMessages(); + _initializeLoading(); } void _setError(ErrorLevel err) => setState(() => _error = err); @@ -55,15 +55,27 @@ class _ConversationScreenState extends State { _setError(_messages == null ? ErrorLevel.MAJOR : ErrorLevel.MINOR); } + /// Load the first conversations + Future _initializeLoading() async { + await _loadMessages(false); + await _loadMessages(true); + } + /// Load a list of messages - Future _loadMessages() async { + Future _loadMessages(bool online) async { //First, get the messages final messages = await _conversationsHelper.getNewMessages( conversationID: widget.conversationID, - lastMessageID: _messages == null ? 0 : _messages.lastMessageID); + lastMessageID: _messages == null ? 0 : _messages.lastMessageID, + online: online); if (messages == null) return _errorLoading(); + // In case we are offline and we did not get any message we do not do + // anything (we wait for the online request) + if(messages.length == 0 && !online) + return; + //Then get information about users final usersToGet = findMissingFromList(_usersInfo.usersID, messages.getUsersID()); @@ -254,6 +266,9 @@ class _ConversationScreenState extends State { return Column( children: [ + Container( + child: _error == ErrorLevel.MINOR ? _buildError() : null, + ), _buildMessagesList(), Divider(), _buildSendMessageForm()