mnemo_cards/mnemo_cards_backend/lib/packs/pack_manager.dart
Dmitry 550b56276e
Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Mobile App CI / test (push) Waiting to run
Mobile App CI / build-android (push) Blocked by required conditions
Mobile App CI / build-ios (push) Blocked by required conditions
Web App CI / test (push) Waiting to run
Web App CI / build (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions
Deploy Telegram Bot / Deploy Telegram Bot (push) Waiting to run
postgress fix
2025-12-13 23:55:50 +03:00

157 lines
No EOL
4.4 KiB
Dart

import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'dart:typed_data';
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 'pack_manager_extensions.dart';
import 'card_pack_drift_extension.dart';
@lazySingleton
class PackManager {
final AppDatabase _db;
final PackDtoConverter packDtoConverter;
PackManager(this._db, this.packDtoConverter);
Future<List<CardPackPreviewDto>> listPacksPreviews(
UserModel? userModel,
Map<String, String>? params,
) async {
// 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) {
return dto.copyWith(subtitle: 'DISABLED ${dto.subtitle}');
}
return dto;
},
)))
.toList();
}
Future<CardPack?> getPack(String id) async {
return await _db.packDao.getPackById(id);
}
Future<List<GameCard>> getCards(String packId) async {
return await _db.packDao.getPackCards(packId);
}
Future<GameCard?> getCard(String id) async {
return await _db.packDao.getCardById(id);
}
Future<VoiceModel?> getVoice(String id) async {
return await _db.packDao.getVoiceById(id);
}
Future<List<VoiceModel>> getVoices(String cardId) async {
return await _db.packDao.getCardVoices(cardId);
}
Future<CardPackDto> getPackDto(String id, UserModel? userModel) async {
final pack = await getPack(id);
if (pack == null) {
throw StateError('Pack not found');
}
final cards = await getCards(id);
final voices = <VoiceModel>[];
for (final card in cards) {
voices.addAll(await getVoices(card.id));
}
return await pack.toDto(cards, voices, userModel);
}
Future<List<String>> getPackPreviewImages(String packId) async {
final pack = await getPack(packId);
if (pack == null) {
throw StateError('Pack not found');
}
final previewCards = await _db.packDao.getPreviewCards(packId);
final cards = previewCards.isNotEmpty ? previewCards : (await getCards(packId)).take(6).toList();
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);
}
}
}
// If we don't have enough preview images, fill with empty strings
while (images.length < 6) {
images.add('');
}
return images;
}
Future<Map<String, Uint8List>> getPackImages(String packId) async {
final cards = await getCards(packId);
final result = <String, Uint8List>{};
for (final card in cards) {
if (card.image.isNotEmpty) {
try {
result[card.id.toString()] =
File('${PackManagerUtils.assetsDirectory.path}/cards/${card.image}')
.readAsBytesSync();
} catch (e, s) {
log('error while reading image', error: e, stackTrace: s);
}
}
}
return result;
}
}
// 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('');
}
}
}