From db1e7ea0bd8fb5412459c679fa00e482a3bdf314 Mon Sep 17 00:00:00 2001 From: Sikander Saleem Date: Sun, 16 Nov 2025 12:53:20 +0300 Subject: [PATCH] 1 to 1 chat ui completed for text message, with 2 users. --- lib/extensions/string_extensions.dart | 2 +- .../cx_module/chat/chat_api_client.dart | 18 +-- lib/modules/cx_module/chat/chat_page.dart | 111 ++++++++++-------- lib/modules/cx_module/chat/chat_provider.dart | 16 +-- 4 files changed, 82 insertions(+), 65 deletions(-) diff --git a/lib/extensions/string_extensions.dart b/lib/extensions/string_extensions.dart index a8fd4f51..16ae3fec 100644 --- a/lib/extensions/string_extensions.dart +++ b/lib/extensions/string_extensions.dart @@ -18,7 +18,7 @@ extension StringExtensions on String { String get chatMsgDateWithYear { DateTime dateTime = DateTime.parse(this); - return DateFormat('EEE dd MMM yyyy').format(dateTime); + return DateFormat('EEE dd MMM, yyyy').format(dateTime); } String get toServiceRequestCardFormat { diff --git a/lib/modules/cx_module/chat/chat_api_client.dart b/lib/modules/cx_module/chat/chat_api_client.dart index b6d5adc8..9641b0ec 100644 --- a/lib/modules/cx_module/chat/chat_api_client.dart +++ b/lib/modules/cx_module/chat/chat_api_client.dart @@ -78,18 +78,18 @@ class ChatApiClient { Response response = await ApiClient().postJsonForResponse( "${URLs.chatHubUrlApi}/UserChatHistory/GetUserChatHistory/$myId/$otherId/0", {"moduleCode": moduleId.toString(), "referenceId": referenceId.toString()}, token: chatLoginResponse!.token); - try { - if (response.statusCode == 200) { - List data = jsonDecode(response.body); - return data.map((elemet) => ChatHistoryResponse.fromJson(elemet)).toList(); + // try { + if (response.statusCode == 200) { + List data = jsonDecode(response.body); + return data.map((elemet) => ChatHistoryResponse.fromJson(elemet)).toList(); - // return UserChatHistoryModel.fromJson(jsonDecode(response.body)); - } else { - return []; - } - } catch (ex) { + // return UserChatHistoryModel.fromJson(jsonDecode(response.body)); + } else { return []; } + // } catch (ex) { + // return []; + // } } Future sendTextMessage(String message, int conversationId) async { diff --git a/lib/modules/cx_module/chat/chat_page.dart b/lib/modules/cx_module/chat/chat_page.dart index f7e8fb5c..ef183c10 100644 --- a/lib/modules/cx_module/chat/chat_page.dart +++ b/lib/modules/cx_module/chat/chat_page.dart @@ -59,8 +59,17 @@ class _ChatPageState extends State { void getChatToken() { String assigneeEmployeeNumber = Provider.of(context, listen: false).currentWorkOrder?.data?.assignedEmployee?.employeeId ?? ""; - Provider.of(context, listen: false).getUserAutoLoginToken(widget.moduleId, widget.requestId, widget.title, - Provider.of(context, listen: false).currentWorkOrder!.data!.workOrderContactPerson.first.employeeId!, assigneeEmployeeNumber); + String myEmployeeId = context.userProvider.user!.username!; + + // String sender = context.settingProvider.username; + String receiver = context.userProvider.isNurse + ? assigneeEmployeeNumber + : (context.userProvider.isEngineer ? Provider.of(context, listen: false).currentWorkOrder!.data!.workOrderContactPerson.first.employeeId! : ""); + + // assigneeEmployeeNumber + + // Provider.of(context, listen: false).getUserAutoLoginToken(widget.moduleId, widget.requestId, widget.title, myEmployeeId, assigneeEmployeeNumber); + Provider.of(context, listen: false).getUserAutoLoginToken(widget.moduleId, widget.requestId, widget.title, myEmployeeId, receiver); } @override @@ -152,21 +161,22 @@ class _ChatPageState extends State { reverse: true, itemBuilder: (cxt, index) { final currentMessage = chatProvider.chatResponseList[index]; - final bool showSenderName = (index == chatProvider.chatResponseList.length - 1) || (currentMessage.userId != chatProvider.chatResponseList[index + 1].userId); - bool isSender = chatProvider.chatResponseList[index].userId == chatProvider.sender?.userId!; + final bool showSenderName = + (index == chatProvider.chatResponseList.length - 1) || (currentMessage.currentUserId != chatProvider.chatResponseList[index + 1].currentUserId); + bool isSender = chatProvider.chatResponseList[index].currentUserId == chatProvider.sender?.userId!; bool showDateHeader = false; if (index == chatProvider.chatResponseList.length - 1) { showDateHeader = true; } else { final nextMessage = chatProvider.chatResponseList[index + 1]; - final currentDate = DateUtils.dateOnly(DateTime.parse(currentMessage.createdAt!)); - final nextDate = DateUtils.dateOnly(DateTime.parse(nextMessage.createdAt!)); + final currentDate = DateUtils.dateOnly(DateTime.parse(currentMessage.createdDate!)); + final nextDate = DateUtils.dateOnly(DateTime.parse(nextMessage.createdDate!)); if (!currentDate.isAtSameMomentAs(nextDate)) { showDateHeader = true; } } return Column(mainAxisSize: MainAxisSize.min, children: [ - if (showDateHeader) dateCard(currentMessage.createdAt?.chatMsgDate ?? ""), + if (showDateHeader) dateCard(currentMessage.createdDate?.chatMsgDateWithYear ?? ""), isSender ? senderMsgCard(showSenderName, chatProvider.chatResponseList[index]) : recipientMsgCard(showSenderName, chatProvider.chatResponseList[index]) ]); }, @@ -200,15 +210,6 @@ class _ChatPageState extends State { suffixIconConstraints: const BoxConstraints(), hintText: "Type your message here...", hintStyle: AppTextStyles.bodyText.copyWith(color: const Color(0xffCCCCCC)), - // suffixIcon: Row( - // mainAxisSize: MainAxisSize.min, - // crossAxisAlignment: CrossAxisAlignment.end, - // mainAxisAlignment: MainAxisAlignment.end, - // children: [ - // - // 8.width, - // ], - // ) ), ).expanded, IconButton( @@ -498,12 +499,12 @@ class _ChatPageState extends State { .center; } - Widget senderMsgCard(bool showHeader, ChatResponse? chatResponse, {bool loading = false, String msg = ""}) { + Widget senderMsgCard(bool showHeader, ChatHistoryResponse? chatResponse, {bool loading = false, String msg = ""}) { Widget senderHeader = Row( mainAxisSize: MainAxisSize.min, children: [ Text( - "${chatResponse?.userName ?? "User"}(Me)", + "${chatResponse?.currentUserName ?? "User"}(Me)", overflow: TextOverflow.ellipsis, maxLines: 1, style: AppTextStyles.bodyText.copyWith(color: AppColor.neutral50, fontWeight: FontWeight.w600), @@ -524,44 +525,60 @@ class _ChatPageState extends State { crossAxisAlignment: CrossAxisAlignment.end, children: [ if (showHeader) ...[senderHeader, 4.height] else 8.height, - Container( - padding: const EdgeInsets.all(8), - margin: const EdgeInsets.only(right: 26 + 8, left: 26 + 8), - decoration: BoxDecoration( - color: loading ? Colors.transparent : AppColor.white10, - borderRadius: BorderRadius.circular(6), + Row( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + Flexible( + fit: FlexFit.loose, + child: Container( + padding: const EdgeInsets.all(8), + margin: const EdgeInsets.only(right: 8, left: 26 + 8), + decoration: BoxDecoration( + color: loading ? Colors.transparent : AppColor.white10, + borderRadius: BorderRadius.circular(6), + ), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + Text( + chatResponse?.contant ?? msg, + style: AppTextStyles.bodyText2.copyWith(color: AppColor.neutral120), + ).toShimmer(context: context, isShow: loading), + if (loading) 4.height, + Text( + chatResponse?.createdDate?.chatMsgTime ?? "2:00 PM", + style: AppTextStyles.textFieldLabelStyle.copyWith(color: AppColor.neutral50.withOpacity(0.5)), + ).toShimmer(context: context, isShow: loading), + ], + )), ), - child: Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.end, - children: [ - Text( - chatResponse?.content ?? msg, - style: AppTextStyles.bodyText2.copyWith(color: AppColor.neutral120), - ).toShimmer(context: context, isShow: loading), - if (loading) 4.height, - Text( - chatResponse?.createdAt?.chatMsgTime ?? "2:00 PM", - style: AppTextStyles.textFieldLabelStyle.copyWith(color: AppColor.neutral50.withOpacity(0.5)), - ).toShimmer(context: context, isShow: loading), - ], - )), + if (chatResponse != null) + if (chatResponse.isSeen!) + "chat_seen".toSvgAsset(width: 16, height: 16) + else if (chatResponse.isDelivered!) + "chat_delivered".toSvgAsset(width: 16, height: 16) + else + "chat_sent".toSvgAsset(width: 16, height: 16), + (26 + 8).width, + ], + ), ], ), ); } - Widget recipientMsgCard(bool showHeader, ChatResponse? chatResponse, {bool loading = false, String msg = ""}) { + Widget recipientMsgCard(bool showHeader, ChatHistoryResponse? chatResponse, {bool loading = false, String msg = ""}) { String extraSpaces = ""; int length = 0; - if ((chatResponse?.content ?? "").isNotEmpty) { - if (chatResponse!.content!.length < 8) { - length = 8 - chatResponse.content!.length; + if ((chatResponse?.contant ?? "").isNotEmpty) { + if (chatResponse!.contant!.length < 8) { + length = 8 - chatResponse.contant!.length; } } - String contentMsg = chatResponse?.content == null ? msg : chatResponse!.content! + extraSpaces; - print("'$contentMsg'"); + String contentMsg = chatResponse?.contant == null ? msg : chatResponse!.contant! + extraSpaces; Widget recipientHeader = Row( mainAxisSize: MainAxisSize.min, children: [ @@ -572,7 +589,7 @@ class _ChatPageState extends State { ).toShimmer(context: context, isShow: loading), 8.width, Text( - chatResponse?.userName ?? "User", + chatResponse?.currentUserName ?? "User", overflow: TextOverflow.ellipsis, maxLines: 1, style: AppTextStyles.bodyText.copyWith(color: AppColor.neutral50, fontWeight: FontWeight.w600), @@ -607,7 +624,7 @@ class _ChatPageState extends State { alignment: Alignment.centerRight, widthFactor: 1, child: Text( - chatResponse?.createdAt?.chatMsgTime ?? "2:00 PM", + chatResponse?.createdDate?.chatMsgTime ?? "2:00 PM", style: AppTextStyles.textFieldLabelStyle.copyWith(color: AppColor.white10), ), ).toShimmer(context: context, isShow: loading), diff --git a/lib/modules/cx_module/chat/chat_provider.dart b/lib/modules/cx_module/chat/chat_provider.dart index 84262c5c..c86c5096 100644 --- a/lib/modules/cx_module/chat/chat_provider.dart +++ b/lib/modules/cx_module/chat/chat_provider.dart @@ -120,7 +120,7 @@ class ChatProvider with ChangeNotifier, DiagnosticableTreeMixin { bool messageIsSending = false; - List chatResponseList = []; + List chatResponseList = []; Participants? sender; Participants? recipient; @@ -137,16 +137,16 @@ class ChatProvider with ChangeNotifier, DiagnosticableTreeMixin { ChatApiClient().chatLoginResponse = null; } - Future getUserAutoLoginToken(int moduleId, int requestId, String title, String employeeNumber, String assigneeEmployeeNumber) async { + Future getUserAutoLoginToken(int moduleId, int requestId, String title, String myId, String assigneeEmployeeNumber) async { reset(); chatLoginTokenLoading = true; notifyListeners(); - chatLoginResponse = await ChatApiClient().getChatLoginToken(moduleId, requestId, title, employeeNumber); + chatLoginResponse = await ChatApiClient().getChatLoginToken(moduleId, requestId, title, myId); chatLoginTokenLoading = false; chatParticipantLoading = true; notifyListeners(); // loadParticipants(moduleId, requestId); - loadChatHistory(moduleId, requestId, employeeNumber, assigneeEmployeeNumber); + loadChatHistory(moduleId, requestId, myId, assigneeEmployeeNumber); } // Future loadParticipants(int moduleId, int requestId) async { @@ -173,8 +173,8 @@ class ChatProvider with ChangeNotifier, DiagnosticableTreeMixin { recipient = chatParticipantModel?.participants?.singleWhere((participant) => participant.employeeNumber == assigneeEmployeeNumber); } catch (e) {} userChatHistory = await ChatApiClient().loadChatHistory(moduleId, requestId, myId, assigneeEmployeeNumber); - // chatResponseList = userChatHistory?.response ?? []; - + chatResponseList = userChatHistory ?? []; + chatResponseList.sort((a, b) => b.createdDate!.compareTo(a.createdDate!)); userChatHistoryLoading = false; notifyListeners(); } @@ -186,9 +186,9 @@ class ChatProvider with ChangeNotifier, DiagnosticableTreeMixin { ChatResponse? chatResponse = await ChatApiClient().sendTextMessage(message, chatParticipantModel!.id!); if (chatResponse != null) { returnStatus = true; - chatResponseList.add(chatResponse); + // chatResponseList.add(chatResponse); try { - chatResponseList.sort((a, b) => b.createdAt!.compareTo(a.createdAt!)); + chatResponseList.sort((a, b) => b.createdDate!.compareTo(a.createdDate!)); } catch (ex) {} } messageIsSending = false;