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
107 lines
3 KiB
Dart
107 lines
3 KiB
Dart
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';
|
|
|
|
@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);
|
|
}
|
|
}
|
|
|
|
// 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('');
|
|
}
|
|
}
|
|
}
|