diff --git a/lib/data/dtos/pokemon_dto.dart b/lib/data/dtos/pokemon_dto.dart index de8e84e..1be5d9d 100644 --- a/lib/data/dtos/pokemon_dto.dart +++ b/lib/data/dtos/pokemon_dto.dart @@ -23,10 +23,14 @@ class PokemonDto { class PokemonDataDto { final String? name; final String? url; + final String? imageUrl; + final PokemonDetailsDto? details; const PokemonDataDto({ this.name, this.url, + this.imageUrl, + this.details, }); factory PokemonDataDto.fromJson(Map json) => _$PokemonDataDtoFromJson(json); diff --git a/lib/data/dtos/pokemon_dto.g.dart b/lib/data/dtos/pokemon_dto.g.dart index 7e7ce83..eeacaf9 100644 --- a/lib/data/dtos/pokemon_dto.g.dart +++ b/lib/data/dtos/pokemon_dto.g.dart @@ -19,6 +19,10 @@ PokemonDataDto _$PokemonDataDtoFromJson(Map json) => PokemonDataDto( name: json['name'] as String?, url: json['url'] as String?, + imageUrl: json['imageUrl'] as String?, + details: json['details'] == null + ? null + : PokemonDetailsDto.fromJson(json['details'] as Map), ); PokemonDetailsDto _$PokemonDetailsDtoFromJson(Map json) => diff --git a/lib/data/mappers/pokemon_mapper.dart b/lib/data/mappers/pokemon_mapper.dart index 5b74a9b..17fb214 100644 --- a/lib/data/mappers/pokemon_mapper.dart +++ b/lib/data/mappers/pokemon_mapper.dart @@ -13,17 +13,12 @@ extension PokemonDtoToModel on PokemonDto { } extension PokemonDataDtoToModel on PokemonDataDto { - CardData toDomain() { - final id = url?.split('/')[6] ?? 'UNKNOWN'; - final imageUrl = 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/$id.png'; - - return CardData( - name ?? 'UNKNOWN', - imageUrl: imageUrl, - descriptionText: 'ID: $id', - id: id, - ); - } + CardData toDomain() => CardData( + name ?? 'UNKNOWN', + imageUrl: imageUrl ?? _imagePlaceholder, + descriptionText: details?.toDescriptionText() ?? 'ID: ${url?.split('/')[6] ?? 'UNKNOWN'}', + id: url?.split('/')[6] ?? 'UNKNOWN', + ); } extension PokemonDetailsDtoToModel on PokemonDetailsDto { diff --git a/lib/data/repositories/pokemon_repository.dart b/lib/data/repositories/pokemon_repository.dart index cb5e825..6115683 100644 --- a/lib/data/repositories/pokemon_repository.dart +++ b/lib/data/repositories/pokemon_repository.dart @@ -17,22 +17,47 @@ class PokeRepository extends ApiInterface { @override Future loadData({ OnErrorCallback? onError, + String? q, int page = 1, int pageSize = 25, }) async { try { final String url = '$_baseUrl/pokemon'; + final Map queryParameters = { + 'offset': page, + 'limit': pageSize, + }; + + if (q != null && q.isNotEmpty) { + queryParameters['name'] = q; + } + final Response response = await _dio.get>( url, - queryParameters: { - 'offset': (page - 1) * pageSize, - 'limit': pageSize, - }, + queryParameters: queryParameters, ); final PokemonDto dto = PokemonDto.fromJson(response.data as Map); - final HomeData data = dto.toDomain(); + final List pokemonDataList = dto.results ?? []; + + final List updatedPokemonDataList = await Future.wait(pokemonDataList.map((pokemonData) async { + final detailsUrl = 'https://pokeapi.co/api/v2/pokemon/${pokemonData.url?.split('/')[6]}'; + final detailsResponse = await _dio.get>(detailsUrl); + final PokemonDetailsDto detailsDto = PokemonDetailsDto.fromJson(detailsResponse.data as Map); + final String imageUrl = 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemonData.url?.split('/')[6]}.png'; + return PokemonDataDto( + name: pokemonData.name, + url: pokemonData.url, + imageUrl: imageUrl, + details: detailsDto, + ); + })); + + final HomeData data = HomeData( + data: updatedPokemonDataList.map((e) => e.toDomain()).toList(), + nextPage: dto.next != null ? int.tryParse(dto.next!.split('=')[1].split('&')[0]) : null, + ); return data; } on DioException catch (e) { onError?.call(e.error?.toString());