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_pagination_model.dart'; import 'package:eatvio_app/features/chat/models/payment_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, mediaData: multipartFiles.isEmpty ? null : multipartFiles, ); } catch (e) { rethrow; } } Future getChatRoom(String chatId) async { try { return (await chatService.getChatRoom(id: chatId))['chatRoom']!; } catch (e) { rethrow; } } Future getChatRooms({String? query, int page = 1}) async { try { return (await chatService.getChatRooms(query: query, page: page)); } 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; } } Future cancelChosenPlan({ required int id, }) async { try { await chatService.cancelChosenPlan(id: id); } catch (e) { rethrow; } } Future choseStartPlanDate({required int id, required Map date}) async { try { await chatService.choseStartPlanDate(id: id, date: date); } catch (e) { rethrow; } } Future getPaymentLink({ required int id, }) async { try { return await chatService.getPaymentLink(id: id); } catch (e) { rethrow; } } }