diff --git a/lib/api/chat/chat_api_client.dart b/lib/api/chat/chat_api_client.dart index 43e9f6c..87e684e 100644 --- a/lib/api/chat/chat_api_client.dart +++ b/lib/api/chat/chat_api_client.dart @@ -46,15 +46,15 @@ class ChatApiClient { return userLoginResponse; } - Future?> getChatMemberFromSearch(String sName, int cUserId) async { - Response response = await ApiClient().getJsonForResponse( - "${ApiConsts.chatLoginTokenUrl}getUserWithStatusAndFavAsync/$sName/$cUserId", - token: AppState().chatDetails!.response!.token, - ); + Future getChatMemberFromSearch(String searchParam, int cUserId, int pageNo) async { + ChatUserModel chatUserModel; + Response response = await ApiClient().postJsonForResponse("${ApiConsts.chatLoginTokenUrl}getUserWithStatusAndFavAsync", {"employeeNumber": cUserId, "userName": searchParam, "pageNumber": pageNo}, + token: AppState().chatDetails!.response!.token); if (!kReleaseMode) { logger.i("res: " + response.body); } - return List.from(json.decode(response.body).map((x) => ChatUser.fromJson(x))); + chatUserModel = chatUserModelFromJson(response.body); + return chatUserModel; } //Get User Recent Chats diff --git a/lib/models/chat/get_search_user_chat_model.dart b/lib/models/chat/get_search_user_chat_model.dart index 3d023fd..e69fd3b 100644 --- a/lib/models/chat/get_search_user_chat_model.dart +++ b/lib/models/chat/get_search_user_chat_model.dart @@ -1,5 +1,10 @@ +import 'dart:convert'; import 'dart:io'; +ChatUserModel chatUserModelFromJson(String str) => ChatUserModel.fromJson(json.decode(str)); + +String chatUserModelToJson(ChatUserModel data) => json.encode(data.toJson()); + class ChatUserModel { ChatUserModel({ this.response, @@ -7,16 +12,40 @@ class ChatUserModel { }); List? response; - dynamic errorResponses; + List? errorResponses; factory ChatUserModel.fromJson(Map json) => ChatUserModel( response: json["response"] == null ? null : List.from(json["response"].map((x) => ChatUser.fromJson(x))), - errorResponses: json["errorResponses"], + errorResponses: json["errorResponses"] == null ? null : List.from(json["errorResponses"].map((x) => ErrorResponse.fromJson(x))), ); Map toJson() => { "response": response == null ? null : List.from(response!.map((x) => x.toJson())), - "errorResponses": errorResponses, + "errorResponses": errorResponses == null ? null : List.from(errorResponses!.map((x) => x.toJson())), + }; +} + +class ErrorResponse { + ErrorResponse({ + this.fieldName, + this.message, + }); + + dynamic? fieldName; + String? message; + + factory ErrorResponse.fromRawJson(String str) => ErrorResponse.fromJson(json.decode(str)); + + String toRawJson() => json.encode(toJson()); + + factory ErrorResponse.fromJson(Map json) => ErrorResponse( + fieldName: json["fieldName"], + message: json["message"] == null ? null : json["message"], + ); + + Map toJson() => { + "fieldName": fieldName, + "message": message == null ? null : message, }; } @@ -34,6 +63,8 @@ class ChatUser { this.isPin, this.isFav, this.isAdmin, + this.rKey, + this.totalCount, this.isTyping, this.isImageLoaded, this.isImageLoading, @@ -44,7 +75,7 @@ class ChatUser { String? userName; String? email; dynamic? phone; - dynamic? title; + String? title; int? userStatus; dynamic? image; int? unreadMessageCount; @@ -52,17 +83,23 @@ class ChatUser { bool? isPin; bool? isFav; bool? isAdmin; + dynamic? rKey; + int? totalCount; bool? isTyping; bool? isImageLoaded; bool? isImageLoading; File? userLocalDownlaodedImage; + factory ChatUser.fromRawJson(String str) => ChatUser.fromJson(json.decode(str)); + + String toRawJson() => json.encode(toJson()); + factory ChatUser.fromJson(Map json) => ChatUser( id: json["id"] == null ? null : json["id"], userName: json["userName"] == null ? null : json["userName"], email: json["email"] == null ? null : json["email"], phone: json["phone"], - title: json["title"], + title: json["title"] == null ? null : json["title"], userStatus: json["userStatus"] == null ? null : json["userStatus"], image: json["image"], unreadMessageCount: json["unreadMessageCount"] == null ? null : json["unreadMessageCount"], @@ -70,6 +107,8 @@ class ChatUser { isPin: json["isPin"] == null ? null : json["isPin"], isFav: json["isFav"] == null ? null : json["isFav"], isAdmin: json["isAdmin"] == null ? null : json["isAdmin"], + rKey: json["rKey"], + totalCount: json["totalCount"] == null ? null : json["totalCount"], isTyping: false, isImageLoaded: false, isImageLoading: true, @@ -80,7 +119,7 @@ class ChatUser { "userName": userName == null ? null : userName, "email": email == null ? null : email, "phone": phone, - "title": title, + "title": title == null ? null : title, "userStatus": userStatus == null ? null : userStatus, "image": image, "unreadMessageCount": unreadMessageCount == null ? null : unreadMessageCount, @@ -88,5 +127,7 @@ class ChatUser { "isPin": isPin == null ? null : isPin, "isFav": isFav == null ? null : isFav, "isAdmin": isAdmin == null ? null : isAdmin, + "rKey": rKey, + "totalCount": totalCount == null ? null : totalCount, }; } diff --git a/lib/provider/chat_provider_model.dart b/lib/provider/chat_provider_model.dart index 57abd3a..b4bd451 100644 --- a/lib/provider/chat_provider_model.dart +++ b/lib/provider/chat_provider_model.dart @@ -69,6 +69,10 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { //Chat Home Page Counter int chatUConvCounter = 0; + /// Search Provider + List? chatUsersList = []; + int pageNo = 1; + Future getUserAutoLoginToken() async { userLoginToken.UserAutoLoginModel userLoginResponse = await ChatApiClient().getUserLoginToken(); if (userLoginResponse.response != null) { @@ -172,7 +176,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { if (isNewChat) { userChatHistory = []; } else if (loadMore) { - Utils.showToast("No More Data To Load"); + } } else { if (loadMore) { @@ -329,6 +333,16 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { user.userStatus = items.first["userStatus"]; } } + if (teamMembersList != null) { + if (teamMembersList.isNotEmpty) { + for (ChatUser user in teamMembersList!) { + if (user.id == items.first["id"]) { + user.userStatus = items.first["userStatus"]; + } + } + } + } + notifyListeners(); } @@ -931,8 +945,26 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { } } } + + for (ChatUser user in chatUsersList!) { + if (user.id == favoriteChatUser.response!.targetUserId!) { + user.isFav = favoriteChatUser.response!.isFav; + dynamic contain = favUsersList!.where((ChatUser element) => element.id == favoriteChatUser.response!.targetUserId!); + if (contain.isEmpty) { + favUsersList.add(user); + } + } + } + } + for (ChatUser user in favUsersList) { + if (user.id == targetUserID) { + user.userLocalDownlaodedImage = null; + user.isImageLoading = false; + user.isImageLoaded = false; + } } notifyListeners(); + } Future unFavoriteUser({required int userID, required int targetUserID}) async { @@ -949,6 +981,12 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { ); } + for (ChatUser user in chatUsersList!) { + if (user.id == favoriteChatUser.response!.targetUserId!) { + user.isFav = favoriteChatUser.response!.isFav; + } + } + notifyListeners(); } @@ -1264,7 +1302,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { await Directory(dirPath).create(); await File('$dirPath/.nomedia').create(); } - File file = File("$dirPath/${data.currentUserId}-${data.targetUserId}-${DateTime.now().microsecondsSinceEpoch}" + ext); + File file = File("$dirPath/${data.currentUserId}-${data.targetUserId}-${DateTime.now().microsecondsSinceEpoch}." + ext); await file.writeAsBytes(bytes); return file.path; } @@ -1288,50 +1326,64 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { teamMembersList = []; isLoading = true; if (AppState().getemployeeSubordinatesList.isNotEmpty) { - print("=============== In App State ====================="); getEmployeeSubordinatesList = AppState().getemployeeSubordinatesList; for (GetEmployeeSubordinatesList element in getEmployeeSubordinatesList) { - print(element.eMPLOYEEEMAILADDRESS); - teamMembersList.add( - ChatUser( - id: int.parse(element.eMPLOYEENUMBER!), - email: element.eMPLOYEEEMAILADDRESS, - userName: element.eMPLOYEENAME, - phone: element.eMPLOYEEMOBILENUMBER, - userStatus: 0, - unreadMessageCount: 0, - isFav: false, - isTyping: false, - isImageLoading: false, - image: element.eMPLOYEEIMAGE ?? "", - isImageLoaded: element.eMPLOYEEIMAGE == null ? false : true, - userLocalDownlaodedImage:element.eMPLOYEEIMAGE == null ? null : await downloadImageLocal(element.eMPLOYEEIMAGE ?? "", element.eMPLOYEENUMBER!), - ), - ); + if (element.eMPLOYEEEMAILADDRESS != null) { + if (element.eMPLOYEEEMAILADDRESS!.isNotEmpty) { + teamMembersList.add( + ChatUser( + id: int.parse(element.eMPLOYEENUMBER!), + email: element.eMPLOYEEEMAILADDRESS, + userName: element.eMPLOYEENAME, + phone: element.eMPLOYEEMOBILENUMBER, + userStatus: 0, + unreadMessageCount: 0, + isFav: false, + isTyping: false, + isImageLoading: false, + image: element.eMPLOYEEIMAGE ?? "", + isImageLoaded: element.eMPLOYEEIMAGE == null ? false : true, + userLocalDownlaodedImage: element.eMPLOYEEIMAGE == null ? null : await downloadImageLocal(element.eMPLOYEEIMAGE ?? "", element.eMPLOYEENUMBER!), + ), + ); + } + } } } else { getEmployeeSubordinatesList = await MyTeamApiClient().getEmployeeSubordinates("", "", ""); AppState().setemployeeSubordinatesList = getEmployeeSubordinatesList; for (GetEmployeeSubordinatesList element in getEmployeeSubordinatesList) { - print(element.eMPLOYEEEMAILADDRESS); - teamMembersList.add( - ChatUser( - id: int.parse(element.eMPLOYEENUMBER!), - email: element.eMPLOYEEEMAILADDRESS, - userName: element.eMPLOYEENAME, - phone: element.eMPLOYEEMOBILENUMBER, - userStatus: 0, - unreadMessageCount: 0, - isFav: false, - isTyping: false, - isImageLoading: false, - image: element.eMPLOYEEIMAGE ?? "", - isImageLoaded: element.eMPLOYEEIMAGE == null ? false : true, - userLocalDownlaodedImage: element.eMPLOYEEIMAGE == null ? null : await downloadImageLocal(element.eMPLOYEEIMAGE ?? "", element.eMPLOYEENUMBER!), - ), - ); + if (element.eMPLOYEEEMAILADDRESS != null) { + if (element.eMPLOYEEEMAILADDRESS!.isNotEmpty) { + teamMembersList.add( + ChatUser( + id: int.parse(element.eMPLOYEENUMBER!), + email: element.eMPLOYEEEMAILADDRESS, + userName: element.eMPLOYEENAME, + phone: element.eMPLOYEEMOBILENUMBER, + userStatus: 0, + unreadMessageCount: 0, + isFav: false, + isTyping: false, + isImageLoading: false, + image: element.eMPLOYEEIMAGE ?? "", + isImageLoaded: element.eMPLOYEEIMAGE == null ? false : true, + userLocalDownlaodedImage: element.eMPLOYEEIMAGE == null ? null : await downloadImageLocal(element.eMPLOYEEIMAGE ?? "", element.eMPLOYEENUMBER!), + ), + ); + } + } } } + + for (ChatUser user in searchedChats!) { + for (ChatUser teamUser in teamMembersList!) { + if (user.id == teamUser.id) { + teamUser.userStatus = user.userStatus; + } + } + } + isLoading = false; notifyListeners(); } diff --git a/lib/ui/chat/chat_home_screen.dart b/lib/ui/chat/chat_home_screen.dart index 395556a..aed4048 100644 --- a/lib/ui/chat/chat_home_screen.dart +++ b/lib/ui/chat/chat_home_screen.dart @@ -238,6 +238,7 @@ class _ChatHomeScreenState extends State { ), ), onPressed: () async { + print(AppState().chatDetails!.response!.token); showMyBottomSheet( context, callBackFunc: () {}, diff --git a/lib/ui/chat/my_team_screen.dart b/lib/ui/chat/my_team_screen.dart index 1752295..5d36a68 100644 --- a/lib/ui/chat/my_team_screen.dart +++ b/lib/ui/chat/my_team_screen.dart @@ -84,24 +84,24 @@ class _MyTeamScreenState extends State { ), ), ), - // Positioned( - // right: 5, - // bottom: 1, - // child: Container( - // width: 10, - // height: 10, - // decoration: BoxDecoration( - // color: m.teamMembersList![index].userStatus == 1 ? MyColors.green2DColor : Colors.red, - // ), - // ).circle(10), - // ) + Positioned( + right: 5, + bottom: 1, + child: Container( + width: 10, + height: 10, + decoration: BoxDecoration( + color: m.teamMembersList![index].userStatus == 1 ? MyColors.green2DColor : Colors.red, + ), + ).circle(10), + ) ], ), Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ - (m.teamMembersList![index].userName! ?? "").toText14(color: MyColors.darkTextColor).paddingOnly(left: 11, top: 13), + (m.teamMembersList![index].userName!.replaceFirst(".", " ").capitalizeFirstofEach ?? "").toText14(color: MyColors.darkTextColor).paddingOnly(left: 11, top: 13), ], ).expanded, // SizedBox( diff --git a/lib/widgets/bottom_sheets/search_employee_bottom_sheet.dart b/lib/widgets/bottom_sheets/search_employee_bottom_sheet.dart index 488b50f..eada55f 100644 --- a/lib/widgets/bottom_sheets/search_employee_bottom_sheet.dart +++ b/lib/widgets/bottom_sheets/search_employee_bottom_sheet.dart @@ -20,10 +20,13 @@ import 'package:mohem_flutter_app/models/chat/get_search_user_chat_model.dart'; import 'package:mohem_flutter_app/models/get_action_history_list_model.dart'; import 'package:mohem_flutter_app/models/worklist/get_favorite_replacements_model.dart'; import 'package:mohem_flutter_app/models/worklist/replacement_list_model.dart'; +import 'package:mohem_flutter_app/provider/chat_provider_model.dart'; import 'package:mohem_flutter_app/ui/chat/chat_detailed_screen.dart'; import 'package:mohem_flutter_app/widgets/button/default_button.dart'; import 'package:mohem_flutter_app/widgets/circular_avatar.dart'; import 'package:mohem_flutter_app/widgets/dynamic_forms/dynamic_textfield_widget.dart'; +import 'package:provider/provider.dart'; +import 'package:pull_to_refresh/pull_to_refresh.dart'; class SearchEmployeeBottomSheet extends StatefulWidget { int? notificationID; @@ -47,6 +50,8 @@ class SearchEmployeeBottomSheet extends StatefulWidget { class _SearchEmployeeBottomSheetState extends State { TextEditingController username = TextEditingController(); + ScrollController sc = ScrollController(); + String searchText = ""; List? optionsList = [ @@ -61,7 +66,7 @@ class _SearchEmployeeBottomSheetState extends State { List? nonFavouriteUserList; // Chat Items - List? chatUsersList = []; + late ChatProviderModel provider; int _selectedSearchIndex = 0; @@ -88,25 +93,55 @@ class _SearchEmployeeBottomSheetState extends State { } void fetchChatUser({bool isNeedLoading = true}) async { + if (provider.pageNo == 1) provider.chatUsersList!.clear(); try { Utils.showLoading(context); - chatUsersList = await ChatApiClient().getChatMemberFromSearch( - searchText, - int.parse(AppState().chatDetails!.response!.id.toString()), - ); - chatUsersList!.removeWhere((ChatUser element) => element.id == AppState().chatDetails!.response!.id); + await ChatApiClient().getChatMemberFromSearch(searchText, AppState().chatDetails!.response!.id!, provider.pageNo).then((ChatUserModel value) { + if (value.response != null) { + if (provider.pageNo == 1) { + provider.chatUsersList = value.response; + } else { + print("--------------------------Added More----------------------"); + provider.chatUsersList!.addAll(value.response!); + } + } + }); + provider.chatUsersList!.removeWhere((ChatUser element) => element.id == AppState().chatDetails!.response!.id); Utils.hideLoading(context); setState(() {}); } catch (e) { Utils.hideLoading(context); Utils.handleException(e, context, null); } - if (isNeedLoading) Utils.hideLoading(context); setState(() {}); return null; } + void scrollListener() async { + if (sc.position.pixels == sc.position.maxScrollExtent) { + provider.pageNo++; + logger.w(provider.chatUsersList!.length); + logger.w(provider.pageNo); + fetchChatUser(); + } + } + + @override + void initState() { + super.initState(); + sc.addListener(scrollListener); + provider = Provider.of(context, listen: false); + } + + @override + void dispose() { + print("// TODO: implement dispose"); + super.dispose(); + provider.chatUsersList = []; + provider.pageNo = 1; + } + @override Widget build(BuildContext context) { return SizedBox( @@ -123,7 +158,7 @@ class _SearchEmployeeBottomSheetState extends State { 11.height, Row( children: [ - radioOption("Name", 0, _selectedSearchIndex), + radioOption(widget.fromChat ? "UserId" : "Name", 0, _selectedSearchIndex), radioOption("User Name", 1, _selectedSearchIndex), radioOption("Email", 2, _selectedSearchIndex), ], @@ -188,73 +223,135 @@ class _SearchEmployeeBottomSheetState extends State { ], ).expanded, if (widget.fromChat) - if (chatUsersList != null && widget.fromChat) - chatUsersList!.isEmpty + if (provider.chatUsersList != null && widget.fromChat) + provider.chatUsersList!.isEmpty ? Column( children: [ 20.height, Utils.getNoDataWidget(context), ], ) - : ListView( - physics: const BouncingScrollPhysics(), - padding: const EdgeInsets.only( - top: 15, - ), - children: [ - ListView.separated( - physics: const NeverScrollableScrollPhysics(), - shrinkWrap: true, - itemBuilder: (BuildContext cxt, int index) { - return SizedBox( - height: 55, - child: ListTile( - leading: Stack( - children: [ - SvgPicture.asset( + : ListView.separated( + itemCount: provider.chatUsersList!.length, + shrinkWrap: true, + physics: const ClampingScrollPhysics(), + controller: sc, + padding: const EdgeInsets.only(bottom: 80.0, top: 20), + itemBuilder: (BuildContext context, int index) { + return SizedBox( + height: 55, + child: Row( + children: [ + 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), + Positioned( + right: 5, + bottom: 1, + child: Container( + width: 10, + height: 10, + decoration: BoxDecoration( + color: provider.chatUsersList![index].userStatus == 1 ? MyColors.green2DColor : Colors.red, + ), + ).circle(10), + ) + ], + ), + Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + (provider.chatUsersList![index].userName!.replaceFirst(".", " ").capitalizeFirstofEach ?? "") + .toText14(color: MyColors.darkTextColor) + .paddingOnly(left: 11, top: 13), + provider.chatUsersList![index].isTyping! + ? 'Typing...' + .toText10( + color: MyColors.textMixColor, + ) + .paddingOnly(left: 11.0) + : const SizedBox() + ], + ).expanded, + SizedBox( + width: 60, + child: Row( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.end, + mainAxisSize: MainAxisSize.max, + children: [ + if (provider.chatUsersList![index].unreadMessageCount! > 0) + Container( + alignment: Alignment.center, + width: 18, + height: 18, + decoration: const BoxDecoration( + color: MyColors.redColor, + borderRadius: BorderRadius.all( + Radius.circular(20), ), ), - ), - ) + child: (provider.chatUsersList![index].unreadMessageCount!.toString()) + .toText10( + color: MyColors.white, + ) + .center, + ).paddingOnly(right: 10).center, + Icon( + provider.chatUsersList![index].isFav != null && provider.chatUsersList![index].isFav == false ? Icons.star_sharp : Icons.star_sharp, + color: provider.chatUsersList![index].isFav != null && provider.chatUsersList![index].isFav == true ? MyColors.yellowColor : MyColors.grey35Color, + ).onPress( + () { + if (provider.chatUsersList![index].isFav == null || provider.chatUsersList![index].isFav == false) { + provider + .favoriteUser( + userID: AppState().chatDetails!.response!.id!, + targetUserID: provider.chatUsersList![index].id!, + ) + .then((value) { + setState(() {}); + }); + } else if (provider.chatUsersList![index].isFav == true) { + provider + .unFavoriteUser( + userID: AppState().chatDetails!.response!.id!, + targetUserID: provider.chatUsersList![index].id!, + ) + .then((value) { + setState(() {}); + }); + } else { + provider + .favoriteUser( + userID: AppState().chatDetails!.response!.id!, + targetUserID: provider.chatUsersList![index].id!, + ) + .then((value) { + setState(() {}); + }); + } + }, + ).center ], ), - title: (chatUsersList![index].userName ?? "").toText14(color: MyColors.darkTextColor), - minVerticalPadding: 0, - onTap: () { - Navigator.pop(context); - Navigator.pushNamed( - context, - AppRoutes.chatDetailed, - arguments: ChatDetailedScreenParams(chatUsersList![index], true), - ); - }, ), - ); - }, - separatorBuilder: (BuildContext context, int index) => const Padding( - padding: EdgeInsets.only(right: 10, left: 70, bottom: 0, top: 0), - child: Divider( - color: Color(0xFFE5E5E5), - ), + ], ), - itemCount: chatUsersList?.length ?? 0, - ), - 12.height, - ], + ).onPress(() { + Navigator.pop(context); + Navigator.pushNamed( + context, + AppRoutes.chatDetailed, + arguments: ChatDetailedScreenParams(provider.chatUsersList![index], true), + ); + }); + }, + separatorBuilder: (BuildContext context, int index) => const Divider(color: MyColors.lightGreyE5Color).paddingOnly(left: 59), ).expanded, ], ).paddingOnly(left: 21, right: 21, bottom: 0, top: 21).expanded, @@ -263,6 +360,8 @@ class _SearchEmployeeBottomSheetState extends State { LocaleKeys.cancel.tr(), () { Navigator.pop(context); + provider.chatUsersList = []; + provider.pageNo = 1; }, textColor: MyColors.grey3AColor, colors: const [ diff --git a/lib/widgets/shimmer/dashboard_shimmer_widget.dart b/lib/widgets/shimmer/dashboard_shimmer_widget.dart index d023898..c11cf16 100644 --- a/lib/widgets/shimmer/dashboard_shimmer_widget.dart +++ b/lib/widgets/shimmer/dashboard_shimmer_widget.dart @@ -273,7 +273,7 @@ class ChatHomeShimmer extends StatelessWidget { children: [ Container( width: double.infinity, - height: 8.0, + height: 20.0, color: Colors.white, ), const Padding( @@ -281,7 +281,7 @@ class ChatHomeShimmer extends StatelessWidget { ), Container( width: double.infinity, - height: 8.0, + height: 15.0, color: Colors.white, ), const Padding( @@ -289,7 +289,7 @@ class ChatHomeShimmer extends StatelessWidget { ), Container( width: 40.0, - height: 8.0, + height: 10.0, color: Colors.white, ), ],