1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2024-12-29 15:11:58 +00: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 'package:flutter/material.dart';
@ -168,62 +170,110 @@ class AssetService {
return a;
}
Future<List<Asset?>> updateAssets(
Future<void> updateAssets(
List<Asset> assets,
UpdateAssetDto updateAssetDto,
) async {
final List<AssetResponseDto?> dtos = await Future.wait(
assets.map(
(a) => _apiService.assetApi.updateAsset(a.remoteId!, updateAssetDto),
return await _apiService.assetApi.updateAssets(
AssetBulkUpdateDto(
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(
List<Asset> assets,
bool isFavorite,
) {
return updateAssets(assets, UpdateAssetDto(isFavorite: isFavorite));
) async {
try {
await updateAssets(assets, UpdateAssetDto(isFavorite: isFavorite));
for (var element in assets) {
element.isFavorite = isFavorite;
}
await _syncService.upsertAssetsWithExif(assets);
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 isArchive) {
return updateAssets(assets, UpdateAssetDto(isArchived: isArchive));
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(
List<Asset> assets,
String updatedDt,
) {
return updateAssets(
assets,
UpdateAssetDto(dateTimeOriginal: updatedDt),
);
) async {
try {
await updateAssets(
assets,
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(
List<Asset> assets,
LatLng location,
) {
return updateAssets(
assets,
UpdateAssetDto(
latitude: location.latitude,
longitude: location.longitude,
),
);
) async {
try {
await updateAssets(
assets,
UpdateAssetDto(
latitude: location.latitude,
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);
}
}
}