import 'dart:io'; import 'dart:math'; import 'package:easy_debounce/easy_debounce.dart'; import 'package:eatvio_app/core/widgets/custom_outlined_button.dart'; import 'package:eatvio_app/core/widgets/data_error_widget.dart'; import 'package:eatvio_app/core/widgets/loader.dart'; import 'package:eatvio_app/features/chat/presentation/bloc/chat_room_cubit/chat_room_cubit.dart'; import 'package:eatvio_app/features/chat/presentation/bloc/chat_room_cubit/chat_room_state.dart'; import 'package:eatvio_app/features/chat/presentation/bloc/chats_cubit/chats_cubit.dart'; import 'package:eatvio_app/features/chat/presentation/widgets/about_order_button.dart'; import 'package:eatvio_app/features/chat/presentation/widgets/chat_bubble_widget.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/presentation/widgets/notification_banner.dart'; import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart'; import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart'; import 'package:eatvio_app/features/order_info/presentation/info_by_order_screen.dart'; import 'package:eatvio_app/features/plans/enums/chosen_plan_state_enum.dart'; import 'package:eatvio_app/features/user_details/presentation/user_details_screen.dart'; import 'package:eatvio_app/generated/l10n.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:go_router/go_router.dart'; import 'package:image_picker/image_picker.dart'; import 'package:intl/intl.dart'; import 'package:visibility_detector/visibility_detector.dart'; class ChatScreen extends StatefulWidget { final String chatId; final bool? sendInitialMessage; const ChatScreen({required this.chatId, super.key, required this.sendInitialMessage}); @override State createState() => _ChatScreenState(); } class _ChatScreenState extends State { final scrollController = ScrollController(); final TextEditingController textEditingController = TextEditingController(); final ChatRoomCubit chatRoomCubit = sl(); final List<({int id, int index})> unreadMessagesIds = []; final List<({int id, int index})> unreadMessagesIdsCopy = []; final showableChatBadgeStates = ChosenPlanStateEnum.getShowableChatBadgeStates(); late AppLifecycleListener _listener; void _onHide() { chatRoomCubit.cancelTimer(); } void _onResume() { chatRoomCubit.updateChosenPlanState(); chatRoomCubit.setTimer(); } @override void initState() { super.initState(); scrollController.addListener(_onScroll); chatRoomCubit.initChat(widget.chatId, sendInitialMessage: widget.sendInitialMessage ?? false); chatRoomCubit.setTimer(); _listener = AppLifecycleListener(onHide: _onHide, onResume: _onResume); } _onScroll() { if (scrollController.offset >= scrollController.position.maxScrollExtent && !scrollController.position.outOfRange) { chatRoomCubit.loadNextPage(widget.chatId); } } @override void dispose() { scrollController.dispose(); _listener.dispose(); chatRoomCubit.close(); super.dispose(); } @override Widget build(BuildContext context) { return BlocProvider( create: (BuildContext context) => chatRoomCubit, child: BlocBuilder( bloc: chatRoomCubit, builder: (context, state) { if (state is ChatRoomStateLoaded) { return _buildChat(context, state); } else if (state is ChatRoomStateError) { return Scaffold(appBar: AppBar(), body: DataErrorWidget(onReload: () => chatRoomCubit.initChat(widget.chatId))); } else { return const Scaffold(body: Loader()); } }, ), ); } _buildChat(BuildContext context, ChatRoomStateLoaded state) { final chosenPlan = state.chatRoom.chosenPlan; final amIConsultant = chosenPlan?.consultantId == state.chatRoom.users.sender.id; final showUserDetailsButton = [ ChosenPlanStateEnum.stateStartDateHasBeenSet, ChosenPlanStateEnum.statePendingPayment, ChosenPlanStateEnum.stateInCoordination, ].contains(chosenPlan?.chosenPlanState); final showAboutOrderButton = state.chatRoom.chosenPlan != null && state.chatRoom.chosenPlan!.chosenPlanState != ChosenPlanStateEnum.stateCanceledByTheInitiator; BannerType? bannerType; if (chosenPlan == null || chosenPlan.chosenPlanState == ChosenPlanStateEnum.stateCanceledByTheInitiator) { bannerType = null; } else if (amIConsultant && !state.hasCard) { bannerType = BannerType.card; } else if (amIConsultant && chosenPlan.chosenPlanState == ChosenPlanStateEnum.stateInCoordination) { bannerType = BannerType.date; } else if (!amIConsultant && (chosenPlan.chosenPlanState == ChosenPlanStateEnum.stateStartDateHasBeenSet || chosenPlan.chosenPlanState == ChosenPlanStateEnum.statePendingPayment)) { bannerType = BannerType.payment; } else if (chosenPlan.chosenPlanState == ChosenPlanStateEnum.stateDone && amIConsultant) { bannerType = BannerType.collectCharge; } else if (chosenPlan.chosenPlanState == ChosenPlanStateEnum.stateLastDayFinished && !amIConsultant) { bannerType = BannerType.finishPlan; } PacketOptions? chosenOption; if (chosenPlan?.optionType == 'options1') { chosenOption = chosenPlan?.plan.packet1Options; } else if (chosenPlan?.optionType == 'options2') { chosenOption = chosenPlan?.plan.packet2Options; } else if (chosenPlan?.optionType == 'options3') { chosenOption = chosenPlan?.plan.packet3Options; } return Scaffold( resizeToAvoidBottomInset: true, appBar: AppBar( titleSpacing: 0, backgroundColor: appTheme.gray100, centerTitle: false, title: GestureDetector( onTap: () { GoRouter.of(context).pushNamed( 'profileScreen', extra: Author( authorSlug: state.chatRoom.users.receiver.slug, authorName: state.chatRoom.users.receiver.name, authorProfileIcon: state.chatRoom.users.receiver.profilePhotoUrl ?? '', authorIsMe: false, ), ); }, child: Row( children: [ if (state.chatRoom.users.receiver.profilePhotoUrl != null) CircleAvatar( radius: 20, foregroundImage: NetworkImage(state.chatRoom.users.receiver.profilePhotoUrl!), backgroundImage: const AssetImage('assets/images/no_avatar.png'), ), if (state.chatRoom.users.receiver.profilePhotoUrl == null) Container( width: 40, height: 40, alignment: Alignment.center, decoration: const BoxDecoration( color: Color(0xFFEBF4FF), shape: BoxShape.circle, ), child: Text( state.chatRoom.users.receiver.name.split(' ').first[0] + state.chatRoom.users.receiver.name.split(' ')[1][0], style: const TextStyle( color: Color(0xFF7F9CF5), fontWeight: FontWeight.w400, fontSize: 20, ), ), ), const SizedBox(width: 16), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( state.chatRoom.users.receiver.name, style: CustomTextStyles.titleSmallBold.copyWith( color: PrimaryColors().black, ), ), AnimatedCrossFade( firstChild: Text( 'Печатает...', style: CustomTextStyles.bodySmallMulishPrimary, ), firstCurve: Curves.easeIn, secondCurve: Curves.easeOut, secondChild: const SizedBox.shrink(), crossFadeState: state.isTyping ? CrossFadeState.showFirst : CrossFadeState.showSecond, duration: const Duration(milliseconds: 150), ), ], ), ], ), ), ), body: SafeArea( child: Stack( children: [ Column( mainAxisSize: MainAxisSize.min, children: [ Expanded( child: Container( color: appTheme.whiteA700, width: double.maxFinite, child: SingleChildScrollView( physics: const BouncingScrollPhysics(), controller: scrollController, reverse: true, child: Column( children: [ const SizedBox(height: 16), ListView.builder( physics: const NeverScrollableScrollPhysics(), reverse: true, shrinkWrap: true, itemBuilder: (context, index) { if (index == state.chatMessages.length) { return const Padding(padding: EdgeInsets.all(8), child: Loader()); } if ((index + 1 != state.chatMessages.length && state.chatMessages[index].updatedAt.day != state.chatMessages[index + 1].updatedAt.day) || index + 1 == state.chatMessages.length) { final isCurrentUserAuthor = state.chatRoom.users.receiver.id != state.chatMessages[index].authorId; return Column( children: [ _messageDate(state.chatMessages[index].updatedAt), VisibilityDetector( key: Key(state.chatMessages[index].id.toString()), onVisibilityChanged: (VisibilityInfo info) { if (!state.chatMessages[index].isRead && !isCurrentUserAuthor && info.visibleFraction > 0) { unreadMessagesIdsCopy.add((id: state.chatMessages[index].id, index: index)); EasyDebounce.debounce('mark_as_read', const Duration(milliseconds: 500), () async { final result = await context.read().markMessagesAsRead(state.chatRoom.id, unreadMessagesIdsCopy); if (!result) return; if (!context.mounted) return; context.read().reduceUnreadMessagesCount( chatId: state.chatRoom.id, count: unreadMessagesIdsCopy.length, ); unreadMessagesIdsCopy.clear(); }); } }, child: ChatBubbleWidget( isRead: state.chatMessages[index].isRead, sendTime: state.chatMessages[index].updatedAt, text: state.chatMessages[index].data.trim(), isCurrentUser: isCurrentUserAuthor, mediaData: state.chatMessages[index].mediaData ?? [], ), ), ], ); } final isCurrentUserAuthor = state.chatRoom.users.receiver.id != state.chatMessages[index].authorId; return VisibilityDetector( key: Key(state.chatMessages[index].id.toString()), onVisibilityChanged: (VisibilityInfo info) { if (!state.chatMessages[index].isRead && !isCurrentUserAuthor && info.visibleFraction > 0) { unreadMessagesIds.add((id: state.chatMessages[index].id, index: index)); EasyDebounce.debounce('mark_as_read', const Duration(milliseconds: 500), () async { final result = await context.read().markMessagesAsRead(state.chatRoom.id, unreadMessagesIds); if (!result) return; if (!context.mounted) return; context.read().reduceUnreadMessagesCount(chatId: state.chatRoom.id, count: unreadMessagesIds.length); unreadMessagesIds.clear(); }); } }, child: ChatBubbleWidget( isRead: state.chatMessages[index].isRead, sendTime: state.chatMessages[index].updatedAt, text: state.chatMessages[index].data.trim(), isCurrentUser: isCurrentUserAuthor, mediaData: state.chatMessages[index].mediaData ?? [], ), ); }, itemCount: state.chatMessages.length + (state.isLoading ? 1 : 0), ), const SizedBox(height: 16), if (showUserDetailsButton) ...[ // OrderCard( // plan: chosenPlan.plan, // isCustomerView: !amIConsultant, // optionType: chosenPlan.optionType, // onCancel: () async { // await context.read().cancelChosenPlan(chatId: state.chatRoom.id, planId: chosenPlan.id); // }, // ), if (amIConsultant) Padding( padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16), child: CustomOutlinedButton( text: S.current.user_info, height: 52, buttonTextStyle: TextStyles.textMSemibold.copyWith(color: appTheme.whiteA700), buttonStyle: ButtonStyle( side: const WidgetStatePropertyAll(BorderSide.none), backgroundColor: WidgetStatePropertyAll(appTheme.blueA400), ), onPressed: () async { GoRouter.of(context).push(UserDetailsScreen.path, extra: state.chatRoom.users.receiver.slug); }, ), ), ], if (bannerType != null) Padding( padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16), child: NotificationBanner( bannerType: bannerType, chosenPlanId: chosenPlan?.id, planName: chosenPlan?.plan.name, planPrice: chosenOption?.price.toDouble(), ), ), ], ), ), ), ), Container( padding: const EdgeInsets.symmetric(vertical: 8), child: Row( crossAxisAlignment: CrossAxisAlignment.end, children: [ const SizedBox(width: 8), InkWell( onTap: () => showImagePicker(), 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: () { if (textEditingController.text.isNotEmpty) { chatRoomCubit.sendMessage(chatId: widget.chatId, data: textEditingController.text.trim()); setState(() { textEditingController.clear(); }); } }, 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), ], ), ), ], ), 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}); }, )), ], ), ), ); } showImagePicker() { 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 chatRoomCubit.sendMessage(chatId: widget.chatId, files: [File(image.path)]); } }, 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 chatRoomCubit.sendMessage(chatId: widget.chatId, files: files); } }, 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('Отмена'), ), ), ); } _messageDate(DateTime date) { return Center( child: Padding( padding: const EdgeInsets.only(top: 32), child: Text(DateFormat.yMMMMd('RU').format(date), style: const TextStyle(height: 1, color: Color(0xFF8393A1), fontWeight: FontWeight.w500)), ), ); } }