2022-04-24 04:08:45 +02:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
2022-06-25 20:46:51 +02:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-04-24 04:08:45 +02:00
|
|
|
import 'package:immich_mobile/modules/sharing/models/shared_album.model.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/immich_asset.model.dart';
|
|
|
|
import 'package:immich_mobile/shared/services/network.service.dart';
|
|
|
|
|
2022-06-25 20:46:51 +02:00
|
|
|
final sharedAlbumServiceProvider =
|
|
|
|
Provider((ref) => SharedAlbumService(ref.watch(networkServiceProvider)));
|
|
|
|
|
2022-04-24 04:08:45 +02:00
|
|
|
class SharedAlbumService {
|
2022-06-25 20:46:51 +02:00
|
|
|
final NetworkService _networkService;
|
|
|
|
SharedAlbumService(this._networkService);
|
2022-04-24 04:08:45 +02:00
|
|
|
|
|
|
|
Future<List<SharedAlbum>> getAllSharedAlbum() async {
|
|
|
|
try {
|
2022-06-18 17:56:36 +02:00
|
|
|
var res = await _networkService.getRequest(url: 'album?shared=true');
|
2022-04-24 04:08:45 +02:00
|
|
|
List<dynamic> decodedData = jsonDecode(res.toString());
|
2022-06-18 17:56:36 +02:00
|
|
|
List<SharedAlbum> result =
|
|
|
|
List.from(decodedData.map((e) => SharedAlbum.fromMap(e)));
|
2022-04-24 04:08:45 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error getAllSharedAlbum ${e.toString()}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:56:36 +02:00
|
|
|
Future<bool> createSharedAlbum(String albumName, Set<ImmichAsset> assets,
|
|
|
|
List<String> sharedUserIds) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-06-18 17:56:36 +02:00
|
|
|
var res = await _networkService.postRequest(url: 'album', data: {
|
2022-04-24 04:08:45 +02:00
|
|
|
"albumName": albumName,
|
|
|
|
"sharedWithUserIds": sharedUserIds,
|
|
|
|
"assetIds": assets.map((asset) => asset.id).toList(),
|
|
|
|
});
|
|
|
|
|
2022-07-01 03:08:49 +02:00
|
|
|
return res != null;
|
2022-04-24 04:08:45 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error createSharedAlbum ${e.toString()}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<SharedAlbum> getAlbumDetail(String albumId) async {
|
|
|
|
try {
|
2022-06-18 17:56:36 +02:00
|
|
|
var res = await _networkService.getRequest(url: 'album/$albumId');
|
2022-04-24 04:08:45 +02:00
|
|
|
dynamic decodedData = jsonDecode(res.toString());
|
|
|
|
SharedAlbum result = SharedAlbum.fromMap(decodedData);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} catch (e) {
|
|
|
|
throw Exception('Error getAllSharedAlbum ${e.toString()}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:56:36 +02:00
|
|
|
Future<bool> addAdditionalAssetToAlbum(
|
|
|
|
Set<ImmichAsset> assets, String albumId) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-06-18 17:56:36 +02:00
|
|
|
var res =
|
|
|
|
await _networkService.putRequest(url: 'album/$albumId/assets', data: {
|
2022-04-24 04:08:45 +02:00
|
|
|
"albumId": albumId,
|
|
|
|
"assetIds": assets.map((asset) => asset.id).toList(),
|
|
|
|
});
|
|
|
|
|
2022-07-01 03:08:49 +02:00
|
|
|
return res != null;
|
2022-04-24 04:08:45 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error addAdditionalAssetToAlbum ${e.toString()}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:56:36 +02:00
|
|
|
Future<bool> addAdditionalUserToAlbum(
|
|
|
|
List<String> sharedUserIds, String albumId) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-06-18 17:56:36 +02:00
|
|
|
var res =
|
|
|
|
await _networkService.putRequest(url: 'album/$albumId/users', data: {
|
2022-04-24 04:08:45 +02:00
|
|
|
"sharedUserIds": sharedUserIds,
|
|
|
|
});
|
|
|
|
|
2022-07-01 03:08:49 +02:00
|
|
|
return res != 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-06-18 17:56:36 +02:00
|
|
|
Response res = await _networkService.deleteRequest(url: 'album/$albumId');
|
2022-04-24 04:08:45 +02:00
|
|
|
|
|
|
|
if (res.statusCode != 200) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error deleteAlbum ${e.toString()}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> leaveAlbum(String albumId) async {
|
|
|
|
try {
|
2022-06-18 17:56:36 +02:00
|
|
|
Response res =
|
|
|
|
await _networkService.deleteRequest(url: 'album/$albumId/user/me');
|
2022-04-24 04:08:45 +02:00
|
|
|
|
|
|
|
if (res.statusCode != 200) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error deleteAlbum ${e.toString()}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:56:36 +02:00
|
|
|
Future<bool> removeAssetFromAlbum(
|
|
|
|
String albumId, List<String> assetIds) async {
|
2022-04-24 04:08:45 +02:00
|
|
|
try {
|
2022-06-18 17:56:36 +02:00
|
|
|
Response res = await _networkService
|
|
|
|
.deleteRequest(url: 'album/$albumId/assets', data: {
|
2022-04-24 04:08:45 +02:00
|
|
|
"assetIds": assetIds,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res.statusCode != 200) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
|
|
|
String albumId, String ownerId, String newAlbumTitle) async {
|
2022-04-29 06:46:37 +02:00
|
|
|
try {
|
2022-06-18 17:56:36 +02:00
|
|
|
Response res =
|
|
|
|
await _networkService.patchRequest(url: 'album/$albumId/', data: {
|
2022-04-29 06:46:37 +02:00
|
|
|
"ownerId": ownerId,
|
|
|
|
"albumName": newAlbumTitle,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res.statusCode != 200) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("Error deleteAlbum ${e.toString()}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|