mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
refactor(mobile): encapsulate most access to photomanager in repository (#12754)
* refactor(mobile): encapsulate most access to photomanager in repository
This commit is contained in:
parent
6740c67ed8
commit
6995cc2b38
45 changed files with 1205 additions and 500 deletions
|
|
@ -3,21 +3,27 @@ import 'dart:io';
|
|||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/extensions/response_extensions.dart';
|
||||
import 'package:immich_mobile/entities/asset.entity.dart';
|
||||
import 'package:immich_mobile/interfaces/file_media.interface.dart';
|
||||
import 'package:immich_mobile/providers/api.provider.dart';
|
||||
import 'package:immich_mobile/repositories/file_media.repository.dart';
|
||||
import 'package:immich_mobile/services/api.service.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
import 'package:photo_manager/photo_manager.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
final imageViewerServiceProvider =
|
||||
Provider((ref) => ImageViewerService(ref.watch(apiServiceProvider)));
|
||||
final imageViewerServiceProvider = Provider(
|
||||
(ref) => ImageViewerService(
|
||||
ref.watch(apiServiceProvider),
|
||||
ref.watch(fileMediaRepositoryProvider),
|
||||
),
|
||||
);
|
||||
|
||||
class ImageViewerService {
|
||||
final ApiService _apiService;
|
||||
final IFileMediaRepository _fileMediaRepository;
|
||||
final Logger _log = Logger("ImageViewerService");
|
||||
|
||||
ImageViewerService(this._apiService);
|
||||
ImageViewerService(this._apiService, this._fileMediaRepository);
|
||||
|
||||
Future<bool> downloadAsset(Asset asset) async {
|
||||
File? imageFile;
|
||||
|
|
@ -46,7 +52,7 @@ class ImageViewerService {
|
|||
return false;
|
||||
}
|
||||
|
||||
AssetEntity? entity;
|
||||
Asset? resultAsset;
|
||||
|
||||
final tempDir = await getTemporaryDirectory();
|
||||
videoFile = await File('${tempDir.path}/livephoto.mov').create();
|
||||
|
|
@ -54,24 +60,21 @@ class ImageViewerService {
|
|||
videoFile.writeAsBytesSync(motionResponse.bodyBytes);
|
||||
imageFile.writeAsBytesSync(imageResponse.bodyBytes);
|
||||
|
||||
entity = await PhotoManager.editor.darwin.saveLivePhoto(
|
||||
imageFile: imageFile,
|
||||
videoFile: videoFile,
|
||||
resultAsset = await _fileMediaRepository.saveLivePhoto(
|
||||
image: imageFile,
|
||||
video: videoFile,
|
||||
title: asset.fileName,
|
||||
);
|
||||
|
||||
if (entity == null) {
|
||||
if (resultAsset == null) {
|
||||
_log.warning(
|
||||
"Asset cannot be saved as a live photo. This is most likely a motion photo. Saving only the image file",
|
||||
);
|
||||
|
||||
entity = await PhotoManager.editor.saveImage(
|
||||
imageResponse.bodyBytes,
|
||||
title: asset.fileName,
|
||||
);
|
||||
resultAsset = await _fileMediaRepository
|
||||
.saveImage(imageResponse.bodyBytes, title: asset.fileName);
|
||||
}
|
||||
|
||||
return entity != null;
|
||||
return resultAsset != null;
|
||||
} else {
|
||||
var res = await _apiService.assetsApi
|
||||
.downloadAssetWithHttpInfo(asset.remoteId!);
|
||||
|
|
@ -81,11 +84,11 @@ class ImageViewerService {
|
|||
return false;
|
||||
}
|
||||
|
||||
final AssetEntity? entity;
|
||||
final Asset? resultAsset;
|
||||
final relativePath = Platform.isAndroid ? 'DCIM/Immich' : null;
|
||||
|
||||
if (asset.isImage) {
|
||||
entity = await PhotoManager.editor.saveImage(
|
||||
resultAsset = await _fileMediaRepository.saveImage(
|
||||
res.bodyBytes,
|
||||
title: asset.fileName,
|
||||
relativePath: relativePath,
|
||||
|
|
@ -94,13 +97,13 @@ class ImageViewerService {
|
|||
final tempDir = await getTemporaryDirectory();
|
||||
videoFile = await File('${tempDir.path}/${asset.fileName}').create();
|
||||
videoFile.writeAsBytesSync(res.bodyBytes);
|
||||
entity = await PhotoManager.editor.saveVideo(
|
||||
resultAsset = await _fileMediaRepository.saveVideo(
|
||||
videoFile,
|
||||
title: asset.fileName,
|
||||
relativePath: relativePath,
|
||||
);
|
||||
}
|
||||
return entity != null;
|
||||
return resultAsset != null;
|
||||
}
|
||||
} catch (error, stack) {
|
||||
_log.severe("Error saving downloaded asset", error, stack);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue