@ -1,10 +1,12 @@
import ' dart:async ' ;
import ' dart:async ' ;
import ' dart:convert ' ;
import ' dart:convert ' ;
import ' dart:io ' ;
import ' dart:io ' ;
import ' dart:typed_data ' ;
import ' package:easy_localization/easy_localization.dart ' ;
import ' package:easy_localization/easy_localization.dart ' ;
import ' package:flutter/cupertino.dart ' ;
import ' package:flutter/cupertino.dart ' ;
import ' package:flutter/foundation.dart ' ;
import ' package:flutter/foundation.dart ' ;
import ' package:http/http.dart ' ;
import ' package:http/http.dart ' ;
import ' package:just_audio/just_audio.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/consts.dart ' ;
@ -53,6 +55,8 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
Future < void > buildHubConnection ( ) async {
Future < void > buildHubConnection ( ) async {
chatHubConnection = await getHubConnection ( ) ;
chatHubConnection = await getHubConnection ( ) ;
await chatHubConnection . start ( ) ;
await chatHubConnection . start ( ) ;
print ( " Startedddddddd " ) ;
chatHubConnection . on ( " OnDeliveredChatUserAsync " , onMsgReceived ) ;
}
}
Future < HubConnection > getHubConnection ( ) async {
Future < HubConnection > getHubConnection ( ) async {
@ -74,7 +78,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
void registerEvents ( ) {
void registerEvents ( ) {
chatHubConnection . on ( " OnUpdateUserStatusAsync " , changeStatus ) ;
chatHubConnection . on ( " OnUpdateUserStatusAsync " , changeStatus ) ;
chatHubConnection . on ( " OnDeliveredChatUserAsync " , onMsgReceived ) ;
/ / chatHubConnection . on ( " OnDeliveredChatUserAsync " , onMsgReceived ) ;
/ / hubConnection . on ( " OnSeenChatUserAsync " , onChatSeen ) ;
/ / hubConnection . on ( " OnSeenChatUserAsync " , onChatSeen ) ;
/ / hubConnection . on ( " OnUserTypingAsync " , onUserTyping ) ;
/ / hubConnection . on ( " OnUserTypingAsync " , onUserTyping ) ;
chatHubConnection . on ( " OnUserCountAsync " , userCountAsync ) ;
chatHubConnection . on ( " OnUserCountAsync " , userCountAsync ) ;
@ -121,7 +125,9 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
) ,
) ,
) ;
) ;
notifyListeners ( ) ;
notifyListeners ( ) ;
getUserImages ( ) ;
if ( searchedChats ! . isNotEmpty ) {
getUserImages ( ) ;
}
}
}
Future invokeUserChatHistoryNotDeliveredAsync ( { required int userId } ) async {
Future invokeUserChatHistoryNotDeliveredAsync ( { required int userId } ) async {
@ -134,6 +140,13 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
if ( isNewChat ) userChatHistory = [ ] ;
if ( isNewChat ) userChatHistory = [ ] ;
if ( ! loadMore ) paginationVal = 0 ;
if ( ! loadMore ) paginationVal = 0 ;
isChatScreenActive = true ;
isChatScreenActive = true ;
/ / if ( chatHubConnection . state ! = HubConnectionState . Connected ) {
/ / getUserAutoLoginToken ( ) . whenComplete ( ( ) async {
/ / await buildHubConnection ( ) ;
/ / getSingleUserChatHistory ( senderUID: senderUID , receiverUID: receiverUID , loadMore: loadMore ) ;
/ / } ) ;
/ / return ;
/ / }
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 ( response . statusCode = = 204 ) {
if ( isNewChat ) {
if ( isNewChat ) {
@ -313,8 +326,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
}
}
Future < void > onMsgReceived ( List < Object ? > ? parameters ) async {
Future < void > onMsgReceived ( List < Object ? > ? parameters ) async {
List < SingleUserChatModel > data = [ ] ;
List < SingleUserChatModel > data = [ ] , temp = [ ] ;
List < SingleUserChatModel > temp = [ ] ;
for ( dynamic msg in parameters ! ) {
for ( dynamic msg in parameters ! ) {
data = getSingleUserChatModel ( jsonEncode ( msg ) ) ;
data = getSingleUserChatModel ( jsonEncode ( msg ) ) ;
temp = getSingleUserChatModel ( jsonEncode ( msg ) ) ;
temp = getSingleUserChatModel ( jsonEncode ( msg ) ) ;
@ -335,16 +347,22 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
}
}
}
}
}
}
dynamic contain = searchedChats ! . where ( ( ChatUser element ) = > element . id = = data . first . currentUserId ) ;
if ( contain . isEmpty ) {
searchedChats ! . add ( ChatUser ( id: data . first . currentUserId , userName: data . first . currentUserName , unreadMessageCount: 0 , isImageLoading: false , image: " " , isImageLoaded: true , userStatus: 1 ) ) ;
}
setMsgTune ( ) ;
userChatHistory . insert ( 0 , data . first ) ;
userChatHistory . insert ( 0 , data . first ) ;
if ( searchedChats ! = null & & ! isChatScreenActive ) {
if ( searchedChats ! = null & & ! isChatScreenActive ) {
for ( ChatUser user in searchedChats ! ) {
for ( ChatUser user in searchedChats ! ) {
if ( user . id = = data . first . currentUserId ) {
if ( user . id = = data . first . currentUserId ) {
var tempCount = user . unreadMessageCount ? ? 0 ;
int tempCount = user . unreadMessageCount ? ? 0 ;
user . unreadMessageCount = tempCount + 1 ;
user . unreadMessageCount = tempCount + 1 ;
}
}
}
}
sort ( ) ;
}
}
List list = [
List list = [
@ -354,6 +372,14 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
notifyListeners ( ) ;
notifyListeners ( ) ;
}
}
void sort ( ) {
searchedChats ! . sort (
( ChatUser a , ChatUser b ) = > b . unreadMessageCount ! . compareTo (
a . unreadMessageCount ! ,
) ,
) ;
}
void onUserTyping ( List < Object ? > ? parameters ) {
void onUserTyping ( List < Object ? > ? parameters ) {
for ( ChatUser user in searchedChats ! ) {
for ( ChatUser user in searchedChats ! ) {
if ( user . id = = parameters ! [ 1 ] & & parameters [ 0 ] = = true ) {
if ( user . id = = parameters ! [ 1 ] & & parameters [ 0 ] = = true ) {
@ -447,34 +473,35 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
Uint8List ? image ,
Uint8List ? image ,
required bool isImageLoaded } ) async {
required bool isImageLoaded } ) async {
Uuid uuid = const Uuid ( ) ;
Uuid uuid = const Uuid ( ) ;
var contentNo = uuid . v4 ( ) ;
String contentNo = uuid . v4 ( ) ;
var msg = message . text ;
String msg = message . text ;
SingleUserChatModel data = SingleUserChatModel (
SingleUserChatModel data = SingleUserChatModel (
chatEventId: chatEventId ,
chatEventId: chatEventId ,
chatSource: 1 ,
chatSource: 1 ,
contant: msg ,
contant: msg ,
contantNo: contentNo ,
contantNo: contentNo ,
conversationId: chatCID ,
conversationId: chatCID ,
createdDate: DateTime . now ( ) ,
createdDate: DateTime . now ( ) ,
currentUserId: AppState ( ) . chatDetails ! . response ! . id ,
currentUserId: AppState ( ) . chatDetails ! . response ! . id ,
currentUserName: AppState ( ) . chatDetails ! . response ! . userName ,
currentUserName: AppState ( ) . chatDetails ! . response ! . userName ,
targetUserId: targetUserId ,
targetUserId: targetUserId ,
targetUserName: targetUserName ,
targetUserName: targetUserName ,
isReplied: false ,
isReplied: false ,
fileTypeId: fileTypeId ,
fileTypeId: fileTypeId ,
userChatReplyResponse: isReply ? UserChatReplyResponse . fromJson ( repliedMsg . first . toJson ( ) ) : null ,
userChatReplyResponse: isReply ? UserChatReplyResponse . fromJson ( repliedMsg . first . toJson ( ) ) : null ,
fileTypeResponse: isAttachment
fileTypeResponse: isAttachment
? FileTypeResponse (
? FileTypeResponse (
fileTypeId: fileTypeId ,
fileTypeId: fileTypeId ,
fileTypeName: getFileType ( getFileExtension ( selectedFile . path ) . toString ( ) ) ,
fileTypeName: getFileType ( getFileExtension ( selectedFile . path ) . toString ( ) ) ,
fileKind: getFileExtension ( selectedFile . path ) ,
fileKind: getFileExtension ( selectedFile . path ) ,
fileName: selectedFile . path . split ( " / " ) . last ,
fileName: selectedFile . path . split ( " / " ) . last ,
fileTypeDescription: getFileTypeDescription ( getFileExtension ( selectedFile . path ) . toString ( ) ) ,
fileTypeDescription: getFileTypeDescription ( getFileExtension ( selectedFile . path ) . toString ( ) ) ,
)
)
: null ,
: null ,
image: image ,
image: image ,
isImageLoaded: isImageLoaded ) ;
isImageLoaded: isImageLoaded ,
) ;
userChatHistory . insert ( 0 , data ) ;
userChatHistory . insert ( 0 , data ) ;
isFileSelected = false ;
isFileSelected = false ;
isMsgReply = false ;
isMsgReply = false ;
@ -491,7 +518,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
dynamic contain = searchedChats ! . where ( ( ChatUser element ) = > element . id = = targetUserId ) ;
dynamic contain = searchedChats ! . where ( ( ChatUser element ) = > element . id = = targetUserId ) ;
if ( contain . isEmpty ) {
if ( contain . isEmpty ) {
searchedChats ! . add (
searchedChats ! . add (
ChatUser ( id: targetUserId , userName: targetUserName , unreadMessageCount: 0 ),
ChatUser ( id: targetUserId , userName: targetUserName , unreadMessageCount: 0 , isImageLoading: false , image: " " , isImageLoaded: true ),
) ;
) ;
notifyListeners ( ) ;
notifyListeners ( ) ;
}
}
@ -728,11 +755,25 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
}
}
}
}
}
}
notifyListeners ( ) ;
notifyListeners ( ) ;
}
}
/ / / getUserAutoLoginToken ( ) . whenComplete ( ( ) {
void setMsgTune ( ) async {
/ / buildHubConnection ( ) ;
AudioPlayer player = AudioPlayer ( ) ;
/ / print ( " After Reconnect State: " + hubConnection . state . toString ( ) ) ;
await player . setVolume ( 1.0 ) ;
/ / } ) ;
String audioAsset = " " ;
if ( Platform . isAndroid ) {
audioAsset = " assets/audio/pulse_tone_android.mp3 " ;
} else {
audioAsset = " assets/audio/pulse_tune_ios.caf " ;
}
try {
await player . setAsset ( audioAsset ) ;
await player . load ( ) ;
player . play ( ) ;
} catch ( e ) {
print ( " Error: $ e " ) ;
}
}
}
}