Chat Fixes & Chat Media Preview Api Implementation

sultan-dev
Aamir Muhammad 2 years ago
parent 967257ec2e
commit 0538a3c313

@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:http/http.dart';
import 'package:mohem_flutter_app/api/api_client.dart';
@ -46,6 +47,7 @@ class ChatApiClient {
);
Future<ChatUserModel> getRecentChats() async {
try {
Response response = await ApiClient().getJsonForResponse(
"${ApiConsts.chatRecentUrl}getchathistorybyuserid",
token: AppState().chatDetails!.response!.token,
@ -53,6 +55,22 @@ class ChatApiClient {
return ChatUserModel.fromJson(
json.decode(response.body),
);
} catch (e) {
e as APIException;
if (e.message == "api_common_unauthorized") {
logger.d("Token Generated On APIIIIII");
user.UserAutoLoginModel userLoginResponse = await ChatApiClient().getUserLoginToken();
if (userLoginResponse.response != null) {
AppState().setchatUserDetails = userLoginResponse;
getRecentChats();
} else {
Utils.showToast(
userLoginResponse.errorResponses!.first.fieldName.toString() + " Erorr",
);
}
}
throw e;
}
}
Future<ChatUserModel> getFavUsers() async {
@ -66,11 +84,27 @@ class ChatApiClient {
}
Future<Response> getSingleUserChatHistory({required int senderUID, required int receiverUID, required bool loadMore, bool isNewChat = false, required int paginationVal}) async {
try {
Response response = await ApiClient().getJsonForResponse(
"${ApiConsts.chatSingleUserHistoryUrl}GetUserChatHistory/$senderUID/$receiverUID/$paginationVal",
token: AppState().chatDetails!.response!.token,
);
return response;
} catch (e) {
e as APIException;
if (e.message == "api_common_unauthorized") {
user.UserAutoLoginModel userLoginResponse = await ChatApiClient().getUserLoginToken();
if (userLoginResponse.response != null) {
AppState().setchatUserDetails = userLoginResponse;
getSingleUserChatHistory(senderUID: senderUID, receiverUID: receiverUID, loadMore: loadMore, paginationVal: paginationVal);
} else {
Utils.showToast(
userLoginResponse.errorResponses!.first.fieldName.toString() + " Erorr",
);
}
}
throw e;
}
}
Future<fav.FavoriteChatUser> favUser({required int userID, required int targetUserID}) async {
@ -120,4 +154,29 @@ class ChatApiClient {
StreamedResponse response = await request.send();
return response;
}
// Download File For Chat
Future<Uint8List> downloadURL({required String fileName, required String fileTypeDescription}) async {
Response response = await ApiClient().postJsonForResponse(
"${ApiConsts.chatMediaImageUploadUrl}download",
{"fileType": fileTypeDescription, "fileName": fileName, "fileSource": 1},
token: AppState().chatDetails!.response!.token,
);
Uint8List data = Uint8List.fromList(response.bodyBytes);
return data;
}
Future getUsersImages({required List encryptedEmails}) async {
Response response = await ApiClient().postJsonForResponse(
"${ApiConsts.chatUserImages}images",
{
"encryptedEmails": ["/g8Rc+s6eEOdci41PwJuV5dX+gXe51G9OTHzb9ahcVlHCmVvNhxReirudF79+hdxVSkCnQ6wC5DBFV8xnJlC74X6157PxF7mNYrAYuHRgp4="],
"fromClient": true
},
token: AppState().chatDetails!.response!.token,
);
logger.d(response.body);
// Uint8List data = Uint8List.fromList(response.body);
}
}

@ -182,7 +182,7 @@ class DashboardApiClient {
Future<ChatUnreadCovnCountModel> getChatCount() async {
Response response = await ApiClient().getJsonForResponse(
"${ApiConsts.chatServerBaseApiUrl}user/unreadconversationcount/${AppState().getUserName}",
"${ApiConsts.chatLoginTokenUrl}unreadconversationcount/${AppState().getUserName}",
);
return chatUnreadCovnCountModelFromJson(response.body);
}

@ -23,6 +23,7 @@ class ApiConsts {
static String chatSingleUserHistoryUrl = chatServerBaseApiUrl + "UserChatHistory/";
static String chatMediaImageUploadUrl = chatServerBaseApiUrl + "shared/";
static String chatFavUser = chatServerBaseApiUrl + "FavUser/";
static String chatUserImages = chatServerBaseUrl + "empservice/api/employee/";
}
class SharedPrefsConsts {

@ -283,6 +283,8 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
}
Future<void> onMsgReceived(List<Object?>? parameters) async {
print("--------------------------------RMSG-----------------------------");
logger.d(parameters);
List<SingleUserChatModel> data = [];
List<SingleUserChatModel> temp = [];
for (dynamic msg in parameters!) {
@ -652,6 +654,11 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
);
}
// 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]);
@ -659,4 +666,13 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
// hubConnection.off("GetUserChatHistoryNotDeliveredAsync", method: chatNotDelivered);
// }
// }
}

@ -295,7 +295,6 @@ class DashboardProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
}
Future<void> getUserAutoLoginToken() async {
logger.d("Token Generated On Home");
UserAutoLoginModel userLoginResponse = await ChatApiClient().getUserLoginToken();
if (userLoginResponse.response != null) {
AppState().setchatUserDetails = userLoginResponse;
@ -315,7 +314,6 @@ class DashboardProviderModel with ChangeNotifier, DiagnosticableTreeMixin {
isChatHubLoding = false;
return hub;
}
void notify() {
notifyListeners();
}

@ -1,8 +1,13 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/api_client.dart';
import 'package:mohem_flutter_app/api/chat/chat_api_client.dart';
import 'package:mohem_flutter_app/classes/colors.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/main.dart';
// todo: @aamir use extension methods, and use correct widgets.
@ -16,7 +21,9 @@ class ChatBubble extends StatelessWidget {
required this.isDelivered,
required this.dateTime,
required this.isReplied,
required this.userName})
required this.userName,
this.fileTypeID,
this.fileTypeDescription})
: super(key: key);
final String text;
final String replyText;
@ -26,6 +33,8 @@ class ChatBubble extends StatelessWidget {
final String dateTime;
final bool isReplied;
final String userName;
final int? fileTypeID;
final String? fileTypeDescription;
@override
Widget build(BuildContext context) {
@ -177,7 +186,8 @@ class ChatBubble extends StatelessWidget {
).expanded,
),
).paddingOnly(right: 5, bottom: 7),
(text).toText12(),
if (fileTypeID == 12 || fileTypeID == 4 || fileTypeID == 3) showImage().paddingOnly(right: 5),
if (fileTypeID != 12 || fileTypeID != 4 || fileTypeID != 3) (text).toText12(),
Align(
alignment: Alignment.centerRight,
child: Row(
@ -237,7 +247,7 @@ class ChatBubble extends StatelessWidget {
).expanded,
),
).paddingOnly(right: 5, bottom: 7),
(text).toText12(color: Colors.white),
if (fileTypeID == 12 || fileTypeID == 4 || fileTypeID == 3) showImage().paddingOnly(right: 5) else (text).toText12(color: Colors.white),
Align(
alignment: Alignment.centerRight,
child: dateTime.toText10(
@ -248,4 +258,26 @@ class ChatBubble extends StatelessWidget {
),
).paddingOnly(right: MediaQuery.of(context).size.width * 0.3);
}
Widget showImage() {
return FutureBuilder<Uint8List>(
future: ChatApiClient().downloadURL(fileName: text, fileTypeDescription: fileTypeDescription!),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState != ConnectionState.waiting) {
if (snapshot.data == null) {
return (text).toText12(color: Colors.white);
} else {
return Image.memory(
snapshot.data,
height: 140,
width: 227,
fit: BoxFit.cover,
);
}
} else {
return const SizedBox(height: 140, width: 227, child: Center(child: CircularProgressIndicator()));
}
},
);
}
}

