Skip to content

Commit

Permalink
Fix message seen issue
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-radhika-s committed Dec 31, 2024
1 parent 1957e46 commit 2cec9d4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
6 changes: 2 additions & 4 deletions app/lib/ui/flow/message/chat/chat_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:collection/collection.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:data/api/auth/auth_models.dart';
import 'package:data/api/message/message_models.dart';
import 'package:data/api/space/space_models.dart';
Expand Down Expand Up @@ -139,13 +138,12 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
: null;
final isSender = message.sender_id != state.currentUser?.id;

final seenBy = (state.thread?.seen_by_ids
.where((e) => e != state.currentUser?.id) ??
[])
final seenBy = (state.thread?.seen_by_ids ?? [])
.map((id) => members.firstWhereOrNull((m) => m.id == id))
.whereType<ApiUser>()
.toList();

//print('seenBy: ${seenBy.length} seen_by_ids ${state.thread?.seen_by_ids} currentUser ${state.currentUser?.id}');
final isSenderLatestMsg = message.sender_id == state.currentUser?.id &&
message.id == messages.first.id;

Expand Down
35 changes: 29 additions & 6 deletions app/lib/ui/flow/message/chat/chat_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
bool _hasMoreItems = true;

StreamSubscription<List<ApiThreadMessage>>? _messageSubscription;
StreamSubscription<ApiThread?>? _threadSubscription;

ChatViewNotifier(this.messageService, this.apiMessageService,
this.userService, this.spaceService, this.currentUser)
Expand All @@ -65,6 +66,7 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
void fetch() async {
try {
_messageSubscription?.cancel();
_threadSubscription?.cancel();

state = state.copyWith(loading: true, error: null);
final spaceId = state.space?.id ?? '';
Expand Down Expand Up @@ -98,6 +100,7 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {

if (thread != null) {
_listenMessages(thread.id);
_listenThread(thread.id);
}
} catch (error, stack) {
state = state.copyWith(error: error, loading: false);
Expand Down Expand Up @@ -126,6 +129,20 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
return {...state.members, ...users};
}

void _listenThread(String threadId) async {
try {
if (threadId.isEmpty) return;

_threadSubscription?.cancel();
_threadSubscription =
messageService.streamThread(threadId).listen((thread) {
state = state.copyWith(thread: thread);
}, onError: _onError);
} catch (error, stack) {
_onError(error, stack);
}
}

void _listenMessages(String threadId) async {
try {
if (threadId.isEmpty) return;
Expand Down Expand Up @@ -153,10 +170,13 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {

void markMessageAsSeen(String threadId) async {
try {
await Future.delayed(const Duration(seconds: 2));
var thread = state.thread;
if (thread?.seen_by_ids.contains(currentUser?.id) ?? false) return;
if (thread == null) return;
if (thread.seen_by_ids.contains(currentUser?.id)) {
return;
}
await messageService.addThreadSeenBy(threadId, currentUser?.id ?? '');
print("XXX markMessageAsSeen $threadId");
} catch (error, stack) {
logger.e(
'ChatViewNotifier: error while message mark as read',
Expand All @@ -175,13 +195,15 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {
senderId: currentUser?.id ?? '',
message: message);
final newMessages = [newMessage, ...state.messages];
state = state.copyWith(messages: newMessages);
state = state.copyWith(
messages: newMessages,
message: TextEditingController(text: ''),
actionError: null,
showMemberSelectionView: false,
thread: state.thread?.copyWith(seen_by_ids: []));
await messageService.sendMessage(newMessage);
state = state.copyWith(
messageSending: false,
message: TextEditingController(text: ''),
showMemberSelectionView: false,
actionError: null,
);
} catch (error, stack) {
state = state.copyWith(messageSending: false, actionError: error);
Expand Down Expand Up @@ -392,6 +414,7 @@ class ChatViewNotifier extends StateNotifier<ChatViewState> {

@override
void dispose() {
_threadSubscription?.cancel();
_messageSubscription?.cancel();
super.dispose();
}
Expand Down
1 change: 0 additions & 1 deletion app/lib/ui/flow/message/thread_list_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import 'package:yourspace_flutter/domain/extenstions/widget_extensions.dart';
import 'package:yourspace_flutter/ui/app_route.dart';
import 'package:yourspace_flutter/ui/components/app_page.dart';
import 'package:yourspace_flutter/ui/components/error_snakebar.dart';
import 'package:yourspace_flutter/ui/components/resume_detector.dart';
import 'package:yourspace_flutter/ui/flow/message/thread_list_view_model.dart';

import '../../../gen/assets.gen.dart';
Expand Down
4 changes: 4 additions & 0 deletions data/lib/service/message_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class MessageService {
return await messageService.getThread(threadId).first;
}

Stream<ApiThread?> streamThread(String threadId) {
return messageService.getThread(threadId);
}

Future<List<ApiThreadMessage>> getMessages(String threadId, DateTime? from, {int limit = 20}) async {
return messageService.getMessages(threadId, from, limit: limit);
}
Expand Down

0 comments on commit 2cec9d4

Please sign in to comment.