From 088fd6f318bec4c63a74125ab8a8a7ce6a6287cc Mon Sep 17 00:00:00 2001 From: Sikander Saleem Date: Sun, 1 Jan 2023 17:03:31 +0300 Subject: [PATCH] Ios Fix --- lib/provider/chat_provider_model.dart | 24 ++++++++---- lib/ui/chat/chat_bubble.dart | 56 +++++++++++++++++---------- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/lib/provider/chat_provider_model.dart b/lib/provider/chat_provider_model.dart index 0851392..d4e90a4 100644 --- a/lib/provider/chat_provider_model.dart +++ b/lib/provider/chat_provider_model.dart @@ -1295,15 +1295,23 @@ class ChatProviderModel with ChangeNotifier, DiagnosticableTreeMixin { return numberStr; } - Future downChatVoice(Uint8List bytes, String ext, SingleUserChatModel data) async { - String dirPath = '${(await getApplicationDocumentsDirectory()).path}/chat_audios'; - if (!await Directory(dirPath).exists()) { - await Directory(dirPath).create(); - await File('$dirPath/.nomedia').create(); + Future downChatVoice(Uint8List bytes, String ext, SingleUserChatModel data) async { + File file; + try { + String dirPath = '${(await getApplicationDocumentsDirectory()).path}/chat_audios'; + if (!await Directory(dirPath).exists()) { + await Directory(dirPath).create(); + await File('$dirPath/.nomedia').create(); + } + file = File("$dirPath/${data.currentUserId}-${data.targetUserId}-${DateTime.now().microsecondsSinceEpoch}" + ext); + await file.writeAsBytes(bytes); + } catch (e) { + if (kDebugMode) { + print(e); + } + file = File(""); } - File file = File("$dirPath/${data.currentUserId}-${data.targetUserId}-${DateTime.now().microsecondsSinceEpoch}." + ext); - await file.writeAsBytes(bytes); - return file.path; + return file; } void scrollToMsg(SingleUserChatModel data) { diff --git a/lib/ui/chat/chat_bubble.dart b/lib/ui/chat/chat_bubble.dart index ee28c95..764ccea 100644 --- a/lib/ui/chat/chat_bubble.dart +++ b/lib/ui/chat/chat_bubble.dart @@ -56,47 +56,64 @@ class ChatBubble extends StatelessWidget { userName = AppState().chatDetails!.response!.userName == cItem.currentUserName.toString() ? "You" : cItem.currentUserName.toString(); } + Future getCurrentUrl(String url)async{ + if(Platform.isIOS){ + String a = url.substring(url.indexOf("Documents/") + 10, url.length) ; + Directory dir = await getApplicationDocumentsDirectory(); + a = "${dir.path}/$a"; + return a; + } + else{ + return url; + } + } + void playVoice( BuildContext context, { required SingleUserChatModel data, }) async { if (data.voice != null && data.voice!.existsSync()) { - await data.voiceController!.setLoopMode(LoopMode.off); if (Platform.isIOS) { Duration? duration = await data.voiceController!.setAudioSource(MyCustomStream(data.voice!.readAsBytesSync())); await data.voiceController!.seek(duration); + await data.voiceController!.setLoopMode(LoopMode.off); + await data.voiceController!.setVolume(1.0); + await data.voiceController!.load(); data.voiceController!.play(); } else { await data.voiceController!.setFilePath(data!.voice!.path); Duration? duration = await data.voiceController!.load(); await data.voiceController!.seek(duration); + await data.voiceController!.setLoopMode(LoopMode.off); await data.voiceController!.play(); } } else { Utils.showLoading(context); Uint8List encodedString = await ChatApiClient().downloadURL(fileName: data.contant!, fileTypeDescription: provider.getFileTypeDescription(data.fileTypeResponse!.fileTypeName ?? "")); - try { - String path = await provider.downChatVoice(encodedString, data.fileTypeResponse!.fileTypeName ?? "", data); - File file = File(path!); - await file.readAsBytes(); - data.voice = file; + // try { + File sFile = await provider.downChatVoice(encodedString, data.fileTypeResponse!.fileTypeName ?? "", data); + data.voice = sFile; if (Platform.isIOS) { - Duration? duration = await data.voiceController!.setAudioSource(MyCustomStream(encodedString)); + Duration? duration = await data.voiceController!.setAudioSource(MyCustomStream(data.voice!.readAsBytesSync())); await data.voiceController!.seek(duration); + await data.voiceController!.setLoopMode(LoopMode.off); + await data.voiceController!.setVolume(1.0); + await data.voiceController!.load(); + Utils.hideLoading(context); data.voiceController!.play(); } else { - Duration? duration = await data.voiceController!.setFilePath(file.path); + Duration? duration = await data.voiceController!.setFilePath(sFile.path); await data.voiceController!.setLoopMode(LoopMode.off); await data.voiceController!.seek(duration); - await data.voiceController!.setVolume(1.0); - await data.voiceController!.load(); + Utils.hideLoading(context); await data.voiceController!.play(); } - } catch (e) { - Utils.hideLoading(context); - Utils.showToast(e.toString()); - } + + // } catch (e) { + // Utils.hideLoading(context); + // Utils.showToast(e.toString()); + // } } } @@ -126,7 +143,7 @@ class ChatBubble extends StatelessWidget { Widget currentUser(BuildContext context) { return Column( crossAxisAlignment: CrossAxisAlignment.start, - children: [ + children: [ if (isReplied) ClipRRect( borderRadius: BorderRadius.circular(5.0), @@ -212,7 +229,7 @@ class ChatBubble extends StatelessWidget { ), ), ], - ).paddingOnly(top: 11, left: 13, right: 13, bottom: 5).objectContainerView(disablePadding: true, radius: 10).paddingOnly(left: MediaQuery.of(context).size.width * 0.3); + ).paddingOnly(top: 11, left: 13, right: 13, bottom: 5).objectContainerView(disablePadding: true).paddingOnly(left: MediaQuery.of(context).size.width * 0.3); } Widget receiptUser(BuildContext context) { @@ -352,7 +369,6 @@ class ChatBubble extends StatelessWidget { Widget currentWaveBubble(BuildContext context, SingleUserChatModel data) { return Container( margin: const EdgeInsets.all(0), - constraints: const BoxConstraints(), decoration: BoxDecoration( border: Border( left: BorderSide(width: 6, color: isCurrentUser ? MyColors.gradiantStartColor : MyColors.white), @@ -424,7 +440,7 @@ class ChatBubble extends StatelessWidget { child: const CircularProgressIndicator(), ); } else if (playing != true) { - return const Icon( + return Icon( Icons.play_arrow, size: 30, color: MyColors.lightGreenColor, @@ -432,7 +448,7 @@ class ChatBubble extends StatelessWidget { playVoice(context, data: modelData); }); } else if (processingState != ProcessingState.completed) { - return const Icon( + return Icon( Icons.pause, size: 30, color: MyColors.lightGreenColor, @@ -440,7 +456,7 @@ class ChatBubble extends StatelessWidget { pausePlaying(context, data: modelData); }); } else { - return const Icon( + return Icon( Icons.replay, size: 30, color: MyColors.lightGreenColor,