From c9eb57586f30159cf1b8faf3aab1a254b1dff0ce Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 27 Apr 2019 08:42:27 +0200 Subject: [PATCH] Cache conversation messages --- lib/helpers/conversations_helper.dart | 17 +++++---- ...conversation_messages_database_helper.dart | 21 +++++++++++ lib/helpers/database/database_contract.dart | 11 ++++++ lib/helpers/database/database_helper.dart | 23 +++++++++--- lib/models/conversation_message.dart | 35 ++++++++++++++++--- 5 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 lib/helpers/database/conversation_messages_database_helper.dart diff --git a/lib/helpers/conversations_helper.dart b/lib/helpers/conversations_helper.dart index 5e7429a..e30220a 100644 --- a/lib/helpers/conversations_helper.dart +++ b/lib/helpers/conversations_helper.dart @@ -1,3 +1,4 @@ +import 'package:comunic/helpers/database/conversation_messages_database_helper.dart'; import 'package:comunic/helpers/database/conversations_database_helper.dart'; import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/lists/conversation_messages_list.dart'; @@ -19,6 +20,8 @@ enum SendMessageResult { SUCCESS, MESSAGE_REJECTED, FAILED } class ConversationsHelper { final ConversationsDatabaseHelper _conversationsDatabaseHelper = ConversationsDatabaseHelper(); + final ConversationMessagesDatabaseHelper _conversationMessagesDatabaseHelper = + ConversationMessagesDatabaseHelper(); /// Download the list of conversations from the server Future downloadList() async { @@ -152,12 +155,16 @@ class ConversationsHelper { response.getArray().forEach((f) { list.add(ConversationMessage( id: f["ID"], + conversationID: conversationID, userID: f["ID_user"], timeInsert: f["time_insert"], message: f["message"], imageURL: f["image_path"])); }); + // Save messages in the cache + _conversationMessagesDatabaseHelper.insertOrUpdateAll(list); + return list; } @@ -173,20 +180,18 @@ class ConversationsHelper { ); //Check for image - if(message.hasImage) - request.addFile("image", message.image); + if (message.hasImage) request.addFile("image", message.image); //Send the message APIResponse response; - if(!message.hasImage) + if (!message.hasImage) response = await request.exec(); else response = await request.execWithFiles(); - if(response.code == 401) + if (response.code == 401) return SendMessageResult.MESSAGE_REJECTED; - else if(response.code != 200) - return SendMessageResult.FAILED; + else if (response.code != 200) return SendMessageResult.FAILED; return SendMessageResult.SUCCESS; } diff --git a/lib/helpers/database/conversation_messages_database_helper.dart b/lib/helpers/database/conversation_messages_database_helper.dart new file mode 100644 index 0000000..58fe2a4 --- /dev/null +++ b/lib/helpers/database/conversation_messages_database_helper.dart @@ -0,0 +1,21 @@ +import 'package:comunic/helpers/database/database_contract.dart'; +import 'package:comunic/helpers/database/model_database_helper.dart'; +import 'package:comunic/models/conversation_message.dart'; + +/// Conversation messages database helper +/// +/// @author Pierre HUBERT + +class ConversationMessagesDatabaseHelper extends ModelDatabaseHelper { + + @override + ConversationMessage initializeFromMap(Map map) { + return ConversationMessage.fromMap(map); + } + + @override + String tableName() { + return ConversationsMessagesTableContract.TABLE_NAME; + } + +} \ No newline at end of file diff --git a/lib/helpers/database/database_contract.dart b/lib/helpers/database/database_contract.dart index 871d084..ac116e5 100644 --- a/lib/helpers/database/database_contract.dart +++ b/lib/helpers/database/database_contract.dart @@ -34,4 +34,15 @@ abstract class ConversationTableContract { static const C_FOLLOWING = "following"; static const C_SAW_LAST_MESSAGE = "saw_last_message"; static const C_MEMBERS = "members"; +} + +/// Conversations messages table contract +abstract class ConversationsMessagesTableContract { + static const TABLE_NAME = "conversations_messages"; + static const C_ID = BaseTableContract.C_ID; + static const C_CONVERSATION_ID = "conversation_id"; + static const C_USER_ID = "user_id"; + static const C_TIME_INSERT = "time_insert"; + static const C_MESSAGE = "message"; + static const C_IMAGE_URL = "image_url"; } \ No newline at end of file diff --git a/lib/helpers/database/database_helper.dart b/lib/helpers/database/database_helper.dart index 94589eb..0e60a32 100644 --- a/lib/helpers/database/database_helper.dart +++ b/lib/helpers/database/database_helper.dart @@ -38,8 +38,12 @@ abstract class DatabaseHelper { await db.execute("DROP TABLE IF EXISTS ${UserTableContract.TABLE_NAME}"); // Drop conversations table - await db - .execute("DROP TABLE IF EXISTS ${ConversationTableContract.TABLE_NAME}"); + await db.execute( + "DROP TABLE IF EXISTS ${ConversationTableContract.TABLE_NAME}"); + + // Drop conversations messages table + await db.execute( + "DROP TABLE IF EXISTS ${ConversationsMessagesTableContract.TABLE_NAME}"); // Initialize database again await _initializeDatabase(db, newVersion); @@ -47,7 +51,7 @@ abstract class DatabaseHelper { /// Initialize the database static Future _initializeDatabase(Database db, int version) async { - //Create users table + // Create users table await db.execute("CREATE TABLE ${UserTableContract.TABLE_NAME} (" "${UserTableContract.C_ID} INTEGER PRIMARY KEY, " "${UserTableContract.C_FIRST_NAME} TEXT, " @@ -57,7 +61,7 @@ abstract class DatabaseHelper { "${UserTableContract.C_ACCOUNT_IMAGE_URL} TEXT" ")"); - //Create conversations table + // Create conversations table await db.execute("CREATE TABLE ${ConversationTableContract.TABLE_NAME} (" "${ConversationTableContract.C_ID} INTEGER PRIMARY KEY, " "${ConversationTableContract.C_OWNER_ID} INTEGER, " @@ -67,5 +71,16 @@ abstract class DatabaseHelper { "${ConversationTableContract.C_SAW_LAST_MESSAGE} INTEGER, " "${ConversationTableContract.C_MEMBERS} TEXT" ")"); + + // Create conversation messages table + await db.execute( + "CREATE TABLE ${ConversationsMessagesTableContract.TABLE_NAME} (" + "${ConversationsMessagesTableContract.C_ID} INTEGER PRIMARY KEY, " + "${ConversationsMessagesTableContract.C_CONVERSATION_ID} INTEGER, " + "${ConversationsMessagesTableContract.C_USER_ID} INTEGER, " + "${ConversationsMessagesTableContract.C_TIME_INSERT} INTEGER, " + "${ConversationsMessagesTableContract.C_MESSAGE} TEXT, " + "${ConversationsMessagesTableContract.C_IMAGE_URL} TEXT" + ")"); } } diff --git a/lib/models/conversation_message.dart b/lib/models/conversation_message.dart index 787f3e0..00fa2e2 100644 --- a/lib/models/conversation_message.dart +++ b/lib/models/conversation_message.dart @@ -1,11 +1,14 @@ +import 'package:comunic/helpers/database/database_contract.dart'; +import 'package:comunic/models/cache_model.dart'; import 'package:meta/meta.dart'; /// Single conversation message /// /// @author Pierre HUBERT -class ConversationMessage implements Comparable { +class ConversationMessage extends CacheModel implements Comparable { final int id; + final int conversationID; final int userID; final int timeInsert; final String message; @@ -13,6 +16,7 @@ class ConversationMessage implements Comparable { const ConversationMessage({ @required this.id, + @required this.conversationID, @required this.userID, @required this.timeInsert, @required this.message, @@ -20,15 +24,38 @@ class ConversationMessage implements Comparable { }) : assert(id != null), assert(userID != null), assert(timeInsert != null), - assert(message != null); + assert(message != null), + super(id: id); DateTime get date => DateTime.fromMillisecondsSinceEpoch(timeInsert * 1000); - bool get hasMessage => message != null && message.length > 0; - bool get hasImage => imageURL != null; + bool get hasMessage => message != null && message.length > 0; + + bool get hasImage => imageURL != null && imageURL != "null"; @override int compareTo(other) { return id.compareTo(other.id); } + + @override + Map toMap() { + return { + ConversationsMessagesTableContract.C_ID: id, + ConversationsMessagesTableContract.C_CONVERSATION_ID: conversationID, + ConversationsMessagesTableContract.C_USER_ID: userID, + ConversationsMessagesTableContract.C_TIME_INSERT: timeInsert, + ConversationsMessagesTableContract.C_MESSAGE: message, + ConversationsMessagesTableContract.C_IMAGE_URL: imageURL + }; + } + + ConversationMessage.fromMap(Map map) + : id = map[ConversationsMessagesTableContract.C_ID], + conversationID = map[ConversationsMessagesTableContract.C_CONVERSATION_ID], + userID = map[ConversationsMessagesTableContract.C_USER_ID], + timeInsert = map[ConversationsMessagesTableContract.C_TIME_INSERT], + message = map[ConversationsMessagesTableContract.C_MESSAGE], + imageURL = map[ConversationsMessagesTableContract.C_IMAGE_URL], + super.fromMap(map); }