ente/lib/ui/zoomable_live_image.dart

167 lines
4.9 KiB
Dart
Raw Normal View History

2021-08-04 06:04:36 +00:00
import 'dart:io' as io;
import 'package:chewie/chewie.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
2021-08-04 06:04:36 +00:00
import 'package:logging/logging.dart';
import 'package:photos/core/constants.dart';
import 'package:photos/models/file.dart';
import 'package:photos/ui/zoomable_image.dart';
2021-08-04 06:04:36 +00:00
import 'package:photos/utils/file_util.dart';
import 'package:photos/utils/toast_util.dart';
2021-08-10 07:21:13 +00:00
import 'package:shared_preferences/shared_preferences.dart';
2021-08-04 06:04:36 +00:00
import 'package:video_player/video_player.dart';
class ZoomableLiveImage extends StatefulWidget {
final File file;
2021-08-04 06:04:36 +00:00
final Function(bool) shouldDisableScroll;
final String tagPrefix;
final Decoration backgroundDecoration;
2021-08-04 06:04:36 +00:00
ZoomableLiveImage(
this.file, {
2021-08-04 06:04:36 +00:00
Key key,
this.shouldDisableScroll,
@required this.tagPrefix,
this.backgroundDecoration,
}) : super(key: key);
@override
_ZoomableLiveImageState createState() => _ZoomableLiveImageState();
}
class _ZoomableLiveImageState extends State<ZoomableLiveImage>
with SingleTickerProviderStateMixin {
final Logger _logger = Logger("ZoomableLiveImage");
File _file;
bool _showVideo = false;
bool _isLoadingVideoPlayer = false;
2021-08-04 06:04:36 +00:00
VideoPlayerController _videoPlayerController;
ChewieController _chewieController;
@override
void initState() {
_file = widget.file;
2021-08-10 07:21:13 +00:00
_showLivePhotoToast();
2021-08-04 06:04:36 +00:00
super.initState();
}
void _onLongPressEvent(bool isPressed) {
2021-08-04 13:29:20 +00:00
if (_videoPlayerController != null && isPressed == false) {
// stop playing video
_videoPlayerController.pause();
}
if (mounted) {
setState(() {
_showVideo = isPressed;
});
2021-08-04 06:04:36 +00:00
}
}
@override
Widget build(BuildContext context) {
Widget content;
// check is long press is selected but videoPlayer is not configured yet
if (_showVideo && _videoPlayerController == null) {
_loadLiveVideo();
}
if (_showVideo && _videoPlayerController != null) {
content = _getVideoPlayer();
2021-08-04 06:04:36 +00:00
} else {
content = ZoomableImage(_file,
tagPrefix: widget.tagPrefix,
shouldDisableScroll: widget.shouldDisableScroll,
backgroundDecoration: widget.backgroundDecoration);
2021-08-04 06:04:36 +00:00
}
return GestureDetector(
onLongPressStart: (_) => {_onLongPressEvent(true)},
onLongPressEnd: (_) => {_onLongPressEvent(false)},
child: content);
2021-08-04 06:04:36 +00:00
}
@override
void dispose() {
if (_videoPlayerController != null) {
_videoPlayerController.pause();
_videoPlayerController.dispose();
}
if (_chewieController != null) {
_chewieController.dispose();
}
super.dispose();
}
Widget _getVideoPlayer() {
_videoPlayerController.seekTo(Duration.zero);
_chewieController = ChewieController(
videoPlayerController: _videoPlayerController,
aspectRatio: _videoPlayerController.value.aspectRatio,
autoPlay: true,
autoInitialize: true,
2021-10-06 08:45:28 +00:00
looping: true,
allowFullScreen: false,
showControls: false);
2021-08-04 06:04:36 +00:00
return Chewie(controller: _chewieController);
}
Future<void> _loadLiveVideo() async {
// do nothing is already loading or loaded
if (_isLoadingVideoPlayer || _videoPlayerController != null) {
return;
}
_isLoadingVideoPlayer = true;
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 ${_file.tag()}", e);
return null;
2021-08-04 06:04:36 +00:00
});
if ((videoFile == null || !videoFile.existsSync()) &&
_file.isRemoteFile()) {
videoFile = await getFileFromServer(widget.file, liveVideo: true)
.timeout(Duration(seconds: 15))
.onError((e, s) {
_logger.info("getRemoteFile failed ${_file.tag()}", e);
return null;
});
}
if (videoFile != null && videoFile.existsSync()) {
_setVideoPlayerController(file: videoFile);
} else {
showToast("download failed", toastLength: Toast.LENGTH_SHORT);
}
_isLoadingVideoPlayer = false;
2021-08-04 06:04:36 +00:00
}
VideoPlayerController _setVideoPlayerController({io.File file}) {
var videoPlayerController = VideoPlayerController.file(file);
return _videoPlayerController = videoPlayerController
..initialize().whenComplete(() {
if (mounted) {
setState(() {
_showVideo = true;
});
2021-08-04 06:04:36 +00:00
}
});
}
2021-08-10 07:21:13 +00:00
void _showLivePhotoToast() async {
var _preferences = await SharedPreferences.getInstance();
int promptTillNow = _preferences.getInt(kLivePhotoToastCounterKey) ?? 0;
if (promptTillNow < kMaxLivePhotoToastCount) {
showToast("press and hold to play video");
2021-08-10 08:08:07 +00:00
_preferences.setInt(kLivePhotoToastCounterKey, promptTillNow + 1);
2021-08-10 07:21:13 +00:00
}
}
2021-08-04 06:04:36 +00:00
}