diff --git a/lib/data/dtos/pokemon_dto.dart b/lib/data/dtos/pokemon_dto.dart index 8f9f72e..b530fed 100644 --- a/lib/data/dtos/pokemon_dto.dart +++ b/lib/data/dtos/pokemon_dto.dart @@ -4,16 +4,12 @@ part 'pokemon_dto.g.dart'; @JsonSerializable(createToJson: false) class PokemonDto { - final List? results; - final int? count; - final String? next; - final String? previous; + final List? data; + final MetaDto? meta; const PokemonDto({ - this.results, - this.count, - this.next, - this.previous, + this.data, + this.meta, }); factory PokemonDto.fromJson(Map json) => _$PokemonDtoFromJson(json); @@ -21,71 +17,48 @@ class PokemonDto { @JsonSerializable(createToJson: false) class PokemonDataDto { + final String? id; final String? name; - final String? url; - final String? imageUrl; - final int? height; - final int? weight; - final List? abilities; - // final PokemonAttributesDataDto? attributes; + final List? types; + final List? evolvesTo; + final PokemonImagesDto? images; const PokemonDataDto({ + this.id, this.name, - this.url, - this.imageUrl, - this.height, - this.weight, - this.abilities, - // this.attributes, + this.types, + this.evolvesTo, + this.images, }); factory PokemonDataDto.fromJson(Map json) => _$PokemonDataDtoFromJson(json); } -// @JsonSerializable(createToJson: false) -// class PokemonAttributesDataDto { -// final String? imageUrl; -// final int? height; -// final int? weight; -// final List? abilities; -// final PokemonSpritesDto? sprites; -// -// const PokemonAttributesDataDto({ -// this.imageUrl, -// this.height, -// this.weight, -// this.abilities, -// this.sprites, -// }); -// -// factory PokemonAttributesDataDto.fromJson(Map json) => _$PokemonAttributesDataDtoFromJson(json); -// } - @JsonSerializable(createToJson: false) -class AbilityDto { - final AbilityDetailDto? ability; +class PokemonImagesDto { + final String? small; - const AbilityDto({this.ability}); + const PokemonImagesDto({this.small}); - factory AbilityDto.fromJson(Map json) => _$AbilityDtoFromJson(json); + factory PokemonImagesDto.fromJson(Map json) => _$PokemonImagesDtoFromJson(json); } @JsonSerializable(createToJson: false) -class AbilityDetailDto { - final String? name; +class MetaDto { + final PaginationDto? pagination; - const AbilityDetailDto({this.name}); + const MetaDto({this.pagination}); - factory AbilityDetailDto.fromJson(Map json) => _$AbilityDetailDtoFromJson(json); + factory MetaDto.fromJson(Map json) => _$MetaDtoFromJson(json); } -// @JsonSerializable(createToJson: false) -// class PokemonSpritesDto { -// final String front_default; -// -// const PokemonSpritesDto({ -// required this.front_default, -// }); -// -// factory PokemonSpritesDto.fromJson(Map json) => _$PokemonSpritesDtoFromJson(json); -// } +@JsonSerializable(createToJson: false) +class PaginationDto { + final int? current; + final int? next; + final int? last; + + const PaginationDto({this.current, this.next, this.last}); + + factory PaginationDto.fromJson(Map json) => _$PaginationDtoFromJson(json); +} \ No newline at end of file diff --git a/lib/data/dtos/pokemon_dto.g.dart b/lib/data/dtos/pokemon_dto.g.dart index c0d0e3c..013fc59 100644 --- a/lib/data/dtos/pokemon_dto.g.dart +++ b/lib/data/dtos/pokemon_dto.g.dart @@ -7,33 +7,42 @@ part of 'pokemon_dto.dart'; // ************************************************************************** PokemonDto _$PokemonDtoFromJson(Map json) => PokemonDto( - results: (json['results'] as List?) + data: (json['data'] as List?) ?.map((e) => PokemonDataDto.fromJson(e as Map)) .toList(), - count: (json['count'] as num?)?.toInt(), - next: json['next'] as String?, - previous: json['previous'] as String?, + meta: json['meta'] == null + ? null + : MetaDto.fromJson(json['meta'] as Map), ); PokemonDataDto _$PokemonDataDtoFromJson(Map json) => PokemonDataDto( + id: json['id'] as String?, name: json['name'] as String?, - url: json['url'] as String?, - imageUrl: json['imageUrl'] as String?, - height: (json['height'] as num?)?.toInt(), - weight: (json['weight'] as num?)?.toInt(), - abilities: (json['abilities'] as List?) - ?.map((e) => AbilityDto.fromJson(e as Map)) + types: + (json['types'] as List?)?.map((e) => e as String).toList(), + evolvesTo: (json['evolvesTo'] as List?) + ?.map((e) => e as String) .toList(), - ); - -AbilityDto _$AbilityDtoFromJson(Map json) => AbilityDto( - ability: json['ability'] == null + images: json['images'] == null ? null - : AbilityDetailDto.fromJson(json['ability'] as Map), + : PokemonImagesDto.fromJson(json['images'] as Map), ); -AbilityDetailDto _$AbilityDetailDtoFromJson(Map json) => - AbilityDetailDto( - name: json['name'] as String?, +PokemonImagesDto _$PokemonImagesDtoFromJson(Map json) => + PokemonImagesDto( + small: json['small'] as String?, + ); + +MetaDto _$MetaDtoFromJson(Map json) => MetaDto( + pagination: json['pagination'] == null + ? null + : PaginationDto.fromJson(json['pagination'] as Map), + ); + +PaginationDto _$PaginationDtoFromJson(Map json) => + PaginationDto( + current: (json['current'] as num?)?.toInt(), + next: (json['next'] as num?)?.toInt(), + last: (json['last'] as num?)?.toInt(), ); diff --git a/lib/data/mappers/pokemon_mapper.dart b/lib/data/mappers/pokemon_mapper.dart index ddaf1de..b95a4a8 100644 --- a/lib/data/mappers/pokemon_mapper.dart +++ b/lib/data/mappers/pokemon_mapper.dart @@ -1,24 +1,29 @@ import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart'; import 'package:mobilki_lab1/domain/models/card.dart'; import 'package:mobilki_lab1/domain/models/home.dart'; +import 'package:flutter/material.dart'; const _imagePlaceholder = 'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png'; extension PokemonDtoToModel on PokemonDto { HomeData toDomain() => HomeData( - data: results?.map((e) => e.toDomain()).toList(), - nextPage: next != null ? int.tryParse(next!.split('=')[1].split('&')[0]) : null, + data: data?.map((e) => e.toDomain()).toList(), + nextPage: meta?.pagination?.next, ); } extension PokemonDataDtoToModel on PokemonDataDto { CardData toDomain() => CardData( name ?? 'UNKNOWN', - imageUrl: imageUrl ?? _imagePlaceholder, - descriptionText: 'Height: ${height ?? 'UNKNOWN'} cm, Weight: ${weight ?? 'UNKNOWN'} g, Abilities: ${abilities?.map((ability) => ability.ability?.name ?? 'UNKNOWN').join(', ') ?? 'UNKNOWN'}', - // imageUrl: attributes?.sprites?.front_default ?? _imagePlaceholder, - // descriptionText: 'Height: ${attributes?.height ?? 'UNKNOWN'} cm, Weight: ${attributes?.weight ?? 'UNKNOWN'} g, Abilities: ${attributes?.abilities?.map((ability) => ability.ability?.name ?? 'UNKNOWN').join(', ') ?? 'UNKNOWN'}', - id: url?.split('/')[6] ?? 'UNKNOWN', + imageUrl: images?.small ?? _imagePlaceholder, + descriptionText: _makeDescriptionText(types, evolvesTo), + id: id, ); + + String _makeDescriptionText(List? types, List? evolvesTo) { + final typeText = types != null && types.isNotEmpty ? 'Types: ${types.join(", ")}' : ''; + final evolvesToText = evolvesTo != null && evolvesTo.isNotEmpty ? 'Evolves To: ${evolvesTo.join(", ")}' : ''; + return '$typeText\n$evolvesToText'.trim(); + } } \ No newline at end of file diff --git a/lib/data/repositories/pokemon_repository.dart b/lib/data/repositories/pokemon_repository.dart index 6da993d..f1cd4e7 100644 --- a/lib/data/repositories/pokemon_repository.dart +++ b/lib/data/repositories/pokemon_repository.dart @@ -5,79 +5,36 @@ import 'package:mobilki_lab1/data/repositories/api_interface.dart'; import 'package:mobilki_lab1/domain/models/home.dart'; import 'package:pretty_dio_logger/pretty_dio_logger.dart'; -class PokeRepository extends ApiInterface { +class PokemonRepository extends ApiInterface { static final Dio _dio = Dio() ..interceptors.add(PrettyDioLogger( requestHeader: true, requestBody: true, )); - static const String _baseUrl = 'https://pokeapi.co/api/v2'; + static const String _baseUrl = 'https://api.pokemontcg.io/v2'; @override Future loadData({ OnErrorCallback? onError, String? q, - int page = 0, - int pageSize = 5, + int page = 1, + int pageSize = 10, }) async { try { - String url = '$_baseUrl/pokemon'; - - Map queryParameters = { - 'offset': page, - 'limit': pageSize, - }; - - // if (q != null && q.isNotEmpty) { - // url = '$_baseUrl/pokemon/$q'; - // queryParameters = { - // 'offset': page, - // 'limit': pageSize, - // }; - // } + final String url = '$_baseUrl/cards'; final Response response = await _dio.get>( url, - queryParameters: queryParameters, + queryParameters: { + 'q': q, + 'page': page, + 'pageSize': pageSize, + }, ); - // final PokemonDto dto = PokemonDto.fromJson(response.data as Map); - // final HomeData data = dto.toDomain(); final PokemonDto dto = PokemonDto.fromJson(response.data as Map); - final List pokemonDataList = dto.results ?? []; - - final List updatedPokemonDataList = await Future.wait(pokemonDataList.map((pokemonData) async { - final id = pokemonData.url?.split('/')[6]; - final detailsUrl = 'https://pokeapi.co/api/v2/pokemon/$id'; - final detailsResponse = await _dio.get>(detailsUrl); - - final int height = detailsResponse.data?['height']; - final int weight = detailsResponse.data?['weight']; - final List abilities = detailsResponse.data?['abilities']; - - final List abilityDtos = abilities.map((ability) { - return AbilityDto( - ability: AbilityDetailDto( - name: ability['ability']['name'], - ), - ); - }).toList(); - - return PokemonDataDto( - name: pokemonData.name, - url: pokemonData.url, - imageUrl: 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/$id.png', - height: height, - weight: weight, - abilities: abilityDtos, - ); - })); - - final HomeData data = HomeData( - data: updatedPokemonDataList.map((e) => e.toDomain()).toList(), - nextPage: dto.next != null ? int.tryParse(dto.next!.split('=')[1].split('&')[0]) : null, - ); + final HomeData data = dto.toDomain(); return data; } on DioException catch (e) { onError?.call(e.error?.toString()); diff --git a/lib/main.dart b/lib/main.dart index 1335ded..5d57e04 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -35,15 +35,15 @@ class MyApp extends StatelessWidget { colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), - home: RepositoryProvider( + home: RepositoryProvider( lazy: true, - create: (_) => PokeRepository(), + create: (_) => PokemonRepository(), child: BlocProvider( lazy: false, create: (context) => LikeBloc(), child: BlocProvider( lazy: false, - create: (context) => HomeBloc(context.read()), + create: (context) => HomeBloc(context.read()), child: const MyHomePage(title: 'Чубыкина Полина Павловна'), ), ), diff --git a/lib/presentation/home_page/bloc/bloc.dart b/lib/presentation/home_page/bloc/bloc.dart index 777d973..1bb033c 100644 --- a/lib/presentation/home_page/bloc/bloc.dart +++ b/lib/presentation/home_page/bloc/bloc.dart @@ -4,7 +4,7 @@ import 'package:mobilki_lab1/presentation/home_page/bloc/events.dart'; import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart'; class HomeBloc extends Bloc { - final PokeRepository repo; + final PokemonRepository repo; HomeBloc(this.repo) : super(const HomeState()) { on(_onLoadData);