PokeDex/lib/Session.dart

108 lines
2.5 KiB
Dart

import 'dart:math';
import 'package:pokedex/pokemon.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Callbacks {
Function()? mainPage;
Function()? filtersPage;
Function()? dexDisplay;
Function()? updateCheckPage;
Future<void> dispatch() async {
if (mainPage != null) mainPage!.call();
if (filtersPage != null) filtersPage!.call();
if (dexDisplay != null) dexDisplay!.call();
if (updateCheckPage != null) updateCheckPage!.call();
}
}
class SessionData {
static Future<void> initialize() async {
SharedPreferencesAsync spa = SharedPreferencesAsync();
darkMode = await spa.getBool("dark") ?? false;
}
static Future<void> finalize() async {
SharedPreferencesAsync spa = SharedPreferencesAsync();
await spa.setBool("dark", darkMode);
}
static Callbacks callbacks = Callbacks();
static bool isUpdateAvailable = false;
static bool enableDescription = true;
static bool darkMode = false;
static int highest = 9;
static int _cachedHighest = -1;
static final List<String> ALPHABET = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"EM",
"QM",
];
static int _lastMaxID = 0;
static void resetHighestGenCache() {
_cachedHighest = -1;
}
static String getRandomUnownSprite() {
int index = Random().nextInt(ALPHABET.length);
var digit = ALPHABET[index];
return "assets/sprites/unown-${digit.toLowerCase()}.png";
}
static int highestGenID() {
if (_lastMaxID != Pokemon.values.length) resetHighestGenCache();
if (_cachedHighest != -1) return _cachedHighest;
List<Generation> gens = [];
if (highest >= 9) gens.add(Generation.Nine);
if (highest >= 8) gens.add(Generation.Eight);
if (highest >= 7) gens.add(Generation.Seven);
if (highest >= 6) gens.add(Generation.Six);
if (highest >= 5) gens.add(Generation.Five);
if (highest >= 4) gens.add(Generation.Four);
if (highest >= 3) gens.add(Generation.Three);
if (highest >= 2) gens.add(Generation.Two);
if (highest >= 1) gens.add(Generation.One);
int max = 0;
// Iterate over the list, and get the count of all pokemon from a specific generation
for (var gen in gens) {
for (var pokemon in Pokemon.values) {
if (pokemon.generation == gen) max++;
}
}
_cachedHighest = max;
_lastMaxID = Pokemon.values.length;
return max;
}
}