From 67f4fdef289f3d1165d978beea1f9c13c4be44f6 Mon Sep 17 00:00:00 2001 From: "Aamir.Muhammad" Date: Thu, 10 Nov 2022 16:06:25 +0300 Subject: [PATCH] Chat Fixes & Favorite Api Implementation --- lib/api/chat/chat_provider_model.dart | 88 +++---------------- lib/classes/consts.dart | 7 +- lib/ui/chat/chat_bubble.dart | 6 +- lib/ui/chat/chat_detailed_screen.dart | 59 ++++--------- lib/ui/chat/chat_home.dart | 4 +- .../search_employee_bottom_sheet.dart | 83 +++++------------ 6 files changed, 56 insertions(+), 191 deletions(-) diff --git a/lib/api/chat/chat_provider_model.dart b/lib/api/chat/chat_provider_model.dart index b3f37ab..b85aa85 100644 --- a/lib/api/chat/chat_provider_model.dart +++ b/lib/api/chat/chat_provider_model.dart @@ -141,20 +141,13 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { ) .build(); hubConnection.onclose( - ({Exception? error}) { - // logger.d(error); - }, + ({Exception? error}) {}, ); hubConnection.onreconnecting( - ({Exception? error}) { - // logger.d(error); - // logger.d("Reconnecting"); - }, + ({Exception? error}) {}, ); hubConnection.onreconnected( - ({String? connectionId}) { - // logger.d("Reconnected"); - }, + ({String? connectionId}) {}, ); if (hubConnection.state != HubConnectionState.Connected) { await hubConnection.start(); @@ -403,7 +396,6 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { sFileType = ""; message.clear(); notifyListeners(); - // scrollDown(); } void sendChatMessage(int targetUserId, String targetUserName) async { @@ -437,7 +429,6 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { } sendChatToServer( chatEventId: 1, fileTypeId: null, targetUserId: targetUserId, targetUserName: targetUserName, chatReplyId: repliedMsg.first.userChatHistoryId, isAttachment: false, isReply: true); -//chatReplyId } if (isFileSelected && isMsgReply) { logger.d("Attachment Message With Reply"); @@ -445,60 +436,14 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { dynamic value = await uploadAttachments(AppState().chatDetails!.response!.id.toString(), selectedFile); String? ext = getFileExtension(selectedFile.path); sendChatToServer( - chatEventId: 2, - fileTypeId: getFileType(ext.toString()), - targetUserId: targetUserId, - targetUserName: targetUserName, - isAttachment: true, - chatReplyId: repliedMsg.first.userChatHistoryId, - isReply: true); - } - - // dynamic contain = searchedChats!.where((ChatUser element) => element.id == targetUserId); - // if (contain.isEmpty) { - // searchedChats!.add( - // ChatUser( - // id: targetUserId, - // userName: targetUserName, - // ), - // ); - // notifyListeners(); - // } - // Uuid uuid = const Uuid(); - // dynamic fileID = fileUploadResponse.isEmpty ? null : getFileType(chatMessage); - // SingleUserChatModel data = SingleUserChatModel( - // chatEventId: fileUploadResponse.isEmpty ? 1 : 2, - // chatSource: 1, - // contant: chatMessage, - // contantNo: uuid.v4(), - // conversationId: uuid.v4(), - // createdDate: DateTime.now(), - // currentUserId: AppState().chatDetails!.response!.id, - // currentUserName: AppState().chatDetails!.response!.userName, - // targetUserId: targetUserId, - // targetUserName: targetUserName, - // fileTypeId: fileID, - // isReplied: false, - // // fileTypeResponse: FileTypeResponse( - // // fileTypeId: 0, - // // fileTypeDescription: fileUploadResponse.isEmpty ? null : fileUploadResponse.first["filePath"], - // // fileName: fileUploadResponse.isEmpty ? null : fileUploadResponse.first["filePath"], - // // fileKind: "image", - // // fileTypeName: fileUploadResponse.isEmpty ? null : fileUploadResponse.first["filePath"].toString().split(".").last), - // ); - // - // String chatData = - // '{"contant":"$chatMessage","contantNo":"${uuid.v4()}","chatEventId":${fileUploadResponse.isEmpty ? 1 : 2},"fileTypeId": $fileID,"currentUserId":${AppState().chatDetails!.response!.id},"chatSource":1,"userChatHistoryLineRequestList":[{"isSeen":false,"isDelivered":false,"targetUserId":$targetUserId,"targetUserStatus":1}],"conversationId":"${uuid.v4()}"}'; - // await hubConnection.invoke("AddChatUserAsync", args: [json.decode(chatData)]); - // userChatHistory.add(data); - // message.clear(); - // notifyListeners(); - // scrollDown(); - } - - void scrollListener() { - if (userChatHistory.length < paginationVal) { - print("Get New Data"); + chatEventId: 2, + fileTypeId: getFileType(ext.toString()), + targetUserId: targetUserId, + targetUserName: targetUserName, + isAttachment: true, + chatReplyId: repliedMsg.first.userChatHistoryId, + isReply: true, + ); } } @@ -513,18 +458,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { } else { Utils.showToast("Max 1 mb size is allowed to upload"); } - //Utils.showLoading(context); notifyListeners(); - //Utils.hideLoading(context); - // Utils.showLoading(context); - // await m.uploadAttachments(AppState().chatDetails!.response!.id.toString(), file).then((value) { - // if (value == null) { - // m.logger.d("Returned EMPTY"); - // } else { - // m.sendChatMessage(value.isEmpty ? m.message.text : value.first["filePath"], userDetails["targetUser"].id, userDetails["targetUser"].userName, value); - // } - // }); - // Utils.hideLoading(context); }); } diff --git a/lib/classes/consts.dart b/lib/classes/consts.dart index c98d136..c5788f9 100644 --- a/lib/classes/consts.dart +++ b/lib/classes/consts.dart @@ -10,6 +10,7 @@ class ApiConsts { static String user = baseUrlServices + "api/User/"; static String cocRest = baseUrlServices + "COCWS.svc/REST/"; + //Chat static String chatServerBaseUrl = "https://apiderichat.hmg.com"; static String chatServerBaseApiUrl = "https://apiderichat.hmg.com/api/"; static String chatHubConnectionUrl = chatServerBaseUrl + "/ConnectionChatHub"; @@ -18,12 +19,6 @@ class ApiConsts { static String chatSingleUserHistoryUrl = "UserChatHistory/GetUserChatHistory"; static String chatMediaImageUploadUrl = "shared/upload"; static String chatFavoriteUsers = "FavUser/getFavUserById/"; - //https://apiderichat.hmg.com/api/FavUser/getFavUserById/42062 -//https://apiderichat.hmg.com/api/shared/upload -// 42062 is CurrentUserID and 36745 is targetUserID and 0 is For Pagination - -// static String chatSearchMember = "https://apiderichat.hmg.com/api/user/getUserWithStatusAndFavAsync/aamir.muhammad/36239"; - } class SharedPrefsConsts { diff --git a/lib/ui/chat/chat_bubble.dart b/lib/ui/chat/chat_bubble.dart index 1e8aec4..5a878ca 100644 --- a/lib/ui/chat/chat_bubble.dart +++ b/lib/ui/chat/chat_bubble.dart @@ -29,7 +29,7 @@ class ChatBubble extends StatelessWidget { @override Widget build(BuildContext context) { return Padding( - // padding: EdgeInsets.zero, + // padding: EdgeInsets.zero, padding: EdgeInsets.only(left: isCurrentUser ? 110 : 20, right: isCurrentUser ? 20 : 110, bottom: 9), child: Align( @@ -51,7 +51,7 @@ class ChatBubble extends StatelessWidget { borderRadius: BorderRadius.circular(10), ), child: Padding( - padding: EdgeInsets.only(top: isReplied ? 8 : 5, right:8, left: 8, bottom: 5), + padding: EdgeInsets.only(top: isReplied ? 8 : 5, right: 8, left: 8, bottom: 5), child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start, @@ -85,7 +85,6 @@ class ChatBubble extends StatelessWidget { ), if (isReplied) 8.height, text.toText12(color: isCurrentUser ? MyColors.grey57Color : MyColors.white), - Row( crossAxisAlignment: CrossAxisAlignment.end, mainAxisAlignment: MainAxisAlignment.end, @@ -100,7 +99,6 @@ class ChatBubble extends StatelessWidget { ), ], ), - ], ), ), diff --git a/lib/ui/chat/chat_detailed_screen.dart b/lib/ui/chat/chat_detailed_screen.dart index d2d3598..fe7a968 100644 --- a/lib/ui/chat/chat_detailed_screen.dart +++ b/lib/ui/chat/chat_detailed_screen.dart @@ -8,19 +8,14 @@ import 'package:flutter_svg/flutter_svg.dart'; import 'package:mohem_flutter_app/api/chat/chat_provider_model.dart'; import 'package:mohem_flutter_app/app_state/app_state.dart'; import 'package:mohem_flutter_app/classes/colors.dart'; -import 'package:mohem_flutter_app/classes/colors.dart'; -import 'package:mohem_flutter_app/classes/utils.dart'; -import 'package:mohem_flutter_app/extensions/int_extensions.dart'; import 'package:mohem_flutter_app/extensions/string_extensions.dart'; import 'package:mohem_flutter_app/extensions/widget_extensions.dart'; import 'package:mohem_flutter_app/generated/locale_keys.g.dart'; import 'package:mohem_flutter_app/ui/chat/chat_bubble.dart'; import 'package:mohem_flutter_app/widgets/app_bar_widget.dart'; -import 'package:mohem_flutter_app/widgets/image_picker.dart'; import 'package:mohem_flutter_app/widgets/shimmer/dashboard_shimmer_widget.dart'; import 'package:provider/provider.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; -import 'package:sizer/sizer.dart'; import 'package:swipe_to/swipe_to.dart'; class ChatDetailScreen extends StatelessWidget { @@ -30,13 +25,13 @@ class ChatDetailScreen extends StatelessWidget { ScrollController scrollController = ScrollController(); RefreshController _refreshController = RefreshController(initialRefresh: false); - void onRefresh() async { - print("Refresh Triggered"); - await Future.delayed(Duration(milliseconds: 1000)); + void getMoreChat() async { + if (userDetails != null) { data.paginationVal = data.paginationVal + 10; data.getSingleUserChatHistory(senderUID: AppState().chatDetails!.response!.id.toString(), receiverUID: userDetails["targetUser"].id, loadMore: true); } + await Future.delayed(Duration(milliseconds: 1000)); _refreshController.refreshCompleted(); } @@ -45,16 +40,6 @@ class ChatDetailScreen extends StatelessWidget { userDetails = ModalRoute.of(context)!.settings.arguments; data = Provider.of(context, listen: false); if (userDetails != null) data.getSingleUserChatHistory(senderUID: AppState().chatDetails!.response!.id.toString(), receiverUID: userDetails["targetUser"].id, loadMore: false); - // - // if (userDetails != null) { - // scrollController.addListener(() { - // if (scrollController.position.minScrollExtent == scrollController.offset) { - // data.paginationVal++; - // data.getSingleUserChatHistory(senderUID: AppState().chatDetails!.response!.id.toString(), receiverUID: userDetails["targetUser"].id, loadMore: true); - // } - // }); - // } - return Scaffold( backgroundColor: const Color(0xFFF8F8F8), appBar: AppBarWidget(context, title: userDetails["targetUser"].userName, showHomeButton: false, image: userDetails["targetUser"].image), @@ -73,34 +58,28 @@ class ChatDetailScreen extends StatelessWidget { color: MyColors.gradiantEndColor, ), controller: _refreshController, - onRefresh: onRefresh, + onRefresh: getMoreChat, child: ListView.builder( controller: scrollController, shrinkWrap: true, itemCount: m.userChatHistory.length, padding: EdgeInsets.zero, itemBuilder: (BuildContext context, int i) { - return GestureDetector( - onTap: () { - m.logger.d(jsonEncode(m.userChatHistory[i])); - m.logger.d(jsonEncode(m.userChatHistory.length)); - }, - child: SwipeTo( - iconColor: MyColors.lightGreenColor, - child: ChatBubble( - text: m.userChatHistory[i].contant.toString(), - replyText: m.userChatHistory[i].userChatReplyResponse != null ? m.userChatHistory[i].userChatReplyResponse!.contant.toString() : "", - isSeen: m.userChatHistory[i].isSeen == true ? true : false, - isCurrentUser: m.userChatHistory[i].currentUserId == AppState().chatDetails!.response!.id ? true : false, - isDelivered: m.userChatHistory[i].currentUserId == AppState().chatDetails!.response!.id && m.userChatHistory[i].isDelivered == true ? true : false, - dateTime: m.dateFormte(m.userChatHistory[i].createdDate!), - isReplied: m.userChatHistory[i].userChatReplyResponse != null ? true : false, - userName: AppState().chatDetails!.response!.userName == m.userChatHistory[i].currentUserName.toString() ? "You" : m.userChatHistory[i].currentUserName.toString(), - ), - onRightSwipe: () { - m.chatReply(m.userChatHistory[i]); - }, + return SwipeTo( + iconColor: MyColors.lightGreenColor, + child: ChatBubble( + text: m.userChatHistory[i].contant.toString(), + replyText: m.userChatHistory[i].userChatReplyResponse != null ? m.userChatHistory[i].userChatReplyResponse!.contant.toString() : "", + isSeen: m.userChatHistory[i].isSeen == true ? true : false, + isCurrentUser: m.userChatHistory[i].currentUserId == AppState().chatDetails!.response!.id ? true : false, + isDelivered: m.userChatHistory[i].currentUserId == AppState().chatDetails!.response!.id && m.userChatHistory[i].isDelivered == true ? true : false, + dateTime: m.dateFormte(m.userChatHistory[i].createdDate!), + isReplied: m.userChatHistory[i].userChatReplyResponse != null ? true : false, + userName: AppState().chatDetails!.response!.userName == m.userChatHistory[i].currentUserName.toString() ? "You" : m.userChatHistory[i].currentUserName.toString(), ), + onRightSwipe: () { + m.chatReply(m.userChatHistory[i]); + }, ); }, ), @@ -203,7 +182,7 @@ class ChatDetailScreen extends StatelessWidget { ], ) : null, - suffixIcon: Container( + suffixIcon: SizedBox( width: 96, child: Row( mainAxisAlignment: MainAxisAlignment.end, diff --git a/lib/ui/chat/chat_home.dart b/lib/ui/chat/chat_home.dart index 106a4b3..38388ca 100644 --- a/lib/ui/chat/chat_home.dart +++ b/lib/ui/chat/chat_home.dart @@ -27,7 +27,7 @@ class ChatHomeScreen extends StatefulWidget { } class _ChatHomeScreenState extends State { - TextEditingController search = new TextEditingController(); + TextEditingController search = TextEditingController(); late ChatProviderModel data; @override @@ -213,8 +213,6 @@ class _ChatHomeScreenState extends State { apiMode: LocaleKeys.delegate.tr(), fromChat: true, onSelectEmployee: (_selectedEmployee) { - // Navigator.pop(context); - // selectedReplacementEmployee = _selectedEmployee; setState(() {}); }, ), diff --git a/lib/widgets/bottom_sheets/search_employee_bottom_sheet.dart b/lib/widgets/bottom_sheets/search_employee_bottom_sheet.dart index fb14141..63ce88b 100644 --- a/lib/widgets/bottom_sheets/search_employee_bottom_sheet.dart +++ b/lib/widgets/bottom_sheets/search_employee_bottom_sheet.dart @@ -136,12 +136,13 @@ class _SearchEmployeeBottomSheetState extends State { }, ).expanded, IconButton( - constraints: const BoxConstraints(), - onPressed: () async { - await SystemChannels.textInput.invokeMethod('TextInput.hide'); - widget.fromChat ? fetchChatUser() : fetchUserByInput(); - }, - icon: Icon(Icons.search)) + constraints: const BoxConstraints(), + onPressed: () async { + await SystemChannels.textInput.invokeMethod('TextInput.hide'); + widget.fromChat ? fetchChatUser() : fetchUserByInput(); + }, + icon: const Icon(Icons.search), + ) ], ), if (replacementList != null) @@ -184,62 +185,21 @@ class _SearchEmployeeBottomSheetState extends State { if (chatUsersList != null && widget.fromChat) chatUsersList!.isEmpty ? Column( - children: [ - 20.height, - Utils.getNoDataWidget(context), - ], - ) + children: [ + 20.height, + Utils.getNoDataWidget(context), + ], + ) : ListView( physics: const BouncingScrollPhysics(), - padding: const EdgeInsets.only(top: 15,), + padding: const EdgeInsets.only( + top: 15, + ), children: [ ListView.separated( physics: const NeverScrollableScrollPhysics(), shrinkWrap: true, itemBuilder: (BuildContext cxt, int index) { - - - - - // return ListTile( - // leading: Stack( - // children: [ - // SvgPicture.asset( - // "assets/images/user.svg", - // height: 48, - // width: 48, - // ), - // Positioned( - // right: 5, - // bottom: 1, - // child: Container( - // width: 10, - // height: 10, - // decoration: BoxDecoration( - // color: chatUsersList![index].userStatus == 1 ? MyColors.green2DColor : Colors.red, - // borderRadius: const BorderRadius.all( - // Radius.circular(10), - // ), - // ), - // ), - // ) - // ], - // ), - // title: (chatUsersList![index].userName ?? "").toText14(color: MyColors.darkTextColor), - // // subtitle: (chatUsersList![index].isTyping == true ? "Something is Typing" : "Last message text").toText11(color: MyColors.normalTextColor), - // // trailing: ("Today").toText10(color: MyColors.lightTextColor), - // minVerticalPadding: 0, - // onTap: () { - // Navigator.pop(context); - // Navigator.pushNamed( - // context, - // AppRoutes.chatDetailed, - // arguments: {"targetUser": chatUsersList![index]}, - // ); - // }, - // ); - - return SizedBox( height: 55, child: ListTile( @@ -279,12 +239,9 @@ class _SearchEmployeeBottomSheetState extends State { onLongPress: () {}, ), ); - - - }, separatorBuilder: (BuildContext context, int index) => const Padding( - padding: EdgeInsets.only(right: 10, left: 70, bottom: 0,top: 0), + padding: EdgeInsets.only(right: 10, left: 70, bottom: 0, top: 0), child: Divider( color: Color(0xFFE5E5E5), ), @@ -349,7 +306,9 @@ class _SearchEmployeeBottomSheetState extends State { decoration: BoxDecoration( color: Colors.transparent, border: Border.all(color: MyColors.borderColor, width: 1), - borderRadius: const BorderRadius.all(Radius.circular(100)), + borderRadius: const BorderRadius.all( + Radius.circular(100), + ), ), padding: const EdgeInsets.all(4), child: Container( @@ -357,7 +316,9 @@ class _SearchEmployeeBottomSheetState extends State { height: double.infinity, decoration: BoxDecoration( color: value == groupValue ? MyColors.grey3AColor : Colors.transparent, - borderRadius: BorderRadius.all(const Radius.circular(100)), + borderRadius: const BorderRadius.all( + Radius.circular(100), + ), ), ), ),