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';
|
2023-02-06 09:13:32 +02:00
|
|
|
import 'package:immich_mobile/shared/models/album.dart';
|
2022-11-08 19:00:24 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2023-02-06 09:13:32 +02:00
|
|
|
import 'package:immich_mobile/shared/models/user.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);
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
Future<List<Album>?> getAlbums({required bool isShared}) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2023-02-06 09:13:32 +02:00
|
|
|
final dto = await _apiService.albumApi
|
2022-08-03 07:04:34 +02:00
|
|
|
.getAllAlbums(shared: isShared ? isShared : null);
|
2023-02-06 09:13:32 +02:00
|
|
|
return dto?.map(Album.remote).toList();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
Future<Album?> createAlbum(
|
2022-07-13 14:23:48 +02:00
|
|
|
String albumName,
|
2023-02-06 09:13:32 +02:00
|
|
|
Iterable<Asset> assets, [
|
|
|
|
Iterable<User> sharedUsers = const [],
|
|
|
|
]) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2023-02-06 09:13:32 +02:00
|
|
|
final dto = await _apiService.albumApi.createAlbum(
|
2022-07-13 14:23:48 +02:00
|
|
|
CreateAlbumDto(
|
|
|
|
albumName: albumName,
|
2023-02-06 09:13:32 +02:00
|
|
|
assetIds: assets.map((asset) => asset.remoteId!).toList(),
|
|
|
|
sharedWithUserIds: sharedUsers.map((e) => e.id).toList(),
|
2022-07-13 14:23:48 +02:00
|
|
|
),
|
|
|
|
);
|
2023-02-06 09:13:32 +02:00
|
|
|
return dto != null ? Album.remote(dto) : null;
|
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), ...
|
|
|
|
*/
|
2023-02-06 09:13:32 +02:00
|
|
|
String _getNextAlbumName(List<Album>? albums) {
|
2022-11-06 03:21:55 +02:00
|
|
|
const baseName = "Untitled";
|
|
|
|
|
|
|
|
if (albums != null) {
|
|
|
|
for (int round = 0; round < albums.length; round++) {
|
|
|
|
final proposedName = "$baseName${round == 0 ? "" : " ($round)"}";
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
if (albums.where((a) => a.name == proposedName).isEmpty) {
|
2022-11-06 03:21:55 +02:00
|
|
|
return proposedName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return baseName;
|
|
|
|
}
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
Future<Album?> createAlbumWithGeneratedName(
|
2022-11-08 19:00:24 +02:00
|
|
|
Iterable<Asset> assets,
|
2022-11-06 03:21:55 +02:00
|
|
|
) async {
|
|
|
|
return createAlbum(
|
2022-11-21 14:13:14 +02:00
|
|
|
_getNextAlbumName(await getAlbums(isShared: false)),
|
|
|
|
assets,
|
|
|
|
[],
|
|
|
|
);
|
2022-11-06 03:21:55 +02:00
|
|
|
}
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
Future<Album?> getAlbumDetail(String albumId) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2023-02-06 09:13:32 +02:00
|
|
|
final dto = await _apiService.albumApi.getAlbumInfo(albumId);
|
|
|
|
return dto != null ? Album.remote(dto) : null;
|
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,
|
2023-02-06 09:13:32 +02:00
|
|
|
Album album,
|
2022-07-13 14:23:48 +02:00
|
|
|
) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
var result = await _apiService.albumApi.addAssetsToAlbum(
|
2023-02-06 09:13:32 +02:00
|
|
|
album.remoteId!,
|
|
|
|
AddAssetsDto(assetIds: assets.map((asset) => asset.remoteId!).toList()),
|
2022-07-13 14:23:48 +02:00
|
|
|
);
|
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,
|
2023-02-06 09:13:32 +02:00
|
|
|
Album album,
|
2022-07-13 14:23:48 +02:00
|
|
|
) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
var result = await _apiService.albumApi.addUsersToAlbum(
|
2023-02-06 09:13:32 +02:00
|
|
|
album.remoteId!,
|
2022-07-13 14:23:48 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
Future<bool> deleteAlbum(Album album) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2023-02-06 09:13:32 +02:00
|
|
|
await _apiService.albumApi.deleteAlbum(album.remoteId!);
|
2022-04-24 04:08:45 +02:00
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error deleteAlbum ${e.toString()}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 09:13:32 +02:00
|
|
|
Future<bool> leaveAlbum(Album album) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2023-02-06 09:13:32 +02:00
|
|
|
await _apiService.albumApi.removeUserFromAlbum(album.remoteId!, "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(
|
2023-02-06 09:13:32 +02:00
|
|
|
Album album,
|
|
|
|
Iterable<Asset> assets,
|
2022-07-13 14:23:48 +02:00
|
|
|
) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
await _apiService.albumApi.removeAssetFromAlbum(
|
2023-02-06 09:13:32 +02:00
|
|
|
album.remoteId!,
|
|
|
|
RemoveAssetsDto(
|
|
|
|
assetIds: assets.map((e) => e.remoteId!).toList(growable: false),
|
|
|
|
),
|
2022-07-13 14:23:48 +02:00
|
|
|
);
|
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(
|
2023-02-06 09:13:32 +02:00
|
|
|
Album album,
|
2022-07-13 14:23:48 +02:00
|
|
|
String newAlbumTitle,
|
|
|
|
) async {
|
2022-04-29 06:46:37 +02:00
|
|
|
try {
|
2022-07-13 14:23:48 +02:00
|
|
|
await _apiService.albumApi.updateAlbumInfo(
|
2023-02-06 09:13:32 +02:00
|
|
|
album.remoteId!,
|
2022-07-13 14:23:48 +02:00
|
|
|
UpdateAlbumDto(
|
|
|
|
albumName: newAlbumTitle,
|
|
|
|
),
|
|
|
|
);
|
2023-02-06 09:13:32 +02:00
|
|
|
album.name = 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
|
|
|
}
|