Files
eatvio_app/lib/features/chat/repositories/chat_repository.dart

88 lines
2.2 KiB
Dart

import 'dart:io';
import 'package:dio/dio.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/models/websocket_info_model.dart';
import 'package:eatvio_app/features/chat/services/chat_service.dart';
import 'package:http_parser/http_parser.dart';
class ChatRepository {
final ChatService chatService;
ChatRepository({
required this.chatService,
});
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<WebsocketInfoModel> getWebsocket() async {
try {
return (await chatService.getWebsocket());
} catch (e) {
rethrow;
}
}
Future<ChatRoomModel> getChatRoomIdByUserSlug({required String slug}) async {
try {
return (await chatService.getChatRoomIdByUserSlug(user: slug));
} catch (e) {
rethrow;
}
}
}