|
|
|
@ -5,19 +5,20 @@ import 'package:easy_localization/easy_localization.dart';
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
|
import 'package:logging/logging.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/classes/consts.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/classes/encryption.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/classes/utils.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/exceptions/api_exception.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_single_user_chat_list_model.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/models/chat/get_user_login_token_model.dart' as login;
|
|
|
|
|
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/ui/landing/dashboard_screen.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';
|
|
|
|
|
|
|
|
|
@ -38,18 +39,58 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
List<ChatUser> favUsersList = [];
|
|
|
|
|
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 {
|
|
|
|
|
chatHubConnection = await getHubConnection();
|
|
|
|
|
await chatHubConnection.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
hubConnection.on("OnUpdateUserStatusAsync", changeStatus);
|
|
|
|
|
hubConnection.on("OnDeliveredChatUserAsync", onMsgReceived);
|
|
|
|
|
// hubConnection.on("OnSeenChatUserAsync", onChatSeen);
|
|
|
|
|
chatHubConnection.on("OnUpdateUserStatusAsync", changeStatus);
|
|
|
|
|
chatHubConnection.on("OnDeliveredChatUserAsync", onMsgReceived);
|
|
|
|
|
// hubConnection.on("OnSeenChatUserAsync", onChatSeen);
|
|
|
|
|
//hubConnection.on("OnUserTypingAsync", onUserTyping);
|
|
|
|
|
hubConnection.on("OnUserCountAsync", userCountAsync);
|
|
|
|
|
hubConnection.on("OnUpdateUserChatHistoryWindowsAsync", updateChatHistoryWindow);
|
|
|
|
|
hubConnection.on("OnGetUserChatHistoryNotDeliveredAsync", chatNotDelivered);
|
|
|
|
|
hubConnection.on("OnUpdateUserChatHistoryStatusAsync", updateUserChatStatus);
|
|
|
|
|
chatHubConnection.on("OnUserCountAsync", userCountAsync);
|
|
|
|
|
// hubConnection.on("OnUpdateUserChatHistoryWindowsAsync", updateChatHistoryWindow);
|
|
|
|
|
chatHubConnection.on("OnGetUserChatHistoryNotDeliveredAsync", chatNotDelivered);
|
|
|
|
|
chatHubConnection.on("OnUpdateUserChatHistoryStatusAsync", updateUserChatStatus);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void getUserRecentChats() async {
|
|
|
|
|
if (chatHubConnection.state != HubConnectionState.Connected) {
|
|
|
|
|
getUserAutoLoginToken().whenComplete(() async {
|
|
|
|
|
await buildHubConnection();
|
|
|
|
|
getUserRecentChats();
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ChatUserModel recentChat = await ChatApiClient().getRecentChats();
|
|
|
|
|
ChatUserModel favUList = await ChatApiClient().getFavUsers();
|
|
|
|
|
|
|
|
|
@ -80,10 +121,11 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
notifyListeners();
|
|
|
|
|
getUserImages();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future invokeUserChatHistoryNotDeliveredAsync({required int userId}) async {
|
|
|
|
|
await hubConnection.invoke("GetUserChatHistoryNotDeliveredAsync", args: [userId]);
|
|
|
|
|
await chatHubConnection.invoke("GetUserChatHistoryNotDeliveredAsync", args: [userId]);
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -92,12 +134,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
if (isNewChat) userChatHistory = [];
|
|
|
|
|
if (!loadMore) paginationVal = 0;
|
|
|
|
|
isChatScreenActive = true;
|
|
|
|
|
Response response = await ChatApiClient().getSingleUserChatHistory(
|
|
|
|
|
senderUID: senderUID,
|
|
|
|
|
receiverUID: receiverUID,
|
|
|
|
|
loadMore: loadMore,
|
|
|
|
|
paginationVal: paginationVal,
|
|
|
|
|
);
|
|
|
|
|
Response response = await ChatApiClient().getSingleUserChatHistory(senderUID: senderUID, receiverUID: receiverUID, loadMore: loadMore, paginationVal: paginationVal);
|
|
|
|
|
if (response.statusCode == 204) {
|
|
|
|
|
if (isNewChat) {
|
|
|
|
|
userChatHistory = [];
|
|
|
|
@ -107,24 +144,15 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (loadMore) {
|
|
|
|
|
List<SingleUserChatModel> temp = getSingleUserChatModel(
|
|
|
|
|
response.body,
|
|
|
|
|
).reversed.toList();
|
|
|
|
|
userChatHistory.addAll(
|
|
|
|
|
temp,
|
|
|
|
|
);
|
|
|
|
|
List<SingleUserChatModel> temp = getSingleUserChatModel(response.body).reversed.toList();
|
|
|
|
|
userChatHistory.addAll(temp);
|
|
|
|
|
} else {
|
|
|
|
|
userChatHistory = getSingleUserChatModel(
|
|
|
|
|
response.body,
|
|
|
|
|
).reversed.toList();
|
|
|
|
|
userChatHistory = getSingleUserChatModel(response.body).reversed.toList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
isLoading = false;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
markRead(
|
|
|
|
|
userChatHistory,
|
|
|
|
|
receiverUID,
|
|
|
|
|
);
|
|
|
|
|
markRead(userChatHistory, receiverUID);
|
|
|
|
|
generateConvId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -138,75 +166,79 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
for (SingleUserChatModel element in data!) {
|
|
|
|
|
if (element.isSeen != null) {
|
|
|
|
|
if (!element.isSeen!) {
|
|
|
|
|
element.isSeen = true;
|
|
|
|
|
dynamic data = [
|
|
|
|
|
{
|
|
|
|
|
"userChatHistoryId": element.userChatHistoryId,
|
|
|
|
|
"TargetUserId": element.targetUserId,
|
|
|
|
|
"TargetUserId": element.currentUserId == receiverID ? element.currentUserId : element.targetUserId,
|
|
|
|
|
"isDelivered": true,
|
|
|
|
|
"isSeen": true,
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
updateUserChatHistoryStatusAsync(data);
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (ChatUser element in searchedChats!) {
|
|
|
|
|
if (element.id == receiverID) {
|
|
|
|
|
element.unreadMessageCount = 0;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
// notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateUserChatHistoryStatusAsync(List data) {
|
|
|
|
|
hubConnection.invoke(
|
|
|
|
|
"UpdateUserChatHistoryStatusAsync",
|
|
|
|
|
args: [data],
|
|
|
|
|
);
|
|
|
|
|
try {
|
|
|
|
|
chatHubConnection.invoke("UpdateUserChatHistoryStatusAsync", args: [data]);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<SingleUserChatModel> getSingleUserChatModel(String str) => List<SingleUserChatModel>.from(
|
|
|
|
|
json.decode(str).map(
|
|
|
|
|
(x) => SingleUserChatModel.fromJson(x),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
void updateUserChatHistoryOnMsg(List data) {
|
|
|
|
|
try {
|
|
|
|
|
chatHubConnection.invoke("UpdateUserChatHistoryStatusAsync", args: [data]);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<SingleUserChatModel> getSingleUserChatModel(String str) => List<SingleUserChatModel>.from(json.decode(str).map((x) => SingleUserChatModel.fromJson(x)));
|
|
|
|
|
|
|
|
|
|
Future<dynamic> uploadAttachments(String userId, File file) async {
|
|
|
|
|
dynamic result;
|
|
|
|
|
try {
|
|
|
|
|
StreamedResponse response = await ChatApiClient().uploadMedia(userId, file);
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
result = jsonDecode(
|
|
|
|
|
await response.stream.bytesToString(),
|
|
|
|
|
);
|
|
|
|
|
result = jsonDecode(await response.stream.bytesToString());
|
|
|
|
|
} else {
|
|
|
|
|
result = [];
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
print(e);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateUserChatStatus(List<Object?>? args) {
|
|
|
|
|
dynamic items = args!.toList();
|
|
|
|
|
for (dynamic cItem in items[0]) {
|
|
|
|
|
for (var cItem in items[0]) {
|
|
|
|
|
for (SingleUserChatModel chat in userChatHistory) {
|
|
|
|
|
if (chat.userChatHistoryId.toString() == cItem["userChatHistoryId"].toString()) {
|
|
|
|
|
if (cItem["contantNo"].toString() == chat.contantNo.toString()) {
|
|
|
|
|
chat.isSeen = cItem["isSeen"];
|
|
|
|
|
chat.isDelivered = cItem["isDelivered"];
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onChatSeen(List<Object?>? args) {
|
|
|
|
|
dynamic items = args!.toList();
|
|
|
|
|
logger.d("---------------------------------Chat Seen -------------------------------------");
|
|
|
|
|
logger.d(items);
|
|
|
|
|
// for (var user in searchedChats!) {
|
|
|
|
|
// if (user.id == items.first["id"]) {
|
|
|
|
|
// user.userStatus = items.first["userStatus"];
|
|
|
|
@ -242,7 +274,6 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
|
|
|
|
|
void chatNotDelivered(List<Object?>? args) {
|
|
|
|
|
dynamic items = args!.toList();
|
|
|
|
|
logger.d(items);
|
|
|
|
|
for (dynamic item in items[0]) {
|
|
|
|
|
searchedChats!.forEach(
|
|
|
|
|
(ChatUser element) {
|
|
|
|
@ -250,7 +281,6 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
int? val = element.unreadMessageCount ?? 0;
|
|
|
|
|
element.unreadMessageCount = val! + 1;
|
|
|
|
|
}
|
|
|
|
|
element.isLoadingCounter = false;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
@ -292,17 +322,35 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
data.first.targetUserName = temp.first.currentUserName;
|
|
|
|
|
data.first.currentUserId = temp.first.targetUserId;
|
|
|
|
|
data.first.currentUserName = temp.first.targetUserName;
|
|
|
|
|
if (data.first.fileTypeId == 12 || data.first.fileTypeId == 4 || data.first.fileTypeId == 3) {
|
|
|
|
|
data.first.image = await ChatApiClient().downloadURL(fileName: data.first.contant!, fileTypeDescription: data.first.fileTypeResponse!.fileTypeDescription ?? "image/jpg");
|
|
|
|
|
}
|
|
|
|
|
if (data.first.userChatReplyResponse != null) {
|
|
|
|
|
if (data.first.fileTypeResponse != null) {
|
|
|
|
|
if (data.first.userChatReplyResponse!.fileTypeId == 12 || data.first.userChatReplyResponse!.fileTypeId == 4 || data.first.userChatReplyResponse!.fileTypeId == 3) {
|
|
|
|
|
data.first.userChatReplyResponse!.image =
|
|
|
|
|
await ChatApiClient().downloadURL(fileName: data.first.userChatReplyResponse!.contant!, fileTypeDescription: data.first.fileTypeResponse!.fileTypeDescription ?? "image/jpg");
|
|
|
|
|
data.first.userChatReplyResponse!.isImageLoaded = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userChatHistory.insert(0, data.first);
|
|
|
|
|
var list = [
|
|
|
|
|
{
|
|
|
|
|
"userChatHistoryId": data.first.userChatHistoryId,
|
|
|
|
|
"TargetUserId": data.first.targetUserId,
|
|
|
|
|
"isDelivered": true,
|
|
|
|
|
"isSeen": isChatScreenActive ? true : false,
|
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
];
|
|
|
|
|
updateUserChatHistoryStatusAsync(list);
|
|
|
|
|
updateUserChatHistoryOnMsg(list);
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -389,34 +437,44 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> sendChatToServer(
|
|
|
|
|
{required int chatEventId, required fileTypeId, required int targetUserId, required String targetUserName, required chatReplyId, required bool isAttachment, required bool isReply}) async {
|
|
|
|
|
{required int chatEventId,
|
|
|
|
|
required fileTypeId,
|
|
|
|
|
required int targetUserId,
|
|
|
|
|
required String targetUserName,
|
|
|
|
|
required chatReplyId,
|
|
|
|
|
required bool isAttachment,
|
|
|
|
|
required bool isReply,
|
|
|
|
|
Uint8List? image,
|
|
|
|
|
required bool isImageLoaded}) async {
|
|
|
|
|
Uuid uuid = const Uuid();
|
|
|
|
|
var contentNo = uuid.v4();
|
|
|
|
|
|
|
|
|
|
var msg = message.text;
|
|
|
|
|
SingleUserChatModel data = SingleUserChatModel(
|
|
|
|
|
chatEventId: chatEventId,
|
|
|
|
|
chatSource: 1,
|
|
|
|
|
contant: msg,
|
|
|
|
|
contantNo: uuid.v4(),
|
|
|
|
|
conversationId: chatCID,
|
|
|
|
|
createdDate: DateTime.now(),
|
|
|
|
|
currentUserId: AppState().chatDetails!.response!.id,
|
|
|
|
|
currentUserName: AppState().chatDetails!.response!.userName,
|
|
|
|
|
targetUserId: targetUserId,
|
|
|
|
|
targetUserName: targetUserName,
|
|
|
|
|
isReplied: false,
|
|
|
|
|
fileTypeId: fileTypeId,
|
|
|
|
|
userChatReplyResponse: isReply ? UserChatReplyResponse.fromJson(repliedMsg.first.toJson()) : null,
|
|
|
|
|
fileTypeResponse: isAttachment
|
|
|
|
|
? FileTypeResponse(
|
|
|
|
|
fileTypeId: fileTypeId,
|
|
|
|
|
fileTypeName: getFileType(getFileExtension(selectedFile.path).toString()),
|
|
|
|
|
fileKind: getFileExtension(selectedFile.path),
|
|
|
|
|
fileName: selectedFile.path.split("/").last,
|
|
|
|
|
fileTypeDescription: getFileTypeDescription(getFileExtension(selectedFile.path).toString()),
|
|
|
|
|
)
|
|
|
|
|
: null,
|
|
|
|
|
);
|
|
|
|
|
chatEventId: chatEventId,
|
|
|
|
|
chatSource: 1,
|
|
|
|
|
contant: msg,
|
|
|
|
|
contantNo: contentNo,
|
|
|
|
|
conversationId: chatCID,
|
|
|
|
|
createdDate: DateTime.now(),
|
|
|
|
|
currentUserId: AppState().chatDetails!.response!.id,
|
|
|
|
|
currentUserName: AppState().chatDetails!.response!.userName,
|
|
|
|
|
targetUserId: targetUserId,
|
|
|
|
|
targetUserName: targetUserName,
|
|
|
|
|
isReplied: false,
|
|
|
|
|
fileTypeId: fileTypeId,
|
|
|
|
|
userChatReplyResponse: isReply ? UserChatReplyResponse.fromJson(repliedMsg.first.toJson()) : null,
|
|
|
|
|
fileTypeResponse: isAttachment
|
|
|
|
|
? FileTypeResponse(
|
|
|
|
|
fileTypeId: fileTypeId,
|
|
|
|
|
fileTypeName: getFileType(getFileExtension(selectedFile.path).toString()),
|
|
|
|
|
fileKind: getFileExtension(selectedFile.path),
|
|
|
|
|
fileName: selectedFile.path.split("/").last,
|
|
|
|
|
fileTypeDescription: getFileTypeDescription(getFileExtension(selectedFile.path).toString()),
|
|
|
|
|
)
|
|
|
|
|
: null,
|
|
|
|
|
image: image,
|
|
|
|
|
isImageLoaded: isImageLoaded);
|
|
|
|
|
userChatHistory.insert(0, data);
|
|
|
|
|
isFileSelected = false;
|
|
|
|
|
isMsgReply = false;
|
|
|
|
@ -425,56 +483,75 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
|
|
String chatData =
|
|
|
|
|
'{"contant":"$msg","contantNo":"${uuid.v4()}","chatEventId":$chatEventId,"fileTypeId": $fileTypeId,"currentUserId":${AppState().chatDetails!.response!.id},"chatSource":1,"userChatHistoryLineRequestList":[{"isSeen":false,"isDelivered":false,"targetUserId":$targetUserId,"targetUserStatus":1}],"chatReplyId":$chatReplyId,"conversationId":"$chatCID"}';
|
|
|
|
|
await hubConnection.invoke("AddChatUserAsync", args: <Object>[json.decode(chatData)]);
|
|
|
|
|
'{"contant":"$msg","contantNo":"$contentNo","chatEventId":$chatEventId,"fileTypeId": $fileTypeId,"currentUserId":${AppState().chatDetails!.response!.id},"chatSource":1,"userChatHistoryLineRequestList":[{"isSeen":false,"isDelivered":false,"targetUserId":$targetUserId,"targetUserStatus":1}],"chatReplyId":$chatReplyId,"conversationId":"$chatCID"}';
|
|
|
|
|
await chatHubConnection.invoke("AddChatUserAsync", args: <Object>[json.decode(chatData)]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sendChatMessage(int targetUserId, String targetUserName, BuildContext context) async {
|
|
|
|
|
dynamic contain = searchedChats!.where((ChatUser element) => element.id == targetUserId);
|
|
|
|
|
if (contain.isEmpty) {
|
|
|
|
|
searchedChats!.add(
|
|
|
|
|
ChatUser(
|
|
|
|
|
id: targetUserId,
|
|
|
|
|
userName: targetUserName,
|
|
|
|
|
unreadMessageCount: 0
|
|
|
|
|
),
|
|
|
|
|
ChatUser(id: targetUserId, userName: targetUserName, unreadMessageCount: 0),
|
|
|
|
|
);
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
if (!isFileSelected && !isMsgReply) {
|
|
|
|
|
print("Normal Text Msg");
|
|
|
|
|
if (message.text == null || message.text.isEmpty) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendChatToServer(chatEventId: 1, fileTypeId: null, targetUserId: targetUserId, targetUserName: targetUserName, isAttachment: false, chatReplyId: null, isReply: false);
|
|
|
|
|
}
|
|
|
|
|
sendChatToServer(
|
|
|
|
|
chatEventId: 1, fileTypeId: null, targetUserId: targetUserId, targetUserName: targetUserName, isAttachment: false, chatReplyId: null, isReply: false, isImageLoaded: false, image: null);
|
|
|
|
|
} // normal Text msg
|
|
|
|
|
if (isFileSelected && !isMsgReply) {
|
|
|
|
|
print("Normal Attachment Msg");
|
|
|
|
|
Utils.showLoading(context);
|
|
|
|
|
dynamic value = await uploadAttachments(AppState().chatDetails!.response!.id.toString(), selectedFile);
|
|
|
|
|
String? ext = getFileExtension(selectedFile.path);
|
|
|
|
|
Utils.hideLoading(context);
|
|
|
|
|
sendChatToServer(chatEventId: 2, fileTypeId: getFileType(ext.toString()), targetUserId: targetUserId, targetUserName: targetUserName, isAttachment: true, chatReplyId: null, isReply: false);
|
|
|
|
|
}
|
|
|
|
|
sendChatToServer(
|
|
|
|
|
chatEventId: 2,
|
|
|
|
|
fileTypeId: getFileType(ext.toString()),
|
|
|
|
|
targetUserId: targetUserId,
|
|
|
|
|
targetUserName: targetUserName,
|
|
|
|
|
isAttachment: true,
|
|
|
|
|
chatReplyId: null,
|
|
|
|
|
isReply: false,
|
|
|
|
|
isImageLoaded: true,
|
|
|
|
|
image: selectedFile.readAsBytesSync());
|
|
|
|
|
} // normal attachemnt msg
|
|
|
|
|
if (!isFileSelected && isMsgReply) {
|
|
|
|
|
print("Normal Text To Text Reply");
|
|
|
|
|
if (message.text == null || message.text.isEmpty) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sendChatToServer(
|
|
|
|
|
chatEventId: 1, fileTypeId: null, targetUserId: targetUserId, targetUserName: targetUserName, chatReplyId: repliedMsg.first.userChatHistoryId, isAttachment: false, isReply: true);
|
|
|
|
|
}
|
|
|
|
|
chatEventId: 1,
|
|
|
|
|
fileTypeId: null,
|
|
|
|
|
targetUserId: targetUserId,
|
|
|
|
|
targetUserName: targetUserName,
|
|
|
|
|
chatReplyId: repliedMsg.first.userChatHistoryId,
|
|
|
|
|
isAttachment: false,
|
|
|
|
|
isReply: true,
|
|
|
|
|
isImageLoaded: repliedMsg.first.isImageLoaded!,
|
|
|
|
|
image: repliedMsg.first.image);
|
|
|
|
|
} // reply msg over image && normal
|
|
|
|
|
if (isFileSelected && isMsgReply) {
|
|
|
|
|
print("Reply With File");
|
|
|
|
|
Utils.showLoading(context);
|
|
|
|
|
dynamic value = await uploadAttachments(AppState().chatDetails!.response!.id.toString(), selectedFile);
|
|
|
|
|
String? ext = getFileExtension(selectedFile.path);
|
|
|
|
|
Utils.hideLoading(context);
|
|
|
|
|
sendChatToServer(
|
|
|
|
|
chatEventId: 2,
|
|
|
|
|
fileTypeId: getFileType(ext.toString()),
|
|
|
|
|
targetUserId: targetUserId,
|
|
|
|
|
targetUserName: targetUserName,
|
|
|
|
|
isAttachment: true,
|
|
|
|
|
chatReplyId: repliedMsg.first.userChatHistoryId,
|
|
|
|
|
isReply: true,
|
|
|
|
|
);
|
|
|
|
|
chatEventId: 2,
|
|
|
|
|
fileTypeId: getFileType(ext.toString()),
|
|
|
|
|
targetUserId: targetUserId,
|
|
|
|
|
targetUserName: targetUserName,
|
|
|
|
|
isAttachment: true,
|
|
|
|
|
chatReplyId: repliedMsg.first.userChatHistoryId,
|
|
|
|
|
isReply: true,
|
|
|
|
|
isImageLoaded: true,
|
|
|
|
|
image: selectedFile.readAsBytesSync());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -604,7 +681,6 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clearSelections() {
|
|
|
|
|
print("Hereee i am ");
|
|
|
|
|
searchedChats = pChatHistory;
|
|
|
|
|
search.clear();
|
|
|
|
|
isChatScreenActive = false;
|
|
|
|
@ -628,50 +704,35 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
|
|
|
|
|
sFileType = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// void scrollListener() {
|
|
|
|
|
// _firstAutoscrollExecuted = true;
|
|
|
|
|
// if (scrollController.hasClients && scrollController.position.pixels == scrollController.position.maxScrollExtent) {
|
|
|
|
|
// _shouldAutoscroll = true;
|
|
|
|
|
// } else {
|
|
|
|
|
// _shouldAutoscroll = false;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// void scrollToBottom() {
|
|
|
|
|
// scrollController.animateTo(
|
|
|
|
|
// scrollController.position.maxScrollExtent + 100,
|
|
|
|
|
// duration: const Duration(milliseconds: 500),
|
|
|
|
|
// curve: Curves.easeIn,
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
void msgScroll() {
|
|
|
|
|
scrollController.animateTo(
|
|
|
|
|
scrollController.position.minScrollExtent - 100,
|
|
|
|
|
duration: const Duration(milliseconds: 500),
|
|
|
|
|
curve: Curves.easeIn,
|
|
|
|
|
);
|
|
|
|
|
void getUserImages() async {
|
|
|
|
|
List<String> emails = [];
|
|
|
|
|
for (ChatUser element in searchedChats!) {
|
|
|
|
|
emails.add(await EmailImageEncryption().encrypt(val: element.email!));
|
|
|
|
|
}
|
|
|
|
|
List<ChatUserImageModel> chatImages = await ChatApiClient().getUsersImages(encryptedEmails: emails);
|
|
|
|
|
for (ChatUser user in searchedChats!) {
|
|
|
|
|
for (ChatUserImageModel uImage in chatImages) {
|
|
|
|
|
if (user.email == uImage.email) {
|
|
|
|
|
user.image = uImage.profilePicture ?? "";
|
|
|
|
|
user.isImageLoading = false;
|
|
|
|
|
user.isImageLoaded = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (ChatUser favUser in favUsersList) {
|
|
|
|
|
for (ChatUserImageModel uImage in chatImages) {
|
|
|
|
|
if (favUser.email == uImage.email) {
|
|
|
|
|
favUser.image = uImage.profilePicture ?? "";
|
|
|
|
|
favUser.isImageLoading = false;
|
|
|
|
|
favUser.isImageLoaded = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|