Skip to content

Commit 14ac870

Browse files
committed
feat(dashboard): introduces the business logic layer for the dashboard feature.
It includes: - DashboardState: Defines the possible states for the dashboard UI (initial, loading, success, failure) and holds the - DashboardSummary data. - DashboardEvent: Defines the events that can be dispatched to the BLoC, starting with DashboardSummaryLoaded. - DashboardBloc: Manages the dashboard's state by fetching summary data from the HtDataRepository<DashboardSummary> and emitting the appropriate states. dashboard.dart: A barrel file for easy importing of BLoC components. The new DashboardBloc has also been registered in app.dart using BlocProvider, making it available throughout the widget tree for the upcoming UI implementation.
1 parent d3eaf31 commit 14ac870

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

lib/app/view/app.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:ht_dashboard/app/config/app_environment.dart';
1111
import 'package:ht_dashboard/app_configuration/bloc/app_configuration_bloc.dart';
1212
import 'package:ht_dashboard/authentication/bloc/authentication_bloc.dart';
1313
import 'package:ht_dashboard/content_management/bloc/content_management_bloc.dart';
14+
import 'package:ht_dashboard/dashboard/bloc/dashboard_bloc.dart';
1415
import 'package:ht_dashboard/l10n/app_localizations.dart';
1516
import 'package:ht_dashboard/router/router.dart';
1617
// Import for app_theme.dart
@@ -102,6 +103,12 @@ class App extends StatelessWidget {
102103
sourcesRepository: context.read<HtDataRepository<Source>>(),
103104
),
104105
),
106+
BlocProvider(
107+
create: (context) => DashboardBloc(
108+
dashboardSummaryRepository: context
109+
.read<HtDataRepository<DashboardSummary>>(),
110+
),
111+
),
105112
],
106113
child: _AppView(
107114
htAuthenticationRepository: _htAuthenticationRepository,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'package:bloc/bloc.dart';
2+
import 'package:equatable/equatable.dart';
3+
import 'package:ht_data_repository/ht_data_repository.dart';
4+
import 'package:ht_shared/ht_shared.dart';
5+
6+
part 'dashboard_event.dart';
7+
part 'dashboard_state.dart';
8+
9+
/// A BLoC to manage the state of the dashboard.
10+
class DashboardBloc extends Bloc<DashboardEvent, DashboardState> {
11+
/// {@macro dashboard_bloc}
12+
DashboardBloc({
13+
required HtDataRepository<DashboardSummary> dashboardSummaryRepository,
14+
}) : _dashboardSummaryRepository = dashboardSummaryRepository,
15+
super(const DashboardState()) {
16+
on<DashboardSummaryLoaded>(_onDashboardSummaryLoaded);
17+
}
18+
19+
final HtDataRepository<DashboardSummary> _dashboardSummaryRepository;
20+
21+
Future<void> _onDashboardSummaryLoaded(
22+
DashboardSummaryLoaded event,
23+
Emitter<DashboardState> emit,
24+
) async {
25+
emit(state.copyWith(status: DashboardStatus.loading));
26+
try {
27+
// The dashboard summary is a singleton-like resource, fetched by a
28+
// well-known ID.
29+
final summary = await _dashboardSummaryRepository.read(id: 'summary');
30+
emit(
31+
state.copyWith(
32+
status: DashboardStatus.success,
33+
summary: summary,
34+
),
35+
);
36+
} on HtHttpException catch (e) {
37+
emit(
38+
state.copyWith(
39+
status: DashboardStatus.failure,
40+
errorMessage: e.message,
41+
),
42+
);
43+
} catch (e) {
44+
emit(
45+
state.copyWith(
46+
status: DashboardStatus.failure,
47+
errorMessage: 'An unknown error occurred: $e',
48+
),
49+
);
50+
}
51+
}
52+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
part of 'dashboard_bloc.dart';
2+
3+
/// Base class for dashboard events.
4+
sealed class DashboardEvent extends Equatable {
5+
const DashboardEvent();
6+
7+
@override
8+
List<Object> get props => [];
9+
}
10+
11+
/// Event to load the dashboard summary data.
12+
final class DashboardSummaryLoaded extends DashboardEvent {}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
part of 'dashboard_bloc.dart';
2+
3+
/// Represents the status of the dashboard data loading.
4+
enum DashboardStatus {
5+
/// Initial state.
6+
initial,
7+
8+
/// Data is being loaded.
9+
loading,
10+
11+
/// Data has been successfully loaded.
12+
success,
13+
14+
/// An error occurred while loading data.
15+
failure,
16+
}
17+
18+
/// The state for the [DashboardBloc].
19+
final class DashboardState extends Equatable {
20+
const DashboardState({
21+
this.status = DashboardStatus.initial,
22+
this.summary,
23+
this.errorMessage,
24+
});
25+
26+
final DashboardStatus status;
27+
final DashboardSummary? summary;
28+
final String? errorMessage;
29+
30+
DashboardState copyWith({
31+
DashboardStatus? status,
32+
DashboardSummary? summary,
33+
String? errorMessage,
34+
}) {
35+
return DashboardState(
36+
status: status ?? this.status,
37+
summary: summary ?? this.summary,
38+
errorMessage: errorMessage ?? this.errorMessage,
39+
);
40+
}
41+
42+
@override
43+
List<Object?> get props => [status, summary, errorMessage];
44+
}

0 commit comments

Comments
 (0)