уже получше работаем

This commit is contained in:
Полина Чубыкина 2024-10-29 14:54:56 +04:00
parent 0861ab4e91
commit 5ccdd70494
5 changed files with 26 additions and 145 deletions

View File

@ -3,67 +3,31 @@ import 'package:json_annotation/json_annotation.dart';
part 'pokemon_dto.g.dart';
@JsonSerializable(createToJson: false)
class PokemonListDto {
class PokemonDto {
final List<PokemonDataDto>? results;
final int? count;
final String? next;
final String? previous;
final List<PokemonDto>? results;
const PokemonListDto({
const PokemonDto({
this.results,
this.count,
this.next,
this.previous,
this.results,
});
factory PokemonListDto.fromJson(Map<String, dynamic> json) => _$PokemonListDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PokemonDto {
final String? name;
final String? url;
const PokemonDto({this.name, this.url});
factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PokemonDetailsDto {
@JsonKey(fromJson: _idFromJson)
final String? id;
class PokemonDataDto {
final String? name;
final int? height;
final int? weight;
final SpritesDto? sprites;
final String? url;
const PokemonDetailsDto({
this.id,
const PokemonDataDto({
this.name,
this.height,
this.weight,
this.sprites,
this.url,
});
factory PokemonDetailsDto.fromJson(Map<String, dynamic> json) => _$PokemonDetailsDtoFromJson(json);
static String? _idFromJson(dynamic id) {
if (id is int) {
return id.toString();
} else if (id is String) {
return id;
} else {
return null;
}
}
}
@JsonSerializable(createToJson: false)
class SpritesDto {
final String? frontDefault;
const SpritesDto({this.frontDefault});
factory SpritesDto.fromJson(Map<String, dynamic> json) => _$SpritesDtoFromJson(json);
factory PokemonDataDto.fromJson(Map<String, dynamic> json) => _$PokemonDataDtoFromJson(json);
}

View File

@ -17,22 +17,6 @@ PokemonDto _$PokemonDtoFromJson(Map<String, dynamic> json) => PokemonDto(
PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
PokemonDataDto(
id: json['id'] as String?,
name: json['name'] as String?,
attributes: json['attributes'] == null
? null
: PokemonAttributesDto.fromJson(
json['attributes'] as Map<String, dynamic>),
);
PokemonAttributesDto _$PokemonAttributesDtoFromJson(
Map<String, dynamic> json) =>
PokemonAttributesDto(
type: json['type'] as String?,
height: (json['height'] as num?)?.toInt(),
weight: (json['weight'] as num?)?.toInt(),
abilities: (json['abilities'] as List<dynamic>?)
?.map((e) => e as String)
.toList(),
image: json['image'] as String?,
url: json['url'] as String?,
);

View File

@ -1,4 +1,3 @@
import 'package:flutter/material.dart';
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';
@ -6,39 +5,23 @@ import 'package:mobilki_lab1/domain/models/home.dart';
const _imagePlaceholder =
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
extension PokemonListDtoToModel on PokemonListDto {
extension PokemonDtoToModel on PokemonDto {
HomeData toDomain() => HomeData(
data: results?.map((e) => e.toDomain()).toList(),
nextPage: next != null ? int.tryParse(next!.split('?')[1].split('=')[1]) : null,
nextPage: next != null ? int.tryParse(next!.split('=')[1].split('&')[0]) : null,
);
}
extension PokemonDtoToModel on PokemonDto {
CardData toDomain() => CardData(
name ?? 'UNKNOWN',
descriptionText: '', // Здесь можно добавить описание, если оно есть
imageUrl: _imagePlaceholder, // Здесь нужно добавить логику для получения URL изображения
id: null, // Здесь можно добавить логику для получения ID
icon: Icons.catching_pokemon, // Используем стандартную иконку
);
}
extension PokemonDataDtoToModel on PokemonDataDto {
CardData toDomain() {
final id = url?.split('/')[6] ?? 'UNKNOWN';
final imageUrl = 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/$id.png';
extension PokemonDetailsDtoToModel on PokemonDetailsDto {
CardData toDomain() => CardData(
name ?? 'UNKNOWN',
descriptionText: _makeDescriptionText(height, weight),
imageUrl: sprites?.frontDefault ?? _imagePlaceholder,
id: id,
icon: Icons.catching_pokemon, // Используем стандартную иконку
);
String _makeDescriptionText(int? height, int? weight) {
return height != null && weight != null
? 'Height: $height, Weight: $weight'
: height != null
? 'Height: $height'
: weight != null
? 'Weight: $weight'
: '';
return CardData(
name ?? 'UNKNOWN',
imageUrl: imageUrl,
descriptionText: 'ID: $id',
id: id,
);
}
}

View File

@ -5,8 +5,6 @@ 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';
import '../../domain/models/card.dart';
class PokeRepository extends ApiInterface {
static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger(
@ -19,7 +17,6 @@ class PokeRepository extends ApiInterface {
@override
Future<HomeData?> loadData({
OnErrorCallback? onError,
String? q,
int page = 1,
int pageSize = 25,
}) async {
@ -34,56 +31,12 @@ class PokeRepository extends ApiInterface {
},
);
final PokemonListDto dto = PokemonListDto.fromJson(response.data as Map<String, dynamic>);
final List<PokemonDto>? pokemonList = dto.results;
if (pokemonList != null) {
final List<CardData> cards = await Future.wait(pokemonList.map((pokemon) async {
final PokemonDetailsDto detailsDto = await loadPokemonDetails(pokemon.url!);
return detailsDto.toDomain();
}));
return HomeData(
data: cards,
nextPage: dto.next,
);
}
return null;
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;
}
}
Future<HomeData?> loadNextPage(String nextPageUrl, {OnErrorCallback? onError}) async {
try {
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(nextPageUrl);
final PokemonListDto dto = PokemonListDto.fromJson(response.data as Map<String, dynamic>);
final List<PokemonDto>? pokemonList = dto.results;
if (pokemonList != null) {
final List<CardData> cards = await Future.wait(pokemonList.map((pokemon) async {
final PokemonDetailsDto detailsDto = await loadPokemonDetails(pokemon.url!);
return detailsDto.toDomain();
}));
return HomeData(
data: cards,
nextPage: dto.next,
);
}
return null;
} on DioException catch (e) {
onError?.call(e.error?.toString());
return null;
}
}
Future<PokemonDetailsDto> loadPokemonDetails(String url) async {
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
return PokemonDetailsDto.fromJson(response.data as Map<String, dynamic>);
}
}

View File

@ -1,11 +1,9 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mobilki_lab1/data/repositories/pokemon_repository.dart';
import 'package:mobilki_lab1/data/repositories/potter_repository.dart';
import 'package:mobilki_lab1/presentation/home_page/bloc/events.dart';
import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart';
class HomeBloc extends Bloc<HomeEvent, HomeState> {
//final PotterRepository repo;
final PokeRepository repo;
HomeBloc(this.repo) : super(const HomeState()) {
@ -22,7 +20,6 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
String? error;
final data = await repo.loadData(
q: event.search,
page: event.nextPage ?? 1,
onError: (e) => error = e,
);
@ -38,4 +35,4 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
error: error,
));
}
}
}