From 4021eccd9f2486b3acf665621a2fbd45c98adfbf Mon Sep 17 00:00:00 2001 From: "Aamir.Muhammad" Date: Tue, 20 Dec 2022 09:11:51 +0300 Subject: [PATCH] Chat Error Handling --- lib/generated_plugin_registrant.dart | 4 +-- lib/provider/chat_provider_model.dart | 51 +++++++++++++++++++++++---- lib/ui/chat/chat_bubble.dart | 10 +++--- lib/ui/chat/chat_detailed_screen.dart | 4 +-- 4 files changed, 55 insertions(+), 14 deletions(-) diff --git a/lib/generated_plugin_registrant.dart b/lib/generated_plugin_registrant.dart index 288ba42..4a90695 100644 --- a/lib/generated_plugin_registrant.dart +++ b/lib/generated_plugin_registrant.dart @@ -16,7 +16,7 @@ import 'package:image_picker_for_web/image_picker_for_web.dart'; import 'package:shared_preferences_web/shared_preferences_web.dart'; import 'package:url_launcher_web/url_launcher_web.dart'; import 'package:video_player_web/video_player_web.dart'; -import 'package:wakelock_web/wakelock_web.dart'; +//import 'package:wakelock_web/wakelock_web.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; @@ -32,6 +32,6 @@ void registerPlugins(Registrar registrar) { SharedPreferencesPlugin.registerWith(registrar); UrlLauncherPlugin.registerWith(registrar); VideoPlayerPlugin.registerWith(registrar); - WakelockWeb.registerWith(registrar); + // WakelockWeb.registerWith(registrar); registrar.registerMessageHandler(); } diff --git a/lib/provider/chat_provider_model.dart b/lib/provider/chat_provider_model.dart index 5f28b0b..91d3e7f 100644 --- a/lib/provider/chat_provider_model.dart +++ b/lib/provider/chat_provider_model.dart @@ -970,15 +970,15 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { //////// Audio Recoding Work //////////////////// - Future initAudio() async { + Future initAudio({required int receiverId}) async { appDirectory = await getApplicationDocumentsDirectory(); - path = "${appDirectory.path}/${AppState().chatDetails!.response!.id}-${DateTime.now().microsecondsSinceEpoch}.aac"; + path = "${appDirectory.path}/${AppState().chatDetails!.response!.id}-$receiverID-${DateTime.now().microsecondsSinceEpoch}.aac"; recorderController = RecorderController() ..androidEncoder = AndroidEncoder.aac ..androidOutputFormat = AndroidOutputFormat.mpeg4 ..iosEncoder = IosEncoder.kAudioFormatMPEG4AAC - ..sampleRate = 8000 - ..bitRate = 32000; + ..sampleRate = 6000 + ..bitRate = 18000; playerController = PlayerController(); } @@ -992,7 +992,6 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { void startRecoding() async { PermissionStatus status = await Permission.microphone.request(); - print(status); if (status.isDenied == true) { startRecoding(); } else { @@ -1014,6 +1013,15 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { }); } + void _pauseTimer() { + _timer?.cancel(); + _timer = Timer.periodic(const Duration(seconds: 1), (Timer t) { + _recodeDuration++; + // buildTimer(); + notifyListeners(); + }); + } + Future pauseRecoding() async { isPause = true; isPlaying = true; @@ -1025,6 +1033,7 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { await playerController.preparePlayer(file.path, 1.0); var tempDuration = _recodeDuration; _recodeDuration = tempDuration; + _pauseTimer(); _timer?.cancel(); notifyListeners(); } @@ -1077,11 +1086,41 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { void playRecoding() async { isPlaying = true; - await playerController.startPlayer(finishMode: FinishMode.stop); + await playerController.startPlayer(finishMode: FinishMode.pause); } void playOrPause() async { playerController.playerState == PlayerState.playing ? await playerController.pausePlayer() : playRecoding(); notifyListeners(); } + + void sendVoiceMessage(BuildContext context, {required int targetUserId, required int userStatus, required String userEmail, required String targetUserName}) async { + recorderController.pause(); + path = await recorderController.stop(false); + print(path); + File voiceFile = File(path!); + voiceFile.readAsBytesSync(); + _pauseTimer(); + _timer?.cancel(); + isPause = false; + isPlaying = false; + isRecoding = false; + Utils.showLoading(context); + dynamic value = await uploadAttachments(AppState().chatDetails!.response!.id.toString(), voiceFile); + logger.d(value); + String? ext = getFileExtension(voiceFile.path); + Utils.hideLoading(context); + sendChatToServer( + chatEventId: 2, + fileTypeId: getFileType(ext.toString()), + targetUserId: targetUserId, + targetUserName: targetUserName, + isAttachment: true, + chatReplyId: null, + isReply: false, + isImageLoaded: true, + image: voiceFile.readAsBytesSync()); + + notifyListeners(); + } } diff --git a/lib/ui/chat/chat_bubble.dart b/lib/ui/chat/chat_bubble.dart index 06afecf..ed20c24 100644 --- a/lib/ui/chat/chat_bubble.dart +++ b/lib/ui/chat/chat_bubble.dart @@ -328,15 +328,17 @@ class WaveBubble extends StatelessWidget { playerController: playerController, padding: EdgeInsets.zero, margin: EdgeInsets.zero, + enableSeekGesture: true, + density: 2, playerWaveStyle: const PlayerWaveStyle( fixedWaveColor: Colors.white, - liveWaveColor:MyColors.lightGreenColor, + liveWaveColor:MyColors.greenColor, showTop: true, showBottom: true, waveCap: StrokeCap.round, - seekLineThickness: 3, - visualizerHeight: 6, - backgroundColor: Colors.transparent + seekLineThickness: 2, + visualizerHeight: 5, + backgroundColor: Colors.transparent, ), ), ], diff --git a/lib/ui/chat/chat_detailed_screen.dart b/lib/ui/chat/chat_detailed_screen.dart index 5d3648e..605a922 100644 --- a/lib/ui/chat/chat_detailed_screen.dart +++ b/lib/ui/chat/chat_detailed_screen.dart @@ -80,7 +80,7 @@ class _ChatDetailScreenState extends State { loadMore: false, isNewChat: params!.isNewChat!, ); - data.initAudio(); + data.initAudio(receiverId: params!.chatUser!.id!); } return Scaffold( @@ -252,7 +252,7 @@ class _ChatDetailScreenState extends State { }), SvgPicture.asset("assets/icons/chat/chat_send_icon.svg", height: 26, width: 26) .onPress( - () => m.sendChatMessage(context, + () => m.sendVoiceMessage(context, targetUserId: params!.chatUser!.id!, userStatus: params!.chatUser!.userStatus ?? 0, userEmail: params!.chatUser!.email!,