2024-08-19 13:37:15 -04:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
|
import 'package:immich_mobile/entities/asset.entity.dart';
|
|
|
|
|
import 'package:immich_mobile/providers/api.provider.dart';
|
2024-09-24 14:50:21 +02:00
|
|
|
import 'package:immich_mobile/repositories/asset.repository.dart';
|
2024-08-19 13:37:15 -04:00
|
|
|
import 'package:immich_mobile/services/api.service.dart';
|
|
|
|
|
import 'package:openapi/api.dart';
|
2025-09-12 18:56:00 -04:00
|
|
|
import 'package:immich_mobile/utils/debug_print.dart';
|
2024-08-19 13:37:15 -04:00
|
|
|
|
|
|
|
|
class StackService {
|
2025-06-25 13:06:24 +05:30
|
|
|
const StackService(this._api, this._assetRepository);
|
2024-08-19 13:37:15 -04:00
|
|
|
|
|
|
|
|
final ApiService _api;
|
2025-06-21 17:41:02 -05:00
|
|
|
final AssetRepository _assetRepository;
|
2024-08-19 13:37:15 -04:00
|
|
|
|
|
|
|
|
Future<StackResponseDto?> getStack(String stackId) async {
|
|
|
|
|
try {
|
|
|
|
|
return _api.stacksApi.getStack(stackId);
|
|
|
|
|
} catch (error) {
|
2025-09-12 18:56:00 -04:00
|
|
|
dPrint(() => "Error while fetching stack: $error");
|
2024-08-19 13:37:15 -04:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<StackResponseDto?> createStack(List<String> assetIds) async {
|
|
|
|
|
try {
|
2025-07-29 00:34:03 +05:30
|
|
|
return _api.stacksApi.createStack(StackCreateDto(assetIds: assetIds));
|
2024-08-19 13:37:15 -04:00
|
|
|
} catch (error) {
|
2025-09-12 18:56:00 -04:00
|
|
|
dPrint(() => "Error while creating stack: $error");
|
2024-08-19 13:37:15 -04:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-29 00:34:03 +05:30
|
|
|
Future<StackResponseDto?> updateStack(String stackId, String primaryAssetId) async {
|
2024-08-19 13:37:15 -04:00
|
|
|
try {
|
2025-07-29 00:34:03 +05:30
|
|
|
return await _api.stacksApi.updateStack(stackId, StackUpdateDto(primaryAssetId: primaryAssetId));
|
2024-08-19 13:37:15 -04:00
|
|
|
} catch (error) {
|
2025-09-12 18:56:00 -04:00
|
|
|
dPrint(() => "Error while updating stack children: $error");
|
2024-08-19 13:37:15 -04:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> deleteStack(String stackId, List<Asset> assets) async {
|
|
|
|
|
try {
|
|
|
|
|
await _api.stacksApi.deleteStack(stackId);
|
|
|
|
|
|
|
|
|
|
// Update local database to trigger rerendering
|
|
|
|
|
final List<Asset> removeAssets = [];
|
|
|
|
|
for (final asset in assets) {
|
|
|
|
|
asset.stackId = null;
|
|
|
|
|
asset.stackPrimaryAssetId = null;
|
|
|
|
|
asset.stackCount = 0;
|
|
|
|
|
|
|
|
|
|
removeAssets.add(asset);
|
|
|
|
|
}
|
2025-07-25 08:07:22 +05:30
|
|
|
await _assetRepository.transaction(() => _assetRepository.updateAll(removeAssets));
|
2024-08-19 13:37:15 -04:00
|
|
|
} catch (error) {
|
2025-09-12 18:56:00 -04:00
|
|
|
dPrint(() => "Error while deleting stack: $error");
|
2024-08-19 13:37:15 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final stackServiceProvider = Provider(
|
2025-07-29 00:34:03 +05:30
|
|
|
(ref) => StackService(ref.watch(apiServiceProvider), ref.watch(assetRepositoryProvider)),
|
2024-08-19 13:37:15 -04:00
|
|
|
);
|