diff --git a/lib/core/locator_service.dart b/lib/core/locator_service.dart index accb729d..9395a49f 100644 --- a/lib/core/locator_service.dart +++ b/lib/core/locator_service.dart @@ -226,6 +226,7 @@ Future init() async { 'accept': 'application/json', 'content-type': 'application/json', }, + validateStatus: (status) => status != null && status < 500, ), )..interceptors.addAll( [ diff --git a/lib/features/chat/repositories/chat_repository.dart b/lib/features/chat/repositories/chat_repository.dart index 28f80a48..4d1e9214 100644 --- a/lib/features/chat/repositories/chat_repository.dart +++ b/lib/features/chat/repositories/chat_repository.dart @@ -154,4 +154,25 @@ class ChatRepository { rethrow; } } + + Future getChatByUserSlug(String slug) async { + try { + final response = await chatService.getChatByUserSlug(slug: slug); + if (response.response.statusCode == 404) { + return '404'; + } else { + return response.data['id']; + } + } catch (e) { + return 'error'; + } + } + + Future sendFirstChatMessageForCreateChatRoom(String userSlug) async { + try { + return await chatService.sendMessageByUserSlug(userSlug: userSlug, data: 'Создал чат!'); + } catch (e) { + rethrow; + } + } } diff --git a/lib/features/chat/services/chat_service.dart b/lib/features/chat/services/chat_service.dart index 6d4c992e..dcdab395 100644 --- a/lib/features/chat/services/chat_service.dart +++ b/lib/features/chat/services/chat_service.dart @@ -24,7 +24,7 @@ abstract class ChatService { @Path() required String id, }); - @POST('/api/chat/{id}/message/send') + @POST('/api/v2/chat/message/send/{id}') @MultiPart() Future sendMessage({ @Path() required String id, @@ -80,4 +80,17 @@ abstract class ChatService { @Path() required int id, @Body() required PlanComplainModel body, }); + + @GET('/api/v2/chat/get_by_user/{slug}') + Future getChatByUserSlug({ + @Path() required String slug, + }); + + @POST('/api/v2/chat/message/send/{userSlug}') + @MultiPart() + Future sendMessageByUserSlug({ + @Path() required String userSlug, + @Part() String? data, + @Part(name: 'media_data[]', contentType: 'multipart/form-data') List? mediaData, + }); } diff --git a/lib/features/profile/presentation/bloc/profile_cubit/profile_cubit.dart b/lib/features/profile/presentation/bloc/profile_cubit/profile_cubit.dart index 28f654f2..530bc9aa 100644 --- a/lib/features/profile/presentation/bloc/profile_cubit/profile_cubit.dart +++ b/lib/features/profile/presentation/bloc/profile_cubit/profile_cubit.dart @@ -1,12 +1,15 @@ import 'dart:developer'; import 'dart:io'; +import 'package:eatvio_app/core/app_router.dart'; import 'package:eatvio_app/core/image_compressing/image_compress_utility.dart'; +import 'package:eatvio_app/core/utils/toasts.dart'; import 'package:eatvio_app/features/chat/repositories/chat_repository.dart'; import 'package:eatvio_app/features/consultant/become_consultant/repositories/consultant_repository.dart'; import 'package:eatvio_app/features/feed/models/feed_blog_post_model.dart'; import 'package:eatvio_app/features/profile/presentation/bloc/profile_cubit/profile_state.dart'; import 'package:eatvio_app/features/profile/repositories/profile_repository.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:go_router/go_router.dart'; class ProfileCubit extends Cubit { final ProfileRepository profileRepository; @@ -103,11 +106,30 @@ class ProfileCubit extends Cubit { } } - Future getChatRoomIdBySlug({ + getChatRoomIdBySlug({ required String slug, }) async { try { - return (await chatRepository.getChatRoomIdByUserSlug(slug: slug)).id; + final chatIdOrError = await chatRepository.getChatByUserSlug(slug); + if (chatIdOrError == '404') { + await chatRepository.sendFirstChatMessageForCreateChatRoom(slug); + final chatIdOrError = await chatRepository.getChatByUserSlug(slug); + AppRouter.navigatorKey.currentContext?.pushNamed( + 'chatScreen', + extra: { + 'chatId': chatIdOrError, + }, + ); + } else if (chatIdOrError == 'error') { + showErrorToast('Ошибка создания чата'); + } else { + AppRouter.navigatorKey.currentContext?.pushNamed( + 'chatScreen', + extra: { + 'chatId': chatIdOrError, + }, + ); + } } catch (e, st) { log(e.toString(), stackTrace: st); return ''; diff --git a/lib/features/profile/presentation/pages/profile_screen.dart b/lib/features/profile/presentation/pages/profile_screen.dart index 2eaa6d90..a847e697 100644 --- a/lib/features/profile/presentation/pages/profile_screen.dart +++ b/lib/features/profile/presentation/pages/profile_screen.dart @@ -1232,14 +1232,7 @@ class _ProfileScreenState extends State { children: [ const SizedBox(width: 16), GestureDetector( - onTap: () async { - GoRouter.of(context).pushNamed( - 'chatScreen', - extra: { - 'chatId': await profileCubit.getChatRoomIdBySlug(slug: widget.author!.authorSlug), - }, - ); - }, + onTap: () async => await profileCubit.getChatRoomIdBySlug(slug: widget.author!.authorSlug), child: Container( alignment: Alignment.center, padding: const EdgeInsets.all(8),