1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-29 14:28:51 +00:00
comunicmobile/lib/helpers/database/conversation_messages_database_helper.dart

61 lines
1.8 KiB
Dart
Raw Normal View History

2019-04-27 06:42:27 +00:00
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';
2019-04-27 06:42:27 +00:00
import 'package:comunic/models/conversation_message.dart';
2019-05-04 08:24:38 +00:00
import 'package:meta/meta.dart';
2019-04-27 06:42:27 +00:00
/// Conversation messages database helper
///
/// @author Pierre HUBERT
class ConversationMessagesDatabaseHelper
extends ModelDatabaseHelper<ConversationMessage> {
2019-04-27 06:42:27 +00:00
@override
ConversationMessage initializeFromMap(Map<String, dynamic> map) {
return ConversationMessage.fromMap(map);
}
@override
String tableName() {
return ConversationsMessagesTableContract.TABLE_NAME;
}
/// Get all the message cached for a given conversation
Future<ConversationMessagesList> 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;
}
2019-05-04 08:24:38 +00:00
/// Update the content of a message
Future<bool> updateMessageContent({
@required int id,
@required String newContent,
}) async {
assert(id != null);
assert(newContent != null);
final message = await get(id);
if(message == null)
return false;
// Update the conversation message using the map
final map = message.toMap();
map[ConversationsMessagesTableContract.C_MESSAGE] = newContent;
await insertOrUpdate(ConversationMessage.fromMap(map));
return true; // Success
}
}