|
|
|
@ -7,13 +7,19 @@ import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:mohem_flutter_app/api/chat/chat_api_client.dart';
|
|
|
|
import 'package:mohem_flutter_app/api/chat/chat_api_client.dart';
|
|
|
|
import 'package:mohem_flutter_app/app_state/app_state.dart';
|
|
|
|
import 'package:mohem_flutter_app/app_state/app_state.dart';
|
|
|
|
|
|
|
|
import 'package:mohem_flutter_app/classes/consts.dart';
|
|
|
|
|
|
|
|
import 'package:mohem_flutter_app/classes/encryption.dart';
|
|
|
|
import 'package:mohem_flutter_app/classes/utils.dart';
|
|
|
|
import 'package:mohem_flutter_app/classes/utils.dart';
|
|
|
|
import 'package:mohem_flutter_app/main.dart';
|
|
|
|
import 'package:mohem_flutter_app/main.dart';
|
|
|
|
|
|
|
|
import 'package:mohem_flutter_app/models/chat/chat_user_image_model.dart';
|
|
|
|
import 'package:mohem_flutter_app/models/chat/get_search_user_chat_model.dart';
|
|
|
|
import 'package:mohem_flutter_app/models/chat/get_search_user_chat_model.dart';
|
|
|
|
import 'package:mohem_flutter_app/models/chat/get_single_user_chat_list_model.dart';
|
|
|
|
import 'package:mohem_flutter_app/models/chat/get_single_user_chat_list_model.dart';
|
|
|
|
|
|
|
|
import 'package:mohem_flutter_app/models/chat/get_user_login_token_model.dart' as userLoginToken;
|
|
|
|
import 'package:mohem_flutter_app/models/chat/make_user_favotire_unfavorite_chat_model.dart' as fav;
|
|
|
|
import 'package:mohem_flutter_app/models/chat/make_user_favotire_unfavorite_chat_model.dart' as fav;
|
|
|
|
import 'package:mohem_flutter_app/ui/landing/dashboard_screen.dart';
|
|
|
|
import 'package:mohem_flutter_app/ui/landing/dashboard_screen.dart';
|
|
|
|
import 'package:mohem_flutter_app/widgets/image_picker.dart';
|
|
|
|
import 'package:mohem_flutter_app/widgets/image_picker.dart';
|
|
|
|
|
|
|
|
import 'package:signalr_netcore/hub_connection.dart';
|
|
|
|
|
|
|
|
import 'package:signalr_netcore/signalr_client.dart';
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
|
|
|
|
|
|
|
class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
@ -33,18 +39,60 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
List<ChatUser> favUsersList = [];
|
|
|
|
List<ChatUser> favUsersList = [];
|
|
|
|
int paginationVal = 0;
|
|
|
|
int paginationVal = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> getUserAutoLoginToken() async {
|
|
|
|
|
|
|
|
userLoginToken.UserAutoLoginModel userLoginResponse = await ChatApiClient().getUserLoginToken();
|
|
|
|
|
|
|
|
if (userLoginResponse.response != null) {
|
|
|
|
|
|
|
|
AppState().setchatUserDetails = userLoginResponse;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Utils.showToast(
|
|
|
|
|
|
|
|
userLoginResponse.errorResponses!.first.fieldName.toString() + " Erorr",
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> buildHubConnection() async {
|
|
|
|
|
|
|
|
hubConnection = await getHubConnection();
|
|
|
|
|
|
|
|
await hubConnection.start()!.then((value) {
|
|
|
|
|
|
|
|
print('-----------------------Hub Started For Chat------------------');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<HubConnection> getHubConnection() async {
|
|
|
|
|
|
|
|
HubConnection hub;
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
HttpConnectionOptions httpOp = HttpConnectionOptions(skipNegotiation: false, logMessageContent: true);
|
|
|
|
|
|
|
|
hub = HubConnectionBuilder()
|
|
|
|
|
|
|
|
.withUrl(ApiConsts.chatHubConnectionUrl + "?UserId=${AppState().chatDetails!.response!.id}&source=Web&access_token=${AppState().chatDetails!.response!.token}", options: httpOp)
|
|
|
|
|
|
|
|
.withAutomaticReconnect(retryDelays: <int>[2000, 5000, 10000, 20000]).build();
|
|
|
|
|
|
|
|
// isChatHubLoding = false;
|
|
|
|
|
|
|
|
return hub;
|
|
|
|
|
|
|
|
// } catch (e) {
|
|
|
|
|
|
|
|
// getUserAutoLoginToken().whenComplete(() {
|
|
|
|
|
|
|
|
// getHubConnection();
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// throw e;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void registerEvents() {
|
|
|
|
void registerEvents() {
|
|
|
|
hubConnection.on("OnUpdateUserStatusAsync", changeStatus);
|
|
|
|
hubConnection.on("OnUpdateUserStatusAsync", changeStatus);
|
|
|
|
hubConnection.on("OnDeliveredChatUserAsync", onMsgReceived);
|
|
|
|
hubConnection.on("OnDeliveredChatUserAsync", onMsgReceived);
|
|
|
|
// hubConnection.on("OnSeenChatUserAsync", onChatSeen);
|
|
|
|
// hubConnection.on("OnSeenChatUserAsync", onChatSeen);
|
|
|
|
//hubConnection.on("OnUserTypingAsync", onUserTyping);
|
|
|
|
//hubConnection.on("OnUserTypingAsync", onUserTyping);
|
|
|
|
hubConnection.on("OnUserCountAsync", userCountAsync);
|
|
|
|
hubConnection.on("OnUserCountAsync", userCountAsync);
|
|
|
|
hubConnection.on("OnUpdateUserChatHistoryWindowsAsync", updateChatHistoryWindow);
|
|
|
|
// hubConnection.on("OnUpdateUserChatHistoryWindowsAsync", updateChatHistoryWindow);
|
|
|
|
hubConnection.on("OnGetUserChatHistoryNotDeliveredAsync", chatNotDelivered);
|
|
|
|
hubConnection.on("OnGetUserChatHistoryNotDeliveredAsync", chatNotDelivered);
|
|
|
|
hubConnection.on("OnUpdateUserChatHistoryStatusAsync", updateUserChatStatus);
|
|
|
|
hubConnection.on("OnUpdateUserChatHistoryStatusAsync", updateUserChatStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void getUserRecentChats() async {
|
|
|
|
void getUserRecentChats() async {
|
|
|
|
|
|
|
|
if (hubConnection.state != HubConnectionState.Connected) {
|
|
|
|
|
|
|
|
getUserAutoLoginToken().whenComplete(() async {
|
|
|
|
|
|
|
|
await buildHubConnection();
|
|
|
|
|
|
|
|
getUserRecentChats();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
ChatUserModel recentChat = await ChatApiClient().getRecentChats();
|
|
|
|
ChatUserModel recentChat = await ChatApiClient().getRecentChats();
|
|
|
|
ChatUserModel favUList = await ChatApiClient().getFavUsers();
|
|
|
|
ChatUserModel favUList = await ChatApiClient().getFavUsers();
|
|
|
|
|
|
|
|
|
|
|
|
@ -75,6 +123,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
notifyListeners();
|
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
getUserImages();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future invokeUserChatHistoryNotDeliveredAsync({required int userId}) async {
|
|
|
|
Future invokeUserChatHistoryNotDeliveredAsync({required int userId}) async {
|
|
|
|
@ -119,21 +168,27 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
for (SingleUserChatModel element in data!) {
|
|
|
|
for (SingleUserChatModel element in data!) {
|
|
|
|
if (element.isSeen != null) {
|
|
|
|
if (element.isSeen != null) {
|
|
|
|
if (!element.isSeen!) {
|
|
|
|
if (!element.isSeen!) {
|
|
|
|
print("Found Un Read");
|
|
|
|
element.isSeen = true;
|
|
|
|
logger.d(jsonEncode(element));
|
|
|
|
|
|
|
|
dynamic data = [
|
|
|
|
dynamic data = [
|
|
|
|
{"userChatHistoryId": element.userChatHistoryId, "TargetUserId": element.targetUserId, "isDelivered": true, "isSeen": true}
|
|
|
|
{
|
|
|
|
|
|
|
|
"userChatHistoryId": element.userChatHistoryId,
|
|
|
|
|
|
|
|
"TargetUserId": element.currentUserId == receiverID ? element.currentUserId : element.targetUserId,
|
|
|
|
|
|
|
|
"isDelivered": true,
|
|
|
|
|
|
|
|
"isSeen": true,
|
|
|
|
|
|
|
|
}
|
|
|
|
];
|
|
|
|
];
|
|
|
|
updateUserChatHistoryStatusAsync(data);
|
|
|
|
updateUserChatHistoryStatusAsync(data);
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (ChatUser element in searchedChats!) {
|
|
|
|
for (ChatUser element in searchedChats!) {
|
|
|
|
if (element.id == receiverID) {
|
|
|
|
if (element.id == receiverID) {
|
|
|
|
element.unreadMessageCount = 0;
|
|
|
|
element.unreadMessageCount = 0;
|
|
|
|
notifyListeners();
|
|
|
|
// notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -186,8 +241,6 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
|
|
|
|
|
|
|
void onChatSeen(List<Object?>? args) {
|
|
|
|
void onChatSeen(List<Object?>? args) {
|
|
|
|
dynamic items = args!.toList();
|
|
|
|
dynamic items = args!.toList();
|
|
|
|
logger.d("---------------------------------Chat Seen -------------------------------------");
|
|
|
|
|
|
|
|
logger.d(items);
|
|
|
|
|
|
|
|
// for (var user in searchedChats!) {
|
|
|
|
// for (var user in searchedChats!) {
|
|
|
|
// if (user.id == items.first["id"]) {
|
|
|
|
// if (user.id == items.first["id"]) {
|
|
|
|
// user.userStatus = items.first["userStatus"];
|
|
|
|
// user.userStatus = items.first["userStatus"];
|
|
|
|
@ -223,7 +276,6 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
|
|
|
|
|
|
|
void chatNotDelivered(List<Object?>? args) {
|
|
|
|
void chatNotDelivered(List<Object?>? args) {
|
|
|
|
dynamic items = args!.toList();
|
|
|
|
dynamic items = args!.toList();
|
|
|
|
logger.d(items);
|
|
|
|
|
|
|
|
for (dynamic item in items[0]) {
|
|
|
|
for (dynamic item in items[0]) {
|
|
|
|
searchedChats!.forEach(
|
|
|
|
searchedChats!.forEach(
|
|
|
|
(ChatUser element) {
|
|
|
|
(ChatUser element) {
|
|
|
|
@ -231,7 +283,6 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
int? val = element.unreadMessageCount ?? 0;
|
|
|
|
int? val = element.unreadMessageCount ?? 0;
|
|
|
|
element.unreadMessageCount = val! + 1;
|
|
|
|
element.unreadMessageCount = val! + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
element.isLoadingCounter = false;
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -289,7 +340,16 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
|
|
|
|
|
|
|
userChatHistory.insert(0, data.first);
|
|
|
|
userChatHistory.insert(0, data.first);
|
|
|
|
|
|
|
|
|
|
|
|
var list = [
|
|
|
|
if (searchedChats != null && !isChatScreenActive) {
|
|
|
|
|
|
|
|
for (ChatUser user in searchedChats!) {
|
|
|
|
|
|
|
|
if (user.id == data.first.currentUserId) {
|
|
|
|
|
|
|
|
var tempCount = user.unreadMessageCount ?? 0;
|
|
|
|
|
|
|
|
user.unreadMessageCount = tempCount + 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List list = [
|
|
|
|
{"userChatHistoryId": data.first.userChatHistoryId, "TargetUserId": temp.first.targetUserId, "isDelivered": true, "isSeen": isChatScreenActive ? true : false}
|
|
|
|
{"userChatHistoryId": data.first.userChatHistoryId, "TargetUserId": temp.first.targetUserId, "isDelivered": true, "isSeen": isChatScreenActive ? true : false}
|
|
|
|
];
|
|
|
|
];
|
|
|
|
updateUserChatHistoryOnMsg(list);
|
|
|
|
updateUserChatHistoryOnMsg(list);
|
|
|
|
@ -449,7 +509,6 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
print("Normal Attachment Msg");
|
|
|
|
print("Normal Attachment Msg");
|
|
|
|
Utils.showLoading(context);
|
|
|
|
Utils.showLoading(context);
|
|
|
|
dynamic value = await uploadAttachments(AppState().chatDetails!.response!.id.toString(), selectedFile);
|
|
|
|
dynamic value = await uploadAttachments(AppState().chatDetails!.response!.id.toString(), selectedFile);
|
|
|
|
logger.d(value);
|
|
|
|
|
|
|
|
String? ext = getFileExtension(selectedFile.path);
|
|
|
|
String? ext = getFileExtension(selectedFile.path);
|
|
|
|
Utils.hideLoading(context);
|
|
|
|
Utils.hideLoading(context);
|
|
|
|
sendChatToServer(
|
|
|
|
sendChatToServer(
|
|
|
|
@ -624,7 +683,6 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void clearSelections() {
|
|
|
|
void clearSelections() {
|
|
|
|
print("Hereee i am ");
|
|
|
|
|
|
|
|
searchedChats = pChatHistory;
|
|
|
|
searchedChats = pChatHistory;
|
|
|
|
search.clear();
|
|
|
|
search.clear();
|
|
|
|
isChatScreenActive = false;
|
|
|
|
isChatScreenActive = false;
|
|
|
|
@ -648,46 +706,35 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
sFileType = "";
|
|
|
|
sFileType = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// void scrollListener() {
|
|
|
|
void getUserImages() async {
|
|
|
|
// _firstAutoscrollExecuted = true;
|
|
|
|
List<String> emails = [];
|
|
|
|
// if (scrollController.hasClients && scrollController.position.pixels == scrollController.position.maxScrollExtent) {
|
|
|
|
for (ChatUser element in searchedChats!) {
|
|
|
|
// _shouldAutoscroll = true;
|
|
|
|
emails.add(await EmailImageEncryption().encrypt(val: element.email!));
|
|
|
|
// } else {
|
|
|
|
}
|
|
|
|
// _shouldAutoscroll = false;
|
|
|
|
List<ChatUserImageModel> chatImages = await ChatApiClient().getUsersImages(encryptedEmails: emails);
|
|
|
|
// }
|
|
|
|
for (ChatUser user in searchedChats!) {
|
|
|
|
// }
|
|
|
|
for (ChatUserImageModel uImage in chatImages) {
|
|
|
|
//
|
|
|
|
if (user.email == uImage.email) {
|
|
|
|
// void scrollToBottom() {
|
|
|
|
user.image = uImage.profilePicture ?? "";
|
|
|
|
// scrollController.animateTo(
|
|
|
|
user.isImageLoading = false;
|
|
|
|
// scrollController.position.maxScrollExtent + 100,
|
|
|
|
user.isImageLoaded = true;
|
|
|
|
// duration: const Duration(milliseconds: 500),
|
|
|
|
}
|
|
|
|
// curve: Curves.easeIn,
|
|
|
|
}
|
|
|
|
// );
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
for (ChatUser favUser in favUsersList) {
|
|
|
|
|
|
|
|
for (ChatUserImageModel uImage in chatImages) {
|
|
|
|
void msgScroll() {
|
|
|
|
if (favUser.email == uImage.email) {
|
|
|
|
// scrollController.animateTo(
|
|
|
|
favUser.image = uImage.profilePicture ?? "";
|
|
|
|
// // index: 150,
|
|
|
|
favUser.isImageLoading = false;
|
|
|
|
// duration: Duration(seconds: 2),
|
|
|
|
favUser.isImageLoaded = true;
|
|
|
|
// curve: Curves.easeInOutCubic);
|
|
|
|
}
|
|
|
|
// scrollController.animateTo(
|
|
|
|
}
|
|
|
|
// scrollController.position.minScrollExtent - 100,
|
|
|
|
}
|
|
|
|
// duration: const Duration(milliseconds: 500),
|
|
|
|
notifyListeners();
|
|
|
|
// curve: Curves.easeIn,
|
|
|
|
}
|
|
|
|
// );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Future<void> getDownLoadFile(String fileName) async {
|
|
|
|
|
|
|
|
// var data = await ChatApiClient().downloadURL(fileName: "data");
|
|
|
|
|
|
|
|
// Image.memory(data);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// void getUserChatHistoryNotDeliveredAsync({required int userId}) async {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// await hubConnection.invoke("GetUserChatHistoryNotDeliveredAsync", args: [userId]);
|
|
|
|
|
|
|
|
// } finally {
|
|
|
|
|
|
|
|
// hubConnection.off("GetUserChatHistoryNotDeliveredAsync", method: chatNotDelivered);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///getUserAutoLoginToken().whenComplete(() {
|
|
|
|
|
|
|
|
// buildHubConnection();
|
|
|
|
|
|
|
|
// print("After Reconnect State: " + hubConnection.state.toString());
|
|
|
|
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|