Skip to content

Commit 96604c5

Browse files
author
Magellan
committed
fix user null
fix markAsRead fix notification not always shown issue
1 parent fd97ea7 commit 96604c5

7 files changed

Lines changed: 51 additions & 26 deletions

File tree

lib/src/db/db_service.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ class DatabaseService {
197197
}
198198
}
199199

200+
Stream<ConversationModel?> watchedConversation(String id) {
201+
return store!
202+
.box<ConversationModel>()
203+
.query(ConversationModel_.id.equals(id))
204+
.watch(triggerImmediately: true)
205+
.map((query) => query.findFirst());
206+
}
207+
200208
/// ////////////////////////
201209
/// User Store Functions ///
202210
/// ////////////////////////
@@ -288,8 +296,14 @@ class DatabaseService {
288296
assignMessage(message, messageInDb);
289297
}
290298
}
299+
try {
300+
await store!.box<MessageModel>().putManyAsync(items, mode: PutMode.put);
301+
} on UniqueViolationException catch (e) {
302+
final id = int.tryParse(e.message.split(' ').last) ?? 0;
303+
print('saveMessagesLocal UniqueViolationException e id= $id');
304+
return saveMessagesLocal(items);
305+
}
291306

292-
await store!.box<MessageModel>().putManyAsync(items, mode: PutMode.put);
293307
return true;
294308
}
295309

lib/src/db/local/message_local_datasource.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import '../../shared/errors/exceptions.dart';
22
import '../db_service.dart';
3-
import '../models/message_model.dart';
3+
import '../models/models.dart';
44

55
class MessageLocalDatasource {
66
final DatabaseService _databaseService = DatabaseService.instance;
@@ -86,4 +86,13 @@ class MessageLocalDatasource {
8686
throw DatabaseException(e.toString());
8787
}
8888
}
89+
90+
Stream<ConversationModel?> watchedConversation(String id) {
91+
print('watchedConversation');
92+
try {
93+
return _databaseService.watchedConversation(id);
94+
} catch (e) {
95+
throw DatabaseException(e.toString());
96+
}
97+
}
8998
}

lib/src/features/conversation/bloc/conversation_bloc.dart

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
3737
final MessagesRepository messagesRepository;
3838
final UserRepository userRepository;
3939

40-
StreamSubscription<ConversationModel>? updateConversationStreamSubscription;
4140
StreamSubscription<ChatMessage>? incomingMessagesSubscription;
4241
StreamSubscription<MessageSendStatus>? statusMessagesSubscription;
42+
StreamSubscription<ConversationModel?>? conversationWatcher;
4343

4444
ConversationBloc({
4545
required this.currentConversation,
@@ -48,7 +48,7 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
4848
required this.userRepository,
4949
}) : super(ConversationState(
5050
conversation: currentConversation,
51-
participants: currentConversation.participants.toSet())) {
51+
participants: Set.of(currentConversation.participants))) {
5252
on<MessagesRequested>(_onMessagesRequested);
5353
on<MessagesMoreRequested>(
5454
_onMessagesMoreRequested,
@@ -79,16 +79,6 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
7979

8080
add(const ParticipantsReceived());
8181

82-
updateConversationStreamSubscription =
83-
conversationRepository.updateConversationStream.listen((chat) async {
84-
if (chat.id != currentConversation.id) return;
85-
86-
if (currentConversation != chat) {
87-
currentConversation = currentConversation.copyWithItem(item: chat);
88-
add(_ConversationUpdated(currentConversation));
89-
}
90-
});
91-
9282
incomingMessagesSubscription =
9383
messagesRepository.incomingMessagesStream.listen((message) async {
9484
if (message.cid != currentConversation.id) return;
@@ -117,6 +107,15 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
117107
break;
118108
}
119109
});
110+
111+
conversationWatcher = messagesRepository.localDatasource
112+
.watchedConversation(currentConversation.id)
113+
.listen((chat) {
114+
if (chat != null && chat != currentConversation) {
115+
currentConversation = currentConversation.copyWithItem(item: chat);
116+
add(_ConversationUpdated(currentConversation));
117+
}
118+
});
120119
}
121120

