mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-30 00:36:28 +00:00
61 lines
1.8 KiB
Dart
61 lines
1.8 KiB
Dart
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';
|
|
import 'package:meta/meta.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;
|
|
}
|
|
|
|
/// 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;
|
|
}
|
|
|
|
/// 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
|
|
}
|
|
}
|