64 lines
1.9 KiB
Dart
64 lines
1.9 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/services.dart' show rootBundle;
|
|
|
|
import '../models/lenormand_pairs.dart';
|
|
|
|
class OghamFedaRepository {
|
|
static final OghamFedaRepository _instance = OghamFedaRepository._internal();
|
|
factory OghamFedaRepository() => _instance;
|
|
OghamFedaRepository._internal();
|
|
|
|
final Map<int, LenormandCardEntry> _byN = {};
|
|
final Map<String, LenormandCardEntry> _byId = {};
|
|
bool _loaded = false;
|
|
|
|
LenormandCardEntry? byNumber(int n) => _byN[n];
|
|
LenormandCardEntry? byCanonId(String id) => _byId[id];
|
|
List<LenormandCardEntry> get allFedas => _byN.values.toList()..sort((a, b) => a.n.compareTo(b.n));
|
|
|
|
Future<void> init() async {
|
|
if (_loaded && _byN.isNotEmpty) return;
|
|
Map<String, dynamic>? decoded;
|
|
final cwd = Directory.current.path;
|
|
for (final path in {
|
|
'assets/data/ogham_feda.json',
|
|
'../assets/data/ogham_feda.json',
|
|
'$cwd/assets/data/ogham_feda.json',
|
|
}) {
|
|
try {
|
|
final file = File(path);
|
|
if (file.existsSync()) {
|
|
decoded = json.decode(file.readAsStringSync()) as Map<String, dynamic>;
|
|
break;
|
|
}
|
|
} catch (e) {
|
|
debugPrint('ogham_feda.json skip $path: $e');
|
|
}
|
|
}
|
|
if (decoded == null) {
|
|
try {
|
|
decoded = json.decode(await rootBundle.loadString('assets/data/ogham_feda.json'))
|
|
as Map<String, dynamic>;
|
|
} catch (e) {
|
|
debugPrint('Could not load ogham_feda.json: $e');
|
|
}
|
|
}
|
|
if (decoded == null) {
|
|
_loaded = true;
|
|
return;
|
|
}
|
|
_byN.clear();
|
|
_byId.clear();
|
|
for (final raw in (decoded['cards'] as List? ?? const [])) {
|
|
if (raw is! Map<String, dynamic>) continue;
|
|
final card = LenormandCardEntry.fromMap(raw);
|
|
_byN[card.n] = card;
|
|
_byId[card.id] = card;
|
|
}
|
|
_loaded = true;
|
|
}
|
|
}
|