diff --git a/lib/components/user_tile_chats.dart b/lib/components/user_tile_chats.dart index ec2392e..ba33210 100644 --- a/lib/components/user_tile_chats.dart +++ b/lib/components/user_tile_chats.dart @@ -1,3 +1,4 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:flutter/material.dart'; import '../models/message.dart'; @@ -45,7 +46,6 @@ class UserTileChats extends StatelessWidget { Widget buildProfileIcon() { return Row( children: [ - // Profile image if (profileImageUrl != null && profileImageUrl!.isNotEmpty) CircleAvatar( backgroundImage: NetworkImage(profileImageUrl!), @@ -53,7 +53,7 @@ class UserTileChats extends StatelessWidget { ), // Icon if profile image is not set if (profileImageUrl == null || profileImageUrl!.isEmpty) - const Icon(Icons.person), + const Icon(Icons.person, size: 36), const SizedBox(width: 12), ], @@ -61,13 +61,10 @@ class UserTileChats extends StatelessWidget { } Widget buildMsgContent() { - String msgDateString = ''; - String msgContent = ''; - bool? outgoing; String chatRoomID = getCompoundId([currentUserId ?? '', otherUserId ?? '']); - return FutureBuilder( - future: ChatService().getLastMessage(chatRoomID), + return StreamBuilder( + stream: ChatService().getLastMessageStream(chatRoomID), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const CircularProgressIndicator(); @@ -82,7 +79,13 @@ class UserTileChats extends StatelessWidget { return buildMsgDetails('No messages yet', '', null); } else { Message lastMessage = snapshot.data!; - msgDateString = formatTimestamp(lastMessage.timestamp); + String msgDateString = ''; + String msgContent = ''; + bool? outgoing; + if (lastMessage.timestamp != + Timestamp.fromMillisecondsSinceEpoch(0)) { + msgDateString = formatTimestamp(lastMessage.timestamp); + } if (lastMessage.senderID == currentUserId) { msgContent = 'Me: ${lastMessage.message}'; diff --git a/lib/services/chat/chat_service.dart b/lib/services/chat/chat_service.dart index 1fac1ce..8be7d38 100644 --- a/lib/services/chat/chat_service.dart +++ b/lib/services/chat/chat_service.dart @@ -58,6 +58,24 @@ class ChatService { await batch.commit(); } + /// Retrieves a stream of the last message of a chatroom. + Stream getLastMessageStream(String chatRoomID) { + return _firestore + .collection(Constants.dbCollectionChatRooms) + .doc(chatRoomID) + .collection(Constants.dbCollectionMessages) + .orderBy(Constants.dbFieldMessageTimestamp, descending: true) + .limit(1) + .snapshots() + .map((snapshot) { + if (snapshot.docs.isEmpty) { + return null; + } + var lastMessageDoc = snapshot.docs.first; + return Message.fromMap(lastMessageDoc.data()); + }); + } + /// Retrieves the last message of a chatroom. /// /// Returns [null] in case of an error. @@ -77,16 +95,20 @@ class ChatService { .limit(1) .get(); - DocumentSnapshot lastMessageDoc = messageSnapshot.docs.first; - if (lastMessageDoc.exists) { - senderID = lastMessageDoc[Constants.dbFieldMessageSenderId]; - senderEmail = lastMessageDoc[Constants.dbFieldMessageSenderEmail]; - receiverID = lastMessageDoc[Constants.dbFieldMessageReceiverId]; - message = lastMessageDoc[Constants.dbFieldMessageText]; - timestamp = lastMessageDoc[Constants.dbFieldMessageTimestamp]; + if(messageSnapshot.docs.isNotEmpty) { + DocumentSnapshot lastMessageDoc = messageSnapshot.docs.first; + if (lastMessageDoc.exists) { + senderID = lastMessageDoc[Constants.dbFieldMessageSenderId]; + senderEmail = lastMessageDoc[Constants.dbFieldMessageSenderEmail]; + receiverID = lastMessageDoc[Constants.dbFieldMessageReceiverId]; + message = lastMessageDoc[Constants.dbFieldMessageText]; + timestamp = lastMessageDoc[Constants.dbFieldMessageTimestamp]; + } + } else { + debugPrint('Info: getLastMessage -> messageSnapshot is EMPTY'); } } catch (e) { - debugPrint('DebugPrint: getLastMessage -> ${e.toString()}'); + debugPrint('Error on getLastMessage: ${e.toString()}'); return null; }