import 'dart:async'; import 'dart:io'; import 'package:injectable/injectable.dart'; import 'package:mnemo_cards_backend/database/database.dart'; import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart' hide VoiceModel; import 'package:mnemo_cards_common/mnemo_cards_common.dart'; import 'pack_dto_converter.dart'; import 'card_pack_drift_extension.dart'; import '../repository/export.dart'; @lazySingleton class PackManager { final PackRepository _packRepository; final PackDtoConverter packDtoConverter; final AppDatabase _db; PackManager(this._packRepository, this.packDtoConverter, this._db); Future> listPacksPreviews( UserModel? userModel, Map? params, ) async { // Get packs from Repository final packModels = await _packRepository.getAllPacks( enabledOnly: true, orderByField: 'order', ); // Convert to Drift models for toPreviewDto (temporary, until we refactor DTO conversion) // TODO: Refactor to use PackRepository and remove direct DAO access final packs = await Future.wait( packModels.map((packModel) async { final pack = await _db.packDao.getPackById(packModel.id!); if (pack == null) { throw StateError('Pack not found: ${packModel.id}'); } return pack; }), ); return (await Future.wait( packs.map((pack) async { final dto = await pack.toPreviewDto(userModel); if (!pack.enabled) { return dto.copyWith(subtitle: 'DISABLED ${dto.subtitle}'); } return dto; }), )).toList(); } Future getPack(String id) async { return await _packRepository.getPackById(id); } Future> getCards(String packId) async { return await _packRepository.getPackCards(packId); } Future getCard(String id) async { return await _packRepository.getCardById(id); } Future getVoice(String id) async { return await _packRepository.getVoiceById(id); } Future> getVoices(String cardId) async { return await _packRepository.getCardVoices(cardId); } Future getPackDto(String id, UserModel? userModel) async { final packModel = await getPack(id); if (packModel == null) { throw StateError('Pack not found'); } // Get pack from DAO for DTO conversion (temporary, until we refactor) // TODO: Refactor to use PackRepository and remove direct DAO access final pack = await _db.packDao.getPackById(id); if (pack == null) { throw StateError('Pack not found'); } // TODO: Refactor to use PackRepository and remove direct DAO access final cardsDrift = await _db.packDao.getPackCards(id); final voices = []; for (final card in cardsDrift) { voices.addAll(await getVoices(card.id)); } return await pack.toDto(cardsDrift, voices, userModel); } } // Static utility functions for PackManager class PackManagerUtils { static Directory get assetsDirectory { String mainPath = Platform.resolvedExecutable; if ((Platform.isMacOS || Platform.isLinux) && !Platform.script.toString().contains('StudioProjects')) { mainPath = mainPath.substring(0, mainPath.lastIndexOf("/")); var dir = Directory("$mainPath/../data"); if (dir.existsSync()) { return dir; } dir = Directory("$mainPath/data"); if (dir.existsSync()) { return dir; } throw Exception('No asset dir! $mainPath'); } if (Platform.isMacOS) { mainPath = mainPath.substring(0, mainPath.lastIndexOf("/")); return Directory( '/Users/dmitry/StudioProjects/mnemo_cards/mnemo_cards_backend/data', ); } else if (Platform.isWindows) { mainPath = mainPath.substring(0, mainPath.lastIndexOf("\\")); return Directory("$mainPath/data/flutter_assets/data"); } else { return Directory(''); } } }