From 75fa9ebc5c09a40e397f02b77780a7ca2dffb706 Mon Sep 17 00:00:00 2001 From: mso7nueh Date: Mon, 23 Jun 2025 19:46:23 +0300 Subject: [PATCH] feat(chat): fix chat first message, add pick images from gallery --- lib/core/app_router.dart | 7 +- lib/core/websockets/ws_manager.dart | 3 +- .../bloc/chat_room_cubit/chat_room_cubit.dart | 2 - .../chat/presentation/pages/chat_screen.dart | 40 ++- .../presentation/pages/empty_chat_screen.dart | 289 ++++++++++++++++++ .../chat/repositories/chat_repository.dart | 9 - .../bloc/profile_cubit/profile_cubit.dart | 13 +- .../presentation/pages/profile_screen.dart | 2 +- .../widgets/sub_action_button_widget.dart | 14 +- .../presentation/widgets/sub_widget.dart | 2 + 10 files changed, 347 insertions(+), 34 deletions(-) create mode 100644 lib/features/chat/presentation/pages/empty_chat_screen.dart diff --git a/lib/core/app_router.dart b/lib/core/app_router.dart index c29754ab..2affcfd1 100644 --- a/lib/core/app_router.dart +++ b/lib/core/app_router.dart @@ -17,6 +17,7 @@ import 'package:eatvio_app/features/bottom_navigation_bar/presentation/widgets/b import 'package:eatvio_app/features/calendar/presentation/pages/calendar_screen.dart'; import 'package:eatvio_app/features/chat/presentation/pages/chat_screen.dart'; import 'package:eatvio_app/features/chat/presentation/pages/chats_screen.dart'; +import 'package:eatvio_app/features/chat/presentation/pages/empty_chat_screen.dart'; import 'package:eatvio_app/features/chat/presentation/pages/payment_screen.dart'; import 'package:eatvio_app/features/chat/presentation/pages/photo_view_screen.dart'; import 'package:eatvio_app/features/consultant/become_consultant/presentation/pages/become_consultant_screen.dart'; @@ -173,7 +174,6 @@ class AppRouter { name: 'welcomeScreen', builder: (context, state) => const WelcomeScreen(), redirect: (context, state) async { - try { if (await TokenStorage.haveTokenInStorage()) { final firebaseApi = sl(); @@ -312,6 +312,11 @@ class AppRouter { ); }, ), + GoRoute( + path: '/empty-chat-screen', + name: 'emptyChatScreen', + builder: (context, state) => EmptyChatScreen(author: state.extra as Author), + ), GoRoute( path: '/settings-screen', name: 'settingsScreen', diff --git a/lib/core/websockets/ws_manager.dart b/lib/core/websockets/ws_manager.dart index 02857ca0..152f0fb9 100644 --- a/lib/core/websockets/ws_manager.dart +++ b/lib/core/websockets/ws_manager.dart @@ -39,6 +39,7 @@ class WsManager { metadata: const PusherChannelsOptionsMetadata.byDefault(), port: 443, ); + client = PusherChannelsClient.websocket( options: options, connectionErrorHandler: (exception, trace, refresh) { @@ -136,7 +137,7 @@ class WsManager { _privateDirectChatChannel?.unsubscribe(); } - Future clear() async{ + Future clear() async { await client?.disconnect(); client = null; userId = null; 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 cbb886c2..369993e0 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 @@ -177,14 +177,12 @@ class ChatRoomCubit extends Cubit { } Future sendMessage({ - required String chatId, String? data, List? files, required String userSlug, }) async { try { await chatRepository.sendMessage( - chatId: chatId, data: data, files: files != null ? await ImageCompressUtility.compressFilesList(files) : null, slug: userSlug, diff --git a/lib/features/chat/presentation/pages/chat_screen.dart b/lib/features/chat/presentation/pages/chat_screen.dart index ba08452a..e2e222cd 100644 --- a/lib/features/chat/presentation/pages/chat_screen.dart +++ b/lib/features/chat/presentation/pages/chat_screen.dart @@ -387,7 +387,6 @@ class _ChatScreenState extends State { onTap: () { if (textEditingController.text.isNotEmpty) { chatRoomCubit.sendMessage( - chatId: widget.chatId, data: textEditingController.text.trim(), userSlug: state.chatRoom.users.receiver.slug, ); @@ -411,14 +410,15 @@ class _ChatScreenState extends State { ), if (showAboutOrderButton) Positioned( - top: 8, - right: 15, - left: 16, - child: AboutOrderButton( - onTap: () async { - GoRouter.of(context).push(InfoByOrderScreen.path, extra: {'planId': state.chatRoom.chosenPlan?.id}); - }, - )), + top: 8, + right: 15, + left: 16, + child: AboutOrderButton( + onTap: () async { + GoRouter.of(context).push(InfoByOrderScreen.path, extra: {'planId': state.chatRoom.chosenPlan?.id}); + }, + ), + ), ], ), ), @@ -436,7 +436,7 @@ class _ChatScreenState extends State { final picker = ImagePicker(); XFile? image = await picker.pickImage(source: ImageSource.camera); if (image != null) { - await chatRoomCubit.sendMessage(chatId: widget.chatId, files: [File(image.path)], userSlug: slug); + await chatRoomCubit.sendMessage(files: [File(image.path)], userSlug: slug); } }, child: const Row( @@ -447,6 +447,24 @@ class _ChatScreenState extends State { ], ), ), + CupertinoActionSheetAction( + onPressed: () async { + Navigator.of(context).pop(); + final picker = ImagePicker(); + List images = await picker.pickMultiImage(); + if (images != []) { + List files = images.map((image) => File(image.path)).toList(); + await chatRoomCubit.sendMessage(files: files, userSlug: slug); + } + }, + child: const Row( + children: [ + Icon(Icons.photo_library_rounded), + SizedBox(width: 8), + Text('Галерея'), + ], + ), + ), CupertinoActionSheetAction( onPressed: () async { Navigator.of(context).pop(); @@ -476,7 +494,7 @@ class _ChatScreenState extends State { ); if (result != null) { List files = result.paths.map((path) => File(path!)).toList(); - await chatRoomCubit.sendMessage(chatId: widget.chatId, files: files, userSlug: slug); + await chatRoomCubit.sendMessage(files: files, userSlug: slug); } }, child: const Row( diff --git a/lib/features/chat/presentation/pages/empty_chat_screen.dart b/lib/features/chat/presentation/pages/empty_chat_screen.dart new file mode 100644 index 00000000..2f8e91b3 --- /dev/null +++ b/lib/features/chat/presentation/pages/empty_chat_screen.dart @@ -0,0 +1,289 @@ +import 'dart:io'; +import 'dart:math'; + +import 'package:eatvio_app/core/app_router.dart'; +import 'package:eatvio_app/core/locator_service.dart'; +import 'package:eatvio_app/core/theme/custom_text_style.dart'; +import 'package:eatvio_app/core/theme/theme_helper.dart'; +import 'package:eatvio_app/features/chat/repositories/chat_repository.dart'; +import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart'; +import 'package:file_picker/file_picker.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; +import 'package:image_picker/image_picker.dart'; + +class EmptyChatScreen extends StatefulWidget { + final Author author; + + const EmptyChatScreen({ + super.key, + required this.author, + }); + + @override + State createState() => _EmptyChatScreenState(); +} + +class _EmptyChatScreenState extends State { + final TextEditingController textEditingController = TextEditingController(); + + @override + Widget build(BuildContext context) { + return Scaffold( + resizeToAvoidBottomInset: true, + appBar: AppBar( + titleSpacing: 0, + backgroundColor: appTheme.gray100, + centerTitle: false, + title: GestureDetector( + onTap: () { + GoRouter.of(context).pushNamed( + 'profileScreen', + extra: Author( + authorSlug: widget.author.authorSlug, + authorName: widget.author.authorName, + authorProfileIcon: widget.author.authorProfileIcon, + authorIsMe: false, + ), + ); + }, + child: Row( + children: [ + CircleAvatar( + radius: 20, + foregroundImage: NetworkImage(widget.author.authorProfileIcon), + backgroundImage: const AssetImage('assets/images/no_avatar.png'), + ), + const SizedBox(width: 16), + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + widget.author.authorName, + style: CustomTextStyles.titleSmallBold.copyWith( + color: PrimaryColors().black, + ), + ), + ], + ), + ], + ), + ), + ), + body: SafeArea( + child: Column( + children: [ + const Spacer(), + Container( + padding: const EdgeInsets.symmetric(vertical: 8), + child: Row( + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + const SizedBox(width: 8), + InkWell( + onTap: () => showImagePicker(widget.author.authorSlug), + customBorder: const CircleBorder(), + child: Ink( + height: 40, + width: 40, + child: Transform.rotate( + angle: pi / 4, + child: const Icon( + Icons.attach_file_rounded, + size: 30, + color: Color(0xFF858E99), + ), + ), + ), + ), + const SizedBox(width: 8), + Expanded( + child: SizedBox( + height: 40, + child: TextFormField( + expands: true, + textCapitalization: TextCapitalization.sentences, + controller: textEditingController, + maxLines: null, + minLines: null, + keyboardType: TextInputType.multiline, + style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w400, color: Colors.black, height: 1), + decoration: InputDecoration( + isDense: true, + contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10), + hintText: 'Сообщение', + hintStyle: const TextStyle(fontSize: 14, fontWeight: FontWeight.w400, height: 1, color: Color(0xFFAEAEB2)), + filled: true, + border: + OutlineInputBorder(borderRadius: BorderRadius.circular(100), borderSide: const BorderSide(color: Color(0xFFD1D1D6), width: 1)), + enabledBorder: OutlineInputBorder( + borderRadius: BorderRadius.circular(100), + borderSide: const BorderSide(color: Color(0xFFD1D1D6), width: 1), + ), + focusedBorder: OutlineInputBorder( + borderRadius: BorderRadius.circular(100), + borderSide: const BorderSide(color: Color(0xFFD1D1D6), width: 1), + ), + ), + ), + ), + ), + const SizedBox(width: 8), + InkWell( + customBorder: const CircleBorder(), + onTap: () async { + if (textEditingController.text.isNotEmpty) { + await sl().sendMessage( + data: textEditingController.text.trim(), + slug: widget.author.authorSlug, + ); + setState(() { + textEditingController.clear(); + }); + final chatIdOrError = await sl().getChatByUserSlug(widget.author.authorSlug); + AppRouter.navigatorKey.currentContext?.pushReplacementNamed( + 'chatScreen', + extra: { + 'chatId': chatIdOrError, + }, + ); + } + }, + child: Ink( + decoration: BoxDecoration(color: appTheme.blueA400, shape: BoxShape.circle), + height: 40, + width: 40, + child: const Icon(Icons.arrow_upward_rounded, size: 24, color: Colors.white), + ), + ), + const SizedBox(width: 8), + ], + ), + ), + ], + ), + ), + ); + } + + showImagePicker(String slug) { + showCupertinoModalPopup( + context: context, + builder: (context) => CupertinoActionSheet( + actions: [ + CupertinoActionSheetAction( + onPressed: () async { + Navigator.of(context).pop(); + final picker = ImagePicker(); + XFile? image = await picker.pickImage(source: ImageSource.camera); + if (image != null) { + await sl().sendMessage( + files: [File(image.path)], + slug: widget.author.authorSlug, + ); + final chatIdOrError = await sl().getChatByUserSlug(widget.author.authorSlug); + AppRouter.navigatorKey.currentContext?.pushReplacementNamed( + 'chatScreen', + extra: { + 'chatId': chatIdOrError, + }, + ); + } + }, + child: const Row( + children: [ + Icon(Icons.photo_library_rounded), + SizedBox(width: 8), + Text('Галерея'), + ], + ), + ), + CupertinoActionSheetAction( + onPressed: () async { + Navigator.of(context).pop(); + final picker = ImagePicker(); + List? images = await picker.pickMultiImage(); + if (images.isNotEmpty) { + List files = images.map((image) => File(image.path)).toList(); + await sl().sendMessage( + files: files, + slug: widget.author.authorSlug, + ); + final chatIdOrError = await sl().getChatByUserSlug(widget.author.authorSlug); + AppRouter.navigatorKey.currentContext?.pushReplacementNamed( + 'chatScreen', + extra: { + 'chatId': chatIdOrError, + }, + ); + } + }, + child: const Row( + children: [ + Icon(Icons.photo_camera), + SizedBox(width: 8), + Text('Фото'), + ], + ), + ), + CupertinoActionSheetAction( + onPressed: () async { + Navigator.of(context).pop(); + FilePickerResult? result = await FilePicker.platform.pickFiles( + allowMultiple: true, + type: FileType.custom, + allowedExtensions: [ + 'pdf', + 'doc', + 'docx', + 'json', + 'ppt', + 'pptx', + 'rtf', + 'txt', + 'xls', + 'xlsx', + 'csv', + 'jpeg', + 'jpg', + 'png', + 'gif', + 'svg', + 'xml', + 'webp', + ], + ); + if (result != null) { + List files = result.paths.map((path) => File(path!)).toList(); + await sl().sendMessage( + files: files, + slug: widget.author.authorSlug, + ); + final chatIdOrError = await sl().getChatByUserSlug(widget.author.authorSlug); + AppRouter.navigatorKey.currentContext?.pushReplacementNamed( + 'chatScreen', + extra: { + 'chatId': chatIdOrError, + }, + ); + } + }, + child: const Row( + children: [ + Icon(Icons.file_present_rounded), + SizedBox(width: 8), + Text('Файлы'), + ], + ), + ), + ], + cancelButton: CupertinoActionSheetAction( + onPressed: () => Navigator.of(context).pop(), + isDestructiveAction: true, + child: const Text('Отмена'), + ), + ), + ); + } +} diff --git a/lib/features/chat/repositories/chat_repository.dart b/lib/features/chat/repositories/chat_repository.dart index 1215a5f6..e249c708 100644 --- a/lib/features/chat/repositories/chat_repository.dart +++ b/lib/features/chat/repositories/chat_repository.dart @@ -27,7 +27,6 @@ class ChatRepository { } Future sendMessage({ - required String chatId, required String slug, String? data, List? files, @@ -168,12 +167,4 @@ class ChatRepository { return 'error'; } } - - Future sendFirstChatMessageForCreateChatRoom(String userSlug) async { - try { - return await chatService.sendMessageByUserSlug(userSlug: userSlug, data: 'Создал чат!'); - } catch (e) { - rethrow; - } - } } 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 530bc9aa..1312dea8 100644 --- a/lib/features/profile/presentation/bloc/profile_cubit/profile_cubit.dart +++ b/lib/features/profile/presentation/bloc/profile_cubit/profile_cubit.dart @@ -5,6 +5,7 @@ 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/diets_pagination_model.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'; @@ -107,18 +108,14 @@ class ProfileCubit extends Cubit { } getChatRoomIdBySlug({ - required String slug, + required Author author, }) async { try { - final chatIdOrError = await chatRepository.getChatByUserSlug(slug); + final chatIdOrError = await chatRepository.getChatByUserSlug(author.authorSlug); if (chatIdOrError == '404') { - await chatRepository.sendFirstChatMessageForCreateChatRoom(slug); - final chatIdOrError = await chatRepository.getChatByUserSlug(slug); AppRouter.navigatorKey.currentContext?.pushNamed( - 'chatScreen', - extra: { - 'chatId': chatIdOrError, - }, + 'emptyChatScreen', + extra: author, ); } else if (chatIdOrError == 'error') { showErrorToast('Ошибка создания чата'); diff --git a/lib/features/profile/presentation/pages/profile_screen.dart b/lib/features/profile/presentation/pages/profile_screen.dart index a847e697..9dc4f880 100644 --- a/lib/features/profile/presentation/pages/profile_screen.dart +++ b/lib/features/profile/presentation/pages/profile_screen.dart @@ -1232,7 +1232,7 @@ class _ProfileScreenState extends State { children: [ const SizedBox(width: 16), GestureDetector( - onTap: () async => await profileCubit.getChatRoomIdBySlug(slug: widget.author!.authorSlug), + onTap: () async => await profileCubit.getChatRoomIdBySlug(author: widget.author!), child: Container( alignment: Alignment.center, padding: const EdgeInsets.all(8), diff --git a/lib/features/profile/presentation/widgets/sub_action_button_widget.dart b/lib/features/profile/presentation/widgets/sub_action_button_widget.dart index c1929a13..bca5e3da 100644 --- a/lib/features/profile/presentation/widgets/sub_action_button_widget.dart +++ b/lib/features/profile/presentation/widgets/sub_action_button_widget.dart @@ -1,5 +1,6 @@ import 'package:eatvio_app/core/locator_service.dart'; import 'package:eatvio_app/core/theme/theme_helper.dart'; +import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart'; import 'package:eatvio_app/features/profile/presentation/bloc/profile_cubit/profile_cubit.dart'; import 'package:eatvio_app/features/profile/presentation/bloc/sub_cubit/sub_cubit.dart'; import 'package:flutter/material.dart'; @@ -11,11 +12,15 @@ enum SubActionType { sendMessage, subscribe } class SubActionButtonWidget extends StatefulWidget { final SubActionType subActionType; final String slug; + final String name; + final String porfileIcon; const SubActionButtonWidget({ super.key, required this.subActionType, required this.slug, + required this.name, + required this.porfileIcon, }); @override @@ -64,7 +69,14 @@ class _SubActionButtonWidgetState extends State { GoRouter.of(context).pushNamed( 'chatScreen', extra: { - 'chatId': await sl().getChatRoomIdBySlug(slug: widget.slug), + 'chatId': await sl().getChatRoomIdBySlug( + author: Author( + authorSlug: widget.slug, + authorName: widget.name, + authorProfileIcon: widget.porfileIcon, + authorIsMe: false, + ), + ), }, ); }, diff --git a/lib/features/profile/presentation/widgets/sub_widget.dart b/lib/features/profile/presentation/widgets/sub_widget.dart index aa17433d..27365fd2 100644 --- a/lib/features/profile/presentation/widgets/sub_widget.dart +++ b/lib/features/profile/presentation/widgets/sub_widget.dart @@ -70,6 +70,8 @@ class _SubWidgetState extends State { SubActionButtonWidget( subActionType: widget.subModel.isEntitySubscribedByMe ? SubActionType.sendMessage : SubActionType.subscribe, slug: widget.subModel.slug, + name: widget.subModel.name, + porfileIcon: widget.subModel.profilePhotoUrl, ), ], ),