аналог пепси колы

This commit is contained in:
Полина Чубыкина 2024-10-18 21:36:44 +04:00
parent 28e44f4703
commit b7013f4e2a
6 changed files with 97 additions and 113 deletions

View File

@ -4,70 +4,61 @@ part 'pokemon_dto.g.dart';
@JsonSerializable(createToJson: false) @JsonSerializable(createToJson: false)
class PokemonDto { class PokemonDto {
final String? name; final List<PokemonDataDto>? data;
final int? baseExperience; final MetaDto? meta;
final List<PokemonTypeDto>? types;
final List<PokemonAbilityDto>? abilities;
final PokemonSpritesDto? sprites;
const PokemonDto({ const PokemonDto({
this.name, this.data,
this.baseExperience, this.meta,
this.types,
this.abilities,
this.sprites,
}); });
factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json); factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json);
} }
@JsonSerializable(createToJson: false) @JsonSerializable(createToJson: false)
class PokemonTypeDto { class PokemonDataDto {
final int? slot; final String? id;
final TypeDto? type; final String? type;
final PokemonAttributesDataDto? attributes;
const PokemonTypeDto({this.slot, this.type}); const PokemonDataDto({this.id, this.type, this.attributes});
factory PokemonTypeDto.fromJson(Map<String, dynamic> json) => _$PokemonTypeDtoFromJson(json); factory PokemonDataDto.fromJson(Map<String, dynamic> json) => _$PokemonDataDtoFromJson(json);
} }
@JsonSerializable(createToJson: false) @JsonSerializable(createToJson: false)
class TypeDto { class PokemonAttributesDataDto {
final String? name; final String? name;
final String? url; final String? height;
final String? weight;
final String? image;
const TypeDto({this.name, this.url}); const PokemonAttributesDataDto({
this.name,
factory TypeDto.fromJson(Map<String, dynamic> json) => _$TypeDtoFromJson(json); this.height,
} this.weight,
this.image,
@JsonSerializable(createToJson: false)
class PokemonAbilityDto {
final int? slot;
final AbilityDto? ability;
const PokemonAbilityDto({this.slot, this.ability});
factory PokemonAbilityDto.fromJson(Map<String, dynamic> json) => _$PokemonAbilityDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class AbilityDto {
final String? name;
final String? url;
const AbilityDto({this.name, this.url});
factory AbilityDto.fromJson(Map<String, dynamic> json) => _$AbilityDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PokemonSpritesDto {
final String front_default;
const PokemonSpritesDto({
required this.front_default,
}); });
factory PokemonSpritesDto.fromJson(Map<String, dynamic> json) => _$PokemonSpritesDtoFromJson(json); factory PokemonAttributesDataDto.fromJson(Map<String, dynamic> json) => _$PokemonAttributesDataDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class MetaDto {
final PaginationDto? pagination;
const MetaDto({this.pagination});
factory MetaDto.fromJson(Map<String, dynamic> 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<String, dynamic> json) => _$PaginationDtoFromJson(json);
} }

View File

@ -7,46 +7,42 @@ part of 'pokemon_dto.dart';
// ************************************************************************** // **************************************************************************
PokemonDto _$PokemonDtoFromJson(Map<String, dynamic> json) => PokemonDto( PokemonDto _$PokemonDtoFromJson(Map<String, dynamic> json) => PokemonDto(
name: json['name'] as String?, data: (json['data'] as List<dynamic>?)
baseExperience: (json['baseExperience'] as num?)?.toInt(), ?.map((e) => PokemonDataDto.fromJson(e as Map<String, dynamic>))
types: (json['types'] as List<dynamic>?)
?.map((e) => PokemonTypeDto.fromJson(e as Map<String, dynamic>))
.toList(), .toList(),
abilities: (json['abilities'] as List<dynamic>?) meta: json['meta'] == null
?.map((e) => PokemonAbilityDto.fromJson(e as Map<String, dynamic>))
.toList(),
sprites: json['sprites'] == null
? null ? null
: PokemonSpritesDto.fromJson(json['sprites'] as Map<String, dynamic>), : MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
); );
PokemonTypeDto _$PokemonTypeDtoFromJson(Map<String, dynamic> json) => PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
PokemonTypeDto( PokemonDataDto(
slot: (json['slot'] as num?)?.toInt(), id: json['id'] as String?,
type: json['type'] == null type: json['type'] as String?,
attributes: json['attributes'] == null
? null ? null
: TypeDto.fromJson(json['type'] as Map<String, dynamic>), : PokemonAttributesDataDto.fromJson(
json['attributes'] as Map<String, dynamic>),
); );
TypeDto _$TypeDtoFromJson(Map<String, dynamic> json) => TypeDto( PokemonAttributesDataDto _$PokemonAttributesDataDtoFromJson(
Map<String, dynamic> json) =>
PokemonAttributesDataDto(
name: json['name'] as String?, 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<String, dynamic> json) => MetaDto _$MetaDtoFromJson(Map<String, dynamic> json) => MetaDto(
PokemonAbilityDto( pagination: json['pagination'] == null
slot: (json['slot'] as num?)?.toInt(),
ability: json['ability'] == null
? null ? null
: AbilityDto.fromJson(json['ability'] as Map<String, dynamic>), : PaginationDto.fromJson(json['pagination'] as Map<String, dynamic>),
); );
AbilityDto _$AbilityDtoFromJson(Map<String, dynamic> json) => AbilityDto( PaginationDto _$PaginationDtoFromJson(Map<String, dynamic> json) =>
name: json['name'] as String?, PaginationDto(
url: json['url'] as String?, current: (json['current'] as num?)?.toInt(),
); next: (json['next'] as num?)?.toInt(),
last: (json['last'] as num?)?.toInt(),
PokemonSpritesDto _$PokemonSpritesDtoFromJson(Map<String, dynamic> json) =>
PokemonSpritesDto(
front_default: json['front_default'] as String,
); );

View File

@ -7,19 +7,26 @@ const _imagePlaceholder =
extension PokemonDtoToModel on PokemonDto { extension PokemonDtoToModel on PokemonDto {
HomeData toDomain() => HomeData( HomeData toDomain() => HomeData(
data: [toCardData()], data: data?.map((e) => e.toDomain()).toList(),
nextPage: null, 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( String _makeDescriptionText(String? height, String? weight) {
name ?? 'UNKNOWN', return height != null && weight != null
descriptionText: _makeDescriptionText(types, abilities), ? 'Height: $height, Weight: $weight'
imageUrl: sprites?.front_default ?? _imagePlaceholder, : height != null
); ? 'Height: $height'
: weight != null
String _makeDescriptionText(List<PokemonTypeDto>? types, List<PokemonAbilityDto>? abilities) { ? 'Weight: $weight'
final typeNames = types?.map((e) => e.type?.name).whereType<String>().join(', ') ?? ''; : '';
final abilityNames = abilities?.map((e) => e.ability?.name).whereType<String>().join(', ') ?? '';
return 'Types: $typeNames\nAbilities: $abilityNames';
} }
} }

View File

@ -2,10 +2,12 @@ import 'package:dio/dio.dart';
import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart'; import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart';
import 'package:mobilki_lab1/data/mappers/pokemon_mapper.dart'; import 'package:mobilki_lab1/data/mappers/pokemon_mapper.dart';
import 'package:mobilki_lab1/data/repositories/api_interface.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/domain/models/home.dart';
import 'package:mobilki_lab1/presentation/home_page/bloc/events.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart'; import 'package:pretty_dio_logger/pretty_dio_logger.dart';
class PokeRepository extends ApiInterface { class PokemonRepository extends ApiInterface {
static final Dio _dio = Dio() static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger( ..interceptors.add(PrettyDioLogger(
requestHeader: true, requestHeader: true,
@ -27,27 +29,15 @@ class PokeRepository extends ApiInterface {
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>( final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url, url,
queryParameters: { queryParameters: {
'offset': (page - 1) * pageSize, 'filter[name_cont]': q,
'limit': pageSize, 'page[number]': page,
'page[size]': pageSize,
}, },
); );
final Map<String, dynamic> data = response.data as Map<String, dynamic>; final PokemonDto dto = PokemonDto.fromJson(response.data as Map<String, dynamic>);
final List<dynamic> results = data['results'] as List<dynamic>; final HomeData data = dto.toDomain();
final List<PokemonDto> pokemons = await Future.wait( return data;
results.map((result) async {
final String pokemonUrl = result['url'] as String;
final Response<dynamic> pokemonResponse = await _dio.get<Map<dynamic, dynamic>>(pokemonUrl);
return PokemonDto.fromJson(pokemonResponse.data as Map<String, dynamic>);
}),
);
final HomeData homeData = HomeData(
data: pokemons.map((pokemon) => pokemon.toCardData()).toList(),
nextPage: page + 1,
);
return homeData;
} on DioException catch (e) { } on DioException catch (e) {
onError?.call(e.error?.toString()); onError?.call(e.error?.toString());
return null; return null;

View File

@ -35,15 +35,15 @@ class MyApp extends StatelessWidget {
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true, useMaterial3: true,
), ),
home: RepositoryProvider<PokeRepository>( home: RepositoryProvider<PokemonRepository>(
lazy: true, lazy: true,
create: (_) => PokeRepository(), create: (_) => PokemonRepository(),
child: BlocProvider<LikeBloc>( child: BlocProvider<LikeBloc>(
lazy: false, lazy: false,
create: (context) => LikeBloc(), create: (context) => LikeBloc(),
child: BlocProvider<HomeBloc>( child: BlocProvider<HomeBloc>(
lazy: false, lazy: false,
create: (context) => HomeBloc(context.read<PokeRepository>()), create: (context) => HomeBloc(context.read<PokemonRepository>()),
child: const MyHomePage(title: 'Чубыкина Полина Павловна'), child: const MyHomePage(title: 'Чубыкина Полина Павловна'),
), ),
), ),

View File

@ -6,7 +6,7 @@ import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart';
class HomeBloc extends Bloc<HomeEvent, HomeState> { class HomeBloc extends Bloc<HomeEvent, HomeState> {
//final PotterRepository repo; //final PotterRepository repo;
final PokeRepository repo; final PokemonRepository repo;
HomeBloc(this.repo) : super(const HomeState()) { HomeBloc(this.repo) : super(const HomeState()) {
on<HomeLoadDataEvent>(_onLoadData); on<HomeLoadDataEvent>(_onLoadData);