mnemo_cards/mnemo_cards_backend/lib/packs/pack_manager.dart

107 lines
3 KiB
Dart
Raw Normal View History

2025-11-16 11:25:27 +00:00
import 'dart:async';
import 'dart:io';
import 'package:injectable/injectable.dart';
2025-12-13 13:27:05 +00:00
import 'package:mnemo_cards_backend/database/database.dart';
import 'package:mnemo_cards_common_backend/mnemo_cards_common_backend.dart' hide VoiceModel;
2025-11-16 11:25:27 +00:00
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
import 'pack_dto_converter.dart';
2025-12-13 13:27:05 +00:00
import 'card_pack_drift_extension.dart';
2025-11-16 11:25:27 +00:00
@lazySingleton
class PackManager {
2025-12-13 13:27:05 +00:00
final AppDatabase _db;
2025-11-16 11:25:27 +00:00
final PackDtoConverter packDtoConverter;
2025-12-13 13:27:05 +00:00
PackManager(this._db, this.packDtoConverter);
2025-11-16 11:25:27 +00:00
Future<List<CardPackPreviewDto>> listPacksPreviews(
UserModel? userModel,
Map<String, String>? params,
) async {
2025-12-13 13:27:05 +00:00
// Get packs from Drift database
final packs = await _db.packDao.getAllPacks(
enabledOnly: true,
orderByField: 'order',
);
return (await Future.wait(packs.map(
(pack) async {
final dto = await pack.toPreviewDto(userModel);
if (!pack.enabled) {
2025-11-16 11:25:27 +00:00
return dto.copyWith(subtitle: 'DISABLED ${dto.subtitle}');
}
return dto;
},
)))
2025-12-13 13:27:05 +00:00
.toList();
2025-11-16 11:25:27 +00:00
}
2025-12-13 20:55:50 +00:00
Future<CardPack?> getPack(String id) async {
2025-12-13 13:27:05 +00:00
return await _db.packDao.getPackById(id);
2025-11-16 11:25:27 +00:00
}
2025-12-13 20:55:50 +00:00
Future<List<GameCard>> getCards(String packId) async {
2025-12-13 13:27:05 +00:00
return await _db.packDao.getPackCards(packId);
2025-11-16 11:25:27 +00:00
}
2025-12-13 20:55:50 +00:00
Future<GameCard?> getCard(String id) async {
2025-12-13 13:27:05 +00:00
return await _db.packDao.getCardById(id);
2025-11-16 11:25:27 +00:00
}
2025-12-13 20:55:50 +00:00
Future<VoiceModel?> getVoice(String id) async {
2025-12-13 14:48:00 +00:00
return await _db.packDao.getVoiceById(id);
2025-11-16 11:25:27 +00:00
}
2025-12-13 20:55:50 +00:00
Future<List<VoiceModel>> getVoices(String cardId) async {
2025-12-13 14:48:00 +00:00
return await _db.packDao.getCardVoices(cardId);
2025-11-16 11:25:27 +00:00
}
2025-12-13 20:55:50 +00:00
Future<CardPackDto> getPackDto(String id, UserModel? userModel) async {
2025-12-13 13:27:05 +00:00
final pack = await getPack(id);
if (pack == null) {
throw StateError('Pack not found');
2025-11-16 11:25:27 +00:00
}
2025-12-13 13:27:05 +00:00
final cards = await getCards(id);
final voices = <VoiceModel>[];
for (final card in cards) {
voices.addAll(await getVoices(card.id));
2025-11-16 11:25:27 +00:00
}
2025-12-13 13:27:05 +00:00
return await pack.toDto(cards, voices, userModel);
2025-11-16 11:25:27 +00:00
}
2025-12-13 13:27:05 +00:00
}
2025-11-16 11:25:27 +00:00
2025-12-13 13:27:05 +00:00
// Static utility functions for PackManager
class PackManagerUtils {
2025-11-16 11:25:27 +00:00
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('');
}
}
2025-12-13 13:27:05 +00:00
}