еще что то
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';
|
||||
|
||||
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: {
|
||||
'offset': (page - 1) * pageSize,
|
||||
'limit': pageSize,
|
||||
},
|
||||
);
|
||||
final String url = '$_baseUrl/pokemon';
|
||||
|
||||
final PokemonDto dto = PokemonDto.fromJson(response.data as Map<String, dynamic>);
|
||||
_allPokemon = dto.results ?? [];
|
||||
_isAllPokemonLoaded = true;
|
||||
final Map<String, dynamic> queryParameters = {
|
||||
'offset': (page - 1) * pageSize,
|
||||
'limit': pageSize,
|
||||
};
|
||||
|
||||
// Если есть поисковый запрос, добавляем его в параметры запроса
|
||||
if (q != null && q.isNotEmpty) {
|
||||
queryParameters['name'] = q;
|
||||
}
|
||||
|
||||
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 Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
||||
url,
|
||||
queryParameters: queryParameters,
|
||||
);
|
||||
|
||||
final HomeData data = HomeData(
|
||||
data: paginatedPokemon.map((e) => e.toDomain()).toList(),
|
||||
nextPage: endIndex < filteredPokemon.length ? page + 1 : 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());
|
||||
|
Loading…
Reference in New Issue
Block a user