patch small issue with trad and theme
This commit is contained in:
@@ -2,24 +2,43 @@ import 'package:sqflite_common/sqflite.dart';
|
||||
import '../../domain/entities/pokemon.dart';
|
||||
import '../dto/pokemon_dto.dart';
|
||||
|
||||
/// Accès SQLite local au Pokédex. Schéma et migrations identiques à l'ancien PokedexDatabase.
|
||||
const _createSql = '''
|
||||
CREATE TABLE IF NOT EXISTS pokemon (
|
||||
id INTEGER PRIMARY KEY NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
name_fr TEXT,
|
||||
name_en TEXT,
|
||||
type1 TEXT NOT NULL,
|
||||
type2 TEXT,
|
||||
hp INTEGER NOT NULL,
|
||||
atk INTEGER NOT NULL,
|
||||
def INTEGER NOT NULL,
|
||||
spd INTEGER NOT NULL,
|
||||
description TEXT,
|
||||
description_en TEXT,
|
||||
isCaught INTEGER NOT NULL DEFAULT 0,
|
||||
isSeen INTEGER NOT NULL DEFAULT 0
|
||||
)
|
||||
''';
|
||||
|
||||
class PokemonLocalDataSource {
|
||||
final String languageCode;
|
||||
|
||||
PokemonLocalDataSource({this.languageCode = 'fr'});
|
||||
|
||||
Future<Database>? _dbFuture;
|
||||
|
||||
Future<Database> _getDb() {
|
||||
return _dbFuture ??= openDatabase(
|
||||
'pokedex.db',
|
||||
version: 2,
|
||||
version: 5,
|
||||
onUpgrade: (db, oldVersion, newVersion) async {
|
||||
if (oldVersion < 2) {
|
||||
await db.execute('DROP TABLE IF EXISTS pokemon');
|
||||
await db.execute(
|
||||
'CREATE TABLE pokemon (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, type1 TEXT NOT NULL, type2 TEXT, hp INTEGER NOT NULL, atk INTEGER NOT NULL, def INTEGER NOT NULL, spd INTEGER NOT NULL, description TEXT, isCaught INTEGER NOT NULL DEFAULT 0, isSeen INTEGER NOT NULL DEFAULT 0)');
|
||||
}
|
||||
// Drop and recreate on any upgrade to pick up new columns cleanly.
|
||||
await db.execute('DROP TABLE IF EXISTS pokemon');
|
||||
await db.execute(_createSql);
|
||||
},
|
||||
onCreate: (db, version) async {
|
||||
await db.execute(
|
||||
'CREATE TABLE IF NOT EXISTS pokemon (id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL, type1 TEXT NOT NULL, type2 TEXT, hp INTEGER NOT NULL, atk INTEGER NOT NULL, def INTEGER NOT NULL, spd INTEGER NOT NULL, description TEXT, isCaught INTEGER NOT NULL DEFAULT 0, isSeen INTEGER NOT NULL DEFAULT 0)');
|
||||
await db.execute(_createSql);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -27,14 +46,14 @@ class PokemonLocalDataSource {
|
||||
Future<List<Pokemon>> getAll() async {
|
||||
final db = await _getDb();
|
||||
final rows = await db.query('pokemon');
|
||||
return rows.map(PokemonDto.fromDb).toList();
|
||||
return rows.map((r) => PokemonDto.fromDb(r, languageCode: languageCode)).toList();
|
||||
}
|
||||
|
||||
Future<Pokemon?> getById(int id) async {
|
||||
final db = await _getDb();
|
||||
final rows = await db.query('pokemon', where: 'id = ?', whereArgs: [id]);
|
||||
if (rows.isEmpty) return null;
|
||||
return PokemonDto.fromDb(rows.first);
|
||||
return PokemonDto.fromDb(rows.first, languageCode: languageCode);
|
||||
}
|
||||
|
||||
Future<void> saveAll(List<Pokemon> pokemons) async {
|
||||
|
||||
@@ -8,12 +8,13 @@ import '../dto/pokemon_dto.dart';
|
||||
/// Accès distant à l'API Tyradex.
|
||||
class PokemonRemoteDataSource {
|
||||
final http.Client _client;
|
||||
final String languageCode;
|
||||
|
||||
PokemonRemoteDataSource({http.Client? client})
|
||||
PokemonRemoteDataSource({http.Client? client, this.languageCode = 'fr'})
|
||||
: _client = client ?? http.Client();
|
||||
|
||||
Future<Pokemon> getById(int id) async {
|
||||
AppLogger.info('API: fetching Pokémon $id');
|
||||
AppLogger.info('API: fetching Pokémon $id (lang: $languageCode)');
|
||||
final response = await _client
|
||||
.get(Uri.https(AppConstants.apiBaseUrl, '${AppConstants.apiPokemonPath}/$id'));
|
||||
if (response.statusCode != 200) {
|
||||
@@ -21,11 +22,29 @@ class PokemonRemoteDataSource {
|
||||
'Erreur récupération du pokémon $id, code ${response.statusCode}');
|
||||
}
|
||||
final json = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
return PokemonDto.fromTyradexJson(json, fallbackId: id);
|
||||
return PokemonDto.fromTyradexJson(json,
|
||||
fallbackId: id, languageCode: languageCode);
|
||||
}
|
||||
|
||||
/// Fetches the English genus (category) from PokéAPI for a given Pokémon id.
|
||||
/// Returns null on any error (network, missing entry, etc.).
|
||||
Future<String?> getEnglishGenus(int id) async {
|
||||
try {
|
||||
final response = await _client
|
||||
.get(Uri.https('pokeapi.co', '/api/v2/pokemon-species/$id/'));
|
||||
if (response.statusCode != 200) return null;
|
||||
final json = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
final genera = json['genera'] as List<dynamic>? ?? [];
|
||||
for (final g in genera) {
|
||||
final lang = (g['language'] as Map<String, dynamic>?)?['name'];
|
||||
if (lang == 'en') return g['genus'] as String?;
|
||||
}
|
||||
} catch (_) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<List<Pokemon>> getAll() async {
|
||||
AppLogger.info('API: fetching ALL Pokémon');
|
||||
AppLogger.info('API: fetching ALL Pokémon (lang: $languageCode)');
|
||||
final response = await _client
|
||||
.get(Uri.https(AppConstants.apiBaseUrl, AppConstants.apiPokemonPath));
|
||||
if (response.statusCode != 200) {
|
||||
@@ -34,9 +53,12 @@ class PokemonRemoteDataSource {
|
||||
final List<dynamic> jsonList = jsonDecode(response.body);
|
||||
final result = <Pokemon>[];
|
||||
for (final json in jsonList) {
|
||||
if (json['pokedex_id'] == 0) continue; // entrée générique Tyradex
|
||||
if (json['pokedex_id'] == 0) continue;
|
||||
try {
|
||||
result.add(PokemonDto.fromTyradexJson(json as Map<String, dynamic>));
|
||||
result.add(PokemonDto.fromTyradexJson(
|
||||
json as Map<String, dynamic>,
|
||||
languageCode: languageCode,
|
||||
));
|
||||
} catch (e, st) {
|
||||
AppLogger.error('Parsing pokemon échoué: ${json['name']}', e, st);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user