diff --git a/lib/data/dtos/news_dto.dart b/lib/data/dtos/news_dto.dart index 5115185..1b7795d 100644 --- a/lib/data/dtos/news_dto.dart +++ b/lib/data/dtos/news_dto.dart @@ -2,7 +2,7 @@ import 'package:json_annotation/json_annotation.dart'; part 'news_dto.g.dart'; @JsonSerializable(createToJson: false) class NewsDto{ - @JsonKey(name: 'Главное') + @JsonKey(name: 'articles') final List? data; const NewsDto({this.data}); factory NewsDto.fromJson(Map json) => _$NewsDtoFromJson(json); @@ -12,10 +12,10 @@ class NewsDto{ @JsonSerializable(createToJson: false) class NewAttributesDataDto{ final String? title; - final String? summary; - @JsonKey(name: 'image_link') + final String? description; + @JsonKey(name: 'urlToImage') final String? imagelink; - const NewAttributesDataDto({this.title, this.summary, this.imagelink }); + const NewAttributesDataDto({this.title, this.description, this.imagelink }); factory NewAttributesDataDto.fromJson(Map json) => _$NewAttributesDataDtoFromJson(json); } \ No newline at end of file diff --git a/lib/data/dtos/news_dto.g.dart b/lib/data/dtos/news_dto.g.dart index adca9de..dc656af 100644 --- a/lib/data/dtos/news_dto.g.dart +++ b/lib/data/dtos/news_dto.g.dart @@ -7,7 +7,7 @@ part of 'news_dto.dart'; // ************************************************************************** NewsDto _$NewsDtoFromJson(Map json) => NewsDto( - data: (json['Главное'] as List?) + data: (json['articles'] as List?) ?.map((e) => NewAttributesDataDto.fromJson(e as Map)) .toList(), ); @@ -17,6 +17,6 @@ NewAttributesDataDto _$NewAttributesDataDtoFromJson( Map json) => NewAttributesDataDto( title: json['title'] as String?, - summary: json['summary'] as String?, - imagelink: json['image_link'] as String?, + description: json['description'] as String?, + imagelink: json['urlToImage'] as String?, ); diff --git a/lib/data/mappers/news_mapper.dart b/lib/data/mappers/news_mapper.dart index af04f92..1429565 100644 --- a/lib/data/mappers/news_mapper.dart +++ b/lib/data/mappers/news_mapper.dart @@ -5,6 +5,6 @@ extension NewDataDtoToModel on NewAttributesDataDto{ CardData toDomain() => CardData( text: title ?? 'UNKNOWN', imageUrl: imagelink, - descText: summary ?? 'NOTHING', + descText: description ?? 'NOTHING', ); } \ No newline at end of file diff --git a/lib/data/repositories/bbc_repository.dart b/lib/data/repositories/bbc_repository.dart index 40eb888..27181cc 100644 --- a/lib/data/repositories/bbc_repository.dart +++ b/lib/data/repositories/bbc_repository.dart @@ -11,16 +11,15 @@ class BbcRepository extends ApiInterface { requestHeader: true, requestBody: true, )); - static const String _baseUrl = 'https://bbc-api.vercel.app'; - + static const String _baseUrl = 'https://newsapi.org/v2'; + static const String _apiKey = '&apiKey=b9848c2aa43e4a0ba12dfe925db8513c'; @override Future?> loadData( {String? q, OnErrorCallback? onError}) async { try { - const String url = '$_baseUrl/news?lang=russian'; + final String url = '$_baseUrl/everything?q=$q$_apiKey'; final Response response = await _dio.get>( url, - //queryParameters: q != null ? {'filter[name_cont]': q} : null, ); final NewsDto dto = NewsDto.fromJson(response.data as Map); diff --git a/lib/presentation/home_page/bloc/bloc.dart b/lib/presentation/home_page/bloc/bloc.dart new file mode 100644 index 0000000..29b0da2 --- /dev/null +++ b/lib/presentation/home_page/bloc/bloc.dart @@ -0,0 +1,15 @@ +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pmu/data/repositories/bbc_repository.dart'; +import 'package:pmu/presentation/home_page/bloc/events.dart'; +import 'package:pmu/presentation/home_page/bloc/state.dart'; + +class HomeBloc extends Bloc{ + final BbcRepository repo; + HomeBloc(this.repo) : super(const HomeState()){ + on(_onLoadData); + } + // мы должны изменить наше состояние, передаем внутрь новое состояние + void _onLoadData(HomeLoadDataEvent event, Emitter emit){ + emit(state.copyWith(data: repo.loadData())); + } +} \ No newline at end of file diff --git a/lib/presentation/home_page/bloc/events.dart b/lib/presentation/home_page/bloc/events.dart new file mode 100644 index 0000000..2dd4a3d --- /dev/null +++ b/lib/presentation/home_page/bloc/events.dart @@ -0,0 +1,6 @@ +abstract class HomeEvent{ + const HomeEvent(); +} +class HomeLoadDataEvent extends HomeEvent{ + const HomeLoadDataEvent(); +} \ No newline at end of file diff --git a/lib/presentation/home_page/bloc/state.dart b/lib/presentation/home_page/bloc/state.dart new file mode 100644 index 0000000..f549c44 --- /dev/null +++ b/lib/presentation/home_page/bloc/state.dart @@ -0,0 +1,10 @@ +import 'package:equatable/equatable.dart'; +import 'package:pmu/domain/models/card.dart'; + +class HomeState extends Equatable{ + final Future?>? data; + const HomeState({this.data}); + HomeState copyWith({Future?>? data}) => HomeState(data: data ?? this.data); + @override + List get props => [data]; +} \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index afba81d..a6d507e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -38,6 +38,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.11.0" + bloc: + dependency: transitive + description: + name: bloc + sha256: "106842ad6569f0b60297619e9e0b1885c2fb9bf84812935490e6c5275777804e" + url: "https://pub.dev" + source: hosted + version: "8.1.4" boolean_selector: dependency: transitive description: @@ -198,6 +206,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.0" + equatable: + dependency: "direct main" + description: + name: equatable + sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2 + url: "https://pub.dev" + source: hosted + version: "2.0.5" fake_async: dependency: transitive description: @@ -227,6 +243,14 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_bloc: + dependency: "direct main" + description: + name: flutter_bloc + sha256: b594505eac31a0518bdcb4b5b79573b8d9117b193cc80cc12e17d639b10aa27a + url: "https://pub.dev" + source: hosted + version: "8.1.6" flutter_lints: dependency: "direct dev" description: @@ -392,6 +416,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.6" + nested: + dependency: transitive + description: + name: nested + sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20" + url: "https://pub.dev" + source: hosted + version: "1.0.0" package_config: dependency: transitive description: @@ -424,6 +456,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.4.0" + provider: + dependency: transitive + description: + name: provider + sha256: c8a055ee5ce3fd98d6fc872478b03823ffdb448699c6ebdbbc71d59b596fd48c + url: "https://pub.dev" + source: hosted + version: "6.1.2" pub_semver: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 091c7e0..2d37e3a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,8 @@ dependencies: json_annotation: ^4.8.1 dio: ^5.4.2+1 pretty_dio_logger: ^1.3.1 + equatable: ^2.0.5 + flutter_bloc: ^8.1.5 dev_dependencies: flutter_test: