diff --git a/lib/ui/video_widget.dart b/lib/ui/video_widget.dart index 818815408..b4b8861a7 100644 --- a/lib/ui/video_widget.dart +++ b/lib/ui/video_widget.dart @@ -1,4 +1,5 @@ import 'package:chewie/chewie.dart'; +import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:fluttertoast/fluttertoast.dart'; @@ -11,8 +12,6 @@ import 'package:photos/utils/toast_util.dart'; import 'package:video_player/video_player.dart'; import 'package:visibility_detector/visibility_detector.dart'; -import 'loading_widget.dart'; - class VideoWidget extends StatefulWidget { final File file; final bool autoPlay; @@ -32,6 +31,7 @@ class _VideoWidgetState extends State { Logger _logger = Logger("VideoWidget"); VideoPlayerController _videoPlayerController; ChewieController _chewieController; + double _progress; @override void initState() { @@ -49,9 +49,19 @@ class _VideoWidgetState extends State { } }); } else { - showToast("Downloading and decrypting video...", - toastLength: Toast.LENGTH_SHORT); - getFileFromServer(widget.file).then((file) { + showToast("Downloading video...", toastLength: Toast.LENGTH_SHORT); + getFileFromServer( + widget.file, + progressCallback: (count, total) { + setState(() { + _progress = count / total; + if (_progress == 1) { + showToast("Decrypting video...", + toastLength: Toast.LENGTH_SHORT); + } + }); + }, + ).then((file) { _setVideoPlayerController(file.path); }); } @@ -109,7 +119,15 @@ class _VideoWidgetState extends State { color: Colors.black12, constraints: BoxConstraints.expand(), ), - loadWidget, + Center( + child: SizedBox.fromSize( + size: Size.square(30), + child: _progress == null || _progress == 1 + ? CupertinoActivityIndicator() + : CupertinoActivityIndicator.partiallyRevealed( + progress: _progress), + ), + ), ]); } diff --git a/lib/utils/file_util.dart b/lib/utils/file_util.dart index b73f15dca..4135e4a1b 100644 --- a/lib/utils/file_util.dart +++ b/lib/utils/file_util.dart @@ -82,7 +82,8 @@ Future getBytesFromDisk(File file, {int quality = 100}) async { } } -Future getFileFromServer(File file) async { +Future getFileFromServer(File file, + {ProgressCallback progressCallback}) async { final cacheManager = file.fileType == FileType.video ? VideoCacheManager() : DefaultCacheManager(); @@ -91,7 +92,11 @@ Future getFileFromServer(File file) async { } else { return cacheManager.getFileFromCache(file.getDownloadUrl()).then((info) { if (info == null) { - return _downloadAndDecrypt(file, cacheManager); + return _downloadAndDecrypt( + file, + cacheManager, + progressCallback: progressCallback, + ); } else { return info.file; } @@ -124,12 +129,18 @@ Future getThumbnailFromServer(File file) async { } } -Future _downloadAndDecrypt( - File file, BaseCacheManager cacheManager) async { +Future _downloadAndDecrypt(File file, BaseCacheManager cacheManager, + {ProgressCallback progressCallback}) async { final temporaryPath = Configuration.instance.getTempDirectory() + file.generatedID.toString() + ".aes"; - return Dio().download(file.getDownloadUrl(), temporaryPath).then((_) async { + return Dio() + .download( + file.getDownloadUrl(), + temporaryPath, + onReceiveProgress: progressCallback, + ) + .then((_) async { final data = await CryptoUtil.decryptFileToData( temporaryPath, Configuration.instance.getKey()); io.File(temporaryPath).deleteSync();