From 0231d636892bc31def9ae54d56c54ec36e8f9346 Mon Sep 17 00:00:00 2001 From: Fajrian Aidil Pratama Date: Mon, 19 Feb 2024 13:46:04 +0800 Subject: [PATCH] refactor(bloc): replace latest national statistic bloc with cubit --- app/pico/lib/app.dart | 5 +-- .../lib/src/presentation/blocs/blocs.dart | 1 - .../latest_national_statistic_bloc.dart | 33 ------------------- .../latest_national_statistic_event.dart | 8 ----- .../lib/src/presentation/cubits/cubits.dart | 1 + .../latest_national_statistic_cubit.dart | 25 ++++++++++++++ .../latest_national_statistic_state.dart | 7 ++-- .../pages/national_statistic_page.dart | 12 +++---- .../national_statistic_summary_section.dart | 9 +++-- ...latest_national_statistic_cubit_test.dart} | 16 ++++----- 10 files changed, 47 insertions(+), 70 deletions(-) delete mode 100644 features/statistic/lib/src/presentation/blocs/latest_national_statistic/latest_national_statistic_bloc.dart delete mode 100644 features/statistic/lib/src/presentation/blocs/latest_national_statistic/latest_national_statistic_event.dart create mode 100644 features/statistic/lib/src/presentation/cubits/latest_national_statistic/latest_national_statistic_cubit.dart rename features/statistic/lib/src/presentation/{blocs => cubits}/latest_national_statistic/latest_national_statistic_state.dart (70%) rename features/statistic/test/src/presentation/{blocs/latest_national_statistic/latest_national_statistic_bloc_test.dart => cubits/latest_national_statistic/latest_national_statistic_cubit_test.dart} (82%) diff --git a/app/pico/lib/app.dart b/app/pico/lib/app.dart index 106f18c..68526ba 100644 --- a/app/pico/lib/app.dart +++ b/app/pico/lib/app.dart @@ -23,10 +23,7 @@ class App extends StatelessWidget { create: (context) => LatestStatisticCubit(), ), BlocProvider( - create: (context) => LatestNationalStatisticBloc() - ..add( - LatestNationalStatisticEvent.load(), - ), + create: (context) => LatestNationalStatisticCubit()..fetch(), ), BlocProvider( create: (context) => LatestCovidTestBloc(), diff --git a/features/statistic/lib/src/presentation/blocs/blocs.dart b/features/statistic/lib/src/presentation/blocs/blocs.dart index a32a545..ad74078 100644 --- a/features/statistic/lib/src/presentation/blocs/blocs.dart +++ b/features/statistic/lib/src/presentation/blocs/blocs.dart @@ -1,2 +1 @@ export 'latest_covid_test/latest_covid_test_bloc.dart'; -export 'latest_national_statistic/latest_national_statistic_bloc.dart'; diff --git a/features/statistic/lib/src/presentation/blocs/latest_national_statistic/latest_national_statistic_bloc.dart b/features/statistic/lib/src/presentation/blocs/latest_national_statistic/latest_national_statistic_bloc.dart deleted file mode 100644 index 2692e5a..0000000 --- a/features/statistic/lib/src/presentation/blocs/latest_national_statistic/latest_national_statistic_bloc.dart +++ /dev/null @@ -1,33 +0,0 @@ -import 'package:core/core.dart'; -import 'package:dependencies/dependencies.dart'; -import 'package:statistic/src/domain/usecases/get_latest_national_statistic.dart'; -import 'package:statistic/statistic.dart'; - -part 'latest_national_statistic_event.dart'; -part 'latest_national_statistic_state.dart'; -part 'latest_national_statistic_bloc.freezed.dart'; - -class LatestNationalStatisticBloc - extends Bloc { - LatestNationalStatisticBloc() - : super(LatestNationalStatisticState.initial()) { - on( - (event, emit) async { - if (event is _LoadLatestNationalStatisticEvent) { - emit(LatestNationalStatisticState.loading()); - final result = await _usecase.call(NoParams()); - result.fold( - (l) => emit( - LatestNationalStatisticState.failed(failure: l), - ), - (r) => emit( - LatestNationalStatisticState.loaded(data: r), - ), - ); - } - }, - ); - } - - final _usecase = getIt(); -} diff --git a/features/statistic/lib/src/presentation/blocs/latest_national_statistic/latest_national_statistic_event.dart b/features/statistic/lib/src/presentation/blocs/latest_national_statistic/latest_national_statistic_event.dart deleted file mode 100644 index 1e1ef17..0000000 --- a/features/statistic/lib/src/presentation/blocs/latest_national_statistic/latest_national_statistic_event.dart +++ /dev/null @@ -1,8 +0,0 @@ -part of 'latest_national_statistic_bloc.dart'; - -@freezed -abstract class LatestNationalStatisticEvent - with _$LatestNationalStatisticEvent { - factory LatestNationalStatisticEvent.load() = - _LoadLatestNationalStatisticEvent; -} diff --git a/features/statistic/lib/src/presentation/cubits/cubits.dart b/features/statistic/lib/src/presentation/cubits/cubits.dart index e6f899c..a93451e 100644 --- a/features/statistic/lib/src/presentation/cubits/cubits.dart +++ b/features/statistic/lib/src/presentation/cubits/cubits.dart @@ -1,5 +1,6 @@ export 'all_national_statistics/all_national_statistics_cubit.dart'; export 'all_statistic_filter/all_statistic_filter_cubit.dart'; export 'all_statistics/all_statistics_cubit.dart'; +export 'latest_national_statistic/latest_national_statistic_cubit.dart'; export 'latest_statistic/latest_statistic_cubit.dart'; export 'line_chart_filter/line_chart_filter_cubit.dart'; diff --git a/features/statistic/lib/src/presentation/cubits/latest_national_statistic/latest_national_statistic_cubit.dart b/features/statistic/lib/src/presentation/cubits/latest_national_statistic/latest_national_statistic_cubit.dart new file mode 100644 index 0000000..86be6f0 --- /dev/null +++ b/features/statistic/lib/src/presentation/cubits/latest_national_statistic/latest_national_statistic_cubit.dart @@ -0,0 +1,25 @@ +import 'package:core/core.dart'; +import 'package:dependencies/dependencies.dart'; +import 'package:statistic/src/domain/usecases/get_latest_national_statistic.dart'; +import 'package:statistic/statistic.dart'; + +part 'latest_national_statistic_state.dart'; +part 'latest_national_statistic_cubit.freezed.dart'; + +class LatestNationalStatisticCubit extends Cubit { + LatestNationalStatisticCubit() + : super( + LatestNationalStatisticState.initial(), + ); + + final _usecase = getIt(); + + Future fetch() async { + emit(LatestNationalStatisticState.loading()); + final result = await _usecase.call(NoParams()); + result.fold( + (l) => emit(LatestNationalStatisticState.failed(failure: l)), + (r) => emit(LatestNationalStatisticState.loaded(data: r)), + ); + } +} diff --git a/features/statistic/lib/src/presentation/blocs/latest_national_statistic/latest_national_statistic_state.dart b/features/statistic/lib/src/presentation/cubits/latest_national_statistic/latest_national_statistic_state.dart similarity index 70% rename from features/statistic/lib/src/presentation/blocs/latest_national_statistic/latest_national_statistic_state.dart rename to features/statistic/lib/src/presentation/cubits/latest_national_statistic/latest_national_statistic_state.dart index 2540ff1..1920a07 100644 --- a/features/statistic/lib/src/presentation/blocs/latest_national_statistic/latest_national_statistic_state.dart +++ b/features/statistic/lib/src/presentation/cubits/latest_national_statistic/latest_national_statistic_state.dart @@ -1,4 +1,4 @@ -part of 'latest_national_statistic_bloc.dart'; +part of 'latest_national_statistic_cubit.dart'; @freezed abstract class LatestNationalStatisticState @@ -7,8 +7,9 @@ abstract class LatestNationalStatisticState LatestNationalStatisticInitialState; factory LatestNationalStatisticState.loading() = LatestNationalStatisticLoadingState; - factory LatestNationalStatisticState.failed({required Failure failure}) = - LatestNationalStatisticFailedState; + factory LatestNationalStatisticState.failed({ + required Failure failure, + }) = LatestNationalStatisticFailedState; factory LatestNationalStatisticState.loaded({ required Statistic data, }) = LatestNationalStatisticLoadedState; diff --git a/features/statistic/lib/src/presentation/pages/national_statistic_page.dart b/features/statistic/lib/src/presentation/pages/national_statistic_page.dart index 2fbf0da..7b96851 100644 --- a/features/statistic/lib/src/presentation/pages/national_statistic_page.dart +++ b/features/statistic/lib/src/presentation/pages/national_statistic_page.dart @@ -34,18 +34,14 @@ class _NationalStatisticPageState extends State { void _onLoading() { _refreshCount = 1; _errorCount = 0; - context.read().add( - LatestNationalStatisticEvent.load(), - ); + context.read().fetch(); } void _onRefresh() { _refreshCount = 1; _errorCount = 0; _refreshController.requestRefresh(); - context.read().add( - LatestNationalStatisticEvent.load(), - ); + context.read().fetch(); } void _onBlocRefreshCompleted() { @@ -77,7 +73,7 @@ class _NationalStatisticPageState extends State { return Scaffold( body: MultiBlocListener( listeners: [ - BlocListener( listener: (context, state) { if (state is LatestNationalStatisticLoadedState) { @@ -96,7 +92,7 @@ class _NationalStatisticPageState extends State { surfaceTintColor: context.picoColors.background.neutral.main, shadowColor: context.picoColors.text.neutral.main.withOpacity(0.1), - title: BlocBuilder( builder: (context, state) => AnimatedSwitcher( duration: 300.milliseconds, diff --git a/features/statistic/lib/src/presentation/widgets/national_statistic_summary_section.dart b/features/statistic/lib/src/presentation/widgets/national_statistic_summary_section.dart index fbe9a43..d9644c1 100644 --- a/features/statistic/lib/src/presentation/widgets/national_statistic_summary_section.dart +++ b/features/statistic/lib/src/presentation/widgets/national_statistic_summary_section.dart @@ -3,15 +3,15 @@ import 'package:dependencies/dependencies.dart'; import 'package:flutter/material.dart'; import 'package:i10n/i10n.dart'; import 'package:pico_ui_kit/pico_ui_kit.dart'; -import 'package:statistic/src/presentation/blocs/blocs.dart'; import 'package:statistic/src/presentation/widgets/statistic_data_list.dart'; +import 'package:statistic/statistic.dart'; class NationalStatisticSummarySection extends StatelessWidget { const NationalStatisticSummarySection({super.key}); @override Widget build(BuildContext context) => - BlocBuilder( + BlocBuilder( builder: (context, state) => AnimatedSwitcher( duration: 300.milliseconds, child: switch (state) { @@ -23,9 +23,8 @@ class NationalStatisticSummarySection extends StatelessWidget { ), LatestNationalStatisticFailedState() => PicoErrorPlaceholder( label: context.i10n.error.statistic, - onRetry: () => context.read().add( - LatestNationalStatisticEvent.load(), - ), + onRetry: () => + context.read().fetch(), ), _ => StatisticDataList( key: const Key('statistic-loading'), diff --git a/features/statistic/test/src/presentation/blocs/latest_national_statistic/latest_national_statistic_bloc_test.dart b/features/statistic/test/src/presentation/cubits/latest_national_statistic/latest_national_statistic_cubit_test.dart similarity index 82% rename from features/statistic/test/src/presentation/blocs/latest_national_statistic/latest_national_statistic_bloc_test.dart rename to features/statistic/test/src/presentation/cubits/latest_national_statistic/latest_national_statistic_cubit_test.dart index 309f7c0..8d7bb91 100644 --- a/features/statistic/test/src/presentation/blocs/latest_national_statistic/latest_national_statistic_bloc_test.dart +++ b/features/statistic/test/src/presentation/cubits/latest_national_statistic/latest_national_statistic_cubit_test.dart @@ -40,19 +40,19 @@ void main() { group( 'LatestNationalStatisticBloc', () { - blocTest( + blocTest( 'should emit nothing when initialize', - build: LatestNationalStatisticBloc.new, + build: LatestNationalStatisticCubit.new, expect: () => [], ); - blocTest( + blocTest( 'should emit [Loading,Loaded] states when success', - build: LatestNationalStatisticBloc.new, + build: LatestNationalStatisticCubit.new, setUp: () => when(() => mockUseCase.call(NoParams())).thenAnswer( (_) async => Right(data), ), - act: (bloc) => bloc.add(LatestNationalStatisticEvent.load()), + act: (cubit) => cubit.fetch(), expect: () => [ LatestNationalStatisticState.loading(), LatestNationalStatisticState.loaded(data: data), @@ -60,13 +60,13 @@ void main() { verify: (_) => verify(() => mockUseCase.call(NoParams())), ); - blocTest( + blocTest( 'should emit [Loading,Failed] states when failed', - build: LatestNationalStatisticBloc.new, + build: LatestNationalStatisticCubit.new, setUp: () => when(() => mockUseCase.call(NoParams())).thenAnswer( (_) async => const Left(StatisticFailure(message: errorMessage)), ), - act: (bloc) => bloc.add(LatestNationalStatisticEvent.load()), + act: (cubit) => cubit.fetch(), expect: () => [ LatestNationalStatisticState.loading(), LatestNationalStatisticState.failed(