Skip to content

Commit

Permalink
feat: link pokemon data between layers domain and presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
brunogabriel committed Oct 19, 2024
1 parent 29feceb commit e6c815d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 11 deletions.
13 changes: 8 additions & 5 deletions pokedex/lib/design/components/pokemon_information.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ class PokemonInformation extends StatelessWidget {
const PokemonInformation({
super.key,
required this.name,
required this.number,
required this.types,
});

final String name;
final String number;
final List<String> types;

@override
Widget build(BuildContext context) {
Expand All @@ -21,15 +25,14 @@ class PokemonInformation extends StatelessWidget {
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('# 6'),
Text('# $number'),
const SizedBox(height: 10.0),
Text(name),
],
),
const Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text('Anytype')],
)
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [Text(types.join(' '))])
],
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:json/json.dart';

import 'pokemon_type_slot_response.dart';

@JsonCodable()
class PokemonListDetailsResponse {
PokemonListDetailsResponse({
required this.name,
required this.types,
required this.id,
});

final String name;
final List<PokemonTypeSlotResponse> types;
final int id;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:json/json.dart';

import '../../../../common/api/model/named_api_response.dart';

@JsonCodable()
class PokemonTypeSlotResponse {
PokemonTypeSlotResponse({
required this.slot,
required this.type,
});

final int slot;
final NamedApiResponse type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../../../common/api/model/named_api_response.dart';
import '../domain/boundary/pokemon_list_repository.dart';
import '../domain/model/pokemon.dart';
import '../domain/model/pokemon_page_data.dart';
import 'model/pokemon_list_details_response.dart';

@Injectable(as: PokemonListRepository)
class PokemonListRepositoryImpl implements PokemonListRepository {
Expand All @@ -26,10 +27,24 @@ class PokemonListRepositoryImpl implements PokemonListRepository {
final namedApiResponse =
results.map((json) => NamedApiResponse.fromJson(json)).toList();

final pokemonResponses = await Future.wait(
namedApiResponse.map((e) => dio.get(e.url)),
);

final pokemons = pokemonResponses
.map((resp) => PokemonListDetailsResponse.fromJson(resp.data))
.toList();

return PokemonPageData(
hasNext: hasNext,
pokemons: namedApiResponse
.map((element) => Pokemon(name: element.name, number: 0))
pokemons: pokemons
.map(
(element) => Pokemon(
name: element.name,
number: element.id,
types: element.types.map((e) => e.type.name).toList(),
),
)
.toList(),
);
}
Expand Down
2 changes: 2 additions & 0 deletions pokedex/lib/features/pokemon_list/domain/model/pokemon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ class Pokemon {
Pokemon({
required this.name,
required this.number,
required this.types,
});

final String name;
final int number;
final List<String> types;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'package:flutter/material.dart';
import '../../../../design/components/pokemon_information.dart';
import '../../domain/model/pokemon.dart';

class PokemonCard extends StatelessWidget {
const PokemonCard({
super.key,
required this.name,
required this.pokemon,
});

final String name;
final Pokemon pokemon;

@override
Widget build(BuildContext context) {
Expand All @@ -20,7 +21,9 @@ class PokemonCard extends StatelessWidget {
child: Stack(
children: [
PokemonInformation(
name: name,
name: pokemon.name,
number: pokemon.number.toString(),
types: pokemon.types,
),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class _PokemonSuccessState extends State<PokemonSuccess> {
itemBuilder: (context, index) {
final pokemon = state.result[index];
return PokemonCard(
name: pokemon.name,
pokemon: pokemon,
);
},
itemCount: state.result.length,
Expand Down

0 comments on commit e6c815d

Please sign in to comment.