From 65217136a8d7bbd683008a2c57c5d164105a6a04 Mon Sep 17 00:00:00 2001 From: Serxiolog Date: Mon, 18 Nov 2024 15:35:22 +0400 Subject: [PATCH] Six on 11:47 --- lib/data/mappers/anime_mapper.dart | 2 +- lib/data/repositories/mock_repository.dart | 6 ++-- lib/main.dart | 14 ++++++-- lib/presentation/home_page/bloc/bloc.dart | 18 ++++++++++ lib/presentation/home_page/bloc/events.dart | 8 +++++ lib/presentation/home_page/bloc/state.dart | 15 ++++++++ lib/presentation/home_page/card.dart | 4 +-- pubspec.lock | 40 +++++++++++++++++++++ pubspec.yaml | 3 ++ 9 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 lib/presentation/home_page/bloc/bloc.dart create mode 100644 lib/presentation/home_page/bloc/events.dart create mode 100644 lib/presentation/home_page/bloc/state.dart diff --git a/lib/data/mappers/anime_mapper.dart b/lib/data/mappers/anime_mapper.dart index 287d309..e217d2d 100644 --- a/lib/data/mappers/anime_mapper.dart +++ b/lib/data/mappers/anime_mapper.dart @@ -5,7 +5,7 @@ extension AnimeDataDtoToModel on AnimeDataDto { CardData toDomain() => CardData( title ?? 'NOT', imageUrl: images?.jpg?.image ?? "NONE", - score ?? 8, + score: score ?? 0, description: synopsis == null ? "NONE" : synopsis!.split('\n').sublist(0, synopsis!.split('\n').length - 1).join('\n'), ); } \ No newline at end of file diff --git a/lib/data/repositories/mock_repository.dart b/lib/data/repositories/mock_repository.dart index fb942ff..03b9dce 100644 --- a/lib/data/repositories/mock_repository.dart +++ b/lib/data/repositories/mock_repository.dart @@ -8,12 +8,12 @@ class MockRepository extends ApiInterface { return [ const CardData( "First", - 5, + score: 5, description: "SomeText", ), const CardData( "Second", - 8, + score: 8, icon: Icons.gamepad, description: "ManyText", imageUrl: @@ -21,7 +21,7 @@ class MockRepository extends ApiInterface { ), const CardData( "Third", - 9, + score: 9, icon: Icons.offline_bolt_outlined, description: "Wow >_<", ), diff --git a/lib/main.dart b/lib/main.dart index f805e9d..4367f18 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,8 @@ +import 'package:first_project/data/repositories/anime_repository.dart'; +import 'package:first_project/presentation/home_page/bloc/bloc.dart'; import 'package:first_project/presentation/home_page/home_page.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; void main() { runApp(const MyApp()); @@ -16,8 +19,15 @@ class MyApp extends StatelessWidget { colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), - home: const MyHomePage(title: 'KozyrevSS'), + home: RepositoryProvider( + lazy: true, + create: (_) => AnimeRepository(), + child: BlocProvider( + lazy: false, + create: (context) => HomeBloc(context.read()), + child: const MyHomePage(title: 'Anime Nya >-<'), + ), + ), ); } } - diff --git a/lib/presentation/home_page/bloc/bloc.dart b/lib/presentation/home_page/bloc/bloc.dart new file mode 100644 index 0000000..3d7583b --- /dev/null +++ b/lib/presentation/home_page/bloc/bloc.dart @@ -0,0 +1,18 @@ + + +import 'package:first_project/data/repositories/anime_repository.dart'; +import 'package:first_project/presentation/home_page/bloc/events.dart'; +import 'package:first_project/presentation/home_page/bloc/state.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; + +class HomeBloc extends Bloc { + final AnimeRepository 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..08edf95 --- /dev/null +++ b/lib/presentation/home_page/bloc/events.dart @@ -0,0 +1,8 @@ + +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..d97baf3 --- /dev/null +++ b/lib/presentation/home_page/bloc/state.dart @@ -0,0 +1,15 @@ +import 'package:equatable/equatable.dart'; +import 'package:first_project/domain/models/card.dart'; +import 'package:first_project/presentation/home_page/home_page.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/lib/presentation/home_page/card.dart b/lib/presentation/home_page/card.dart index bb75c67..d3b5c77 100644 --- a/lib/presentation/home_page/card.dart +++ b/lib/presentation/home_page/card.dart @@ -10,10 +10,10 @@ class CardData { final String imageUrl; const CardData( - this.text, - this.score, { + this.text, { required this.description, this.icon = Icons.add_call, + this.score = 0, this.imageUrl = "https://i.pinimg.com/736x/5f/14/b3/5f14b3f14fcd157bc4dffa39085396cc.jpg", }); diff --git a/pubspec.lock b/pubspec.lock index afc0e0f..1f60748 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: "2.0.0" + 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 c415a84..db1c6b0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -39,6 +39,9 @@ dependencies: json_annotation: ^4.8.1 dio: ^5.4.2+1 pretty_dio_logger: ^1.3.1 + #BLoC + equatable: ^2.0.5 + flutter_bloc: ^8.1.5 dev_dependencies: flutter_test: sdk: flutter