From 5ccdd70494fa7e040875bc00133eb0a7c5d2abe4 Mon Sep 17 00:00:00 2001 From: ohwhylin Date: Tue, 29 Oct 2024 14:54:56 +0400 Subject: [PATCH] =?UTF-8?q?=D1=83=D0=B6=D0=B5=20=D0=BF=D0=BE=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D1=88=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/data/dtos/pokemon_dto.dart | 54 ++++--------------- lib/data/dtos/pokemon_dto.g.dart | 18 +------ lib/data/mappers/pokemon_mapper.dart | 41 +++++--------- lib/data/repositories/pokemon_repository.dart | 53 ++---------------- lib/presentation/home_page/bloc/bloc.dart | 5 +- 5 files changed, 26 insertions(+), 145 deletions(-) diff --git a/lib/data/dtos/pokemon_dto.dart b/lib/data/dtos/pokemon_dto.dart index cb42d5a..021402e 100644 --- a/lib/data/dtos/pokemon_dto.dart +++ b/lib/data/dtos/pokemon_dto.dart @@ -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? results; final int? count; final String? next; final String? previous; - final List? results; - const PokemonListDto({ + const PokemonDto({ + this.results, this.count, this.next, this.previous, - this.results, }); - factory PokemonListDto.fromJson(Map json) => _$PokemonListDtoFromJson(json); -} - -@JsonSerializable(createToJson: false) -class PokemonDto { - final String? name; - final String? url; - - const PokemonDto({this.name, this.url}); - factory PokemonDto.fromJson(Map 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 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 json) => _$SpritesDtoFromJson(json); + factory PokemonDataDto.fromJson(Map json) => _$PokemonDataDtoFromJson(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 476073f..1f8787d 100644 --- a/lib/data/dtos/pokemon_dto.g.dart +++ b/lib/data/dtos/pokemon_dto.g.dart @@ -17,22 +17,6 @@ PokemonDto _$PokemonDtoFromJson(Map json) => PokemonDto( PokemonDataDto _$PokemonDataDtoFromJson(Map json) => PokemonDataDto( - id: json['id'] as String?, name: json['name'] as String?, - attributes: json['attributes'] == null - ? null - : PokemonAttributesDto.fromJson( - json['attributes'] as Map), - ); - -PokemonAttributesDto _$PokemonAttributesDtoFromJson( - Map json) => - PokemonAttributesDto( - type: json['type'] as String?, - height: (json['height'] as num?)?.toInt(), - weight: (json['weight'] as num?)?.toInt(), - abilities: (json['abilities'] as List?) - ?.map((e) => e as String) - .toList(), - image: json['image'] as String?, + url: json['url'] as String?, ); diff --git a/lib/data/mappers/pokemon_mapper.dart b/lib/data/mappers/pokemon_mapper.dart index ff3ec43..177be9b 100644 --- a/lib/data/mappers/pokemon_mapper.dart +++ b/lib/data/mappers/pokemon_mapper.dart @@ -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, + ); } } \ No newline at end of file diff --git a/lib/data/repositories/pokemon_repository.dart b/lib/data/repositories/pokemon_repository.dart index 81a7c10..cb5e825 100644 --- a/lib/data/repositories/pokemon_repository.dart +++ b/lib/data/repositories/pokemon_repository.dart @@ -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 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); - final List? pokemonList = dto.results; - - if (pokemonList != null) { - final List 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); + final HomeData data = dto.toDomain(); + return data; } on DioException catch (e) { onError?.call(e.error?.toString()); return null; } } - - Future loadNextPage(String nextPageUrl, {OnErrorCallback? onError}) async { - try { - final Response response = await _dio.get>(nextPageUrl); - - final PokemonListDto dto = PokemonListDto.fromJson(response.data as Map); - final List? pokemonList = dto.results; - - if (pokemonList != null) { - final List 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 loadPokemonDetails(String url) async { - final Response response = await _dio.get>(url); - return PokemonDetailsDto.fromJson(response.data as Map); - } } \ No newline at end of file diff --git a/lib/presentation/home_page/bloc/bloc.dart b/lib/presentation/home_page/bloc/bloc.dart index da6968a..777d973 100644 --- a/lib/presentation/home_page/bloc/bloc.dart +++ b/lib/presentation/home_page/bloc/bloc.dart @@ -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 { - //final PotterRepository repo; final PokeRepository repo; HomeBloc(this.repo) : super(const HomeState()) { @@ -22,7 +20,6 @@ class HomeBloc extends Bloc { 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 { error: error, )); } -} +} \ No newline at end of file