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