122121
Future<void> _onMessagesRequested(
@@ -132,7 +131,7 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
132131
status: ConversationStatus.success,
133132
messages: messages,
134133
hasReachedMax: false,
135-
participants: currentConversation.participants.toSet(),
134+
participants: Set.of(currentConversation.participants),
136135
initial: true),
137136
);
138137
add(const MessagesRequested());
@@ -188,8 +187,8 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
188187

189188
Future<void> _onParticipantsReceived(
190189
ParticipantsReceived event, Emitter<ConversationState> emit) async {
191-
var participants =
192-
await conversationRepository.updateParticipants(currentConversation);
190+
var participants = await conversationRepository
191+
.updateParticipants(currentConversation.copyWith());
193192
emit(state.copyWith(participants: Set.of(participants)));
194193
}
195194

@@ -277,9 +276,9 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
277276

278277
@override
279278
Future<void> close() {
280-
updateConversationStreamSubscription?.cancel();
281279
incomingMessagesSubscription?.cancel();
282280
statusMessagesSubscription?.cancel();
281+
conversationWatcher?.cancel();
283282
return super.close();
284283
}
285284
}

lib/src/features/group_info/bloc/group_info_bloc.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class GroupInfoBloc extends Bloc<GroupInfoEvent, GroupInfoState> {
2929
on<GroupInfoResetChanges>(_onResetChanges);
3030
on<GroupInfoSubmitted>(_onSubmitted);
3131

32-
add(GroupParticipantsReceived(conversation.participants.toList()));
32+
add(GroupParticipantsReceived(conversation.participants));
3333

3434
_conversationRepository
3535
.getParticipants([state.conversation.id]).then((users) {

lib/src/features/group_info/view/group_info_form.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,13 @@ class _ParticipantsListForm extends StatelessWidget {
312312
@override
313313
Widget build(BuildContext context) {
314314
var state = context.read<GroupInfoBloc>().state;
315-
315+
var participants = state.participants.value.toList()
316+
..sort((a, b) => a.login!.compareTo(b.login!));
316317
return ListView.builder(
317318
shrinkWrap: true,
318-
itemCount: state.participants.value.length,
319+
itemCount: participants.length,
319320
itemBuilder: (BuildContext context, int index) {
320-
final user = state.participants.value.elementAt(index);
321+
final user = participants.elementAt(index);
321322
return ListTile(
322323
leading: AvatarLetterIcon(name: user.login!, avatar: user.avatar),
323324
title:

lib/src/repository/messages/messages_repository.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class MessagesRepository {
6565
await userRepository.getUserById(message.from!);
6666
var isOwn = currentUser?.id == message.from;
6767
var chatMessage = message.toChatMessage(
68-
sender,
68+
sender?? UserModel(),
6969
isOwn,
7070
i == 0 ||
7171
isServiceMessage(data[i - 1]) ||
@@ -123,7 +123,7 @@ class MessagesRepository {
123123
var isOwn = currentUser?.id == message.from;
124124

125125
var chatMessage = message.toChatMessage(
126-
sender,
126+
sender?? UserModel(),
127127
isOwn,
128128
i == 0 ||
129129
isServiceMessage(messages[i - 1]) ||

lib/src/repository/user/user_repository.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ class UserRepository {
9191
Future<UserModel?> getUserById(String id) async {
9292
var user = await localDataSource.getUserLocal(id);
9393
if (user == null) {
94-
user = (await api.getUsersByIds({id})).first.toUserModel();
95-
user = (await localDataSource.saveUsersLocal([user])).first;
94+
user = (await api.getUsersByIds({id})).firstOrNull?.toUserModel();
95+
if(user != null) {
96+
user = (await localDataSource.saveUsersLocal([user])).first;
97+
}
9698
}
9799
return user;
98100
}

0 commit comments

Comments
 (0)