From a01fa8075e736a8787783dbf71950123453a3f5e Mon Sep 17 00:00:00 2001 From: Micah Morrison Date: Mon, 22 Jan 2024 12:02:55 -0500 Subject: [PATCH] Add indicator for image downloads --- lib/shared/image_viewer.dart | 69 ++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/lib/shared/image_viewer.dart b/lib/shared/image_viewer.dart index 7b37c5af5..38aee9e3f 100644 --- a/lib/shared/image_viewer.dart +++ b/lib/shared/image_viewer.dart @@ -56,6 +56,7 @@ class _ImageViewerState extends State with TickerProviderStateMixin /// User Settings bool isUserLoggedIn = false; bool isDownloadingMedia = false; + bool isSavingMedia = false; late double imageWidth = 0; late double imageHeight = 0; late double maxZoomLevel = 3; @@ -419,39 +420,53 @@ class _ImageViewerState extends State with TickerProviderStateMixin return; } - // Save image on Linux platform - if (Platform.isLinux) { - final filePath = '${(await getApplicationDocumentsDirectory()).path}/Thunder/${basename(file.path)}'; + setState(() => isSavingMedia = true); - File(filePath) - ..createSync(recursive: true) - ..writeAsBytesSync(file.readAsBytesSync()); + try { + // Save image on Linux platform + if (Platform.isLinux) { + final filePath = '${(await getApplicationDocumentsDirectory()).path}/Thunder/${basename(file.path)}'; - return setState(() => downloaded = true); - } + File(filePath) + ..createSync(recursive: true) + ..writeAsBytesSync(file.readAsBytesSync()); - // Save image on all other supported platforms (Android, iOS, macOS, Windows) - try { - await Gal.putImage(file.path, album: "Thunder"); - setState(() => downloaded = true); - } on GalException catch (e) { - if (context.mounted) showSnackbar(context, e.type.message, customState: _imageViewer.currentState); - setState(() => downloaded = false); + return setState(() => downloaded = true); + } + + // Save image on all other supported platforms (Android, iOS, macOS, Windows) + try { + await Gal.putImage(file.path, album: "Thunder"); + setState(() => downloaded = true); + } on GalException catch (e) { + if (context.mounted) showSnackbar(context, e.type.message, customState: _imageViewer.currentState); + setState(() => downloaded = false); + } + } finally { + setState(() => isSavingMedia = false); } }, - icon: downloaded - ? const Icon( - Icons.check_circle, - semanticLabel: 'Downloaded', - color: Colors.white, - shadows: [Shadow(color: Colors.black45, blurRadius: 50.0)], + icon: isSavingMedia + ? SizedBox( + height: 20, + width: 20, + child: CircularProgressIndicator( + color: Colors.white.withOpacity(0.90), + ), ) - : Icon( - Icons.download, - semanticLabel: "Download", - color: Colors.white.withOpacity(0.90), - shadows: const [Shadow(color: Colors.black, blurRadius: 50.0)], - ), + : downloaded + ? const Icon( + Icons.check_circle, + semanticLabel: 'Downloaded', + color: Colors.white, + shadows: [Shadow(color: Colors.black45, blurRadius: 50.0)], + ) + : Icon( + Icons.download, + semanticLabel: "Download", + color: Colors.white.withOpacity(0.90), + shadows: const [Shadow(color: Colors.black, blurRadius: 50.0)], + ), ), ), if (widget.navigateToPost != null)