@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
@ -9,6 +10,7 @@ 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/main.dart';
import 'package:mohem_flutter_app/models/chat/call.dart';
import 'package:mohem_flutter_app/provider/chat_provider_model.dart';
import 'package:mohem_flutter_app/ui/chat/call/chat_outgoing_call_screen.dart';
@ -74,21 +76,14 @@ class _ChatDetailScreenState extends State<ChatDetailScreen> {
showHomeButton: false,
image: userDetails["targetUser"].image,
actions: [
IconButton(
constraints: const BoxConstraints(),
onPressed: () {
SvgPicture.asset("assets/icons/chat/call.svg", width: 21, height: 23).onPress(() {
// makeCall(callType: "AUDIO", con: hubConnection);
},
icon: SvgPicture.asset("assets/icons/chat/call.svg", width: 22, height: 22),
),
IconButton(
constraints: const BoxConstraints(),
onPressed: () {
}),
24.width,
SvgPicture.asset("assets/icons/chat/video_call.svg", width: 21, height: 18).onPress(() {
// makeCall(callType: "VIDEO", con: hubConnection);
},
icon: SvgPicture.asset("assets/icons/chat/video_call.svg", width: 20, height: 20),
),
10.width,
}),
21.width,
],
),
body: Consumer<ChatProviderModel>(
@ -128,13 +123,17 @@ class _ChatDetailScreenState extends State<ChatDetailScreen> {
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(),
fileTypeID: m.userChatHistory[i].fileTypeId,
fileTypeDescription: m.userChatHistory[i].fileTypeResponse!.fileTypeDescription,
),
onRightSwipe: () {
m.chatReply(
m.userChatHistory[i],
);
},
);
).onPress(() {
logger.d(jsonEncode(m.userChatHistory[i]));
});
},
),
).expanded,
@ -181,12 +180,12 @@ class _ChatDetailScreenState extends State<ChatDetailScreen> {
disabledBorder: InputBorder.none,
filled: true,
fillColor: MyColors.white,
contentPadding: EdgeInsets.only(
contentPadding: const EdgeInsets.only(
left: 21,
top: 20,
bottom: 20,
),
prefixIconConstraints: BoxConstraints(),
prefixIconConstraints: const BoxConstraints(),
prefixIcon: m.sFileType.isNotEmpty
? SvgPicture.asset(m.getType(m.sFileType), height: 30, width: 22, alignment: Alignment.center, fit: BoxFit.cover).paddingOnly(left: 21, right: 15)
: null,

@ -169,7 +169,17 @@ class _ChatHomeScreenState extends State<ChatHomeScreen> {
),
],
),
);
).onPress(() {
Navigator.pushNamed(
context,
AppRoutes.chatDetailed,
arguments: {"targetUser": m.searchedChats![index], "isNewChat": false},
).then((Object? value) {
// m.GetUserChatHistoryNotDeliveredAsync(userId: int.parse(AppState().chatDetails!.response!.id.toString()));
m.clearSelections();
m.notifyListeners();
});
});
},
separatorBuilder: (BuildContext context, int index) => const Divider(color: MyColors.lightGreyE5Color).paddingOnly(left: 59),
).paddingOnly(bottom: 70).expanded,

@ -87,7 +87,17 @@ class ChatFavoriteUsersScreen extends StatelessWidget {
),
],
),
).onPress(() {
Navigator.pushNamed(
context,
AppRoutes.chatDetailed,
arguments: {"targetUser": m.favUsersList![index], "isNewChat": false},
).then(
(Object? value) {
m.clearSelections();
},
);
});
},
separatorBuilder: (BuildContext context, int index) => const Divider(color: MyColors.lightGreyE5Color).paddingOnly(left: 70),
).paddingAll(21)

Loading…
Cancel
Save