From 82fa67ef9eb9179bb3ccc353d5d9222587fc3350 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta Date: Wed, 4 Aug 2021 15:41:38 +0530 Subject: [PATCH] Minor refactoring --- lib/core/cache/image_cache.dart | 8 ++++---- lib/ui/zoomable_live_image.dart | 8 ++++---- lib/utils/file_util.dart | 24 +++++++++++++----------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/core/cache/image_cache.dart b/lib/core/cache/image_cache.dart index 314dafafc..39f1b8aff 100644 --- a/lib/core/cache/image_cache.dart +++ b/lib/core/cache/image_cache.dart @@ -6,11 +6,11 @@ import 'package:photos/models/file.dart'; class FileLruCache { static final LRUMap _map = LRUMap(25); - static io.File get(File file) { - return _map.get(file.tag()); + static io.File get(String key) { + return _map.get(key); } - static void put(File file, io.File imageData) { - _map.put(file.tag(), imageData); + static void put(String key, io.File imageData) { + _map.put(key, imageData); } } diff --git a/lib/ui/zoomable_live_image.dart b/lib/ui/zoomable_live_image.dart index 023f05428..d0dd9e509 100644 --- a/lib/ui/zoomable_live_image.dart +++ b/lib/ui/zoomable_live_image.dart @@ -77,7 +77,7 @@ class _ZoomableLiveImageState extends State } else { _loadLocalImage(context); } - _loadLiveVide(); + _loadLiveVideo(); if (_imageProvider != null) { Widget content; if (_loadLivePhotoVideo && _videoPlayerController != null @@ -146,15 +146,15 @@ class _ZoomableLiveImageState extends State return Chewie(controller: _chewieController); } - void _loadLiveVide() { + void _loadLiveVideo() { if (_videoPlayerController != null) { return ; } - getLiveVideo(widget.photo).then((file) { + getFile(widget.photo, liveVideo: true).then((file) { if (file != null && file.existsSync()) { _logger.fine("loading live from local"); _setVideoPlayerController(file: file); - } else { + } else if (widget.photo.uploadedFileID != null) { _logger.fine("loading live from remote"); getFileFromServer(widget.photo, liveVideo: true).then((file) { if (file != null && file.existsSync()) { diff --git a/lib/utils/file_util.dart b/lib/utils/file_util.dart index beeaad556..7b06be7b5 100644 --- a/lib/utils/file_util.dart +++ b/lib/utils/file_util.dart @@ -27,31 +27,33 @@ void preloadFile(ente.File file) { getFile(file); } -Future getFile(ente.File file) async { +Future getFile(ente.File file, + {bool liveVideo = false} // only relevant for live photos + ) async { if (file.isRemoteFile()) { - return getFileFromServer(file); + return getFileFromServer(file, liveVideo: liveVideo); } else { - final cachedFile = FileLruCache.get(file); + String key = file.tag() + liveVideo.toString(); + final cachedFile = FileLruCache.get(key); if (cachedFile == null) { - final diskFile = await _getLocalDiskFile(file); - FileLruCache.put(file, diskFile); + final diskFile = await _getLocalDiskFile(file, liveVideo: liveVideo); + FileLruCache.put(key, diskFile); return diskFile; } return cachedFile; } } -Future getLiveVideo(ente.File file) async { - return Motionphoto.getLivePhotoFile(file.localID); -} - -Future _getLocalDiskFile(ente.File file) async { +Future _getLocalDiskFile(ente.File file, {bool liveVideo = false}) async { if (file.isSharedMediaToAppSandbox()) { var localFile = io.File(getSharedMediaFilePath(file)); return localFile.exists().then((exist) { return exist ? localFile : null; }); - } else { + } else if (file.fileType == FileType.livePhoto && liveVideo) { + return Motionphoto.getLivePhotoFile(file.localID); + } + else { return file.getAsset().then((asset) async { if (asset == null || !(await asset.exists)) { return null;