From 7fe7aa5de538f42c4f8fdbe11aa396d94b965b9a Mon Sep 17 00:00:00 2001 From: Sachin Singh Date: Wed, 11 May 2022 17:46:48 +0530 Subject: [PATCH 1/2] file download percentage added for mobile and desktop --- lib/app.dart | 2 + lib/data_models/file_transfer.dart | 27 ++-- .../desktop_received_file_details.dart | 65 +++++--- .../labelled_circular_progress.dart | 32 ++++ .../widgets/received_file_list_tile.dart | 152 +++++++++++++----- .../welcome_screen/welcome_screen.dart | 17 -- lib/services/file_transfer_service.dart | 58 +++++-- lib/utils/text_strings.dart | 2 +- lib/view_models/file_progress_provider.dart | 29 ++++ lib/view_models/history_provider.dart | 47 ++++-- 10 files changed, 305 insertions(+), 126 deletions(-) create mode 100644 lib/screens/common_widgets/labelled_circular_progress.dart create mode 100644 lib/view_models/file_progress_provider.dart diff --git a/lib/app.dart b/lib/app.dart index adb28c90..e55b7904 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -1,5 +1,6 @@ import 'package:atsign_atmosphere_pro/view_models/file_download_checker.dart'; import 'package:atsign_atmosphere_pro/desktop_routes/desktop_routes.dart'; +import 'package:atsign_atmosphere_pro/view_models/file_progress_provider.dart'; import 'package:atsign_atmosphere_pro/view_models/internet_connectivity_checker.dart'; import 'package:atsign_atmosphere_pro/view_models/side_bar_provider.dart'; import 'package:atsign_atmosphere_pro/view_models/switch_atsign_provider.dart'; @@ -51,6 +52,7 @@ class _MyAppState extends State { ChangeNotifierProvider(create: (context) => NestedRouteProvider()), ChangeNotifierProvider(create: (context) => SwitchAtsignProvider()), ChangeNotifierProvider(create: (context) => FileDownloadChecker()), + ChangeNotifierProvider(create: (context) => FileProgressProvider()), ChangeNotifierProvider( create: (context) => InternetConnectivityChecker()) ], diff --git a/lib/data_models/file_transfer.dart b/lib/data_models/file_transfer.dart index c997c4ce..0e0c7079 100644 --- a/lib/data_models/file_transfer.dart +++ b/lib/data_models/file_transfer.dart @@ -11,18 +11,17 @@ class FileTransfer { bool? isUpdate; bool? isDownloading; bool? isWidgetOpen; - FileTransferProgress? fileTransferProgress; - FileTransfer( - {this.url, - this.files, - this.expiry, - this.platformFiles, - this.date, - this.key, - this.isUpdate = false, - this.isDownloading = false, - this.isWidgetOpen = false, - this.fileTransferProgress}) { + FileTransfer({ + this.url, + this.files, + this.expiry, + this.platformFiles, + this.date, + this.key, + this.isUpdate = false, + this.isDownloading = false, + this.isWidgetOpen = false, + }) { this.expiry = expiry ?? DateTime.now().add(Duration(days: 6)); this.date = date ?? DateTime.now(); @@ -216,8 +215,8 @@ class DownloadAcknowledgement { class FileTransferProgress { FileState fileState; - double percent; - String fileName; + double? percent; + String? fileName; FileTransferProgress(this.fileState, this.percent, this.fileName); } diff --git a/lib/desktop_screens/desktop_history/widgets/desktop_received_file_details.dart b/lib/desktop_screens/desktop_history/widgets/desktop_received_file_details.dart index 04458532..cb21de96 100644 --- a/lib/desktop_screens/desktop_history/widgets/desktop_received_file_details.dart +++ b/lib/desktop_screens/desktop_history/widgets/desktop_received_file_details.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'package:atsign_atmosphere_pro/data_models/file_transfer.dart'; +import 'package:atsign_atmosphere_pro/screens/common_widgets/labelled_circular_progress.dart'; import 'package:atsign_atmosphere_pro/services/backend_service.dart'; import 'package:atsign_atmosphere_pro/services/common_utility_functions.dart'; import 'package:atsign_atmosphere_pro/services/navigation_service.dart'; @@ -9,6 +10,7 @@ import 'package:atsign_atmosphere_pro/utils/colors.dart'; import 'package:atsign_atmosphere_pro/utils/constants.dart'; import 'package:atsign_atmosphere_pro/utils/text_strings.dart'; import 'package:atsign_atmosphere_pro/utils/text_styles.dart'; +import 'package:atsign_atmosphere_pro/view_models/file_progress_provider.dart'; import 'package:atsign_atmosphere_pro/view_models/history_provider.dart'; import 'package:flutter/material.dart'; import 'package:at_common_flutter/services/size_config.dart'; @@ -152,10 +154,14 @@ class _DesktopReceivedFileDetailsState widget.fileTransfer!.isDownloading! ? Padding( padding: const EdgeInsets.only(right: 10.0), - child: SizedBox( - width: 30, - height: 30, - child: CircularProgressIndicator()), + child: Consumer( + builder: (_c, provider, _) { + var fileTransferProgress = + provider.receivedFileProgress[ + widget.fileTransfer!.key!]; + + return getDownloadStatus(fileTransferProgress); + }), ) : isDownloadAvailable ? Padding( @@ -284,18 +290,25 @@ class _DesktopReceivedFileDetailsState ], ), SizedBox(height: 15.toHeight), - RichText( - text: TextSpan( - text: - '${DateFormat("MM-dd-yyyy").format(widget.fileTransfer!.date!)} | ${DateFormat('kk: mm').format(widget.fileTransfer!.date!)} | ', - style: CustomTextStyles.greyText15, - children: [ - widget.fileTransfer!.isDownloading! - ? TextSpan( - text: '${getFileStateMessage()}', - style: CustomTextStyles.blueRegular14) - : TextSpan(text: ''), - ]), + Consumer( + builder: (_context, provider, _widget) { + var fileTransferProgress = + provider.receivedFileProgress[widget.fileTransfer!.key!]; + return RichText( + text: TextSpan( + text: + '${DateFormat("MM-dd-yyyy").format(widget.fileTransfer!.date!)} | ${DateFormat('kk: mm').format(widget.fileTransfer!.date!)} | ', + style: CustomTextStyles.greyText15, + children: [ + widget.fileTransfer!.isDownloading! + ? TextSpan( + text: + '${getFileStateMessage(fileTransferProgress)}', + style: CustomTextStyles.blueRegular14) + : TextSpan(text: ''), + ]), + ); + }, ), SizedBox(height: 15.toHeight), SizedBox(height: 15.toHeight), @@ -470,9 +483,7 @@ class _DesktopReceivedFileDetailsState } } - String getFileStateMessage() { - FileTransferProgress? fileTransferProgress = - widget.fileTransfer!.fileTransferProgress; + String getFileStateMessage(FileTransferProgress? fileTransferProgress) { if (fileTransferProgress == null) { return ''; } @@ -492,4 +503,20 @@ class _DesktopReceivedFileDetailsState } return fileState; } + + Widget getDownloadStatus(FileTransferProgress? fileTransferProgress) { + Widget spinner = CircularProgressIndicator(); + + if (fileTransferProgress == null) { + return spinner; + } + + if (fileTransferProgress.fileState == FileState.download && + fileTransferProgress.percent != null) { + spinner = LabelledCircularProgressIndicator( + value: (fileTransferProgress.percent! / 100)); + } + + return spinner; + } } diff --git a/lib/screens/common_widgets/labelled_circular_progress.dart b/lib/screens/common_widgets/labelled_circular_progress.dart new file mode 100644 index 00000000..2ed80334 --- /dev/null +++ b/lib/screens/common_widgets/labelled_circular_progress.dart @@ -0,0 +1,32 @@ +import 'package:flutter/material.dart'; + +import '../../utils/colors.dart'; + +class LabelledCircularProgressIndicator extends StatelessWidget { + double? value; + LabelledCircularProgressIndicator({this.value}); + @override + Widget build(BuildContext context) { + return SizedBox( + child: Stack( + children: [ + CircularProgressIndicator(value: value), + value != null + ? Positioned( + top: 10, + child: Padding( + padding: EdgeInsets.only(left: 11), + child: Text((value! * 100).toStringAsFixed(0), + style: TextStyle( + fontSize: 10, + fontWeight: FontWeight.bold, + color: ColorConstants.blueText, + )), + ), + ) + : SizedBox() + ], + ), + ); + } +} diff --git a/lib/screens/history/widgets/received_file_list_tile.dart b/lib/screens/history/widgets/received_file_list_tile.dart index 56f8fb95..f5834095 100644 --- a/lib/screens/history/widgets/received_file_list_tile.dart +++ b/lib/screens/history/widgets/received_file_list_tile.dart @@ -9,6 +9,7 @@ import 'package:atsign_atmosphere_pro/screens/common_widgets/add_contact.dart'; import 'package:atsign_atmosphere_pro/screens/common_widgets/contact_initial.dart'; import 'package:atsign_atmosphere_pro/screens/common_widgets/custom_button.dart'; import 'package:atsign_atmosphere_pro/screens/common_widgets/custom_circle_avatar.dart'; +import 'package:atsign_atmosphere_pro/screens/common_widgets/labelled_circular_progress.dart'; import 'package:atsign_atmosphere_pro/services/backend_service.dart'; import 'package:atsign_atmosphere_pro/services/common_utility_functions.dart'; import 'package:atsign_atmosphere_pro/services/navigation_service.dart'; @@ -19,6 +20,7 @@ import 'package:atsign_atmosphere_pro/utils/file_types.dart'; import 'package:atsign_atmosphere_pro/utils/images.dart'; import 'package:atsign_atmosphere_pro/utils/text_strings.dart'; import 'package:atsign_atmosphere_pro/utils/text_styles.dart'; +import 'package:atsign_atmosphere_pro/view_models/file_progress_provider.dart'; import 'package:atsign_atmosphere_pro/view_models/history_provider.dart'; import 'package:flutter/material.dart'; import 'package:at_common_flutter/services/size_config.dart'; @@ -54,6 +56,7 @@ class _ReceivedFilesListTileState extends State { int fileSize = 0; List existingFileNamesToOverwrite = []; String nickName = ''; + Map _futureBuilder = {}; Future videoThumbnailBuilder(String path) async { videoThumbnail = await VideoThumbnail.thumbnailData( @@ -77,6 +80,7 @@ class _ReceivedFilesListTileState extends State { getAtSignDetail(); getDisplayDetails(); isFilesAlreadyDownloaded(); + getFutureBuilders(); super.initState(); } @@ -153,6 +157,12 @@ class _ReceivedFilesListTileState extends State { }); } + getFutureBuilders() { + widget.receivedHistory!.files!.forEach((element) { + _futureBuilder[element.name] = isFilePresent(element.name!); + }); + } + @override Widget build(BuildContext context) { sendTime = DateTime.now(); @@ -266,22 +276,29 @@ class _ReceivedFilesListTileState extends State { }, child: Padding( padding: EdgeInsets.only(top: 5.0), - child: isDownloadAvailable - ? widget.receivedHistory!.isDownloading! - ? CircularProgressIndicator() - : ((isDownloaded || - isFilesAvailableOfline) && - !isOverwrite) - ? Icon( - Icons.done, - color: Color(0xFF08CB21), - size: 25.toFont, - ) - : Icon( - Icons.download_sharp, - size: 25.toFont, - ) - : SizedBox())) + child: Consumer( + builder: (_c, provider, _) { + var fileTransferProgress = + provider.receivedFileProgress[ + widget.receivedHistory!.key!]; + return isDownloadAvailable + ? widget.receivedHistory!.isDownloading! + ? getDownloadStatus( + fileTransferProgress) + : ((isDownloaded || + isFilesAvailableOfline) && + !isOverwrite) + ? Icon( + Icons.done, + color: Color(0xFF08CB21), + size: 25.toFont, + ) + : Icon( + Icons.download_sharp, + size: 25.toFont, + ) + : SizedBox(); + }))) ], ), Row( @@ -335,12 +352,21 @@ class _ReceivedFilesListTileState extends State { SizedBox(width: 10.toHeight), widget.receivedHistory!.isDownloading! ? Expanded( - child: Text(getFileStateMessage(), - style: TextStyle( - fontSize: 11.toFont, - color: ColorConstants.blueText, - fontWeight: FontWeight.normal, - )), + child: Consumer( + builder: (_context, provider, _widget) { + var fileTransferProgress = + provider.receivedFileProgress[ + widget.receivedHistory!.key!]; + return Text( + getFileStateMessage( + fileTransferProgress), + style: TextStyle( + fontSize: 11.toFont, + color: ColorConstants.blueText, + fontWeight: FontWeight.normal, + )); + }, + ), ) : SizedBox() ], @@ -477,13 +503,11 @@ class _ReceivedFilesListTileState extends State { height: 50.toHeight, width: 50.toHeight, child: FutureBuilder( - future: isFilePresent(widget - .receivedHistory! - .files![index] - .name ?? - ''), - builder: (context, - AsyncSnapshot snapshot) { + key: Key(widget + .receivedHistory!.files![index].name!), + future: _futureBuilder[widget + .receivedHistory!.files![index].name], + builder: (context, snapshot) { return snapshot.connectionState == ConnectionState.done && snapshot.data != null @@ -499,7 +523,8 @@ class _ReceivedFilesListTileState extends State { (widget.receivedHistory! .files![index].name ?? ''), - isFilePresent: snapshot.data) + isFilePresent: + snapshot.data as bool) : SizedBox(); }), ), @@ -556,14 +581,25 @@ class _ReceivedFilesListTileState extends State { .secondaryRegular12, ), SizedBox(width: 10.toHeight), - Text( - (widget.receivedHistory!.files![index] - .isDownloading ?? - false) - ? TextStrings().downloading - : '', - style: CustomTextStyles.redSmall12, - ), + Consumer(builder: + (_context, _provider, _widget) { + var fileTransferProgress = + _provider.receivedFileProgress[ + widget.receivedHistory!.key!]; + return Text( + (widget + .receivedHistory! + .files![index] + .isDownloading ?? + false) + ? getSingleFileDownloadMessage( + fileTransferProgress, + widget.receivedHistory! + .files![index].name!) + : '', + style: CustomTextStyles.redSmall12, + ); + }), (CommonUtilityFunctions() .isFileDownloadAvailable( widget.receivedHistory! @@ -809,6 +845,7 @@ class _ReceivedFilesListTileState extends State { if (result is bool && result) { if (mounted) { + getFutureBuilders(); setState(() { isDownloaded = true; isFilesAvailableOfline = true; @@ -960,9 +997,7 @@ class _ReceivedFilesListTileState extends State { return textSpansMessage; } - String getFileStateMessage() { - FileTransferProgress? fileTransferProgress = - widget.receivedHistory!.fileTransferProgress; + String getFileStateMessage(FileTransferProgress? fileTransferProgress) { if (fileTransferProgress == null) { return ''; } @@ -994,4 +1029,39 @@ class _ReceivedFilesListTileState extends State { receivedHistoryLogs[index].isWidgetOpen = isOpen; } } + + Widget getDownloadStatus(FileTransferProgress? fileTransferProgress) { + Widget spinner = CircularProgressIndicator(); + + if (fileTransferProgress == null) { + return spinner; + } + + if (fileTransferProgress.fileState == FileState.download && + fileTransferProgress.percent != null) { + spinner = LabelledCircularProgressIndicator( + value: (fileTransferProgress.percent! / 100)); + } + + return spinner; + } + + String getSingleFileDownloadMessage( + FileTransferProgress? fileTransferProgress, String fileName) { + String downloadMessage = TextStrings().downloading; + + if (fileTransferProgress == null) { + return downloadMessage; + } + + if (fileTransferProgress.fileState == FileState.download && + fileTransferProgress.percent != null && + fileTransferProgress.fileName == fileName) { + downloadMessage += + '(${fileTransferProgress.percent!.toStringAsFixed(0)}%)'; + } else if (fileTransferProgress.fileState == FileState.decrypt) { + downloadMessage = 'Decrypting...'; + } + return downloadMessage; + } } diff --git a/lib/screens/welcome_screen/welcome_screen.dart b/lib/screens/welcome_screen/welcome_screen.dart index 53303c9a..fd086d93 100644 --- a/lib/screens/welcome_screen/welcome_screen.dart +++ b/lib/screens/welcome_screen/welcome_screen.dart @@ -58,7 +58,6 @@ class _WelcomeScreenState extends State { WidgetsBinding.instance!.addPostFrameCallback((_) async { WelcomeScreenProvider().isExpanded = false; await initPackages(); - // await getSentAndReceivedHistory(); }); super.initState(); @@ -69,22 +68,6 @@ class _WelcomeScreenState extends State { super.dispose(); } - // getSentAndReceivedHistory() async { - // await Provider.of(NavService.navKey.currentState!.context, - // listen: false) - // .getFileDownloadedAcknowledgement(); - // await Provider.of(NavService.navKey.currentState!.context, - // listen: false) - // .getSentHistory(); - // await Provider.of(NavService.navKey.currentState!.context, - // listen: false) - // .getReceivedHistory(); - - // Provider.of(NavService.navKey.currentContext!, - // listen: false) - // .checkForUndownloadedFiles(); - // } - listenForFlushBarStatus() { FileTransferProvider().flushBarStatusStream.listen((flushbarStatus) async { OverlayService.instance.showOverlay(flushbarStatus); diff --git a/lib/services/file_transfer_service.dart b/lib/services/file_transfer_service.dart index 0a9ff035..5fb09214 100644 --- a/lib/services/file_transfer_service.dart +++ b/lib/services/file_transfer_service.dart @@ -10,7 +10,7 @@ import 'package:atsign_atmosphere_pro/data_models/file_transfer.dart'; import 'package:atsign_atmosphere_pro/services/backend_service.dart'; import 'package:atsign_atmosphere_pro/services/navigation_service.dart'; import 'package:atsign_atmosphere_pro/utils/constants.dart'; -import 'package:atsign_atmosphere_pro/view_models/history_provider.dart'; +import 'package:atsign_atmosphere_pro/view_models/file_progress_provider.dart'; import 'package:provider/provider.dart'; import 'package:uuid/uuid.dart'; import 'package:http/http.dart' as http; @@ -43,6 +43,10 @@ class FileTransferService { Future> _uploadFiles( String transferId, List files, String encryptionKey) async { + var fileUploadProvider = Provider.of( + NavService.navKey.currentContext!, + listen: false); + var _preference = BackendService.getInstance().atClientPreference; var fileStatuses = []; @@ -53,6 +57,8 @@ class FileTransferService { size: await file.length(), ); try { + fileUploadProvider.updateSentFileTransferProgress = + FileTransferProgress(FileState.encrypt, null, null); final encryptedFile = await encryptFile( file, encryptionKey, @@ -183,13 +189,24 @@ class FileTransferService { Future uploadToFileBinWithStreamedRequest( File file, String container, String fileName) async { try { + var fileUploadProvider = Provider.of( + NavService.navKey.currentContext!, + listen: false); var postUri = Uri.parse(MixedConstants.FILEBIN_URL + '$container/' + fileName); final streamedRequest = http.StreamedRequest('POST', postUri); - streamedRequest.contentLength = await file.length(); + var uploadedBytes = 0; + var fileLength = await file.length(); + + streamedRequest.contentLength = fileLength; file.openRead().listen((chunk) { streamedRequest.sink.add(chunk); + + uploadedBytes += chunk.length; + var percent = (uploadedBytes / fileLength) * 100; + fileUploadProvider.updateSentFileTransferProgress = + FileTransferProgress(FileState.upload, percent, fileName); }, onDone: () { streamedRequest.sink.close(); }); @@ -240,7 +257,7 @@ class FileTransferService { updateFileTransferState( encryptedFile.path.split(Platform.pathSeparator).last, transferId, - 0, + null, FileState.decrypt); var decryptedFile = await decryptFile( File(encryptedFile.path), @@ -301,12 +318,12 @@ class FileTransferService { updateFileTransferState( fileName, fileTransferObject.transferId, - 0, + null, FileState.download, ); - var downloadResponse = - await downloadIndividualFile(fileUrl, tempDirectory.path, fileName); + var downloadResponse = await downloadIndividualFile(fileUrl, + tempDirectory.path, fileName, fileTransferObject.transferId); if (downloadResponse.isError) { fileDownloadResponse = FileDownloadResponse( isError: true, @@ -326,8 +343,8 @@ class FileTransferService { return completer.future; } - Future downloadIndividualFile( - String fileUrl, String tempPath, String fileName) async { + Future downloadIndividualFile(String fileUrl, String tempPath, + String fileName, String transferId) async { final Completer completer = Completer(); var httpClient = http.Client(); @@ -343,7 +360,7 @@ class FileTransferService { late StreamSubscription downloadSubscription; File file = File(tempPath + Platform.pathSeparator + fileName); - int downloaded = 0; + double downloaded = 0; try { downloadSubscription = @@ -352,9 +369,16 @@ class FileTransferService { (List chunk) { file.writeAsBytesSync(chunk, mode: FileMode.append); downloaded += chunk.length; - // if (r.contentLength != null) { - // print('percentage: ${(downloaded / r.contentLength!)}'); - // } + if (r.contentLength != null) { + var percent = (downloaded / r.contentLength!) * 100; + + updateFileTransferState( + fileName, + transferId, + percent.roundToDouble(), + FileState.download, + ); + } }, onDone: () async { await downloadSubscription.cancel(); @@ -378,17 +402,17 @@ class FileTransferService { } /// [updateFileTransferState] sets download/decrypt file state in history screen. - updateFileTransferState( - String fileName, String transferId, double percent, FileState fileState) { + updateFileTransferState(String fileName, String transferId, double? percent, + FileState fileState) { var fileTransferProgress = FileTransferProgress( fileState, - 0, // currently not showing download/decrypt % + percent, // currently not showing download/decrypt % fileName, ); - Provider.of(NavService.navKey.currentContext!, + Provider.of(NavService.navKey.currentContext!, listen: false) - .updateFileTransferState( + .updateReceivedFileProgress( transferId, fileTransferProgress, ); diff --git a/lib/utils/text_strings.dart b/lib/utils/text_strings.dart index 57f32d16..5d484d10 100644 --- a/lib/utils/text_strings.dart +++ b/lib/utils/text_strings.dart @@ -288,7 +288,7 @@ class TextStrings { String byDate = 'By date'; String apply = 'Apply'; - String downloading = 'Downloading...'; + String downloading = 'Downloading'; // Error Message diff --git a/lib/view_models/file_progress_provider.dart b/lib/view_models/file_progress_provider.dart new file mode 100644 index 00000000..56899a55 --- /dev/null +++ b/lib/view_models/file_progress_provider.dart @@ -0,0 +1,29 @@ +import 'package:atsign_atmosphere_pro/data_models/file_transfer.dart'; +import 'package:atsign_atmosphere_pro/view_models/base_model.dart'; + +class FileProgressProvider extends BaseModel { + FileTransferProgress? _sentFileTransferProgress; + Map _receivedFileProgress = {}; + + FileTransferProgress? get sentFileTransferProgress => + _sentFileTransferProgress; + + Map get receivedFileProgress => + _receivedFileProgress; + + set updateSentFileTransferProgress( + FileTransferProgress fileTransferProgress) { + _sentFileTransferProgress = fileTransferProgress; + notifyListeners(); + } + + updateReceivedFileProgress( + String transferId, FileTransferProgress fileTransferProgress) { + _receivedFileProgress[transferId] = fileTransferProgress; + notifyListeners(); + } + + removeReceiveProgressItem(String transferId) { + _receivedFileProgress.remove(transferId); + } +} diff --git a/lib/view_models/history_provider.dart b/lib/view_models/history_provider.dart index 4be04593..1b0a63d4 100644 --- a/lib/view_models/history_provider.dart +++ b/lib/view_models/history_provider.dart @@ -29,9 +29,9 @@ import 'package:atsign_atmosphere_pro/utils/constants.dart'; import 'package:atsign_atmosphere_pro/utils/file_types.dart'; import 'package:atsign_atmosphere_pro/view_models/base_model.dart'; import 'package:atsign_atmosphere_pro/view_models/file_download_checker.dart'; +import 'package:atsign_atmosphere_pro/view_models/file_progress_provider.dart'; import 'package:flutter/cupertino.dart'; import 'package:at_client/src/stream/file_transfer_object.dart'; -import 'package:at_client/src/service/encryption_service.dart'; import 'package:flutter/services.dart'; import 'package:http/http.dart' as http; import 'package:at_client/src/service/notification_service.dart'; @@ -985,8 +985,10 @@ class HistoryProvider extends BaseModel { listen: false) .checkForUndownloadedFiles(); - updateFileTransferState( - transferId, null); //setting filetransfer progress as null + Provider.of(NavService.navKey.currentContext!, + listen: false) + .removeReceiveProgressItem( + transferId); //setting filetransfer progress as null if (files is List) { await sortFiles(receivedHistoryLogs); populateTabs(); @@ -1084,12 +1086,9 @@ class HistoryProvider extends BaseModel { var tempDirectory = await Directory(downloadPath).createTemp('encrypted-files'); - var fileDownloadReponse = - await FileTransferService.getInstance().downloadIndividualFile( - fileTransferObject.fileUrl, - tempDirectory.path, - fileName, - ); + var fileDownloadReponse = await FileTransferService.getInstance() + .downloadIndividualFile(fileTransferObject.fileUrl, tempDirectory.path, + fileName, transferId!); if (fileDownloadReponse.isError) { throw Exception('download fail'); @@ -1097,13 +1096,21 @@ class HistoryProvider extends BaseModel { var encryptedFileList = tempDirectory.listSync(); try { for (var encryptedFile in encryptedFileList) { - var decryptedFile = await EncryptionService().decryptFileInChunks( + FileTransferService.getInstance().updateFileTransferState( + fileName, + fileTransferObject.transferId, + null, + FileState.decrypt, + ); + + var decryptedFile = await FileTransferService.getInstance().decryptFile( File(encryptedFile.path), fileTransferObject.fileEncryptionKey, BackendService.getInstance() .atClientPreference .fileEncryptionChunkSize, ); + decryptedFile.copySync(downloadPath + Platform.pathSeparator + encryptedFile.path.split(Platform.pathSeparator).last); @@ -1112,6 +1119,10 @@ class HistoryProvider extends BaseModel { encryptedFile.path.split(Platform.pathSeparator).last)); decryptedFile.deleteSync(); } + + Provider.of(NavService.navKey.currentContext!, + listen: false) + .removeReceiveProgressItem(transferId); // deleting temp directory Directory(fileDownloadReponse.filePath!).deleteSync(recursive: true); return downloadedFiles; @@ -1277,13 +1288,15 @@ class HistoryProvider extends BaseModel { } updateFileTransferState( - String transferId, FileTransferProgress? fileTransferProgress) { - var index = - receivedHistoryLogs.indexWhere((element) => element.key == transferId); - if (index != -1) { - receivedHistoryLogs[index].fileTransferProgress = fileTransferProgress; - notifyListeners(); - } + String transferId, FileTransferProgress fileTransferProgress) { + Provider.of(NavService.navKey.currentContext!, + listen: false) + .updateReceivedFileProgress( + transferId, + fileTransferProgress, + ); + + notifyListeners(); } refreshReceivedFile({bool setLoading = true}) async { From be4e6ec03cf148b2a7681c1deec3b6d146b06a78 Mon Sep 17 00:00:00 2001 From: Sachin Singh Date: Thu, 12 May 2022 14:15:17 +0530 Subject: [PATCH 2/2] % sign added and nul check issue resolved --- .../common_widgets/labelled_circular_progress.dart | 4 ++-- lib/screens/history/widgets/received_file_list_tile.dart | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/screens/common_widgets/labelled_circular_progress.dart b/lib/screens/common_widgets/labelled_circular_progress.dart index 2ed80334..ae76cc9e 100644 --- a/lib/screens/common_widgets/labelled_circular_progress.dart +++ b/lib/screens/common_widgets/labelled_circular_progress.dart @@ -15,8 +15,8 @@ class LabelledCircularProgressIndicator extends StatelessWidget { ? Positioned( top: 10, child: Padding( - padding: EdgeInsets.only(left: 11), - child: Text((value! * 100).toStringAsFixed(0), + padding: EdgeInsets.only(left: 8), + child: Text((value! * 100).toStringAsFixed(0) + '%', style: TextStyle( fontSize: 10, fontWeight: FontWeight.bold, diff --git a/lib/screens/history/widgets/received_file_list_tile.dart b/lib/screens/history/widgets/received_file_list_tile.dart index f5834095..ba7dadd6 100644 --- a/lib/screens/history/widgets/received_file_list_tile.dart +++ b/lib/screens/history/widgets/received_file_list_tile.dart @@ -58,7 +58,7 @@ class _ReceivedFilesListTileState extends State { String nickName = ''; Map _futureBuilder = {}; - Future videoThumbnailBuilder(String path) async { + Future videoThumbnailBuilder(String path) async { videoThumbnail = await VideoThumbnail.thumbnailData( video: path, imageFormat: ImageFormat.JPEG, @@ -672,7 +672,7 @@ class _ReceivedFilesListTileState extends State { Widget thumbnail( String? extension, String path, { - bool? isFilePresent = true, + bool isFilePresent = true, }) { // when file overwrite is true, we are not showing file preview. if (isOverwrite) { @@ -684,7 +684,7 @@ class _ReceivedFilesListTileState extends State { child: Container( height: 50.toHeight, width: 50.toWidth, - child: isFilePresent! + child: isFilePresent ? Image.file( File(path), fit: BoxFit.cover, @@ -719,7 +719,7 @@ class _ReceivedFilesListTileState extends State { fit: BoxFit.cover, ) : Image.memory( - videoThumbnail!, + snapshot.data! as Uint8List, fit: BoxFit.cover, errorBuilder: (BuildContext _context, _, __) { return Container(