mnemo_cards/mnemo_cards_backend/lib/packs/pack_manager.dart

157 lines
4.4 KiB
Dart
Raw Normal View History

2025-11-16 11:25:27 +00:00
import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'dart:typed_data';
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 'pack_manager_extensions.dart';
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 20:55:50 +00:00
Future<List<String>> getPackPreviewImages(String packId) async {
2025-12-13 13:27:05 +00:00
final pack = await getPack(packId);
if (pack == null) {
throw StateError('Pack not found');
}
2025-11-16 11:25:27 +00:00
2025-12-13 14:48:00 +00:00
final previewCards = await _db.packDao.getPreviewCards(packId);
final cards = previewCards.isNotEmpty ? previewCards : (await getCards(packId)).take(6).toList();
2025-11-16 11:25:27 +00:00
2025-12-13 13:27:05 +00:00
final images = <String>[];
for (final card in previewCards) {
if (card.image.isNotEmpty) {
try {
final image = await card.image.base64Image;
images.add(image);
} catch (e, s) {
log('error while reading image', error: e, stackTrace: s);
}
2025-11-16 11:25:27 +00:00
}
}
2025-12-13 13:27:05 +00:00
// If we don't have enough preview images, fill with empty strings
while (images.length < 6) {
images.add('');
2025-11-16 11:25:27 +00:00
}
2025-12-13 13:27:05 +00:00
return images;
2025-11-16 11:25:27 +00:00
}
2025-12-13 20:55:50 +00:00
Future<Map<String, Uint8List>> getPackImages(String packId) async {
2025-12-13 13:27:05 +00:00
final cards = await getCards(packId);
final result = <String, Uint8List>{};
2025-11-16 11:25:27 +00:00
2025-12-13 13:27:05 +00:00
for (final card in cards) {
if (card.image.isNotEmpty) {
2025-11-16 11:25:27 +00:00
try {
2025-12-13 13:27:05 +00:00
result[card.id.toString()] =
File('${PackManagerUtils.assetsDirectory.path}/cards/${card.image}')
.readAsBytesSync();
2025-11-16 11:25:27 +00:00
} catch (e, s) {
log('error while reading image', error: e, stackTrace: s);
}
2025-12-13 13:27:05 +00:00
}
}
return result;
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
}