From 3309541142f9351bd56b6cfad8427fd533f1b776 Mon Sep 17 00:00:00 2001 From: "Aamir.Muhammad" Date: Tue, 29 Nov 2022 16:36:15 +0300 Subject: [PATCH] Chat Fixes & Chat Media Preview Api Implementation / On Msg Received Image --- lib/provider/chat_provider_model.dart | 47 ++++++++++++++---------- lib/ui/chat/chat_bubble.dart | 12 ++++-- lib/ui/chat/chat_full_image_preview.dart | 40 ++++++++++++++++++++ 3 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 lib/ui/chat/chat_full_image_preview.dart diff --git a/lib/provider/chat_provider_model.dart b/lib/provider/chat_provider_model.dart index 2d01e40..b0a3b31 100644 --- a/lib/provider/chat_provider_model.dart +++ b/lib/provider/chat_provider_model.dart @@ -292,16 +292,21 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { 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); + 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.userChatReplyResponse!.fileTypeId == 12 || data.first.userChatReplyResponse!.fileTypeId == 4 || data.first.userChatReplyResponse!.fileTypeId == 3) { - data.first.userChatReplyResponse!.image = await ChatApiClient().downloadURL(fileName: data.first.contant!, fileTypeDescription: data.first.fileTypeResponse!.fileTypeDescription); - data.first.userChatReplyResponse!.isImageLoaded = true; + 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, @@ -310,7 +315,9 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { "isSeen": isChatScreenActive ? true : false, } ]; + updateUserChatHistoryStatusAsync(list); + notifyListeners(); } @@ -666,22 +673,22 @@ 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 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( diff --git a/lib/ui/chat/chat_bubble.dart b/lib/ui/chat/chat_bubble.dart index e69e683..62156c8 100644 --- a/lib/ui/chat/chat_bubble.dart +++ b/lib/ui/chat/chat_bubble.dart @@ -10,6 +10,8 @@ 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'; import 'package:mohem_flutter_app/models/chat/get_single_user_chat_list_model.dart'; +import 'package:mohem_flutter_app/ui/chat/chat_full_image_preview.dart'; +import 'package:mohem_flutter_app/widgets/bottom_sheet.dart'; // todo: @aamir use extension methods, and use correct widgets. @@ -87,7 +89,9 @@ class ChatBubble extends StatelessWidget { ), ).paddingOnly(right: 5, bottom: 7), if (fileTypeID == 12 || fileTypeID == 4 || fileTypeID == 3) - showImage(isReplyPreview: false, fileName: cItem.contant!, fileTypeDescription: cItem.fileTypeResponse!.fileTypeDescription).paddingOnly(right: 5), + showImage(isReplyPreview: false, fileName: cItem.contant!, fileTypeDescription: cItem.fileTypeResponse!.fileTypeDescription).paddingOnly(right: 5).onPress(() { + showDialog(context: context, builder: (index) => ChatImagePreviewScreen(imgTitle: cItem.contant!, img: cItem.image!)); + }), cItem.contant!.toText12(), Align( alignment: Alignment.centerRight, @@ -160,7 +164,9 @@ class ChatBubble extends StatelessWidget { ), ).paddingOnly(right: 5, bottom: 7), if (fileTypeID == 12 || fileTypeID == 4 || fileTypeID == 3) - showImage(isReplyPreview: false, fileName: cItem.contant!, fileTypeDescription: cItem.fileTypeResponse!.fileTypeDescription).paddingOnly(right: 5) + showImage(isReplyPreview: false, fileName: cItem.contant!, fileTypeDescription: cItem.fileTypeResponse!.fileTypeDescription ?? "image/jpg").paddingOnly(right: 5).onPress(() { + showDialog(context: context, builder: (index) => ChatImagePreviewScreen(imgTitle: cItem.contant!, img: cItem.image!)); + }) else (cItem.contant! ?? "").toText12(color: Colors.white), Align( @@ -188,7 +194,7 @@ class ChatBubble extends StatelessWidget { builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.connectionState != ConnectionState.waiting) { if (snapshot.data == null) { - return (cItem.contant.toString()).toText12(); + return SizedBox(); } else { //data = image; cItem.image = snapshot.data; diff --git a/lib/ui/chat/chat_full_image_preview.dart b/lib/ui/chat/chat_full_image_preview.dart new file mode 100644 index 0000000..2a5d794 --- /dev/null +++ b/lib/ui/chat/chat_full_image_preview.dart @@ -0,0 +1,40 @@ +import 'dart:typed_data'; + +import 'package:flutter/material.dart'; +import 'package:mohem_flutter_app/classes/colors.dart'; +import 'package:mohem_flutter_app/extensions/widget_extensions.dart'; + +class ChatImagePreviewScreen extends StatelessWidget { + const ChatImagePreviewScreen({Key? key, required this.imgTitle, required this.img}) : super(key: key); + + final String imgTitle; + final Uint8List img; + + @override + Widget build(BuildContext context) { + return GestureDetector( + onTap: () { + Navigator.of(context).pop(); + }, + child: Dialog( + backgroundColor: Colors.transparent, + insetPadding: const EdgeInsets.all(10), + child: Stack( + alignment: Alignment.center, + children: [ + Image.memory( + img, + fit: BoxFit.cover, + + ).paddingAll(10), + const Positioned( + right: 0, + top: 0, + child: Icon(Icons.cancel, color: MyColors.redA3Color, size: 35), + ) + ], + ), + ), + ); + } +}