1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 14:29:22 +00:00

Cache conversation messages

This commit is contained in:
Pierre HUBERT 2019-04-27 08:42:27 +02:00
parent eb5b6d6fae
commit c9eb57586f
5 changed files with 93 additions and 14 deletions

View File

@ -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<ConversationsList> 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;
}

View File

@ -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<ConversationMessage> {
@override
ConversationMessage initializeFromMap(Map<String, dynamic> map) {
return ConversationMessage.fromMap(map);
}
@override
String tableName() {
return ConversationsMessagesTableContract.TABLE_NAME;
}
}

View File

@ -35,3 +35,14 @@ abstract class ConversationTableContract {
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";
}

View File

@ -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<void> _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"
")");
}
}

View File

@ -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<String, dynamic> 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<String, dynamic> 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);
}