forked from eatvio/eatvio_app
97 lines
2.6 KiB
Dart
97 lines
2.6 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:eatvio_app/features/chat/models/chat_message_model.dart';
|
|
import 'package:eatvio_app/features/chat/models/chat_messages_model.dart';
|
|
import 'package:eatvio_app/features/chat/models/chat_room_model.dart';
|
|
import 'package:eatvio_app/features/chat/models/chat_room_preview_model.dart';
|
|
import 'package:eatvio_app/features/chat/services/chat_service.dart';
|
|
import 'package:http_parser/http_parser.dart';
|
|
|
|
class ChatRepository {
|
|
final ChatService chatService;
|
|
|
|
final _newUserMessagesController = StreamController<ChatMessageModel>();
|
|
|
|
Stream<ChatMessageModel> get newUserMessagesStream => _newUserMessagesController.stream;
|
|
|
|
ChatRepository({
|
|
required this.chatService,
|
|
});
|
|
|
|
void emitNewMessageToChats(ChatMessageModel message) {
|
|
_newUserMessagesController.add(message);
|
|
}
|
|
|
|
Future<void> sendMessage({
|
|
required String chatId,
|
|
String? data,
|
|
List<File>? files,
|
|
}) async {
|
|
try {
|
|
final multipartFiles = <MultipartFile>[];
|
|
|
|
if (files != null) {
|
|
for (final file in files) {
|
|
final fileBytes = await file.readAsBytes();
|
|
final multipartFile = MultipartFile.fromBytes(
|
|
fileBytes,
|
|
filename: file.path.split('/').last,
|
|
contentType: MediaType('application', 'octet-stream'),
|
|
);
|
|
multipartFiles.add(multipartFile);
|
|
}
|
|
}
|
|
|
|
await chatService.sendMessage(
|
|
id: chatId,
|
|
data: data,
|
|
media_data: multipartFiles.isEmpty ? null : multipartFiles,
|
|
);
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<ChatRoomModel> getChatRoom(String chatId) async {
|
|
try {
|
|
return (await chatService.getChatRoom(id: chatId))['chatRoom']!;
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<List<ChatRoom>> getChatRoomPreviews({String? query}) async {
|
|
try {
|
|
return (await chatService.getChatRoomPreviews(query: query)).chatRooms;
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<ChatMessagesModel> getChatMessages(String chatId, int page) async {
|
|
try {
|
|
return (await chatService.getChatMessages(id: chatId, page: page));
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<ChatRoomModel> getChatRoomIdByUserSlug({required String slug}) async {
|
|
try {
|
|
return (await chatService.getChatRoomIdByUserSlug(user: slug));
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> markMessagesAsRead(String chatId, List<int> messageIds) async {
|
|
try {
|
|
await chatService.markMessagesAsRead(id: chatId, body: {'messages_ids': messageIds});
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
}
|