2022-04-24 04:08:45 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.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/shared/models/asset.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';
|
|
|
|
import 'package:openapi/api.dart';
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2022-08-03 07:04:34 +02:00
|
|
|
final albumServiceProvider = Provider(
|
|
|
|
(ref) => AlbumService(
|
2022-07-13 14:23:48 +02:00
|
|
|
ref.watch(apiServiceProvider),
|
|
|
|
),
|
|
|
|
);
|
2022-06-25 20:46:51 +02:00
|
|
|
|
2022-08-03 07:04:34 +02:00
|
|
|
class AlbumService {
|
2022-07-13 14:23:48 +02:00
|
|
|
final ApiService _apiService;
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2022-08-03 07:04:34 +02:00
|
|
|
AlbumService(this._apiService);
|
|
|
|
|
|
|
|
Future<List<AlbumResponseDto>?> getAlbums({required bool isShared}) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-08-03 07:04:34 +02:00
|
|
|
return await _apiService.albumApi
|
|
|
|
.getAllAlbums(shared: isShared ? isShared : null);
|
2022-04-24 04:08:45 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error getAllSharedAlbum ${e.toString()}");
|
2022-07-13 14:23:48 +02:00
|
|
|
return null;
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 07:04:34 +02:00
|
|
|
Future<AlbumResponseDto?> createAlbum(
|
2022-07-13 14:23:48 +02:00
|
|
|
String albumName,
|
2022-11-08 19:00:24 +02:00
|
|
|
Iterable<Asset> assets,
|
2022-07-13 14:23:48 +02:00
|
|
|
List<String> sharedUserIds,
|
|
|
|
) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-08-03 07:04:34 +02:00
|
|
|
return await _apiService.albumApi.createAlbum(
|
2022-07-13 14:23:48 +02:00
|
|
|
CreateAlbumDto(
|
|
|
|
albumName: albumName,
|
|
|
|
assetIds: assets.map((asset) => asset.id).toList(),
|
|
|
|
sharedWithUserIds: sharedUserIds,
|
|
|
|
),
|
|
|
|
);
|
2022-04-24 04:08:45 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error createSharedAlbum ${e.toString()}");
|
2022-08-03 07:04:34 +02:00
|
|
|
return null;
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 03:21:55 +02:00
|
|
|
/*
|
|
|
|
* Creates names like Untitled, Untitled (1), Untitled (2), ...
|
|
|
|
*/
|
|
|
|
String _getNextAlbumName(List<AlbumResponseDto>? albums) {
|
|
|
|
const baseName = "Untitled";
|
|
|
|
|
|
|
|
if (albums != null) {
|
|
|
|
for (int round = 0; round < albums.length; round++) {
|
|
|
|
final proposedName = "$baseName${round == 0 ? "" : " ($round)"}";
|
|
|
|
|
|
|
|
if (albums.where((a) => a.albumName == proposedName).isEmpty) {
|
|
|
|
return proposedName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return baseName;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<AlbumResponseDto?> createAlbumWithGeneratedName(
|
2022-11-08 19:00:24 +02:00
|
|
|
Iterable<Asset> assets,
|
2022-11-06 03:21:55 +02:00
|
|
|
) async {
|
|
|
|
return createAlbum(
|
|
|
|
_getNextAlbumName(await getAlbums(isShared: false)), assets, []);
|
|
|
|
}
|
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
Future<AlbumResponseDto?> getAlbumDetail(String albumId) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
return await _apiService.albumApi.getAlbumInfo(albumId);
|
2022-04-24 04:08:45 +02:00
|
|
|
} catch (e) {
|
2022-07-13 14:23:48 +02:00
|
|
|
debugPrint('Error [getAlbumDetail] ${e.toString()}');
|
|
|
|
return null;
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 03:21:55 +02:00
|
|
|
Future<AddAssetsResponseDto?> addAdditionalAssetToAlbum(
|
2022-11-08 19:00:24 +02:00
|
|
|
Iterable<Asset> assets,
|
2022-07-13 14:23:48 +02:00
|
|
|
String albumId,
|
|
|
|
) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
var result = await _apiService.albumApi.addAssetsToAlbum(
|
|
|
|
albumId,
|
|
|
|
AddAssetsDto(assetIds: assets.map((asset) => asset.id).toList()),
|
|
|
|
);
|
2022-11-06 03:21:55 +02:00
|
|
|
return result;
|
2022-04-24 04:08:45 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error addAdditionalAssetToAlbum ${e.toString()}");
|
2022-11-06 03:21:55 +02:00
|
|
|
return null;
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:56:36 +02:00
|
|
|
Future<bool> addAdditionalUserToAlbum(
|
2022-07-13 14:23:48 +02:00
|
|
|
List<String> sharedUserIds,
|
|
|
|
String albumId,
|
|
|
|
) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
var result = await _apiService.albumApi.addUsersToAlbum(
|
|
|
|
albumId,
|
|
|
|
AddUsersDto(sharedUserIds: sharedUserIds),
|
|
|
|
);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2022-07-13 14:23:48 +02:00
|
|
|
return result != null;
|
2022-04-24 04:08:45 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error addAdditionalUserToAlbum ${e.toString()}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> deleteAlbum(String albumId) async {
|
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
await _apiService.albumApi.deleteAlbum(albumId);
|
2022-04-24 04:08:45 +02:00
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error deleteAlbum ${e.toString()}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> leaveAlbum(String albumId) async {
|
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
await _apiService.albumApi.removeUserFromAlbum(albumId, "me");
|
2022-04-24 04:08:45 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error deleteAlbum ${e.toString()}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:56:36 +02:00
|
|
|
Future<bool> removeAssetFromAlbum(
|
2022-07-13 14:23:48 +02:00
|
|
|
String albumId,
|
|
|
|
List<String> assetIds,
|
|
|
|
) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
await _apiService.albumApi.removeAssetFromAlbum(
|
|
|
|
albumId,
|
|
|
|
RemoveAssetsDto(assetIds: assetIds),
|
|
|
|
);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error deleteAlbum ${e.toString()}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-04-29 06:46:37 +02:00
|
|
|
|
2022-06-18 17:56:36 +02:00
|
|
|
Future<bool> changeTitleAlbum(
|
2022-07-13 14:23:48 +02:00
|
|
|
String albumId,
|
|
|
|
String ownerId,
|
|
|
|
String newAlbumTitle,
|
|
|
|
) async {
|
2022-04-29 06:46:37 +02:00
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
await _apiService.albumApi.updateAlbumInfo(
|
|
|
|
albumId,
|
|
|
|
UpdateAlbumDto(
|
|
|
|
albumName: newAlbumTitle,
|
|
|
|
),
|
|
|
|
);
|
2022-04-29 06:46:37 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error deleteAlbum ${e.toString()}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|