diff --git a/lib/components/utils/debounce.dart b/lib/components/utils/debounce.dart index 6e1c470..197c25f 100644 --- a/lib/components/utils/debounce.dart +++ b/lib/components/utils/debounce.dart @@ -12,7 +12,7 @@ class Debounce { static void run( VoidCallback action, { - Duration delay = const Duration(milliseconds: 500), + Duration delay = const Duration(milliseconds: 2000), }) { _timer?.cancel(); _timer = Timer(delay, action); diff --git a/lib/data/repositories/films_repository.dart b/lib/data/repositories/films_repository.dart index 967a4b2..a892984 100644 --- a/lib/data/repositories/films_repository.dart +++ b/lib/data/repositories/films_repository.dart @@ -17,11 +17,14 @@ class FilmsRepository { static const String _baseUrl = 'https://kinopoiskapiunofficial.tech'; static const String _apiKey = '67c830e4-b979-48ba-903d-a00c8f96fd4b'; + List _allFilms = []; + List _someFilms = []; + Future loadData({ OnErrorCallback? onError, String? q, int page = 1, - int pageSize = 7, + int pageSize = 5, }) async { try { String url = '$_baseUrl/api/v2.2/films/premieres'; @@ -30,52 +33,73 @@ class FilmsRepository { url = '$_baseUrl/api/v2.1/films/search-by-keyword'; } - final List months = ['JANUARY']; - - final List allFilms = []; - final List someFilms = []; - - for (final month in months) { - final Response response = - await _dio.get>( - url, - queryParameters: { - 'year': DateTime - .now() - .year, - 'month': month, - 'keyword': q, - 'page': page, - 'pageSize': pageSize, - }, - options: Options( - headers: { - 'X-API-KEY': _apiKey, - 'Content-Type': 'application/json', - }, - ), - ); - - if (response.statusCode == 200) { - if (q != null && q.isNotEmpty) { - final SearchDto dtos = - SearchDto.fromJson(response.data as Map); - someFilms.addAll(dtos.films!); - } else { - final FilmsDto dto = - FilmsDto.fromJson(response.data as Map); - allFilms.addAll(dto.items!); - } - } else { - onError?.call('Request failed with status: ${response.statusCode}'); - return null; + final String month = (() { + switch (DateTime.now().month) { + case 1: + return 'JANUARY'; + case 2: + return 'FEBRUARY'; + case 3: + return 'MARCH'; + case 4: + return 'APRIL'; + case 5: + return 'MAY'; + case 6: + return 'JUNE'; + case 7: + return 'JULY'; + case 8: + return 'AUGUST'; + case 9: + return 'SEPTEMBER'; + case 10: + return 'OCTOBER'; + case 11: + return 'NOVEMBER'; + case 12: + return 'DECEMBER'; + default: + throw Exception('Invalid month'); } + })(); + + final Response response = await _dio.get>( + url, + queryParameters: { + 'year': DateTime.now().year, + 'month': month, + 'keyword': q, + 'page': page, + 'pageSize': pageSize, + }, + options: Options( + headers: { + 'X-API-KEY': _apiKey, + 'Content-Type': 'application/json', + }, + ), + ); + + if (response.statusCode == 200) { + if (q != null && q.isNotEmpty) { + final SearchDto dtos = + SearchDto.fromJson(response.data as Map); + _someFilms.addAll(dtos.films!); + } else { + final FilmsDto dto = + FilmsDto.fromJson(response.data as Map); + _allFilms.addAll(dto.items!); + } + } else { + onError?.call('Request failed with status: ${response.statusCode}'); + return null; } final HomeData data = HomeData( data: q != null && q.isNotEmpty - ? someFilms.map((e) => e.toDomain()).toList() - : allFilms.map((e) => e.toDomain()).toList(), + ? _someFilms.skip((page - 1) * pageSize).take(pageSize).map((e) => e.toDomain()).toList() + : _allFilms.skip((page - 1) * pageSize).take(pageSize).map((e) => e.toDomain()).toList(), nextPage: page + 1, // Увеличиваем номер страницы для следующего запроса ); diff --git a/pubspec.lock b/pubspec.lock index fbd4d3d..d897765 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -34,10 +34,10 @@ packages: dependency: transitive description: name: args - sha256: "7cf60b9f0cc88203c5a190b4cd62a99feea42759a7fa695010eb5de1c0b2252a" + sha256: bf9f5caeea8d8fe6721a9c358dd8a5c1947b27f1cfaa18b39c301273594919e6 url: "https://pub.dev" source: hosted - version: "2.5.0" + version: "2.6.0" async: dependency: transitive description: @@ -162,10 +162,10 @@ packages: dependency: transitive description: name: code_builder - sha256: f692079e25e7869c14132d39f223f8eec9830eb76131925143b2129c4bb01b37 + sha256: "0ec10bf4a89e4c613960bf1e8b42c64127021740fb21640c29c909826a5eea3e" url: "https://pub.dev" source: hosted - version: "4.10.0" + version: "4.10.1" collection: dependency: transitive description: @@ -178,10 +178,10 @@ packages: dependency: transitive description: name: convert - sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592" + sha256: b30acd5944035672bc15c6b7a8b47d773e41e2f17de064350988c5d02adb1c68 url: "https://pub.dev" source: hosted - version: "3.1.1" + version: "3.1.2" copy_with_extension: dependency: transitive description: @@ -202,10 +202,10 @@ packages: dependency: transitive description: name: crypto - sha256: ec30d999af904f33454ba22ed9a86162b35e52b44ac4807d1d93c288041d7d27 + sha256: "1e445881f28f22d6140f181e07737b22f1e099a5e1ff94b0af2f9e4a463f4855" url: "https://pub.dev" source: hosted - version: "3.0.5" + version: "3.0.6" cupertino_icons: dependency: "direct main" description: @@ -242,10 +242,10 @@ packages: dependency: "direct main" description: name: equatable - sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2 + sha256: "567c64b3cb4cf82397aac55f4f0cbd3ca20d77c6c03bedbc4ceaddc08904aef7" url: "https://pub.dev" source: hosted - version: "2.0.5" + version: "2.0.7" fake_async: dependency: transitive description: @@ -266,18 +266,18 @@ packages: dependency: transitive description: name: file - sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c" + sha256: a3b4f84adafef897088c160faf7dfffb7696046cb13ae90b508c2cbc95d3b8d4 url: "https://pub.dev" source: hosted - version: "7.0.0" + version: "7.0.1" fixnum: dependency: transitive description: name: fixnum - sha256: "25517a4deb0c03aa0f32fd12db525856438902d9c16536311e76cdc57b31d7d1" + sha256: b6dc7065e46c974bc7c5f143080a6764ec7a4be6da1285ececdc37be96de53be url: "https://pub.dev" source: hosted - version: "1.1.0" + version: "1.1.1" flutter: dependency: "direct main" description: flutter @@ -422,10 +422,10 @@ packages: dependency: "direct dev" description: name: json_serializable - sha256: ea1432d167339ea9b5bb153f0571d0039607a873d6e04e0117af043f14a1fd4b + sha256: c2fcb3920cf2b6ae6845954186420fca40bc0a8abcc84903b7801f17d7050d7c url: "https://pub.dev" source: hosted - version: "6.8.0" + version: "6.9.0" leak_tracker: dependency: transitive description: @@ -462,10 +462,10 @@ packages: dependency: transitive description: name: logging - sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340" + sha256: c8245ada5f1717ed44271ed1c26b8ce85ca3228fd2ffdb75468ab01979309d61 url: "https://pub.dev" source: hosted - version: "1.2.0" + version: "1.3.0" macros: dependency: transitive description: @@ -694,10 +694,10 @@ packages: dependency: transitive description: name: shelf_web_socket - sha256: "073c147238594ecd0d193f3456a5fe91c4b0abbcc68bf5cd95b36c4e194ac611" + sha256: cc36c297b52866d203dbf9332263c94becc2fe0ceaa9681d07b6ef9807023b67 url: "https://pub.dev" source: hosted - version: "2.0.0" + version: "2.0.1" sky_engine: dependency: transitive description: flutter @@ -787,18 +787,18 @@ packages: dependency: transitive description: name: typed_data - sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c + sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006 url: "https://pub.dev" source: hosted - version: "1.3.2" + version: "1.4.0" vector_graphics: dependency: transitive description: name: vector_graphics - sha256: "773c9522d66d523e1c7b25dfb95cc91c26a1e17b107039cfe147285e92de7878" + sha256: "27d5fefe86fb9aace4a9f8375b56b3c292b64d8c04510df230f849850d912cb7" url: "https://pub.dev" source: hosted - version: "1.1.14" + version: "1.1.15" vector_graphics_codec: dependency: transitive description: @@ -811,10 +811,10 @@ packages: dependency: transitive description: name: vector_graphics_compiler - sha256: ab9ff38fc771e9ee1139320adbe3d18a60327370c218c60752068ebee4b49ab1 + sha256: "1b4b9e706a10294258727674a340ae0d6e64a7231980f9f9a3d12e4b42407aad" url: "https://pub.dev" source: hosted - version: "1.1.15" + version: "1.1.16" vector_math: dependency: transitive description: