1
0
mirror of https://github.com/immich-app/immich.git synced 2024-12-26 10:50:29 +02:00

chore(mobile): Use bulk update endpoint (#9110)

* chore(mobile): bulk update

* deterministic update on client
This commit is contained in:
Alex 2024-04-27 13:10:27 -05:00 committed by GitHub
parent 90882a9b26
commit cf01ec1eb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,3 +1,5 @@
// ignore_for_file: null_argument_to_non_null_type
import 'dart:async'; import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -168,62 +170,110 @@ class AssetService {
return a; return a;
} }
Future<List<Asset?>> updateAssets( Future<void> updateAssets(
List<Asset> assets, List<Asset> assets,
UpdateAssetDto updateAssetDto, UpdateAssetDto updateAssetDto,
) async { ) async {
final List<AssetResponseDto?> dtos = await Future.wait( return await _apiService.assetApi.updateAssets(
assets.map( AssetBulkUpdateDto(
(a) => _apiService.assetApi.updateAsset(a.remoteId!, updateAssetDto), ids: assets.map((e) => e.remoteId!).toList(),
dateTimeOriginal: updateAssetDto.dateTimeOriginal,
isFavorite: updateAssetDto.isFavorite,
isArchived: updateAssetDto.isArchived,
latitude: updateAssetDto.latitude,
longitude: updateAssetDto.longitude,
), ),
); );
bool allInDb = true;
for (int i = 0; i < assets.length; i++) {
final dto = dtos[i], old = assets[i];
if (dto != null) {
final remote = Asset.remote(dto);
if (old.canUpdate(remote)) {
assets[i] = old.updatedCopy(remote);
}
allInDb &= assets[i].isInDb;
}
}
final toUpdate = allInDb ? assets : assets.where((e) => e.isInDb).toList();
await _syncService.upsertAssetsWithExif(toUpdate);
return assets;
} }
Future<List<Asset?>> changeFavoriteStatus( Future<List<Asset?>> changeFavoriteStatus(
List<Asset> assets, List<Asset> assets,
bool isFavorite, bool isFavorite,
) { ) async {
return updateAssets(assets, UpdateAssetDto(isFavorite: isFavorite)); try {
await updateAssets(assets, UpdateAssetDto(isFavorite: isFavorite));
for (var element in assets) {
element.isFavorite = isFavorite;
} }
Future<List<Asset?>> changeArchiveStatus(List<Asset> assets, bool isArchive) { await _syncService.upsertAssetsWithExif(assets);
return updateAssets(assets, UpdateAssetDto(isArchived: isArchive));
return assets;
} catch (error, stack) {
log.severe("Error while changing favorite status", error, stack);
return Future.value(null);
}
}
Future<List<Asset?>> changeArchiveStatus(
List<Asset> assets,
bool isArchived,
) async {
try {
await updateAssets(assets, UpdateAssetDto(isArchived: isArchived));
for (var element in assets) {
element.isArchived = isArchived;
}
await _syncService.upsertAssetsWithExif(assets);
return assets;
} catch (error, stack) {
log.severe("Error while changing archive status", error, stack);
return Future.value(null);
}
} }
Future<List<Asset?>> changeDateTime( Future<List<Asset?>> changeDateTime(
List<Asset> assets, List<Asset> assets,
String updatedDt, String updatedDt,
) { ) async {
return updateAssets( try {
await updateAssets(
assets, assets,
UpdateAssetDto(dateTimeOriginal: updatedDt), UpdateAssetDto(dateTimeOriginal: updatedDt),
); );
for (var element in assets) {
element.fileCreatedAt = DateTime.parse(updatedDt);
element.exifInfo?.dateTimeOriginal = DateTime.parse(updatedDt);
}
await _syncService.upsertAssetsWithExif(assets);
return assets;
} catch (error, stack) {
log.severe("Error while changing date/time status", error, stack);
return Future.value(null);
}
} }
Future<List<Asset?>> changeLocation( Future<List<Asset?>> changeLocation(
List<Asset> assets, List<Asset> assets,
LatLng location, LatLng location,
) { ) async {
return updateAssets( try {
await updateAssets(
assets, assets,
UpdateAssetDto( UpdateAssetDto(
latitude: location.latitude, latitude: location.latitude,
longitude: location.longitude, longitude: location.longitude,
), ),
); );
for (var element in assets) {
element.exifInfo?.lat = location.latitude;
element.exifInfo?.long = location.longitude;
}
await _syncService.upsertAssetsWithExif(assets);
return assets;
} catch (error, stack) {
log.severe("Error while changing location status", error, stack);
return Future.value(null);
}
} }
} }