chat screen
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
library chat_repository;
|
||||
|
||||
export 'src/entities/entities.dart';
|
||||
export 'src/repositories/firebase_chat_repository.dart';
|
||||
@@ -0,0 +1,49 @@
|
||||
import 'package:chat_repository/src/entities/entities.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class ChatEntity extends Equatable {
|
||||
final String? id;
|
||||
final String userId;
|
||||
final String professionalId;
|
||||
final List<MessageEntity> messages;
|
||||
|
||||
const ChatEntity({
|
||||
required this.id,
|
||||
required this.userId,
|
||||
required this.professionalId,
|
||||
required this.messages,
|
||||
});
|
||||
|
||||
static ChatEntity fromDocument(Map<String, dynamic> doc) {
|
||||
return ChatEntity(
|
||||
id: doc['id'] as String,
|
||||
userId: doc['user_id'] as String,
|
||||
professionalId: doc['professional_id'] as String,
|
||||
messages: (doc['messages'] as List)
|
||||
.map((e) => MessageEntity.fromDocument(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toDocument() {
|
||||
return {
|
||||
'id': id,
|
||||
'user_id': userId,
|
||||
'professional_id': professionalId,
|
||||
'messages': messages.map((e) => e.toDocument()).toList(),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [id, userId, professionalId, messages];
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return '''ChatEntity{
|
||||
id: $id,
|
||||
userId: $userId,
|
||||
professionalId: $professionalId,
|
||||
messages: $messages
|
||||
}''';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export '/src/entities/chat_entity.dart';
|
||||
export '/src/entities/message_entity.dart';
|
||||
@@ -0,0 +1,45 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class MessageEntity extends Equatable {
|
||||
final String ownerId;
|
||||
final String content;
|
||||
final DateTime createdAt;
|
||||
|
||||
const MessageEntity({
|
||||
required this.ownerId,
|
||||
required this.content,
|
||||
required this.createdAt,
|
||||
});
|
||||
|
||||
static MessageEntity fromDocument(Map<String, dynamic> doc) {
|
||||
return MessageEntity(
|
||||
ownerId: doc['owner_id'] as String,
|
||||
content: doc['content'] as String,
|
||||
createdAt: DateTime.parse(doc['created_at'] as String),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toDocument() {
|
||||
return {
|
||||
'owner_id': ownerId,
|
||||
'content': content,
|
||||
'created_at': createdAt.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
ownerId,
|
||||
content,
|
||||
createdAt,
|
||||
];
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return '''MessageEntity{
|
||||
ownerId: $ownerId,
|
||||
content: $content,
|
||||
createdAt: $createdAt
|
||||
}''';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:chat_repository/chat_repository.dart';
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
|
||||
class FirebaseChatRepository {
|
||||
final chatCollection = FirebaseFirestore.instance.collection('chats');
|
||||
|
||||
Stream<ChatEntity?> getChatById(String chatId) {
|
||||
return chatCollection.doc(chatId).snapshots().map((snapshot) {
|
||||
try {
|
||||
if (snapshot.exists) {
|
||||
return ChatEntity.fromDocument(snapshot.data()!);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<ChatEntity> createNewChat(
|
||||
String chatId, String userId, String professionalId) async {
|
||||
ChatEntity chat = ChatEntity(
|
||||
id: chatId,
|
||||
userId: userId,
|
||||
professionalId: professionalId,
|
||||
messages: const [],
|
||||
);
|
||||
|
||||
await chatCollection.doc(chatId).set(chat.toDocument());
|
||||
|
||||
return chat;
|
||||
}
|
||||
|
||||
sendMessage(String chatId, MessageEntity message) {
|
||||
chatCollection.doc(chatId).update({
|
||||
'messages': FieldValue.arrayUnion([message.toDocument()])
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user