Skip to content

Commit

Permalink
refactor(bloc): replace latest national statistic bloc with cubit
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanaidilp committed Feb 19, 2024
1 parent cc4cf84 commit 0231d63
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 70 deletions.
5 changes: 1 addition & 4 deletions app/pico/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 0 additions & 1 deletion features/statistic/lib/src/presentation/blocs/blocs.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export 'latest_covid_test/latest_covid_test_bloc.dart';
export 'latest_national_statistic/latest_national_statistic_bloc.dart';

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions features/statistic/lib/src/presentation/cubits/cubits.dart
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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<LatestNationalStatisticState> {
LatestNationalStatisticCubit()
: super(
LatestNationalStatisticState.initial(),
);

final _usecase = getIt<GetLatestNationalStatistic>();

Future<void> 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)),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of 'latest_national_statistic_bloc.dart';
part of 'latest_national_statistic_cubit.dart';

@freezed
abstract class LatestNationalStatisticState
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,14 @@ class _NationalStatisticPageState extends State<NationalStatisticPage> {
void _onLoading() {
_refreshCount = 1;
_errorCount = 0;
context.read<LatestNationalStatisticBloc>().add(
LatestNationalStatisticEvent.load(),
);
context.read<LatestNationalStatisticCubit>().fetch();
}

void _onRefresh() {
_refreshCount = 1;
_errorCount = 0;
_refreshController.requestRefresh();
context.read<LatestNationalStatisticBloc>().add(
LatestNationalStatisticEvent.load(),
);
context.read<LatestNationalStatisticCubit>().fetch();
}

void _onBlocRefreshCompleted() {
Expand Down Expand Up @@ -77,7 +73,7 @@ class _NationalStatisticPageState extends State<NationalStatisticPage> {
return Scaffold(
body: MultiBlocListener(
listeners: [
BlocListener<LatestNationalStatisticBloc,
BlocListener<LatestNationalStatisticCubit,
LatestNationalStatisticState>(
listener: (context, state) {
if (state is LatestNationalStatisticLoadedState) {
Expand All @@ -96,7 +92,7 @@ class _NationalStatisticPageState extends State<NationalStatisticPage> {
surfaceTintColor: context.picoColors.background.neutral.main,
shadowColor:
context.picoColors.text.neutral.main.withOpacity(0.1),
title: BlocBuilder<LatestNationalStatisticBloc,
title: BlocBuilder<LatestNationalStatisticCubit,
LatestNationalStatisticState>(
builder: (context, state) => AnimatedSwitcher(
duration: 300.milliseconds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LatestNationalStatisticBloc, LatestNationalStatisticState>(
BlocBuilder<LatestNationalStatisticCubit, LatestNationalStatisticState>(
builder: (context, state) => AnimatedSwitcher(
duration: 300.milliseconds,
child: switch (state) {
Expand All @@ -23,9 +23,8 @@ class NationalStatisticSummarySection extends StatelessWidget {
),
LatestNationalStatisticFailedState() => PicoErrorPlaceholder(
label: context.i10n.error.statistic,
onRetry: () => context.read<LatestNationalStatisticBloc>().add(
LatestNationalStatisticEvent.load(),
),
onRetry: () =>
context.read<LatestNationalStatisticCubit>().fetch(),
),
_ => StatisticDataList(
key: const Key('statistic-loading'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,33 @@ void main() {
group(
'LatestNationalStatisticBloc',
() {
blocTest<LatestNationalStatisticBloc, LatestNationalStatisticState>(
blocTest<LatestNationalStatisticCubit, LatestNationalStatisticState>(
'should emit nothing when initialize',
build: LatestNationalStatisticBloc.new,
build: LatestNationalStatisticCubit.new,
expect: () => <LatestNationalStatisticState>[],
);

blocTest<LatestNationalStatisticBloc, LatestNationalStatisticState>(
blocTest<LatestNationalStatisticCubit, LatestNationalStatisticState>(
'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>[
LatestNationalStatisticState.loading(),
LatestNationalStatisticState.loaded(data: data),
],
verify: (_) => verify(() => mockUseCase.call(NoParams())),
);

blocTest<LatestNationalStatisticBloc, LatestNationalStatisticState>(
blocTest<LatestNationalStatisticCubit, LatestNationalStatisticState>(
'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>[
LatestNationalStatisticState.loading(),
LatestNationalStatisticState.failed(
Expand Down

0 comments on commit 0231d63

Please sign in to comment.