2022-07-13 14:23:48 +02:00
|
|
|
import 'dart:async';
|
2022-02-03 18:06:44 +02:00
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:hive/hive.dart';
|
2022-06-25 20:46:51 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/background_service/background.service.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/models/hive_backup_albums.model.dart';
|
|
|
|
import 'package:immich_mobile/modules/backup/services/backup.service.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2023-02-09 19:32:08 +02:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2022-08-03 07:04:34 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:immich_mobile/shared/services/api.service.dart';
|
2022-11-26 18:16:02 +02:00
|
|
|
import 'package:immich_mobile/utils/openapi_extensions.dart';
|
|
|
|
import 'package:immich_mobile/utils/tuple.dart';
|
2022-11-30 18:58:07 +02:00
|
|
|
import 'package:logging/logging.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:openapi/api.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
final assetServiceProvider = Provider(
|
|
|
|
(ref) => AssetService(
|
|
|
|
ref.watch(apiServiceProvider),
|
2022-11-08 19:00:24 +02:00
|
|
|
ref.watch(backupServiceProvider),
|
|
|
|
ref.watch(backgroundServiceProvider),
|
2022-07-13 14:23:48 +02:00
|
|
|
),
|
|
|
|
);
|
2022-06-25 20:46:51 +02:00
|
|
|
|
2022-02-03 18:06:44 +02:00
|
|
|
class AssetService {
|
2022-07-13 14:23:48 +02:00
|
|
|
final ApiService _apiService;
|
2022-11-08 19:00:24 +02:00
|
|
|
final BackupService _backupService;
|
|
|
|
final BackgroundService _backgroundService;
|
2022-11-30 18:58:07 +02:00
|
|
|
final log = Logger('AssetService');
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-11-08 19:00:24 +02:00
|
|
|
AssetService(this._apiService, this._backupService, this._backgroundService);
|
2022-02-07 04:31:32 +02:00
|
|
|
|
2022-11-26 18:16:02 +02:00
|
|
|
/// Returns `null` if the server state did not change, else list of assets
|
2022-12-02 22:55:10 +02:00
|
|
|
Future<Pair<List<Asset>?, String?>> getRemoteAssets({String? etag}) async {
|
2022-11-30 18:58:07 +02:00
|
|
|
try {
|
2023-02-21 17:25:31 +02:00
|
|
|
// temporary fix for race condition that the _apiService
|
|
|
|
// get called before accessToken is set
|
|
|
|
var userInfoHiveBox = await Hive.openBox(userInfoBox);
|
|
|
|
var accessToken = userInfoHiveBox.get(accessTokenKey);
|
|
|
|
_apiService.setAccessToken(accessToken);
|
|
|
|
|
2022-12-02 22:55:10 +02:00
|
|
|
final Pair<List<AssetResponseDto>, String?>? remote =
|
|
|
|
await _apiService.assetApi.getAllAssetsWithETag(eTag: etag);
|
2022-11-30 18:58:07 +02:00
|
|
|
if (remote == null) {
|
2023-02-09 19:32:08 +02:00
|
|
|
return Pair(null, etag);
|
2022-11-30 18:58:07 +02:00
|
|
|
}
|
2022-12-02 22:55:10 +02:00
|
|
|
return Pair(
|
|
|
|
remote.first.map(Asset.remote).toList(growable: false),
|
|
|
|
remote.second,
|
|
|
|
);
|
2022-11-30 18:58:07 +02:00
|
|
|
} catch (e, stack) {
|
|
|
|
log.severe('Error while getting remote assets', e, stack);
|
2023-02-21 05:43:39 +02:00
|
|
|
debugPrint("[ERROR] [getRemoteAssets] $e");
|
2023-02-09 19:32:08 +02:00
|
|
|
return Pair(null, etag);
|
2022-11-08 19:00:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// if [urgent] is `true`, do not block by waiting on the background service
|
2022-11-26 18:16:02 +02:00
|
|
|
/// to finish running. Returns `null` instead after a timeout.
|
|
|
|
Future<List<Asset>?> getLocalAssets({bool urgent = false}) async {
|
2022-11-08 19:00:24 +02:00
|
|
|
try {
|
|
|
|
final Future<bool> hasAccess = urgent
|
|
|
|
? _backgroundService.hasAccess
|
|
|
|
.timeout(const Duration(milliseconds: 250))
|
|
|
|
: _backgroundService.hasAccess;
|
|
|
|
if (!await hasAccess) {
|
|
|
|
throw Exception("Error [getAllAsset] failed to gain access");
|
|
|
|
}
|
|
|
|
final box = await Hive.openBox<HiveBackupAlbums>(hiveBackupInfoBox);
|
|
|
|
final HiveBackupAlbums? backupAlbumInfo = box.get(backupInfoKey);
|
2023-02-09 19:32:08 +02:00
|
|
|
final String userId = Store.get(StoreKey.userRemoteId);
|
2022-11-26 18:16:02 +02:00
|
|
|
if (backupAlbumInfo != null) {
|
|
|
|
return (await _backupService
|
|
|
|
.buildUploadCandidates(backupAlbumInfo.deepCopy()))
|
2023-02-04 22:42:42 +02:00
|
|
|
.map((e) => Asset.local(e, userId))
|
2022-11-26 18:16:02 +02:00
|
|
|
.toList(growable: false);
|
|
|
|
}
|
2023-02-23 08:36:17 +02:00
|
|
|
} catch (e, stackTrace) {
|
|
|
|
log.severe('Error while getting local assets', e, stackTrace);
|
2022-11-08 19:00:24 +02:00
|
|
|
debugPrint("Error [_getLocalAssets] ${e.toString()}");
|
2022-02-07 04:31:32 +02:00
|
|
|
}
|
2022-11-26 18:16:02 +02:00
|
|
|
return null;
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
2022-02-11 04:40:11 +02:00
|
|
|
|
2022-11-08 19:00:24 +02:00
|
|
|
Future<Asset?> getAssetById(String assetId) async {
|
2022-02-11 04:40:11 +02:00
|
|
|
try {
|
2023-02-04 22:42:42 +02:00
|
|
|
final dto = await _apiService.assetApi.getAssetById(assetId);
|
|
|
|
if (dto != null) {
|
|
|
|
return Asset.remote(dto);
|
|
|
|
}
|
2022-02-13 23:10:42 +02:00
|
|
|
} catch (e) {
|
2022-07-13 14:23:48 +02:00
|
|
|
debugPrint("Error [getAssetById] ${e.toString()}");
|
2022-02-13 23:10:42 +02:00
|
|
|
}
|
2023-02-04 22:42:42 +02:00
|
|
|
return null;
|
2022-02-13 23:10:42 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
Future<List<DeleteAssetResponseDto>?> deleteAssets(
|
2023-02-04 22:42:42 +02:00
|
|
|
Iterable<Asset> deleteAssets,
|
2022-07-13 14:23:48 +02:00
|
|
|
) async {
|
2022-02-13 23:10:42 +02:00
|
|
|
try {
|
2022-11-08 19:00:24 +02:00
|
|
|
final List<String> payload = [];
|
2022-02-13 23:10:42 +02:00
|
|
|
|
2022-11-08 19:00:24 +02:00
|
|
|
for (final asset in deleteAssets) {
|
2023-02-04 22:42:42 +02:00
|
|
|
payload.add(asset.remoteId!);
|
2022-02-13 23:10:42 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
return await _apiService.assetApi
|
|
|
|
.deleteAsset(DeleteAssetDto(ids: payload));
|
2022-02-11 04:40:11 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error getAllAsset ${e.toString()}");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2023-02-05 05:25:11 +02:00
|
|
|
|
2023-02-09 19:32:08 +02:00
|
|
|
Future<Asset?> updateAsset(
|
|
|
|
Asset asset,
|
|
|
|
UpdateAssetDto updateAssetDto,
|
|
|
|
) async {
|
|
|
|
final dto =
|
|
|
|
await _apiService.assetApi.updateAsset(asset.remoteId!, updateAssetDto);
|
|
|
|
return dto == null ? null : Asset.remote(dto);
|
2023-02-05 05:25:11 +02:00
|
|
|
}
|
|
|
|
|
2023-02-09 19:32:08 +02:00
|
|
|
Future<Asset?> changeFavoriteStatus(Asset asset, bool isFavorite) {
|
2023-02-05 05:25:11 +02:00
|
|
|
return updateAsset(asset, UpdateAssetDto(isFavorite: isFavorite));
|
|
|
|
}
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|