Fix download toast & auto-play liveVideo post download

This commit is contained in:
Neeraj Gupta 2021-10-06 13:32:37 +05:30
parent e3e34c6f80
commit 56a4262a9e
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1
2 changed files with 42 additions and 28 deletions

View file

@ -14,14 +14,14 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:video_player/video_player.dart';
class ZoomableLiveImage extends StatefulWidget {
final File photo;
final File file;
final Function(bool) shouldDisableScroll;
final String tagPrefix;
final Decoration backgroundDecoration;
ZoomableLiveImage(
this.photo, {
this.file, {
Key key,
this.shouldDisableScroll,
@required this.tagPrefix,
@ -35,8 +35,8 @@ class ZoomableLiveImage extends StatefulWidget {
class _ZoomableLiveImageState extends State<ZoomableLiveImage>
with SingleTickerProviderStateMixin {
final Logger _logger = Logger("ZoomableLiveImage");
File _livePhoto;
bool _showLiveVideo = false;
File _file;
bool _showVideo = false;
bool _isLoadingVideoPlayer = false;
VideoPlayerController _videoPlayerController;
@ -44,8 +44,7 @@ class _ZoomableLiveImageState extends State<ZoomableLiveImage>
@override
void initState() {
_livePhoto = widget.photo;
_loadLiveVideo();
_file = widget.file;
_showLivePhotoToast();
super.initState();
}
@ -57,7 +56,7 @@ class _ZoomableLiveImageState extends State<ZoomableLiveImage>
}
if (mounted) {
setState(() {
_showLiveVideo = isPressed;
_showVideo = isPressed;
});
}
}
@ -66,17 +65,14 @@ class _ZoomableLiveImageState extends State<ZoomableLiveImage>
Widget build(BuildContext context) {
Widget content;
// check is long press is selected but videoPlayer is not configured yet
if (_showLiveVideo &&
_videoPlayerController == null &&
_livePhoto.isRemoteFile()) {
showToast("downloading... ", toastLength: Toast.LENGTH_SHORT);
if (_showVideo && _videoPlayerController == null) {
_loadLiveVideo();
}
if (_showLiveVideo && _videoPlayerController != null) {
if (_showVideo && _videoPlayerController != null) {
content = _getVideoPlayer();
} else {
content = ZoomableImage(_livePhoto,
content = ZoomableImage(_file,
tagPrefix: widget.tagPrefix,
shouldDisableScroll: widget.shouldDisableScroll,
backgroundDecoration: widget.backgroundDecoration);
@ -118,19 +114,23 @@ class _ZoomableLiveImageState extends State<ZoomableLiveImage>
return;
}
_isLoadingVideoPlayer = true;
var videoFile = await getFile(widget.photo, liveVideo: true)
.timeout(Duration(seconds: 5))
if (_file.isRemoteFile() && !(await isFileCached(_file, liveVideo: true))) {
showToast("downloading...", toastLength: Toast.LENGTH_LONG);
}
var videoFile = await getFile(widget.file, liveVideo: true)
.timeout(Duration(seconds: 15))
.onError((e, s) {
_logger.info("getFile failed ${_livePhoto.tag()}", e);
_logger.info("getFile failed ${_file.tag()}", e);
return null;
});
if ((videoFile == null || !videoFile.existsSync()) &&
_livePhoto.isRemoteFile()) {
videoFile = await getFileFromServer(widget.photo, liveVideo: true)
.timeout(Duration(seconds: 10))
_file.isRemoteFile()) {
videoFile = await getFileFromServer(widget.file, liveVideo: true)
.timeout(Duration(seconds: 15))
.onError((e, s) {
_logger.info("getRemoteFile failed ${_livePhoto.tag()}", e);
_logger.info("getRemoteFile failed ${_file.tag()}", e);
return null;
});
}
@ -144,12 +144,13 @@ class _ZoomableLiveImageState extends State<ZoomableLiveImage>
}
VideoPlayerController _setVideoPlayerController({io.File file}) {
_logger.fine("configuring video player for ${_livePhoto.tag()}");
var videoPlayerController = VideoPlayerController.file(file);
return _videoPlayerController = videoPlayerController
..initialize().whenComplete(() {
if (mounted) {
setState(() {});
setState(() {
_showVideo = true;
});
}
});
}

View file

@ -119,18 +119,30 @@ Future<io.File> getFileFromServer(
if (file.fileType == FileType.livePhoto) {
fileDownloadsInProgress[downloadID] = _getLivePhotoFromServer(file,
progressCallback: progressCallback, needLiveVideo: liveVideo)
.whenComplete(() => fileDownloadsInProgress.remove(downloadID));
.whenComplete(() {
fileDownloadsInProgress.remove(downloadID);
});
} else {
fileDownloadsInProgress[downloadID] = _downloadAndCache(
file,
cacheManager,
progressCallback: progressCallback,
).whenComplete(() => fileDownloadsInProgress.remove(downloadID));
file, cacheManager,
progressCallback: progressCallback)
.whenComplete(() {
fileDownloadsInProgress.remove(downloadID);
});
}
}
return fileDownloadsInProgress[downloadID];
}
Future<bool> isFileCached(ente.File file,
{bool liveVideo = false}) async {
final cacheManager = (file.fileType == FileType.video || liveVideo)
? VideoCacheManager.instance
: DefaultCacheManager();
final fileInfo = await cacheManager.getFileFromCache(file.getDownloadUrl());
return fileInfo != null;
}
final Map<int, Future<_LivePhoto>> livePhotoDownloadsTracker =
<int, Future<_LivePhoto>>{};
@ -148,7 +160,8 @@ Future<io.File> _getLivePhotoFromServer(ente.File file,
return null;
}
return needLiveVideo ? livePhoto.video : livePhoto.image;
} catch (e) {
} catch (e,s) {
_logger.warning("live photo get failed", e, s);
livePhotoDownloadsTracker.remove(downloadID);
return null;
}