fixed deletion off the main list and offline storage

This commit is contained in:
Deven Joshi
2021-11-25 16:54:23 +05:30
parent 5916a282d1
commit 065df414df
4 changed files with 27 additions and 5 deletions
@@ -1663,7 +1663,11 @@ class ChannelClientState {
void _listenMessageDeleted() {
_subscriptions.add(_channel.on(EventType.messageDeleted).listen((event) {
final message = event.message!;
addMessage(message);
if (event.hardDelete == true) {
removeMessage(message, hardDelete: true);
} else {
addMessage(message);
}
}));
}
@@ -1718,7 +1722,7 @@ class ChannelClientState {
}
/// Remove a [message] from this [channelState].
void removeMessage(Message message) {
void removeMessage(Message message, {bool hardDelete = false}) {
final parentId = message.parentId;
// i.e. it's a thread message
// 1. Remove the thread message
@@ -1740,7 +1744,10 @@ class ChannelClientState {
} else {
// Remove regular message
final allMessages = [...messages];
if (allMessages.remove(message)) {
if (hardDelete) {
allMessages.removeWhere((e) => e.id == message.id);
_channelState = _channelState.copyWith(messages: allMessages);
} else if (allMessages.remove(message)) {
_channelState = _channelState.copyWith(messages: allMessages);
}
}
@@ -1213,8 +1213,14 @@ class StreamChatClient {
);
/// Deletes the given message
Future<EmptyResponse> deleteMessage(String messageId, {bool? hard}) =>
_chatApi.message.deleteMessage(messageId, hard: hard);
Future<EmptyResponse> deleteMessage(String messageId, {bool? hard}) async {
final response =
await _chatApi.message.deleteMessage(messageId, hard: hard);
if (hard == true) {
await _chatPersistenceClient?.deleteMessageById(messageId);
}
return response;
}
/// Get a message by [messageId]
Future<GetMessageResponse> getMessage(String messageId) =>
@@ -27,6 +27,7 @@ class Event {
this.channelId,
this.channelType,
this.parentId,
this.hardDelete,
this.extraData = const {},
this.isLocal = true,
}) : createdAt = createdAt?.toUtc() ?? DateTime.now().toUtc();
@@ -91,6 +92,9 @@ class Event {
@JsonKey(defaultValue: false)
final bool isLocal;
/// This is true if the message has been hard deleted
final bool? hardDelete;
/// Map of custom channel extraData
final Map<String, Object?> extraData;
@@ -113,6 +117,7 @@ class Event {
'channel_id',
'channel_type',
'parent_id',
'hard_delete',
'is_local',
];
@@ -139,6 +144,7 @@ class Event {
int? unreadChannels,
bool? online,
String? parentId,
bool? hardDelete,
Map<String, Object?>? extraData,
}) =>
Event(
@@ -158,6 +164,7 @@ class Event {
channelId: channelId ?? this.channelId,
channelType: channelType ?? this.channelType,
parentId: parentId ?? this.parentId,
hardDelete: hardDelete ?? this.hardDelete,
extraData: extraData ?? this.extraData,
isLocal: isLocal,
);
@@ -37,6 +37,7 @@ Event _$EventFromJson(Map<String, dynamic> json) => Event(
channelId: json['channel_id'] as String?,
channelType: json['channel_type'] as String?,
parentId: json['parent_id'] as String?,
hardDelete: json['hard_delete'] as bool?,
extraData: json['extra_data'] as Map<String, dynamic>? ?? const {},
isLocal: json['is_local'] as bool? ?? false,
);
@@ -59,6 +60,7 @@ Map<String, dynamic> _$EventToJson(Event instance) => <String, dynamic>{
'online': instance.online,
'parent_id': instance.parentId,
'is_local': instance.isLocal,
'hard_delete': instance.hardDelete,
'extra_data': instance.extraData,
};