Show progress dialog while downloading a video

This commit is contained in:
Vishnu Mohandas 2020-08-14 05:31:37 +05:30
parent a1ebee60b8
commit 1ac16347bd
2 changed files with 40 additions and 11 deletions

View file

@ -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<VideoWidget> {
Logger _logger = Logger("VideoWidget");
VideoPlayerController _videoPlayerController;
ChewieController _chewieController;
double _progress;
@override
void initState() {
@ -49,9 +49,19 @@ class _VideoWidgetState extends State<VideoWidget> {
}
});
} 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<VideoWidget> {
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),
),
),
]);
}

View file

@ -82,7 +82,8 @@ Future<Uint8List> getBytesFromDisk(File file, {int quality = 100}) async {
}
}
Future<io.File> getFileFromServer(File file) async {
Future<io.File> getFileFromServer(File file,
{ProgressCallback progressCallback}) async {
final cacheManager = file.fileType == FileType.video
? VideoCacheManager()
: DefaultCacheManager();
@ -91,7 +92,11 @@ Future<io.File> 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<io.File> getThumbnailFromServer(File file) async {
}
}
Future<io.File> _downloadAndDecrypt(
File file, BaseCacheManager cacheManager) async {
Future<io.File> _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();