Add a handler for the filters on evolutions.

This commit is contained in:
zontreck 2025-03-23 22:36:44 -07:00
parent 3f4462435b
commit 670c23d264
6 changed files with 94 additions and 52 deletions

View file

@ -1,7 +1,7 @@
import 'dart:io';
class Constants {
static const VERSION = "1.0.032325+2027";
static const VERSION = "1.0.032325+2236";
static bool get isMobile => Platform.isAndroid || Platform.isIOS;
}

View file

@ -47,7 +47,7 @@ class _HomeState extends State<Home> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('PokeDex'),
title: Text("PokeDex - ${SessionData.highestGenID()} Entries"),
backgroundColor:
SessionData.darkMode
? LibACFlutterConstants.TITLEBAR_COLOR
@ -69,7 +69,10 @@ class _HomeState extends State<Home> {
DrawerHeader(
child: Column(
children: [
Text("PokeDex", style: TextStyle(fontSize: 24)),
Text(
"PokeDex - ${SessionData.highestGenID()} Entries",
style: TextStyle(fontSize: 24),
),
Text(
"Version: ${Constants.VERSION}",
style: TextStyle(fontSize: 24),
@ -81,8 +84,9 @@ class _HomeState extends State<Home> {
title: Text("Filters"),
leading: Icon(Icons.filter),
subtitle: Text("Opens the PokeDex filters"),
onTap: () {
Navigator.pushNamed(context, "/filters");
onTap: () async {
await Navigator.pushNamed(context, "/filters");
setState(() {});
},
),
],
@ -96,8 +100,9 @@ class _HomeState extends State<Home> {
elevation: 50,
color: Color.fromARGB(255, 194, 94, 0),
child: InkWell(
onTap: () {
Navigator.pushNamed(context, "/dex", arguments: index);
onTap: () async {
await Navigator.pushNamed(context, "/dex", arguments: index);
setState(() {});
},
child: SizedBox(
width: 300,
@ -129,7 +134,7 @@ class _HomeState extends State<Home> {
),
);
},
itemCount: Pokemon.values.length,
itemCount: SessionData.highestGenID(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: Constants.isMobile ? 3 : 4,
),

View file

@ -1,6 +1,39 @@
import 'package:pokedex/filters.dart';
import 'package:pokedex/pokemon.dart';
class SessionData {
static bool darkMode = false;
static int filter = Filters.All;
static int highest = 9;
static int _cachedHighest = -1;
static void resetHighestGenCache() {
_cachedHighest = -1;
}
static int highestGenID() {
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;
return max;
}
}

View file

@ -3,21 +3,6 @@ import 'package:flutter/widgets.dart';
import 'package:libacflutter/Constants.dart';
import 'package:pokedex/Session.dart';
class Filters {
static final int GEN1 = 1;
static final int GEN2 = 2;
static final int GEN3 = 4;
static final int GEN4 = 8;
static final int GEN5 = 16;
static final int GEN6 = 32;
static final int GEN7 = 64;
static final int GEN8 = 128;
static final int GEN9 = 255;
static final int All =
GEN1 | GEN2 | GEN3 | GEN4 | GEN5 | GEN6 | GEN7 | GEN8 | GEN9;
}
class FilterPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
@ -46,92 +31,101 @@ class _filterPage extends State<FilterPage> {
style: TextStyle(fontSize: 24),
),
SwitchListTile(
value: (SessionData.filter & Filters.GEN1 == Filters.GEN1),
value: (SessionData.highest >= 1),
title: Text("Generation 1"),
onChanged: (val) {
setState(() {
SessionData.filter = SessionData.filter ^ Filters.GEN1;
SessionData.highest = 1;
SessionData.resetHighestGenCache();
});
},
),
SwitchListTile(
value: (SessionData.filter & Filters.GEN2 == Filters.GEN2),
value: (SessionData.highest >= 2),
title: Text("Generation 2"),
onChanged: (val) {
setState(() {
SessionData.filter = SessionData.filter ^ Filters.GEN2;
SessionData.highest = 2;
SessionData.resetHighestGenCache();
});
},
),
SwitchListTile(
value: (SessionData.filter & Filters.GEN3 == Filters.GEN3),
value: (SessionData.highest >= 3),
title: Text("Generation 3"),
onChanged: (val) {
setState(() {
SessionData.filter = SessionData.filter ^ Filters.GEN3;
SessionData.highest = 3;
SessionData.resetHighestGenCache();
});
},
),
SwitchListTile(
value: (SessionData.filter & Filters.GEN4 == Filters.GEN4),
value: (SessionData.highest >= 4),
title: Text("Generation 4"),
onChanged: (val) {
setState(() {
SessionData.filter = SessionData.filter ^ Filters.GEN4;
SessionData.highest = 4;
SessionData.resetHighestGenCache();
});
},
),
SwitchListTile(
value: (SessionData.filter & Filters.GEN5 == Filters.GEN5),
value: (SessionData.highest >= 5),
title: Text("Generation 5"),
onChanged: (val) {
setState(() {
SessionData.filter = SessionData.filter ^ Filters.GEN5;
SessionData.highest = 5;
SessionData.resetHighestGenCache();
});
},
),
SwitchListTile(
value: (SessionData.filter & Filters.GEN6 == Filters.GEN6),
value: (SessionData.highest >= 6),
title: Text("Generation 6"),
onChanged: (val) {
setState(() {
SessionData.filter = SessionData.filter ^ Filters.GEN6;
SessionData.highest = 6;
SessionData.resetHighestGenCache();
});
},
),
SwitchListTile(
value: (SessionData.filter & Filters.GEN7 == Filters.GEN7),
value: (SessionData.highest >= 7),
title: Text("Generation 7"),
onChanged: (val) {
setState(() {
SessionData.filter = SessionData.filter ^ Filters.GEN7;
SessionData.highest = 7;
SessionData.resetHighestGenCache();
});
},
),
SwitchListTile(
value: (SessionData.filter & Filters.GEN8 == Filters.GEN8),
value: (SessionData.highest >= 8),
title: Text("Generation 8"),
onChanged: (val) {
setState(() {
SessionData.filter = SessionData.filter ^ Filters.GEN8;
SessionData.highest = 8;
SessionData.resetHighestGenCache();
});
},
),
SwitchListTile(
value: (SessionData.filter & Filters.GEN9 == Filters.GEN9),
value: (SessionData.highest >= 9),
title: Text("Generation 9"),
onChanged: (val) {
setState(() {
SessionData.filter = SessionData.filter ^ Filters.GEN9;
SessionData.highest = 9;
SessionData.resetHighestGenCache();
});
},
),

View file

@ -2,6 +2,7 @@
import 'package:flutter/material.dart';
import 'package:pokedex/Consts.dart';
import 'package:pokedex/Session.dart';
enum Generation {
One(1, 151),
@ -379,6 +380,8 @@ enum Pokemon {
null,
previousPokemon: 21,
),
Ekans(23, Generation.One, [Type.Poison], SingleEvolution(24, 22)),
Arbok(24, Generation.One, [Type.Poison], null, previousPokemon: 23),
Pikachu(
25,
Generation.One,
@ -868,25 +871,32 @@ enum Pokemon {
List<Pokemon> Evs = [];
if (previousPokemon != -1 && subID == 0) {
Pokemon pkmn = Pokemon.values.where((x) => x.id == previousPokemon).first;
return pkmn.getEvolutions(0);
// ID must be in inclusive range of 1...MAXGeneration
if (previousPokemon != -1 &&
previousPokemon <= SessionData.highestGenID()) {
if (previousPokemon != -1 && subID == 0) {
Pokemon pkmn =
Pokemon.values.where((x) => x.id == previousPokemon).first;
return pkmn.getEvolutions(0);
}
}
//print("Processing evolutions for ${pokemonName}");
if (evolution is SingleEvolution) {
//print("Single Evolution identified");
Evs.add(
Pokemon.values
.where((x) => x.id == (evolution! as SingleEvolution).to)
.first,
);
var pokemon =
Pokemon.values
.where((x) => x.id == (evolution! as SingleEvolution).to)
.first;
if (pokemon.id <= SessionData.highestGenID()) Evs.add(pokemon);
} else {
//print("Branching Evolution identified");
// Handle branched evolutions. Refactor below to accomodate a list of pokemon.
for (var ev in (evolution! as BranchedEvolution).alternates) {
Evs.add(Pokemon.values.where((x) => x.id == ev).first);
var pokemon = Pokemon.values.where((x) => x.id == ev).first;
if (pokemon.id <= SessionData.highestGenID()) Evs.add(pokemon);
}
}

View file

@ -16,7 +16,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.032325+2027
version: 1.0.032325+2236
environment:
sdk: ^3.7.0