Update UserTileChats to auto-refresh UI on Firebase changes.
parent
63bedb0280
commit
f1c0542e49
|
@ -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<Message?>(
|
||||
future: ChatService().getLastMessage(chatRoomID),
|
||||
return StreamBuilder<Message?>(
|
||||
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!;
|
||||
String msgDateString = '';
|
||||
String msgContent = '';
|
||||
bool? outgoing;
|
||||
if (lastMessage.timestamp !=
|
||||
Timestamp.fromMillisecondsSinceEpoch(0)) {
|
||||
msgDateString = formatTimestamp(lastMessage.timestamp);
|
||||
}
|
||||
|
||||
if (lastMessage.senderID == currentUserId) {
|
||||
msgContent = 'Me: ${lastMessage.message}';
|
||||
|
|
|
@ -58,6 +58,24 @@ class ChatService {
|
|||
await batch.commit();
|
||||
}
|
||||
|
||||
/// Retrieves a stream of the last message of a chatroom.
|
||||
Stream<Message?> 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,6 +95,7 @@ class ChatService {
|
|||
.limit(1)
|
||||
.get();
|
||||
|
||||
if(messageSnapshot.docs.isNotEmpty) {
|
||||
DocumentSnapshot lastMessageDoc = messageSnapshot.docs.first;
|
||||
if (lastMessageDoc.exists) {
|
||||
senderID = lastMessageDoc[Constants.dbFieldMessageSenderId];
|
||||
|
@ -85,8 +104,11 @@ class ChatService {
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue