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(); Stream get newUserMessagesStream => _newUserMessagesController.stream; ChatRepository({ required this.chatService, }); void emitNewMessageToChats(ChatMessageModel message) { _newUserMessagesController.add(message); } Future sendMessage({ required String chatId, String? data, List? files, }) async { try { final multipartFiles = []; 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 getChatRoom(String chatId) async { try { return (await chatService.getChatRoom(id: chatId))['chatRoom']!; } catch (e) { rethrow; } } Future> getChatRoomPreviews({String? query}) async { try { return (await chatService.getChatRoomPreviews(query: query)).chatRooms; } catch (e) { rethrow; } } Future getChatMessages(String chatId, int page) async { try { return (await chatService.getChatMessages(id: chatId, page: page)); } catch (e) { rethrow; } } Future getChatRoomIdByUserSlug({required String slug}) async { try { return (await chatService.getChatRoomIdByUserSlug(user: slug)); } catch (e) { rethrow; } } Future markMessagesAsRead(String chatId, List messageIds) async { try { await chatService.markMessagesAsRead(id: chatId, body: {'messages_ids': messageIds}); } catch (e) { rethrow; } } }