From 0728cc3dd4fd12f5c10768a2ff8625f82b3ff7fe Mon Sep 17 00:00:00 2001 From: haroon amjad Date: Sun, 14 Jan 2024 11:03:27 +0300 Subject: [PATCH] Updated Mowadhafi Attachment Handling --- android/app/src/main/AndroidManifest.xml | 12 ++++++ android/app/src/main/res/xml/filepaths.xml | 5 +++ .../screens/mowadhafhi/request_details.dart | 40 +++++++++---------- 3 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 android/app/src/main/res/xml/filepaths.xml diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index ea79d76..15b0c54 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,4 +1,5 @@ @@ -34,6 +35,17 @@ android:extractNativeLibs="true" android:networkSecurityConfig="@xml/network_security_config" android:roundIcon="@mipmap/ic_launcher_round"> + + + + + + + \ No newline at end of file diff --git a/lib/ui/screens/mowadhafhi/request_details.dart b/lib/ui/screens/mowadhafhi/request_details.dart index 0dcaa13..b2371ab 100644 --- a/lib/ui/screens/mowadhafhi/request_details.dart +++ b/lib/ui/screens/mowadhafhi/request_details.dart @@ -17,6 +17,7 @@ import 'package:mohem_flutter_app/models/mowadhafhi/get_ticket_transactions.dart import 'package:mohem_flutter_app/models/mowadhafhi/get_transaction_attachment_model.dart'; import 'package:mohem_flutter_app/ui/screens/mowadhafhi/view_transaction_attachment.dart'; import 'package:mohem_flutter_app/widgets/app_bar_widget.dart'; +import 'package:open_file/open_file.dart'; import 'package:path_provider/path_provider.dart'; class MowadhafhiRequestDetails extends StatefulWidget { @@ -245,9 +246,7 @@ class _RequestDetailsState extends State { try { Utils.showLoading(context); getTransactionAttachmentModel = await MowadhafhiApiClient().getTransactionAttachments(attachmentID); - debugPrint(getTransactionAttachmentModel?.fileName); Utils.hideLoading(context); - setState(() {}); handleTransactionAttachment(); } catch (ex) { Utils.hideLoading(context); @@ -255,32 +254,29 @@ class _RequestDetailsState extends State { } } + Future _createFileFromString(String encodedStr, String ext) async { + Uint8List bytes = base64.decode(encodedStr); + String dir = (await getApplicationDocumentsDirectory()).path; + File file = File("$dir/" + DateTime.now().millisecondsSinceEpoch.toString() + "." + ext); + await file.writeAsBytes(bytes); + return file.path; + } + void handleTransactionAttachment() async { String ext = ''; String? rFile = getTransactionAttachmentModel!.base64String; String? rFileExt = getTransactionAttachmentModel!.fileName; - ext = "." + rFileExt!.split(".").last.toLowerCase(); + ext = rFileExt!.split(".").last.toLowerCase(); - if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".tiff") { - try { - Uint8List decodedBytes = base64Decode(rFile!.split("base64,").last); - Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); // 1 - imageFile = Io.File("${appDocumentsDirectory.path}/addImage$ext"); - imageFile.writeAsBytesSync(decodedBytes); - Navigator.push( - context, - MaterialPageRoute( - builder: (BuildContext context) => ViewTransactionAttachment( - imageFile: imageFile, - ), - ), - ); - } catch (e) { - logger.d(e); - } - } else { - Utils.showErrorDialog(context: context, onOkTapped: () {}, message: "Unable to view attachment"); + try { + String path = await _createFileFromString(rFile!.split("base64,").last ?? "", ext ?? ""); + debugPrint(path); + + print(" file here: ${File(path).existsSync()}"); + await OpenFile.open(path); + } catch (ex) { + Utils.showToast("Cannot open file."); } } }