mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
refactor(mobile): reworked Asset, store all required fields from local & remote (#1539)
replace usage of AssetResponseDto with Asset Add new class ExifInfo to store data from ExifResponseDto
This commit is contained in:
parent
7bd2455175
commit
0048662182
28 changed files with 626 additions and 504 deletions
|
|
@ -1,63 +1,128 @@
|
|||
import 'package:hive/hive.dart';
|
||||
import 'package:immich_mobile/constants/hive_box.dart';
|
||||
import 'package:immich_mobile/shared/models/exif_info.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
import 'package:photo_manager/photo_manager.dart';
|
||||
import 'package:immich_mobile/utils/builtin_extensions.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
/// Asset (online or local)
|
||||
class Asset {
|
||||
Asset.remote(this.remote) {
|
||||
local = null;
|
||||
}
|
||||
Asset.remote(AssetResponseDto remote)
|
||||
: remoteId = remote.id,
|
||||
createdAt = DateTime.parse(remote.createdAt),
|
||||
modifiedAt = DateTime.parse(remote.modifiedAt),
|
||||
durationInSeconds = remote.duration.toDuration().inSeconds,
|
||||
fileName = p.basename(remote.originalPath),
|
||||
height = remote.exifInfo?.exifImageHeight?.toInt(),
|
||||
width = remote.exifInfo?.exifImageWidth?.toInt(),
|
||||
livePhotoVideoId = remote.livePhotoVideoId,
|
||||
deviceAssetId = remote.deviceAssetId,
|
||||
deviceId = remote.deviceId,
|
||||
ownerId = remote.ownerId,
|
||||
latitude = remote.exifInfo?.latitude?.toDouble(),
|
||||
longitude = remote.exifInfo?.longitude?.toDouble(),
|
||||
exifInfo =
|
||||
remote.exifInfo != null ? ExifInfo.fromDto(remote.exifInfo!) : null;
|
||||
|
||||
Asset.local(this.local) {
|
||||
remote = null;
|
||||
}
|
||||
|
||||
late final AssetResponseDto? remote;
|
||||
late final AssetEntity? local;
|
||||
|
||||
bool get isRemote => remote != null;
|
||||
bool get isLocal => local != null;
|
||||
|
||||
String get deviceId =>
|
||||
isRemote ? remote!.deviceId : Hive.box(userInfoBox).get(deviceIdKey);
|
||||
|
||||
String get deviceAssetId => isRemote ? remote!.deviceAssetId : local!.id;
|
||||
|
||||
String get id => isLocal ? local!.id : remote!.id;
|
||||
|
||||
double? get latitude =>
|
||||
isLocal ? local!.latitude : remote!.exifInfo?.latitude?.toDouble();
|
||||
|
||||
double? get longitude =>
|
||||
isLocal ? local!.longitude : remote!.exifInfo?.longitude?.toDouble();
|
||||
|
||||
DateTime get createdAt {
|
||||
if (isLocal) {
|
||||
if (local!.createDateTime.year == 1970) {
|
||||
return local!.modifiedDateTime;
|
||||
}
|
||||
return local!.createDateTime;
|
||||
} else {
|
||||
return DateTime.parse(remote!.createdAt);
|
||||
Asset.local(AssetEntity local, String owner)
|
||||
: localId = local.id,
|
||||
latitude = local.latitude,
|
||||
longitude = local.longitude,
|
||||
durationInSeconds = local.duration,
|
||||
height = local.height,
|
||||
width = local.width,
|
||||
fileName = local.title!,
|
||||
deviceAssetId = local.id,
|
||||
deviceId = Hive.box(userInfoBox).get(deviceIdKey),
|
||||
ownerId = owner,
|
||||
modifiedAt = local.modifiedDateTime.toUtc(),
|
||||
createdAt = local.createDateTime.toUtc() {
|
||||
if (createdAt.year == 1970) {
|
||||
createdAt = modifiedAt;
|
||||
}
|
||||
}
|
||||
|
||||
bool get isImage => isLocal
|
||||
? local!.type == AssetType.image
|
||||
: remote!.type == AssetTypeEnum.IMAGE;
|
||||
Asset({
|
||||
this.localId,
|
||||
this.remoteId,
|
||||
required this.deviceAssetId,
|
||||
required this.deviceId,
|
||||
required this.ownerId,
|
||||
required this.createdAt,
|
||||
required this.modifiedAt,
|
||||
this.latitude,
|
||||
this.longitude,
|
||||
required this.durationInSeconds,
|
||||
this.width,
|
||||
this.height,
|
||||
required this.fileName,
|
||||
this.livePhotoVideoId,
|
||||
this.exifInfo,
|
||||
});
|
||||
|
||||
String get duration => isRemote
|
||||
? remote!.duration
|
||||
: Duration(seconds: local!.duration).toString();
|
||||
AssetEntity? _local;
|
||||
|
||||
/// use only for tests
|
||||
set createdAt(DateTime val) {
|
||||
if (isRemote) {
|
||||
remote!.createdAt = val.toIso8601String();
|
||||
AssetEntity? get local {
|
||||
if (isLocal && _local == null) {
|
||||
_local = AssetEntity(
|
||||
id: localId!.toString(),
|
||||
typeInt: isImage ? 1 : 2,
|
||||
width: width!,
|
||||
height: height!,
|
||||
duration: durationInSeconds,
|
||||
createDateSecond: createdAt.millisecondsSinceEpoch ~/ 1000,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
modifiedDateSecond: modifiedAt.millisecondsSinceEpoch ~/ 1000,
|
||||
title: fileName,
|
||||
);
|
||||
}
|
||||
return _local;
|
||||
}
|
||||
|
||||
String? localId;
|
||||
|
||||
String? remoteId;
|
||||
|
||||
String deviceAssetId;
|
||||
|
||||
String deviceId;
|
||||
|
||||
String ownerId;
|
||||
|
||||
DateTime createdAt;
|
||||
|
||||
DateTime modifiedAt;
|
||||
|
||||
double? latitude;
|
||||
|
||||
double? longitude;
|
||||
|
||||
int durationInSeconds;
|
||||
|
||||
int? width;
|
||||
|
||||
int? height;
|
||||
|
||||
String fileName;
|
||||
|
||||
String? livePhotoVideoId;
|
||||
|
||||
ExifInfo? exifInfo;
|
||||
|
||||
String get id => isLocal ? localId.toString() : remoteId!;
|
||||
|
||||
String get name => p.withoutExtension(fileName);
|
||||
|
||||
bool get isRemote => remoteId != null;
|
||||
|
||||
bool get isLocal => localId != null;
|
||||
|
||||
bool get isImage => durationInSeconds == 0;
|
||||
|
||||
Duration get duration => Duration(seconds: durationInSeconds);
|
||||
|
||||
@override
|
||||
bool operator ==(other) {
|
||||
if (other is! Asset) return false;
|
||||
|
|
@ -67,12 +132,26 @@ class Asset {
|
|||
@override
|
||||
int get hashCode => id.hashCode;
|
||||
|
||||
// methods below are only required for caching as JSON
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final json = <String, dynamic>{};
|
||||
if (isLocal) {
|
||||
json["local"] = _assetEntityToJson(local!);
|
||||
} else {
|
||||
json["remote"] = remote!.toJson();
|
||||
json["localId"] = localId;
|
||||
json["remoteId"] = remoteId;
|
||||
json["deviceAssetId"] = deviceAssetId;
|
||||
json["deviceId"] = deviceId;
|
||||
json["ownerId"] = ownerId;
|
||||
json["createdAt"] = createdAt.millisecondsSinceEpoch;
|
||||
json["modifiedAt"] = modifiedAt.millisecondsSinceEpoch;
|
||||
json["latitude"] = latitude;
|
||||
json["longitude"] = longitude;
|
||||
json["durationInSeconds"] = durationInSeconds;
|
||||
json["width"] = width;
|
||||
json["height"] = height;
|
||||
json["fileName"] = fileName;
|
||||
json["livePhotoVideoId"] = livePhotoVideoId;
|
||||
if (exifInfo != null) {
|
||||
json["exifInfo"] = exifInfo!.toJson();
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
|
@ -80,55 +159,28 @@ class Asset {
|
|||
static Asset? fromJson(dynamic value) {
|
||||
if (value is Map) {
|
||||
final json = value.cast<String, dynamic>();
|
||||
final l = json["local"];
|
||||
if (l != null) {
|
||||
return Asset.local(_assetEntityFromJson(l));
|
||||
} else {
|
||||
return Asset.remote(AssetResponseDto.fromJson(json["remote"]));
|
||||
}
|
||||
return Asset(
|
||||
localId: json["localId"],
|
||||
remoteId: json["remoteId"],
|
||||
deviceAssetId: json["deviceAssetId"],
|
||||
deviceId: json["deviceId"],
|
||||
ownerId: json["ownerId"],
|
||||
createdAt:
|
||||
DateTime.fromMillisecondsSinceEpoch(json["createdAt"], isUtc: true),
|
||||
modifiedAt: DateTime.fromMillisecondsSinceEpoch(
|
||||
json["modifiedAt"],
|
||||
isUtc: true,
|
||||
),
|
||||
latitude: json["latitude"],
|
||||
longitude: json["longitude"],
|
||||
durationInSeconds: json["durationInSeconds"],
|
||||
width: json["width"],
|
||||
height: json["height"],
|
||||
fileName: json["fileName"],
|
||||
livePhotoVideoId: json["livePhotoVideoId"],
|
||||
exifInfo: ExifInfo.fromJson(json["exifInfo"]),
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> _assetEntityToJson(AssetEntity a) {
|
||||
final json = <String, dynamic>{};
|
||||
json["id"] = a.id;
|
||||
json["typeInt"] = a.typeInt;
|
||||
json["width"] = a.width;
|
||||
json["height"] = a.height;
|
||||
json["duration"] = a.duration;
|
||||
json["orientation"] = a.orientation;
|
||||
json["isFavorite"] = a.isFavorite;
|
||||
json["title"] = a.title;
|
||||
json["createDateSecond"] = a.createDateSecond;
|
||||
json["modifiedDateSecond"] = a.modifiedDateSecond;
|
||||
json["latitude"] = a.latitude;
|
||||
json["longitude"] = a.longitude;
|
||||
json["mimeType"] = a.mimeType;
|
||||
json["subtype"] = a.subtype;
|
||||
return json;
|
||||
}
|
||||
|
||||
AssetEntity? _assetEntityFromJson(dynamic value) {
|
||||
if (value is Map) {
|
||||
final json = value.cast<String, dynamic>();
|
||||
return AssetEntity(
|
||||
id: json["id"],
|
||||
typeInt: json["typeInt"],
|
||||
width: json["width"],
|
||||
height: json["height"],
|
||||
duration: json["duration"],
|
||||
orientation: json["orientation"],
|
||||
isFavorite: json["isFavorite"],
|
||||
title: json["title"],
|
||||
createDateSecond: json["createDateSecond"],
|
||||
modifiedDateSecond: json["modifiedDateSecond"],
|
||||
latitude: json["latitude"],
|
||||
longitude: json["longitude"],
|
||||
mimeType: json["mimeType"],
|
||||
subtype: json["subtype"],
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
86
mobile/lib/shared/models/exif_info.dart
Normal file
86
mobile/lib/shared/models/exif_info.dart
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import 'package:openapi/api.dart';
|
||||
import 'package:immich_mobile/utils/builtin_extensions.dart';
|
||||
|
||||
class ExifInfo {
|
||||
int? fileSize;
|
||||
String? make;
|
||||
String? model;
|
||||
String? orientation;
|
||||
String? lensModel;
|
||||
double? fNumber;
|
||||
double? focalLength;
|
||||
int? iso;
|
||||
double? exposureTime;
|
||||
String? city;
|
||||
String? state;
|
||||
String? country;
|
||||
|
||||
ExifInfo.fromDto(ExifResponseDto dto)
|
||||
: fileSize = dto.fileSizeInByte,
|
||||
make = dto.make,
|
||||
model = dto.model,
|
||||
orientation = dto.orientation,
|
||||
lensModel = dto.lensModel,
|
||||
fNumber = dto.fNumber?.toDouble(),
|
||||
focalLength = dto.focalLength?.toDouble(),
|
||||
iso = dto.iso?.toInt(),
|
||||
exposureTime = dto.exposureTime?.toDouble(),
|
||||
city = dto.city,
|
||||
state = dto.state,
|
||||
country = dto.country;
|
||||
|
||||
// stuff below is only required for caching as JSON
|
||||
|
||||
ExifInfo(
|
||||
this.fileSize,
|
||||
this.make,
|
||||
this.model,
|
||||
this.orientation,
|
||||
this.lensModel,
|
||||
this.fNumber,
|
||||
this.focalLength,
|
||||
this.iso,
|
||||
this.exposureTime,
|
||||
this.city,
|
||||
this.state,
|
||||
this.country,
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final json = <String, dynamic>{};
|
||||
json["fileSize"] = fileSize;
|
||||
json["make"] = make;
|
||||
json["model"] = model;
|
||||
json["orientation"] = orientation;
|
||||
json["lensModel"] = lensModel;
|
||||
json["fNumber"] = fNumber;
|
||||
json["focalLength"] = focalLength;
|
||||
json["iso"] = iso;
|
||||
json["exposureTime"] = exposureTime;
|
||||
json["city"] = city;
|
||||
json["state"] = state;
|
||||
json["country"] = country;
|
||||
return json;
|
||||
}
|
||||
|
||||
static ExifInfo? fromJson(dynamic value) {
|
||||
if (value is Map) {
|
||||
final json = value.cast<String, dynamic>();
|
||||
return ExifInfo(
|
||||
json["fileSize"],
|
||||
json["make"],
|
||||
json["model"],
|
||||
json["orientation"],
|
||||
json["lensModel"],
|
||||
json["fNumber"],
|
||||
json["focalLength"],
|
||||
json["iso"],
|
||||
json["exposureTime"],
|
||||
json["city"],
|
||||
json["state"],
|
||||
json["country"],
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -4,8 +4,8 @@ import 'package:flutter/foundation.dart';
|
|||
import 'package:hive/hive.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/constants/hive_box.dart';
|
||||
import 'package:immich_mobile/modules/home/services/asset.service.dart';
|
||||
import 'package:immich_mobile/modules/home/services/asset_cache.service.dart';
|
||||
import 'package:immich_mobile/shared/services/asset.service.dart';
|
||||
import 'package:immich_mobile/shared/services/asset_cache.service.dart';
|
||||
import 'package:immich_mobile/modules/home/ui/asset_grid/asset_grid_data_structure.dart';
|
||||
import 'package:immich_mobile/modules/settings/providers/app_settings.provider.dart';
|
||||
import 'package:immich_mobile/modules/settings/services/app_settings.service.dart';
|
||||
|
|
@ -36,7 +36,7 @@ class AssetsState {
|
|||
return AssetsState([...allAssets, ...toAdd]);
|
||||
}
|
||||
|
||||
_groupByDate() async {
|
||||
Future<Map<String, List<Asset>>> _groupByDate() async {
|
||||
sortCompare(List<Asset> assets) {
|
||||
assets.sortByCompare<DateTime>(
|
||||
(e) => e.createdAt,
|
||||
|
|
@ -50,11 +50,11 @@ class AssetsState {
|
|||
return await compute(sortCompare, allAssets.toList());
|
||||
}
|
||||
|
||||
static fromAssetList(List<Asset> assets) {
|
||||
static AssetsState fromAssetList(List<Asset> assets) {
|
||||
return AssetsState(assets);
|
||||
}
|
||||
|
||||
static empty() {
|
||||
static AssetsState empty() {
|
||||
return AssetsState([]);
|
||||
}
|
||||
}
|
||||
|
|
@ -82,7 +82,10 @@ class AssetNotifier extends StateNotifier<AssetsState> {
|
|||
this._settingsService,
|
||||
) : super(AssetsState.fromAssetList([]));
|
||||
|
||||
_updateAssetsState(List<Asset> newAssetList, {bool cache = true}) async {
|
||||
Future<void> _updateAssetsState(
|
||||
List<Asset> newAssetList, {
|
||||
bool cache = true,
|
||||
}) async {
|
||||
if (cache) {
|
||||
_assetCacheService.put(newAssetList);
|
||||
}
|
||||
|
|
@ -101,20 +104,26 @@ class AssetNotifier extends StateNotifier<AssetsState> {
|
|||
final stopwatch = Stopwatch();
|
||||
try {
|
||||
_getAllAssetInProgress = true;
|
||||
final bool isCacheValid = await _assetCacheService.isValid();
|
||||
bool isCacheValid = await _assetCacheService.isValid();
|
||||
stopwatch.start();
|
||||
final Box box = Hive.box(userInfoBox);
|
||||
if (isCacheValid && state.allAssets.isEmpty) {
|
||||
final List<Asset>? cachedData = await _assetCacheService.get();
|
||||
if (cachedData == null) {
|
||||
isCacheValid = false;
|
||||
log.warning("Cached asset data is invalid, fetching new data");
|
||||
} else {
|
||||
await _updateAssetsState(cachedData, cache: false);
|
||||
log.info(
|
||||
"Reading assets ${state.allAssets.length} from cache: ${stopwatch.elapsedMilliseconds}ms",
|
||||
);
|
||||
}
|
||||
stopwatch.reset();
|
||||
}
|
||||
final localTask = _assetService.getLocalAssets(urgent: !isCacheValid);
|
||||
final remoteTask = _assetService.getRemoteAssets(
|
||||
etag: isCacheValid ? box.get(assetEtagKey) : null,
|
||||
);
|
||||
if (isCacheValid && state.allAssets.isEmpty) {
|
||||
await _updateAssetsState(await _assetCacheService.get(), cache: false);
|
||||
log.info(
|
||||
"Reading assets ${state.allAssets.length} from cache: ${stopwatch.elapsedMilliseconds}ms",
|
||||
);
|
||||
stopwatch.reset();
|
||||
}
|
||||
|
||||
int remoteBegin = state.allAssets.indexWhere((a) => a.isRemote);
|
||||
remoteBegin = remoteBegin == -1 ? state.allAssets.length : remoteBegin;
|
||||
|
|
@ -184,7 +193,7 @@ class AssetNotifier extends StateNotifier<AssetsState> {
|
|||
_updateAssetsState([]);
|
||||
}
|
||||
|
||||
onNewAssetUploaded(AssetResponseDto newAsset) {
|
||||
void onNewAssetUploaded(Asset newAsset) {
|
||||
final int i = state.allAssets.indexWhere(
|
||||
(a) =>
|
||||
a.isRemote ||
|
||||
|
|
@ -192,13 +201,13 @@ class AssetNotifier extends StateNotifier<AssetsState> {
|
|||
);
|
||||
|
||||
if (i == -1 || state.allAssets[i].deviceAssetId != newAsset.deviceAssetId) {
|
||||
_updateAssetsState([...state.allAssets, Asset.remote(newAsset)]);
|
||||
_updateAssetsState([...state.allAssets, newAsset]);
|
||||
} else {
|
||||
// order is important to keep all local-only assets at the beginning!
|
||||
_updateAssetsState([
|
||||
...state.allAssets.slice(0, i),
|
||||
...state.allAssets.slice(i + 1),
|
||||
Asset.remote(newAsset),
|
||||
newAsset,
|
||||
]);
|
||||
// TODO here is a place to unify local/remote assets by replacing the
|
||||
// local-only asset in the state with a local&remote asset
|
||||
|
|
@ -230,7 +239,7 @@ class AssetNotifier extends StateNotifier<AssetsState> {
|
|||
// Delete asset from device
|
||||
for (final Asset asset in assetsToDelete) {
|
||||
if (asset.isLocal) {
|
||||
local.add(asset.id);
|
||||
local.add(asset.localId!);
|
||||
} else if (asset.deviceId == deviceId) {
|
||||
// Delete asset on device if it is still present
|
||||
var localAsset = await AssetEntity.fromId(asset.deviceAssetId);
|
||||
|
|
@ -252,8 +261,7 @@ class AssetNotifier extends StateNotifier<AssetsState> {
|
|||
Future<Iterable<String>> _deleteRemoteAssets(
|
||||
Set<Asset> assetsToDelete,
|
||||
) async {
|
||||
final Iterable<AssetResponseDto> remote =
|
||||
assetsToDelete.where((e) => e.isRemote).map((e) => e.remote!);
|
||||
final Iterable<Asset> remote = assetsToDelete.where((e) => e.isRemote);
|
||||
final List<DeleteAssetResponseDto> deleteAssetResult =
|
||||
await _assetService.deleteAssets(remote) ?? [];
|
||||
return deleteAssetResult
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import 'package:hive/hive.dart';
|
|||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/constants/hive_box.dart';
|
||||
import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
|
||||
import 'package:immich_mobile/shared/models/asset.dart';
|
||||
import 'package:immich_mobile/shared/providers/asset.provider.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
|
@ -91,14 +92,7 @@ class WebsocketNotifier extends StateNotifier<WebsocketState> {
|
|||
state = WebsocketState(isConnected: false, socket: null);
|
||||
});
|
||||
|
||||
socket.on('on_upload_success', (data) {
|
||||
var jsonString = jsonDecode(data.toString());
|
||||
AssetResponseDto? newAsset = AssetResponseDto.fromJson(jsonString);
|
||||
|
||||
if (newAsset != null) {
|
||||
ref.watch(assetProvider.notifier).onNewAssetUploaded(newAsset);
|
||||
}
|
||||
});
|
||||
socket.on('on_upload_success', _handleOnUploadSuccess);
|
||||
} catch (e) {
|
||||
debugPrint("[WEBSOCKET] Catch Websocket Error - ${e.toString()}");
|
||||
}
|
||||
|
|
@ -122,14 +116,16 @@ class WebsocketNotifier extends StateNotifier<WebsocketState> {
|
|||
|
||||
listenUploadEvent() {
|
||||
debugPrint("Start listening to event on_upload_success");
|
||||
state.socket?.on('on_upload_success', (data) {
|
||||
var jsonString = jsonDecode(data.toString());
|
||||
AssetResponseDto? newAsset = AssetResponseDto.fromJson(jsonString);
|
||||
state.socket?.on('on_upload_success', _handleOnUploadSuccess);
|
||||
}
|
||||
|
||||
if (newAsset != null) {
|
||||
ref.watch(assetProvider.notifier).onNewAssetUploaded(newAsset);
|
||||
}
|
||||
});
|
||||
_handleOnUploadSuccess(dynamic data) {
|
||||
final jsonString = jsonDecode(data.toString());
|
||||
final dto = AssetResponseDto.fromJson(jsonString);
|
||||
if (dto != null) {
|
||||
final newAsset = Asset.remote(dto);
|
||||
ref.watch(assetProvider.notifier).onNewAssetUploaded(newAsset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
107
mobile/lib/shared/services/asset.service.dart
Normal file
107
mobile/lib/shared/services/asset.service.dart
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
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';
|
||||
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
||||
import 'package:immich_mobile/shared/services/api.service.dart';
|
||||
import 'package:immich_mobile/utils/openapi_extensions.dart';
|
||||
import 'package:immich_mobile/utils/tuple.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
final assetServiceProvider = Provider(
|
||||
(ref) => AssetService(
|
||||
ref.watch(apiServiceProvider),
|
||||
ref.watch(backupServiceProvider),
|
||||
ref.watch(backgroundServiceProvider),
|
||||
),
|
||||
);
|
||||
|
||||
class AssetService {
|
||||
final ApiService _apiService;
|
||||
final BackupService _backupService;
|
||||
final BackgroundService _backgroundService;
|
||||
final log = Logger('AssetService');
|
||||
|
||||
AssetService(this._apiService, this._backupService, this._backgroundService);
|
||||
|
||||
/// Returns `null` if the server state did not change, else list of assets
|
||||
Future<Pair<List<Asset>?, String?>> getRemoteAssets({String? etag}) async {
|
||||
try {
|
||||
final Pair<List<AssetResponseDto>, String?>? remote =
|
||||
await _apiService.assetApi.getAllAssetsWithETag(eTag: etag);
|
||||
if (remote == null) {
|
||||
return const Pair(null, null);
|
||||
}
|
||||
return Pair(
|
||||
remote.first.map(Asset.remote).toList(growable: false),
|
||||
remote.second,
|
||||
);
|
||||
} catch (e, stack) {
|
||||
log.severe('Error while getting remote assets', e, stack);
|
||||
return const Pair(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
/// if [urgent] is `true`, do not block by waiting on the background service
|
||||
/// to finish running. Returns `null` instead after a timeout.
|
||||
Future<List<Asset>?> getLocalAssets({bool urgent = false}) async {
|
||||
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);
|
||||
final String userId = Hive.box(userInfoBox).get(userIdKey);
|
||||
if (backupAlbumInfo != null) {
|
||||
return (await _backupService
|
||||
.buildUploadCandidates(backupAlbumInfo.deepCopy()))
|
||||
.map((e) => Asset.local(e, userId))
|
||||
.toList(growable: false);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("Error [_getLocalAssets] ${e.toString()}");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<Asset?> getAssetById(String assetId) async {
|
||||
try {
|
||||
final dto = await _apiService.assetApi.getAssetById(assetId);
|
||||
if (dto != null) {
|
||||
return Asset.remote(dto);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint("Error [getAssetById] ${e.toString()}");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<List<DeleteAssetResponseDto>?> deleteAssets(
|
||||
Iterable<Asset> deleteAssets,
|
||||
) async {
|
||||
try {
|
||||
final List<String> payload = [];
|
||||
|
||||
for (final asset in deleteAssets) {
|
||||
payload.add(asset.remoteId!);
|
||||
}
|
||||
|
||||
return await _apiService.assetApi
|
||||
.deleteAsset(DeleteAssetDto(ids: payload));
|
||||
} catch (e) {
|
||||
debugPrint("Error getAllAsset ${e.toString()}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
41
mobile/lib/shared/services/asset_cache.service.dart
Normal file
41
mobile/lib/shared/services/asset_cache.service.dart
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/shared/models/asset.dart';
|
||||
import 'package:immich_mobile/shared/services/json_cache.dart';
|
||||
|
||||
class AssetCacheService extends JsonCache<List<Asset>> {
|
||||
AssetCacheService() : super("asset_cache");
|
||||
|
||||
static Future<List<Map<String, dynamic>>> _computeSerialize(
|
||||
List<Asset> assets,
|
||||
) async {
|
||||
return assets.map((e) => e.toJson()).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
void put(List<Asset> data) async {
|
||||
putRawData(await compute(_computeSerialize, data));
|
||||
}
|
||||
|
||||
static Future<List<Asset>> _computeEncode(List<dynamic> data) async {
|
||||
return data.map((e) => Asset.fromJson(e)).whereNotNull().toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Asset>?> get() async {
|
||||
try {
|
||||
final mapList = await readRawData() as List<dynamic>;
|
||||
final responseData = await compute(_computeEncode, mapList);
|
||||
return responseData;
|
||||
} catch (e) {
|
||||
debugPrint(e.toString());
|
||||
await invalidate();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final assetCacheServiceProvider = Provider(
|
||||
(ref) => AssetCacheService(),
|
||||
);
|
||||
|
|
@ -60,5 +60,5 @@ abstract class JsonCache<T> {
|
|||
}
|
||||
|
||||
void put(T data);
|
||||
Future<T> get();
|
||||
Future<T?> get();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import 'package:flutter/material.dart';
|
|||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/shared/models/asset.dart';
|
||||
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
import 'api.service.dart';
|
||||
|
|
@ -25,11 +24,10 @@ class ShareService {
|
|||
final downloadedXFiles = assets.map<Future<XFile>>((asset) async {
|
||||
if (asset.isRemote) {
|
||||
final tempDir = await getTemporaryDirectory();
|
||||
final fileName = basename(asset.remote!.originalPath);
|
||||
final fileName = asset.fileName;
|
||||
final tempFile = await File('${tempDir.path}/$fileName').create();
|
||||
final res = await _apiService.assetApi.downloadFileWithHttpInfo(
|
||||
asset.remote!.id,
|
||||
);
|
||||
final res = await _apiService.assetApi
|
||||
.downloadFileWithHttpInfo(asset.remoteId!);
|
||||
tempFile.writeAsBytesSync(res.bodyBytes);
|
||||
return XFile(tempFile.path);
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:immich_mobile/constants/hive_box.dart';
|
||||
import 'package:immich_mobile/shared/models/asset.dart';
|
||||
|
|
@ -15,13 +16,28 @@ class ImmichImage extends StatelessWidget {
|
|||
this.useGrayBoxPlaceholder = false,
|
||||
super.key,
|
||||
});
|
||||
final Asset asset;
|
||||
final Asset? asset;
|
||||
final bool useGrayBoxPlaceholder;
|
||||
final double width;
|
||||
final double height;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (this.asset == null) {
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.grey,
|
||||
),
|
||||
child: SizedBox(
|
||||
width: width,
|
||||
height: height,
|
||||
child: const Center(
|
||||
child: Icon(Icons.no_photography),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
final Asset asset = this.asset!;
|
||||
if (asset.isLocal) {
|
||||
return Image(
|
||||
image: AssetEntityImageProvider(
|
||||
|
|
@ -49,7 +65,16 @@ class ImmichImage extends StatelessWidget {
|
|||
));
|
||||
},
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
debugPrint("Error getting thumb for assetId=${asset.id}: $error");
|
||||
if (error is PlatformException &&
|
||||
error.code == "The asset not found!") {
|
||||
debugPrint(
|
||||
"Asset ${asset.localId} does not exist anymore on device!",
|
||||
);
|
||||
} else {
|
||||
debugPrint(
|
||||
"Error getting thumb for assetId=${asset.localId}: $error",
|
||||
);
|
||||
}
|
||||
return Icon(
|
||||
Icons.image_not_supported_outlined,
|
||||
color: Theme.of(context).primaryColor,
|
||||
|
|
@ -57,12 +82,12 @@ class ImmichImage extends StatelessWidget {
|
|||
},
|
||||
);
|
||||
}
|
||||
final String token = Hive.box(userInfoBox).get(accessTokenKey);
|
||||
final String thumbnailRequestUrl = getThumbnailUrl(asset.remote!);
|
||||
final String? token = Hive.box(userInfoBox).get(accessTokenKey);
|
||||
final String thumbnailRequestUrl = getThumbnailUrl(asset);
|
||||
return CachedNetworkImage(
|
||||
imageUrl: thumbnailRequestUrl,
|
||||
httpHeaders: {"Authorization": "Bearer $token"},
|
||||
cacheKey: getThumbnailCacheKey(asset.remote!),
|
||||
cacheKey: getThumbnailCacheKey(asset),
|
||||
width: width,
|
||||
height: height,
|
||||
// keeping memCacheWidth, memCacheHeight, maxWidthDiskCache and
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue