1 to 1 chat ui completed for text message, with 2 users.

design_3.0_cx_module
Sikander Saleem 2 weeks ago
parent a902204ff1
commit db1e7ea0bd

@ -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 {

@ -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<ChatResponse?> sendTextMessage(String message, int conversationId) async {

@ -59,8 +59,17 @@ class _ChatPageState extends State<ChatPage> {
void getChatToken() {
String assigneeEmployeeNumber = Provider.of<ServiceRequestDetailProvider>(context, listen: false).currentWorkOrder?.data?.assignedEmployee?.employeeId ?? "";
Provider.of<ChatProvider>(context, listen: false).getUserAutoLoginToken(widget.moduleId, widget.requestId, widget.title,
Provider.of<ServiceRequestDetailProvider>(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<ServiceRequestDetailProvider>(context, listen: false).currentWorkOrder!.data!.workOrderContactPerson.first.employeeId! : "");
// assigneeEmployeeNumber
// Provider.of<ChatProvider>(context, listen: false).getUserAutoLoginToken(widget.moduleId, widget.requestId, widget.title, myEmployeeId, assigneeEmployeeNumber);
Provider.of<ChatProvider>(context, listen: false).getUserAutoLoginToken(widget.moduleId, widget.requestId, widget.title, myEmployeeId, receiver);
}
@override
@ -152,21 +161,22 @@ class _ChatPageState extends State<ChatPage> {
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<ChatPage> {
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<ChatPage> {
.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<ChatPage> {
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<ChatPage> {
).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<ChatPage> {
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),

@ -120,7 +120,7 @@ class ChatProvider with ChangeNotifier, DiagnosticableTreeMixin {
bool messageIsSending = false;
List<ChatResponse> chatResponseList = [];
List<ChatHistoryResponse> chatResponseList = [];
Participants? sender;
Participants? recipient;
@ -137,16 +137,16 @@ class ChatProvider with ChangeNotifier, DiagnosticableTreeMixin {
ChatApiClient().chatLoginResponse = null;
}
Future<void> getUserAutoLoginToken(int moduleId, int requestId, String title, String employeeNumber, String assigneeEmployeeNumber) async {
Future<void> 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<void> 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;

Loading…
Cancel
Save