уже получше работаем
This commit is contained in:
parent
0861ab4e91
commit
5ccdd70494
@ -3,67 +3,31 @@ import 'package:json_annotation/json_annotation.dart';
|
|||||||
part 'pokemon_dto.g.dart';
|
part 'pokemon_dto.g.dart';
|
||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
class PokemonListDto {
|
class PokemonDto {
|
||||||
|
final List<PokemonDataDto>? results;
|
||||||
final int? count;
|
final int? count;
|
||||||
final String? next;
|
final String? next;
|
||||||
final String? previous;
|
final String? previous;
|
||||||
final List<PokemonDto>? results;
|
|
||||||
|
|
||||||
const PokemonListDto({
|
const PokemonDto({
|
||||||
|
this.results,
|
||||||
this.count,
|
this.count,
|
||||||
this.next,
|
this.next,
|
||||||
this.previous,
|
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);
|
factory PokemonDto.fromJson(Map<String, dynamic> json) => _$PokemonDtoFromJson(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
class PokemonDetailsDto {
|
class PokemonDataDto {
|
||||||
@JsonKey(fromJson: _idFromJson)
|
|
||||||
final String? id;
|
|
||||||
final String? name;
|
final String? name;
|
||||||
final int? height;
|
final String? url;
|
||||||
final int? weight;
|
|
||||||
final SpritesDto? sprites;
|
|
||||||
|
|
||||||
const PokemonDetailsDto({
|
const PokemonDataDto({
|
||||||
this.id,
|
|
||||||
this.name,
|
this.name,
|
||||||
this.height,
|
this.url,
|
||||||
this.weight,
|
|
||||||
this.sprites,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
factory PokemonDetailsDto.fromJson(Map<String, dynamic> json) => _$PokemonDetailsDtoFromJson(json);
|
factory PokemonDataDto.fromJson(Map<String, dynamic> json) => _$PokemonDataDtoFromJson(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);
|
|
||||||
}
|
}
|
@ -17,22 +17,6 @@ PokemonDto _$PokemonDtoFromJson(Map<String, dynamic> json) => PokemonDto(
|
|||||||
|
|
||||||
PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
|
PokemonDataDto _$PokemonDataDtoFromJson(Map<String, dynamic> json) =>
|
||||||
PokemonDataDto(
|
PokemonDataDto(
|
||||||
id: json['id'] as String?,
|
|
||||||
name: json['name'] as String?,
|
name: json['name'] as String?,
|
||||||
attributes: json['attributes'] == null
|
url: json['url'] as String?,
|
||||||
? 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?,
|
|
||||||
);
|
);
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart';
|
import 'package:mobilki_lab1/data/dtos/pokemon_dto.dart';
|
||||||
import 'package:mobilki_lab1/domain/models/card.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';
|
||||||
@ -6,39 +5,23 @@ import 'package:mobilki_lab1/domain/models/home.dart';
|
|||||||
const _imagePlaceholder =
|
const _imagePlaceholder =
|
||||||
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
|
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
|
||||||
|
|
||||||
extension PokemonListDtoToModel on PokemonListDto {
|
extension PokemonDtoToModel on PokemonDto {
|
||||||
HomeData toDomain() => HomeData(
|
HomeData toDomain() => HomeData(
|
||||||
data: results?.map((e) => e.toDomain()).toList(),
|
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 {
|
extension PokemonDataDtoToModel on PokemonDataDto {
|
||||||
CardData toDomain() => CardData(
|
CardData toDomain() {
|
||||||
name ?? 'UNKNOWN',
|
final id = url?.split('/')[6] ?? 'UNKNOWN';
|
||||||
descriptionText: '', // Здесь можно добавить описание, если оно есть
|
final imageUrl = 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/$id.png';
|
||||||
imageUrl: _imagePlaceholder, // Здесь нужно добавить логику для получения URL изображения
|
|
||||||
id: null, // Здесь можно добавить логику для получения ID
|
|
||||||
icon: Icons.catching_pokemon, // Используем стандартную иконку
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
extension PokemonDetailsDtoToModel on PokemonDetailsDto {
|
return CardData(
|
||||||
CardData toDomain() => CardData(
|
name ?? 'UNKNOWN',
|
||||||
name ?? 'UNKNOWN',
|
imageUrl: imageUrl,
|
||||||
descriptionText: _makeDescriptionText(height, weight),
|
descriptionText: 'ID: $id',
|
||||||
imageUrl: sprites?.frontDefault ?? _imagePlaceholder,
|
id: id,
|
||||||
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'
|
|
||||||
: '';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,8 +5,6 @@ import 'package:mobilki_lab1/data/repositories/api_interface.dart';
|
|||||||
import 'package:mobilki_lab1/domain/models/home.dart';
|
import 'package:mobilki_lab1/domain/models/home.dart';
|
||||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||||
|
|
||||||
import '../../domain/models/card.dart';
|
|
||||||
|
|
||||||
class PokeRepository extends ApiInterface {
|
class PokeRepository extends ApiInterface {
|
||||||
static final Dio _dio = Dio()
|
static final Dio _dio = Dio()
|
||||||
..interceptors.add(PrettyDioLogger(
|
..interceptors.add(PrettyDioLogger(
|
||||||
@ -19,7 +17,6 @@ class PokeRepository extends ApiInterface {
|
|||||||
@override
|
@override
|
||||||
Future<HomeData?> loadData({
|
Future<HomeData?> loadData({
|
||||||
OnErrorCallback? onError,
|
OnErrorCallback? onError,
|
||||||
String? q,
|
|
||||||
int page = 1,
|
int page = 1,
|
||||||
int pageSize = 25,
|
int pageSize = 25,
|
||||||
}) async {
|
}) async {
|
||||||
@ -34,56 +31,12 @@ class PokeRepository extends ApiInterface {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
final PokemonListDto dto = PokemonListDto.fromJson(response.data as Map<String, dynamic>);
|
final PokemonDto dto = PokemonDto.fromJson(response.data as Map<String, dynamic>);
|
||||||
final List<PokemonDto>? pokemonList = dto.results;
|
final HomeData data = dto.toDomain();
|
||||||
|
return data;
|
||||||
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) {
|
} on DioException catch (e) {
|
||||||
onError?.call(e.error?.toString());
|
onError?.call(e.error?.toString());
|
||||||
return null;
|
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>);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,11 +1,9 @@
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:mobilki_lab1/data/repositories/pokemon_repository.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/events.dart';
|
||||||
import 'package:mobilki_lab1/presentation/home_page/bloc/state.dart';
|
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 PokeRepository repo;
|
final PokeRepository repo;
|
||||||
|
|
||||||
HomeBloc(this.repo) : super(const HomeState()) {
|
HomeBloc(this.repo) : super(const HomeState()) {
|
||||||
@ -22,7 +20,6 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
|||||||
String? error;
|
String? error;
|
||||||
|
|
||||||
final data = await repo.loadData(
|
final data = await repo.loadData(
|
||||||
q: event.search,
|
|
||||||
page: event.nextPage ?? 1,
|
page: event.nextPage ?? 1,
|
||||||
onError: (e) => error = e,
|
onError: (e) => error = e,
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user