refactor(presentation): pokemon list consumes pokedexProvider
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
d4c8936c80
commit
7fd6d16020
@ -1,95 +1,38 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import '../models/pokemon.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import '../components/pokemon_tile.dart';
|
import '../../domain/entities/pokemon.dart';
|
||||||
import '../database/pokedex_database.dart';
|
import '../providers/pokedex_provider.dart';
|
||||||
import '../api/pokemon_api.dart';
|
import '../widgets/pokemon_tile.dart';
|
||||||
|
|
||||||
class PokemonListPage extends StatefulWidget {
|
class PokemonListPage extends ConsumerStatefulWidget {
|
||||||
const PokemonListPage({Key? key}) : super(key: key);
|
const PokemonListPage({Key? key}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<PokemonListPage> createState() => _PokemonListPageState();
|
ConsumerState<PokemonListPage> createState() => _PokemonListPageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _PokemonListPageState extends State<PokemonListPage> {
|
class _PokemonListPageState extends ConsumerState<PokemonListPage> {
|
||||||
String _filter = 'ALL'; // ALL, CAUGHT, NEW
|
String _filter = 'ALL'; // ALL, CAUGHT
|
||||||
int _caughtCount = 0;
|
|
||||||
List<Pokemon> _allPokemon = [];
|
|
||||||
List<Pokemon> _filteredPokemon = [];
|
|
||||||
bool _isSyncing = false;
|
|
||||||
final ScrollController _scrollController = ScrollController();
|
final ScrollController _scrollController = ScrollController();
|
||||||
|
|
||||||
@override
|
|
||||||
void initState() {
|
|
||||||
super.initState();
|
|
||||||
_loadPokemonData();
|
|
||||||
PokedexDatabase.onDatabaseUpdate.addListener(_loadPokemonData);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
PokedexDatabase.onDatabaseUpdate.removeListener(_loadPokemonData);
|
|
||||||
_scrollController.dispose();
|
_scrollController.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _loadPokemonData() async {
|
List<Pokemon> _applyFilter(List<Pokemon> all) {
|
||||||
setState(() => _isSyncing = true);
|
if (_filter == 'CAUGHT') return all.where((p) => p.isCaught).toList();
|
||||||
|
return all;
|
||||||
final count = await PokedexDatabase.getCaughtCount();
|
|
||||||
|
|
||||||
// Check if database needs sync (less than 1025 pokemon)
|
|
||||||
List<Pokemon> localData = await PokedexDatabase.getPokemonList();
|
|
||||||
if (localData.length < 1025) {
|
|
||||||
try {
|
|
||||||
final List<Pokemon> remoteData = await PokemonApi.getAllPokemon();
|
|
||||||
// Insert all missing pokemon using batch for performance
|
|
||||||
await PokedexDatabase.batchInsertPokemon(remoteData);
|
|
||||||
localData = await PokedexDatabase.getPokemonList();
|
|
||||||
} catch (e) {
|
|
||||||
debugPrint('Sync Error: $e');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort by ID to ensure order
|
|
||||||
localData.sort((a, b) => a.id.compareTo(b.id));
|
|
||||||
|
|
||||||
if (mounted) {
|
|
||||||
setState(() {
|
|
||||||
_allPokemon = localData;
|
|
||||||
_caughtCount = count;
|
|
||||||
_applyFilter();
|
|
||||||
_isSyncing = false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void _applyFilter() {
|
|
||||||
setState(() {
|
|
||||||
if (_filter == 'ALL') {
|
|
||||||
_filteredPokemon = _allPokemon;
|
|
||||||
} else if (_filter == 'CAUGHT') {
|
|
||||||
_filteredPokemon = _allPokemon.where((p) => p.isCaught).toList();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Reset scroll position to top when filter changes
|
|
||||||
if (_scrollController.hasClients) {
|
|
||||||
_scrollController.jumpTo(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _buildPokemonTile(BuildContext context, int index) {
|
|
||||||
final pokemon = _filteredPokemon[index];
|
|
||||||
return PokemonTile(pokemon);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
final pokedexAsync = ref.watch(pokedexProvider);
|
||||||
|
final caughtCount = ref.watch(caughtCountProvider);
|
||||||
|
|
||||||
return Container(
|
return Container(
|
||||||
decoration: const BoxDecoration(
|
decoration: const BoxDecoration(color: Color(0xFFC8D1D8)),
|
||||||
color: Color(0xFFC8D1D8), // Silver-ish grey background
|
|
||||||
),
|
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
// Header
|
// Header
|
||||||
@ -100,10 +43,8 @@ class _PokemonListPageState extends State<PokemonListPage> {
|
|||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
children: [
|
children: [
|
||||||
Icon(Icons.menu, color: Colors.black87),
|
Icon(Icons.menu, color: Colors.black87),
|
||||||
Text(
|
Text('LIST - NATIONAL',
|
||||||
'LIST - NATIONAL',
|
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, letterSpacing: 2)),
|
||||||
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, letterSpacing: 2),
|
|
||||||
),
|
|
||||||
Icon(Icons.search, color: Colors.black87),
|
Icon(Icons.search, color: Colors.black87),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@ -119,7 +60,6 @@ class _PokemonListPageState extends State<PokemonListPage> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
// Caught Count Bar
|
// Caught Count Bar
|
||||||
Container(
|
Container(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 12.0),
|
padding: const EdgeInsets.symmetric(vertical: 12.0),
|
||||||
@ -130,25 +70,21 @@ class _PokemonListPageState extends State<PokemonListPage> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
'${_caughtCount.toString().padLeft(3, '0')} / ${_allPokemon.length}',
|
'${caughtCount.toString().padLeft(3, '0')} / ${pokedexAsync.valueOrNull?.length ?? 0}',
|
||||||
style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
|
style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
|
||||||
),
|
),
|
||||||
const Text(
|
const Text('POKEMON DISCOVERED',
|
||||||
'POKEMON DISCOVERED',
|
style: TextStyle(fontSize: 14, color: Colors.black54, letterSpacing: 1)),
|
||||||
style: TextStyle(fontSize: 14, color: Colors.black54, letterSpacing: 1),
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
// The List
|
// The List
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Stack(
|
child: Stack(
|
||||||
children: [
|
children: [
|
||||||
// Scanlines effect
|
|
||||||
Positioned.fill(
|
Positioned.fill(
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
itemCount: 100, // drawing artificial scanlines
|
itemCount: 100,
|
||||||
physics: const NeverScrollableScrollPhysics(),
|
physics: const NeverScrollableScrollPhysics(),
|
||||||
itemBuilder: (context, index) => Container(
|
itemBuilder: (context, index) => Container(
|
||||||
height: 4,
|
height: 4,
|
||||||
@ -157,42 +93,47 @@ class _PokemonListPageState extends State<PokemonListPage> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (_isSyncing && _allPokemon.isEmpty)
|
pokedexAsync.when(
|
||||||
const Center(child: CircularProgressIndicator())
|
loading: () => const Center(child: CircularProgressIndicator()),
|
||||||
else if (_filteredPokemon.isEmpty)
|
error: (e, _) => Center(
|
||||||
Center(
|
child: Text('Erreur de chargement\n$e',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: const TextStyle(color: Colors.black54)),
|
||||||
|
),
|
||||||
|
data: (all) {
|
||||||
|
final filtered = _applyFilter(all);
|
||||||
|
if (filtered.isEmpty) {
|
||||||
|
return Center(
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
const Icon(Icons.search_off, size: 64, color: Colors.black26),
|
const Icon(Icons.search_off, size: 64, color: Colors.black26),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
Text(
|
Text('NO POKEMON FOUND IN $_filter',
|
||||||
'NO POKEMON FOUND IN $_filter',
|
style: const TextStyle(
|
||||||
style: const TextStyle(color: Colors.black45, fontSize: 18, fontWeight: FontWeight.bold),
|
color: Colors.black45, fontSize: 18, fontWeight: FontWeight.bold)),
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
)
|
);
|
||||||
else
|
}
|
||||||
ListView.builder(
|
return ListView.builder(
|
||||||
controller: _scrollController,
|
controller: _scrollController,
|
||||||
padding: const EdgeInsets.all(12),
|
padding: const EdgeInsets.all(12),
|
||||||
itemCount: _filteredPokemon.length,
|
itemCount: filtered.length,
|
||||||
itemBuilder: _buildPokemonTile,
|
itemBuilder: (context, index) => PokemonTile(filtered[index]),
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
||||||
// Footer
|
// Footer
|
||||||
Container(
|
Container(
|
||||||
height: 24,
|
height: 24,
|
||||||
color: const Color(0xFF1B2333),
|
color: const Color(0xFF1B2333),
|
||||||
alignment: Alignment.center,
|
alignment: Alignment.center,
|
||||||
child: const Text(
|
child: const Text('NATIONAL POKEDEX V2.0',
|
||||||
'NATIONAL POKEDEX V2.0',
|
style: TextStyle(color: Colors.white70, fontSize: 12, letterSpacing: 1)),
|
||||||
style: TextStyle(color: Colors.white70, fontSize: 12, letterSpacing: 1),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@ -204,26 +145,23 @@ class _PokemonListPageState extends State<PokemonListPage> {
|
|||||||
child: GestureDetector(
|
child: GestureDetector(
|
||||||
onTap: () {
|
onTap: () {
|
||||||
if (_filter != title) {
|
if (_filter != title) {
|
||||||
_filter = title;
|
setState(() => _filter = title);
|
||||||
_applyFilter();
|
if (_scrollController.hasClients) _scrollController.jumpTo(0);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: Container(
|
child: Container(
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
color: isSelected ? const Color(0xFFB0BEC5) : Colors.transparent,
|
color: isSelected ? const Color(0xFFB0BEC5) : Colors.transparent,
|
||||||
border: isSelected ? const Border(
|
border: isSelected
|
||||||
bottom: BorderSide(color: Color(0xFFD32F2F), width: 3),
|
? const Border(bottom: BorderSide(color: Color(0xFFD32F2F), width: 3))
|
||||||
) : null,
|
: null,
|
||||||
),
|
),
|
||||||
alignment: Alignment.center,
|
alignment: Alignment.center,
|
||||||
child: Text(
|
child: Text(title,
|
||||||
title,
|
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
fontSize: 18,
|
fontSize: 18,
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
color: isSelected ? Colors.black : Colors.black54,
|
color: isSelected ? Colors.black : Colors.black54)),
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user