diff --git a/lib/data/repositories/pokemon_repository.dart b/lib/data/repositories/pokemon_repository.dart index 75e03ad..93a9daa 100644 --- a/lib/data/repositories/pokemon_repository.dart +++ b/lib/data/repositories/pokemon_repository.dart @@ -14,55 +14,33 @@ class PokeRepository extends ApiInterface { static const String _baseUrl = 'https://pokeapi.co/api/v2'; - List _allPokemon = []; - bool _isAllPokemonLoaded = false; - @override Future loadData({ OnErrorCallback? onError, - String? q, + String? q, // Добавляем параметр для поискового запроса int page = 1, int pageSize = 25, }) async { try { - if (!_isAllPokemonLoaded) { - final String url = '$_baseUrl/pokemon'; - final Response response = await _dio.get>( - url, - queryParameters: { - 'offset': (page - 1) * pageSize, - 'limit': pageSize, - }, - ); + final String url = '$_baseUrl/pokemon'; - final PokemonDto dto = PokemonDto.fromJson(response.data as Map); - _allPokemon = dto.results ?? []; - _isAllPokemonLoaded = true; + final Map queryParameters = { + 'offset': (page - 1) * pageSize, + 'limit': pageSize, + }; + + // Если есть поисковый запрос, добавляем его в параметры запроса + if (q != null && q.isNotEmpty) { + queryParameters['name'] = q; } - final List 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 paginatedPokemon = filteredPokemon.sublist( - startIndex, - endIndex > filteredPokemon.length ? filteredPokemon.length : endIndex, + final Response response = await _dio.get>( + 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); + final HomeData data = dto.toDomain(); return data; } on DioException catch (e) { onError?.call(e.error?.toString());