ente/lib/ui/viewer/file/zoomable_live_image.dart

223 lines
6.9 KiB
Dart
Raw Normal View History

2023-05-01 10:47:31 +00:00
import "dart:io";
2021-08-04 06:04:36 +00:00
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
2023-05-01 10:47:31 +00:00
import 'package:motion_photos/motion_photos.dart';
import "package:photos/core/configuration.dart";
2021-08-04 06:04:36 +00:00
import 'package:photos/core/constants.dart';
2023-04-07 05:41:42 +00:00
import "package:photos/generated/l10n.dart";
import "package:photos/models/file/extensions/file_props.dart";
2023-08-25 04:39:30 +00:00
import 'package:photos/models/file/file.dart';
import 'package:photos/models/file/file_type.dart';
import "package:photos/models/metadata/file_magic.dart";
import "package:photos/services/file_magic_service.dart";
import 'package:photos/ui/viewer/file/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 {
2023-08-24 16:56:24 +00:00
final EnteFile file;
final Function(bool)? shouldDisableScroll;
final String? tagPrefix;
final Decoration? backgroundDecoration;
2021-08-04 06:04:36 +00:00
const ZoomableLiveImage(
this.file, {
Key? key,
2021-08-04 06:04:36 +00:00
this.shouldDisableScroll,
required this.tagPrefix,
2021-08-04 06:04:36 +00:00
this.backgroundDecoration,
}) : super(key: key);
@override
2022-07-03 09:45:00 +00:00
State<ZoomableLiveImage> createState() => _ZoomableLiveImageState();
2021-08-04 06:04:36 +00:00
}
2022-07-03 09:45:00 +00:00
class _ZoomableLiveImageState extends State<ZoomableLiveImage>
with SingleTickerProviderStateMixin {
2021-08-04 06:04:36 +00:00
final Logger _logger = Logger("ZoomableLiveImage");
2023-08-24 16:56:24 +00:00
late EnteFile _file;
bool _showVideo = false;
bool _isLoadingVideoPlayer = false;
2021-08-04 06:04:36 +00:00
VideoPlayerController? _videoPlayerController;
ChewieController? _chewieController;
2021-08-04 06:04:36 +00:00
@override
void initState() {
_file = widget.file;
Future.microtask(() => _showHintForMotionPhotoPlay).ignore();
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();
2021-08-04 13:29:20 +00:00
}
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 {
2022-06-11 08:23:52 +00:00
content = ZoomableImage(
_file,
tagPrefix: widget.tagPrefix,
shouldDisableScroll: widget.shouldDisableScroll,
backgroundDecoration: widget.backgroundDecoration,
);
2021-08-04 06:04:36 +00:00
}
return GestureDetector(
2022-06-11 08:23:52 +00:00
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();
2021-08-04 06:04:36 +00:00
}
if (_chewieController != null) {
_chewieController!.dispose();
2021-08-04 06:04:36 +00:00
}
super.dispose();
}
Widget _getVideoPlayer() {
_videoPlayerController!.seekTo(Duration.zero);
2021-08-04 06:04:36 +00:00
_chewieController = ChewieController(
videoPlayerController: _videoPlayerController!,
aspectRatio: _videoPlayerController!.value.aspectRatio,
2022-06-11 08:23:52 +00:00
autoPlay: true,
autoInitialize: true,
looping: true,
allowFullScreen: false,
showControls: false,
);
return Container(
2022-07-03 09:45:00 +00:00
color: Colors.black,
child: Chewie(controller: _chewieController!), // same for both theme
);
2021-08-04 06:04:36 +00:00
}
Future<void> _loadLiveVideo() async {
// do nothing is already loading or loaded
if (_isLoadingVideoPlayer || _videoPlayerController != null) {
return;
}
_isLoadingVideoPlayer = true;
2023-08-24 16:56:24 +00:00
final File? videoFile = _file.fileType == FileType.livePhoto
2023-05-01 10:47:31 +00:00
? await _getLivePhotoVideo()
: await _getMotionPhotoVideo();
if (videoFile != null && videoFile.existsSync()) {
_setVideoPlayerController(file: videoFile);
} else if (_file.fileType == FileType.livePhoto) {
showShortToast(context, S.of(context).downloadFailed);
}
_isLoadingVideoPlayer = false;
}
2023-08-24 16:56:24 +00:00
Future<File?> _getLivePhotoVideo() async {
if (_file.isRemoteFile && !(await isFileCached(_file, liveVideo: true))) {
2023-04-07 05:41:42 +00:00
showShortToast(context, S.of(context).downloading);
}
2023-08-24 16:56:24 +00:00
File? videoFile = await getFile(widget.file, liveVideo: true)
2022-07-04 06:02:17 +00:00
.timeout(const Duration(seconds: 15))
.onError((dynamic e, s) {
_logger.info("getFile failed ${_file.tag}", e);
return null;
2021-08-04 06:04:36 +00:00
});
// FixMe: Here, we are fetching video directly when getFile failed
// getFile with liveVideo as true can fail for file with localID when
// the live photo was downloaded from remote.
2022-07-03 09:45:00 +00:00
if ((videoFile == null || !videoFile.existsSync()) &&
_file.uploadedFileID != null) {
videoFile = await getFileFromServer(widget.file, liveVideo: true)
2022-07-04 06:02:17 +00:00
.timeout(const Duration(seconds: 15))
.onError((dynamic e, s) {
_logger.info("getRemoteFile failed ${_file.tag}", e);
return null;
});
}
2023-05-01 10:47:31 +00:00
return videoFile;
}
2023-08-24 16:56:24 +00:00
Future<File?> _getMotionPhotoVideo() async {
2023-05-01 10:47:31 +00:00
if (_file.isRemoteFile && !(await isFileCached(_file))) {
showShortToast(context, S.of(context).downloading);
}
2023-05-01 10:47:31 +00:00
2023-08-24 16:56:24 +00:00
final File? imageFile = await getFile(
2023-05-01 10:47:31 +00:00
widget.file,
isOrigin: !Platform.isAndroid,
).timeout(const Duration(seconds: 15)).onError((dynamic e, s) {
_logger.info("getFile failed ${_file.tag}", e);
return null;
});
if (imageFile != null) {
final motionPhoto = MotionPhotos(imageFile.path);
2023-05-05 12:36:25 +00:00
final index = await motionPhoto.getMotionVideoIndex();
2023-05-01 10:47:31 +00:00
if (index != null) {
if (widget.file.pubMagicMetadata?.mvi == null &&
(widget.file.ownerID ?? 0) == Configuration.instance.getUserID()!) {
FileMagicService.instance.updatePublicMagicMetadata(
[widget.file],
{motionVideoIndexKey: index.start},
).ignore();
}
2023-05-01 10:47:31 +00:00
return motionPhoto.getMotionVideoFile(
index: index,
);
}
}
return null;
2021-08-04 06:04:36 +00:00
}
2023-08-24 16:56:24 +00:00
VideoPlayerController _setVideoPlayerController({required File file}) {
2022-08-29 14:43:31 +00:00
final videoPlayerController = VideoPlayerController.file(file);
2021-08-04 06:04:36 +00:00
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 _showHintForMotionPhotoPlay() async {
if (!_file.isLiveOrMotionPhoto) {
return;
}
2022-08-29 14:43:31 +00:00
final preferences = await SharedPreferences.getInstance();
final int promptTillNow = preferences.getInt(livePhotoToastCounterKey) ?? 0;
if (promptTillNow < maxLivePhotoToastCount && mounted) {
showShortToast(context, S.of(context).pressAndHoldToPlayVideo);
preferences.setInt(livePhotoToastCounterKey, promptTillNow + 1);
2021-08-10 07:21:13 +00:00
}
}
2021-08-04 06:04:36 +00:00
}