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

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)
class PokemonDto {
final String? name;
final int? baseExperience;
final List<PokemonTypeDto>? types;
final List<PokemonAbilityDto>? abilities;
final PokemonSpritesDto? sprites;
final List<PokemonDataDto>? data;
final MetaDto? meta;
const PokemonDto({
this.name,
this.baseExperience,
this.types,
this.abilities,
this.sprites,
this.data,
this.meta,
});
factory PokemonDto.fromJson(Map<String, dynamic> 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<String, dynamic> json) => _$PokemonTypeDtoFromJson(json);
factory PokemonDataDto.fromJson(Map<String, dynamic> 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<String, dynamic> json) => _$TypeDtoFromJson(json);
}
@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,
const PokemonAttributesDataDto({
this.name,
this.height,
this.weight,
this.image,
});
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(
name: json['name'] as String?,
baseExperience: (json['baseExperience'] as num?)?.toInt(),
types: (json['types'] as List<dynamic>?)
?.map((e) => PokemonTypeDto.fromJson(e as Map<String, dynamic>))
data: (json['data'] as List<dynamic>?)
?.map((e) => PokemonDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
abilities: (json['abilities'] as List<dynamic>?)
?.map((e) => PokemonAbilityDto.fromJson(e as Map<String, dynamic>))
.toList(),
sprites: json['sprites'] == null
meta: json['meta'] == null
? null
: PokemonSpritesDto.fromJson(json['sprites'] as Map<String, dynamic>),
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
);
PokemonTypeDto _$PokemonTypeDtoFromJson(Map<String, dynamic> json) =>
PokemonTypeDto(
slot: (json['slot'] as num?)?.toInt(),
type: json['type'] == null
PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
PokemonDataDto(
id: json['id'] as String?,
type: json['type'] as String?,
attributes: json['attributes'] == 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?,
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) =>
PokemonAbilityDto(
slot: (json['slot'] as num?)?.toInt(),
ability: json['ability'] == null
MetaDto _$MetaDtoFromJson(Map<String, dynamic> json) => MetaDto(
pagination: json['pagination'] == 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(
name: json['name'] as String?,
url: json['url'] as String?,
);
PokemonSpritesDto _$PokemonSpritesDtoFromJson(Map<String, dynamic> json) =>
PokemonSpritesDto(
front_default: json['front_default'] as String,
PaginationDto _$PaginationDtoFromJson(Map<String, dynamic> json) =>
PaginationDto(
current: (json['current'] as num?)?.toInt(),
next: (json['next'] as num?)?.toInt(),
last: (json['last'] as num?)?.toInt(),
);

View File

@ -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<PokemonTypeDto>? types, List<PokemonAbilityDto>? abilities) {
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';
String _makeDescriptionText(String? height, String? weight) {
return height != null && weight != null
? 'Height: $height, Weight: $weight'
: height != null
? 'Height: $height'
: weight != null
? 'Weight: $weight'
: '';
}
}

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/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<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url,
queryParameters: {
'offset': (page - 1) * pageSize,
'limit': pageSize,
'filter[name_cont]': q,
'page[number]': page,
'page[size]': pageSize,
},
);
final Map<String, dynamic> data = response.data as Map<String, dynamic>;
final List<dynamic> results = data['results'] as List<dynamic>;
final List<PokemonDto> pokemons = await Future.wait(
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;
final PokemonDto dto = PokemonDto.fromJson(response.data as Map<String, dynamic>);
final HomeData data = dto.toDomain();
return data;
} on DioException catch (e) {
onError?.call(e.error?.toString());
return null;

View File

@ -35,15 +35,15 @@ class MyApp extends StatelessWidget {
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: RepositoryProvider<PokeRepository>(
home: RepositoryProvider<PokemonRepository>(
lazy: true,
create: (_) => PokeRepository(),
create: (_) => PokemonRepository(),
child: BlocProvider<LikeBloc>(
lazy: false,
create: (context) => LikeBloc(),
child: BlocProvider<HomeBloc>(
lazy: false,
create: (context) => HomeBloc(context.read<PokeRepository>()),
create: (context) => HomeBloc(context.read<PokemonRepository>()),
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> {
//final PotterRepository repo;
final PokeRepository repo;
final PokemonRepository repo;
HomeBloc(this.repo) : super(const HomeState()) {
on<HomeLoadDataEvent>(_onLoadData);