еще что то
This commit is contained in:
parent
97cbcba226
commit
6129789048
@ -14,55 +14,33 @@ class PokeRepository extends ApiInterface {
|
|||||||
|
|
||||||
static const String _baseUrl = 'https://pokeapi.co/api/v2';
|
static const String _baseUrl = 'https://pokeapi.co/api/v2';
|
||||||
|
|
||||||
List<PokemonDataDto> _allPokemon = [];
|
|
||||||
bool _isAllPokemonLoaded = false;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<HomeData?> loadData({
|
Future<HomeData?> loadData({
|
||||||
OnErrorCallback? onError,
|
OnErrorCallback? onError,
|
||||||
String? q,
|
String? q, // Добавляем параметр для поискового запроса
|
||||||
int page = 1,
|
int page = 1,
|
||||||
int pageSize = 25,
|
int pageSize = 25,
|
||||||
}) async {
|
}) async {
|
||||||
try {
|
try {
|
||||||
if (!_isAllPokemonLoaded) {
|
|
||||||
final String url = '$_baseUrl/pokemon';
|
final String url = '$_baseUrl/pokemon';
|
||||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
|
||||||
url,
|
final Map<String, dynamic> queryParameters = {
|
||||||
queryParameters: {
|
|
||||||
'offset': (page - 1) * pageSize,
|
'offset': (page - 1) * pageSize,
|
||||||
'limit': pageSize,
|
'limit': pageSize,
|
||||||
},
|
};
|
||||||
|
|
||||||
|
// Если есть поисковый запрос, добавляем его в параметры запроса
|
||||||
|
if (q != null && q.isNotEmpty) {
|
||||||
|
queryParameters['name'] = q;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
||||||
|
url,
|
||||||
|
queryParameters: queryParameters,
|
||||||
);
|
);
|
||||||
|
|
||||||
final PokemonDto dto = PokemonDto.fromJson(response.data as Map<String, dynamic>);
|
final PokemonDto dto = PokemonDto.fromJson(response.data as Map<String, dynamic>);
|
||||||
_allPokemon = dto.results ?? [];
|
final HomeData data = dto.toDomain();
|
||||||
_isAllPokemonLoaded = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
final List<PokemonDataDto> filteredPokemon = q != null && q.isNotEmpty
|
|
||||||
? _allPokemon.where((pokemon) => pokemon.name!.contains(q)).toList()
|
|
||||||
: _allPokemon;
|
|
||||||
|
|
||||||
final int startIndex = (page - 1) * pageSize;
|
|
||||||
final int endIndex = startIndex + pageSize;
|
|
||||||
|
|
||||||
if (startIndex >= filteredPokemon.length) {
|
|
||||||
return HomeData(
|
|
||||||
data: [],
|
|
||||||
nextPage: null,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
final List<PokemonDataDto> paginatedPokemon = filteredPokemon.sublist(
|
|
||||||
startIndex,
|
|
||||||
endIndex > filteredPokemon.length ? filteredPokemon.length : endIndex,
|
|
||||||
);
|
|
||||||
|
|
||||||
final HomeData data = HomeData(
|
|
||||||
data: paginatedPokemon.map((e) => e.toDomain()).toList(),
|
|
||||||
nextPage: endIndex < filteredPokemon.length ? page + 1 : null,
|
|
||||||
);
|
|
||||||
return data;
|
return data;
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
onError?.call(e.error?.toString());
|
onError?.call(e.error?.toString());
|
||||||
|
Loading…
Reference in New Issue
Block a user