Skip to content

Commit

Permalink
Add indicator when image is being downloaded (#1075)
Browse files Browse the repository at this point in the history
  • Loading branch information
micahmo authored Jan 22, 2024
1 parent 80f5eb1 commit 26d5ecf
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions lib/shared/image_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class _ImageViewerState extends State<ImageViewer> 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;
Expand Down Expand Up @@ -419,39 +420,53 @@ class _ImageViewerState extends State<ImageViewer> 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>[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>[Shadow(color: Colors.black, blurRadius: 50.0)],
),
: downloaded
? const Icon(
Icons.check_circle,
semanticLabel: 'Downloaded',
color: Colors.white,
shadows: <Shadow>[Shadow(color: Colors.black45, blurRadius: 50.0)],
)
: Icon(
Icons.download,
semanticLabel: "Download",
color: Colors.white.withOpacity(0.90),
shadows: const <Shadow>[Shadow(color: Colors.black, blurRadius: 50.0)],
),
),
),
if (widget.navigateToPost != null)
Expand Down

0 comments on commit 26d5ecf

Please sign in to comment.