import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:immich_mobile/entities/asset.entity.dart'; import 'package:immich_mobile/providers/api.provider.dart'; import 'package:immich_mobile/providers/db.provider.dart'; import 'package:immich_mobile/services/api.service.dart'; import 'package:isar/isar.dart'; import 'package:openapi/api.dart'; class StackService { StackService(this._api, this._db); final ApiService _api; final Isar _db; Future getStack(String stackId) async { try { return _api.stacksApi.getStack(stackId); } catch (error) { debugPrint("Error while fetching stack: $error"); } return null; } Future createStack(List assetIds) async { try { return _api.stacksApi.createStack( StackCreateDto(assetIds: assetIds), ); } catch (error) { debugPrint("Error while creating stack: $error"); } return null; } Future updateStack( String stackId, String primaryAssetId, ) async { try { return await _api.stacksApi.updateStack( stackId, StackUpdateDto(primaryAssetId: primaryAssetId), ); } catch (error) { debugPrint("Error while updating stack children: $error"); } return null; } Future deleteStack(String stackId, List assets) async { try { await _api.stacksApi.deleteStack(stackId); // Update local database to trigger rerendering final List removeAssets = []; for (final asset in assets) { asset.stackId = null; asset.stackPrimaryAssetId = null; asset.stackCount = 0; removeAssets.add(asset); } _db.writeTxn(() async { await _db.assets.putAll(removeAssets); }); } catch (error) { debugPrint("Error while deleting stack: $error"); } } } final stackServiceProvider = Provider( (ref) => StackService( ref.watch(apiServiceProvider), ref.watch(dbProvider), ), );