From ea2ec05ed62696ad62353e54118f1b083df6cbca Mon Sep 17 00:00:00 2001 From: mso7nueh Date: Wed, 9 Jul 2025 15:06:51 +0300 Subject: [PATCH] fix(chat): websockets --- lib/core/const.dart | 1 - lib/core/websockets/ws_manager.dart | 1 - .../bloc/chat_room_cubit/chat_room_cubit.dart | 24 ++++++++++----- .../registration_cubit.dart | 29 +++++++------------ .../repositories/registration_repository.dart | 10 ++++++- 5 files changed, 36 insertions(+), 29 deletions(-) diff --git a/lib/core/const.dart b/lib/core/const.dart index 5d1aded6..618a9866 100644 --- a/lib/core/const.dart +++ b/lib/core/const.dart @@ -1,4 +1,3 @@ -// String baseUrl = 'https://mobile-stage.eatvio.ru'; String baseUrl = 'https://eatvio.ru'; const List durationValues = [7, 14, 21]; diff --git a/lib/core/websockets/ws_manager.dart b/lib/core/websockets/ws_manager.dart index 0a5f7241..528f2ae9 100644 --- a/lib/core/websockets/ws_manager.dart +++ b/lib/core/websockets/ws_manager.dart @@ -98,7 +98,6 @@ class WsManager { const Duration(milliseconds: 300), () => sl().getChatRooms(), ); - log(event.rootObject.toString()); }); _subscribe(); } catch (e, st) { diff --git a/lib/features/chat/presentation/bloc/chat_room_cubit/chat_room_cubit.dart b/lib/features/chat/presentation/bloc/chat_room_cubit/chat_room_cubit.dart index 32244e78..f4c95d12 100644 --- a/lib/features/chat/presentation/bloc/chat_room_cubit/chat_room_cubit.dart +++ b/lib/features/chat/presentation/bloc/chat_room_cubit/chat_room_cubit.dart @@ -89,7 +89,10 @@ class ChatRoomCubit extends Cubit { } } - Future initChat(String uuid, {bool sendInitialMessage = false}) async { + Future initChat( + String uuid, { + bool sendInitialMessage = false, + }) async { try { final chatRoom = await chatRepository.getChatRoom(uuid); ChosenPlanModel? chosenPlan; @@ -98,8 +101,7 @@ class ChatRoomCubit extends Cubit { } final chatMessages = await chatRepository.getChatMessages(uuid, currentPage); lastPage = chatMessages.meta.lastPage; - - await _subscribeToChatChannel(uuid: uuid); + await _subscribeToChatChannel(uuid: uuid, chatId: chatRoom.id); emit( ChatRoomState.loaded( chatRoom: chatRoom.copyWith(chosenPlan: chosenPlan), @@ -158,14 +160,14 @@ class ChatRoomCubit extends Cubit { } } - Future _subscribeToChatChannel({required String uuid}) async { + Future _subscribeToChatChannel({required String uuid, required String chatId}) async { final client = wsManager.client; if (client == null) return; - await wsManager.initPrivateDirectChatChannel(uuid); + await wsManager.initPrivateDirectChatChannel(chatId); wsManager.subscribeDirectChatChannel(); messagesSubscription = wsManager.directChatStream?.listen((event) { AudioPlayer().play((AssetSource('audio/recive_message.wav'))); - final message = ChatMessageModel.fromJson(jsonDecode(event.data)['message']); + final message = ChatMessageModel.fromJson(jsonDecode(event.data)); List messagesList = [message, ...(state as ChatRoomStateLoaded).chatMessages]; chatRepository.emitNewMessageToChats(message); emit((state as ChatRoomStateLoaded).copyWith(chatMessages: messagesList, isLoading: isLoading)); @@ -179,7 +181,15 @@ class ChatRoomCubit extends Cubit { typingSubscription = wsManager.clientTypingStream?.listen((event) { emit((state as ChatRoomStateLoaded).copyWith(isTyping: true, isLoading: isLoading)); EasyDebounce.debounce( - 'client-typing', const Duration(seconds: 3), () => emit((state as ChatRoomStateLoaded).copyWith(isTyping: false, isLoading: isLoading))); + 'client-typing', + const Duration(seconds: 3), + () => emit( + (state as ChatRoomStateLoaded).copyWith( + isTyping: false, + isLoading: isLoading, + ), + ), + ); }); } diff --git a/lib/features/registration/presentation/bloc/registration_cubit/registration_cubit.dart b/lib/features/registration/presentation/bloc/registration_cubit/registration_cubit.dart index 9581779d..83592c3b 100644 --- a/lib/features/registration/presentation/bloc/registration_cubit/registration_cubit.dart +++ b/lib/features/registration/presentation/bloc/registration_cubit/registration_cubit.dart @@ -3,7 +3,6 @@ import 'dart:developer'; import 'package:eatvio_app/core/app_router.dart'; import 'package:eatvio_app/core/token_storage.dart'; -import 'package:eatvio_app/core/widgets/show_error_dialog.dart'; import 'package:eatvio_app/features/registration/models/user_registration_model.dart'; import 'package:eatvio_app/features/registration/presentation/bloc/registration_cubit/registration_state.dart'; import 'package:eatvio_app/features/registration/repositories/registration_repository.dart'; @@ -51,25 +50,17 @@ class RegistrationCubit extends Cubit { void updateSheet(UserRegistrationSheet sheet) => emit(RegistrationState(sheet: sheet)); Future tryRegistration() async { - if (await registrationRepository.emailIsReachable(state.sheet.email)) { - try { - final token = await registrationRepository.tryRegister( - state.sheet.copyWith( - phone: state.sheet.phone.replaceAll(' ', '').replaceAll('(', '').replaceAll(')', ''), - responseBody: '{}', - ), - ); - await TokenStorage.saveTokenInStorage(token); - AppRouter.navigatorKey.currentContext?.pushNamed('verifyEmailScreen'); - } catch (e) { - log(e.toString()); - } - } else { - showErrorDialog( - AppRouter.navigatorKey.currentContext!, - title: 'Ошибка регистрации', - description: 'Несуществующая почта.', + try { + final token = await registrationRepository.tryRegister( + state.sheet.copyWith( + phone: state.sheet.phone.replaceAll(' ', '').replaceAll('(', '').replaceAll(')', ''), + responseBody: '{}', + ), ); + await TokenStorage.saveTokenInStorage(token); + AppRouter.navigatorKey.currentContext?.pushNamed('verifyEmailScreen'); + } catch (e, st) { + log(e.toString(), stackTrace: st); } } diff --git a/lib/features/registration/repositories/registration_repository.dart b/lib/features/registration/repositories/registration_repository.dart index 7a4a14ae..0c3008e0 100644 --- a/lib/features/registration/repositories/registration_repository.dart +++ b/lib/features/registration/repositories/registration_repository.dart @@ -1,5 +1,7 @@ + import 'package:dio/dio.dart'; import 'package:eatvio_app/core/locator_service.dart'; +import 'package:eatvio_app/core/utils/toasts.dart'; import 'package:eatvio_app/features/registration/models/user_registration_model.dart'; import 'package:eatvio_app/features/registration/models/location_model.dart'; import 'package:eatvio_app/features/registration/services/registration_service.dart'; @@ -11,7 +13,13 @@ class RegistrationRepository { Future tryRegister(UserRegistrationSheet sheet) async { try { - return (await registrationService.register(sheet: sheet.toJson()))['token']!; + final response = await registrationService.register(sheet: sheet.toJson()); + try { + return (response['token']!); + } catch (e) { + showErrorToast(response.toString()); + rethrow; + } } catch (e) { rethrow; }