From b7013f4e2a816be55f6389dedb4c8139ef13b153 Mon Sep 17 00:00:00 2001 From: ohwhylin Date: Fri, 18 Oct 2024 21:36:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B0=D0=BD=D0=B0=D0=BB=D0=BE=D0=B3=20=D0=BF?= =?UTF-8?q?=D0=B5=D0=BF=D1=81=D0=B8=20=D0=BA=D0=BE=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/data/dtos/pokemon_dto.dart | 89 +++++++++---------- lib/data/dtos/pokemon_dto.g.dart | 54 ++++++----- lib/data/mappers/pokemon_mapper.dart | 31 ++++--- lib/data/repositories/pokemon_repository.dart | 28 ++---- lib/main.dart | 6 +- lib/presentation/home_page/bloc/bloc.dart | 2 +- 6 files changed, 97 insertions(+), 113 deletions(-) diff --git a/lib/data/dtos/pokemon_dto.dart b/lib/data/dtos/pokemon_dto.dart index 861f7bb..1feb7c8 100644 --- a/lib/data/dtos/pokemon_dto.dart +++ b/lib/data/dtos/pokemon_dto.dart @@ -4,70 +4,61 @@ part 'pokemon_dto.g.dart'; @JsonSerializable(createToJson: false) class PokemonDto { - final String? name; - final int? baseExperience; - final List? types; - final List? abilities; - final PokemonSpritesDto? sprites; + final List? data; + final MetaDto? meta; const PokemonDto({ - this.name, - this.baseExperience, - this.types, - this.abilities, - this.sprites, + this.data, + this.meta, }); factory PokemonDto.fromJson(Map json) => _$PokemonDtoFromJson(json); } @JsonSerializable(createToJson: false) -class PokemonTypeDto { - final int? slot; - final TypeDto? type; +class PokemonDataDto { + final String? id; + final String? type; + final PokemonAttributesDataDto? attributes; - const PokemonTypeDto({this.slot, this.type}); + const PokemonDataDto({this.id, this.type, this.attributes}); - factory PokemonTypeDto.fromJson(Map json) => _$PokemonTypeDtoFromJson(json); + factory PokemonDataDto.fromJson(Map json) => _$PokemonDataDtoFromJson(json); } @JsonSerializable(createToJson: false) -class TypeDto { +class PokemonAttributesDataDto { final String? name; - final String? url; + final String? height; + final String? weight; + final String? image; - const TypeDto({this.name, this.url}); - - factory TypeDto.fromJson(Map json) => _$TypeDtoFromJson(json); -} - -@JsonSerializable(createToJson: false) -class PokemonAbilityDto { - final int? slot; - final AbilityDto? ability; - - const PokemonAbilityDto({this.slot, this.ability}); - - factory PokemonAbilityDto.fromJson(Map json) => _$PokemonAbilityDtoFromJson(json); -} - -@JsonSerializable(createToJson: false) -class AbilityDto { - final String? name; - final String? url; - - const AbilityDto({this.name, this.url}); - - factory AbilityDto.fromJson(Map json) => _$AbilityDtoFromJson(json); -} - -@JsonSerializable(createToJson: false) -class PokemonSpritesDto { - final String front_default; - - const PokemonSpritesDto({ - required this.front_default, + const PokemonAttributesDataDto({ + this.name, + this.height, + this.weight, + this.image, }); - factory PokemonSpritesDto.fromJson(Map json) => _$PokemonSpritesDtoFromJson(json); + factory PokemonAttributesDataDto.fromJson(Map json) => _$PokemonAttributesDataDtoFromJson(json); +} + +@JsonSerializable(createToJson: false) +class MetaDto { + final PaginationDto? pagination; + + const MetaDto({this.pagination}); + + factory MetaDto.fromJson(Map json) => _$MetaDtoFromJson(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 ac37f37..4a71cdc 100644 --- a/lib/data/dtos/pokemon_dto.g.dart +++ b/lib/data/dtos/pokemon_dto.g.dart @@ -7,46 +7,42 @@ part of 'pokemon_dto.dart'; // ************************************************************************** PokemonDto _$PokemonDtoFromJson(Map json) => PokemonDto( - name: json['name'] as String?, - baseExperience: (json['baseExperience'] as num?)?.toInt(), - types: (json['types'] as List?) - ?.map((e) => PokemonTypeDto.fromJson(e as Map)) + data: (json['data'] as List?) + ?.map((e) => PokemonDataDto.fromJson(e as Map)) .toList(), - abilities: (json['abilities'] as List?) - ?.map((e) => PokemonAbilityDto.fromJson(e as Map)) - .toList(), - sprites: json['sprites'] == null + meta: json['meta'] == null ? null - : PokemonSpritesDto.fromJson(json['sprites'] as Map), + : MetaDto.fromJson(json['meta'] as Map), ); -PokemonTypeDto _$PokemonTypeDtoFromJson(Map json) => - PokemonTypeDto( - slot: (json['slot'] as num?)?.toInt(), - type: json['type'] == null +PokemonDataDto _$PokemonDataDtoFromJson(Map json) => + PokemonDataDto( + id: json['id'] as String?, + type: json['type'] as String?, + attributes: json['attributes'] == null ? null - : TypeDto.fromJson(json['type'] as Map), + : PokemonAttributesDataDto.fromJson( + json['attributes'] as Map), ); -TypeDto _$TypeDtoFromJson(Map json) => TypeDto( +PokemonAttributesDataDto _$PokemonAttributesDataDtoFromJson( + Map json) => + PokemonAttributesDataDto( name: json['name'] as String?, - url: json['url'] as String?, + height: json['height'] as String?, + weight: json['weight'] as String?, + image: json['image'] as String?, ); -PokemonAbilityDto _$PokemonAbilityDtoFromJson(Map json) => - PokemonAbilityDto( - slot: (json['slot'] as num?)?.toInt(), - ability: json['ability'] == null +MetaDto _$MetaDtoFromJson(Map json) => MetaDto( + pagination: json['pagination'] == null ? null - : AbilityDto.fromJson(json['ability'] as Map), + : PaginationDto.fromJson(json['pagination'] as Map), ); -AbilityDto _$AbilityDtoFromJson(Map json) => AbilityDto( - name: json['name'] as String?, - url: json['url'] as String?, - ); - -PokemonSpritesDto _$PokemonSpritesDtoFromJson(Map json) => - PokemonSpritesDto( - front_default: json['front_default'] as String, +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 8d7acf5..0d4ad3d 100644 --- a/lib/data/mappers/pokemon_mapper.dart +++ b/lib/data/mappers/pokemon_mapper.dart @@ -7,19 +7,26 @@ const _imagePlaceholder = extension PokemonDtoToModel on PokemonDto { HomeData toDomain() => HomeData( - data: [toCardData()], - nextPage: null, + data: data?.map((e) => e.toDomain()).toList(), + nextPage: meta?.pagination?.next, + ); +} + +extension PokemonDataDtoToModel on PokemonDataDto { + CardData toDomain() => CardData( + attributes?.name ?? 'UNKNOWN', + imageUrl: attributes?.image ?? _imagePlaceholder, + descriptionText: _makeDescriptionText(attributes?.height, attributes?.weight), + id: id, ); - CardData toCardData() => CardData( - name ?? 'UNKNOWN', - descriptionText: _makeDescriptionText(types, abilities), - imageUrl: sprites?.front_default ?? _imagePlaceholder, - ); - - String _makeDescriptionText(List? types, List? abilities) { - final typeNames = types?.map((e) => e.type?.name).whereType().join(', ') ?? ''; - final abilityNames = abilities?.map((e) => e.ability?.name).whereType().join(', ') ?? ''; - return 'Types: $typeNames\nAbilities: $abilityNames'; + String _makeDescriptionText(String? height, String? weight) { + return height != null && weight != null + ? 'Height: $height, Weight: $weight' + : height != null + ? 'Height: $height' + : weight != null + ? 'Weight: $weight' + : ''; } } \ No newline at end of file diff --git a/lib/data/repositories/pokemon_repository.dart b/lib/data/repositories/pokemon_repository.dart index 0e294c4..05b164c 100644 --- a/lib/data/repositories/pokemon_repository.dart +++ b/lib/data/repositories/pokemon_repository.dart @@ -2,10 +2,12 @@ import 'package:dio/dio.dart'; import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart'; import 'package:mobilki_lab1/data/mappers/pokemon_mapper.dart'; import 'package:mobilki_lab1/data/repositories/api_interface.dart'; +import 'package:mobilki_lab1/domain/models/card.dart'; import 'package:mobilki_lab1/domain/models/home.dart'; +import 'package:mobilki_lab1/presentation/home_page/bloc/events.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, @@ -27,27 +29,15 @@ class PokeRepository extends ApiInterface { final Response response = await _dio.get>( url, queryParameters: { - 'offset': (page - 1) * pageSize, - 'limit': pageSize, + 'filter[name_cont]': q, + 'page[number]': page, + 'page[size]': pageSize, }, ); - final Map data = response.data as Map; - final List results = data['results'] as List; - final List pokemons = await Future.wait( - results.map((result) async { - final String pokemonUrl = result['url'] as String; - final Response pokemonResponse = await _dio.get>(pokemonUrl); - return PokemonDto.fromJson(pokemonResponse.data as Map); - }), - ); - - final HomeData homeData = HomeData( - data: pokemons.map((pokemon) => pokemon.toCardData()).toList(), - nextPage: page + 1, - ); - - return homeData; + final PokemonDto dto = PokemonDto.fromJson(response.data as Map); + final HomeData data = dto.toDomain(); + return data; } on DioException catch (e) { onError?.call(e.error?.toString()); return null; 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 da6968a..9749a7b 100644 --- a/lib/presentation/home_page/bloc/bloc.dart +++ b/lib/presentation/home_page/bloc/bloc.dart @@ -6,7 +6,7 @@ import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart'; class HomeBloc extends Bloc { //final PotterRepository repo; - final PokeRepository repo; + final PokemonRepository repo; HomeBloc(this.repo) : super(const HomeState()) { on(_onLoadData);