diff --git a/mobile/lib/shared/services/asset.service.dart b/mobile/lib/shared/services/asset.service.dart index a9a65d2632..eaad5e5f01 100644 --- a/mobile/lib/shared/services/asset.service.dart +++ b/mobile/lib/shared/services/asset.service.dart @@ -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> updateAssets( + Future updateAssets( List assets, UpdateAssetDto updateAssetDto, ) async { - final List 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> changeFavoriteStatus( List 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> changeArchiveStatus(List assets, bool isArchive) { - return updateAssets(assets, UpdateAssetDto(isArchived: isArchive)); + Future> changeArchiveStatus( + List 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> changeDateTime( List 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> changeLocation( List 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); + } } }