еще что то

This commit is contained in:
Полина Чубыкина 2024-11-16 13:45:46 +04:00
parent 97cbcba226
commit 6129789048

View File

@ -14,55 +14,33 @@ class PokeRepository extends ApiInterface {
static const String _baseUrl = 'https://pokeapi.co/api/v2';
List<PokemonDataDto> _allPokemon = [];
bool _isAllPokemonLoaded = false;
@override
Future<HomeData?> loadData({
OnErrorCallback? onError,
String? q,
String? q, // Добавляем параметр для поискового запроса
int page = 1,
int pageSize = 25,
}) async {
try {
if (!_isAllPokemonLoaded) {
final String url = '$_baseUrl/pokemon';
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url,
queryParameters: {
final Map<String, dynamic> queryParameters = {
'offset': (page - 1) * 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>);
_allPokemon = dto.results ?? [];
_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,
);
final HomeData data = dto.toDomain();
return data;
} on DioException catch (e) {
onError?.call(e.error?.toString());