-
Notifications
You must be signed in to change notification settings - Fork 0
Feature dashboard #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d3eaf31
14ac870
3dbf219
bbaab62
5f3b14e
562a6c1
a099827
afe941c
b517c0a
1d05c3c
40ce039
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:ht_data_repository/ht_data_repository.dart'; | ||
import 'package:ht_shared/ht_shared.dart'; | ||
|
||
part 'dashboard_event.dart'; | ||
part 'dashboard_state.dart'; | ||
|
||
/// A BLoC to manage the state of the dashboard. | ||
class DashboardBloc extends Bloc<DashboardEvent, DashboardState> { | ||
/// {@macro dashboard_bloc} | ||
DashboardBloc({ | ||
required HtDataRepository<DashboardSummary> dashboardSummaryRepository, | ||
required HtDataRepository<AppConfig> appConfigRepository, | ||
required HtDataRepository<Headline> headlinesRepository, | ||
}) : _dashboardSummaryRepository = dashboardSummaryRepository, | ||
_appConfigRepository = appConfigRepository, | ||
_headlinesRepository = headlinesRepository, | ||
super(const DashboardState()) { | ||
on<DashboardSummaryLoaded>(_onDashboardSummaryLoaded); | ||
} | ||
|
||
final HtDataRepository<DashboardSummary> _dashboardSummaryRepository; | ||
final HtDataRepository<AppConfig> _appConfigRepository; | ||
final HtDataRepository<Headline> _headlinesRepository; | ||
|
||
Future<void> _onDashboardSummaryLoaded( | ||
DashboardSummaryLoaded event, | ||
Emitter<DashboardState> emit, | ||
) async { | ||
emit(state.copyWith(status: DashboardStatus.loading)); | ||
try { | ||
// Fetch summary, app config, and recent headlines concurrently | ||
final [ | ||
summaryResponse, | ||
appConfigResponse, | ||
recentHeadlinesResponse, | ||
] = await Future.wait([ | ||
_dashboardSummaryRepository.read(id: 'dashboard_summary'), | ||
_appConfigRepository.read(id: 'app_config'), | ||
_headlinesRepository.readAll( | ||
sortBy: 'createdAt', | ||
sortOrder: SortOrder.desc, | ||
limit: 5, | ||
), | ||
]); | ||
|
||
final summary = summaryResponse as DashboardSummary; | ||
final appConfig = appConfigResponse as AppConfig; | ||
final recentHeadlines = | ||
(recentHeadlinesResponse as PaginatedResponse<Headline>).items; | ||
emit( | ||
state.copyWith( | ||
status: DashboardStatus.success, | ||
summary: summary, | ||
appConfig: appConfig, | ||
recentHeadlines: recentHeadlines, | ||
), | ||
); | ||
} on HtHttpException catch (e) { | ||
emit( | ||
state.copyWith( | ||
status: DashboardStatus.failure, | ||
errorMessage: e.message, | ||
), | ||
); | ||
} catch (e) { | ||
emit( | ||
state.copyWith( | ||
status: DashboardStatus.failure, | ||
errorMessage: 'An unknown error occurred: $e', | ||
), | ||
); | ||
} | ||
Comment on lines
+67
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The generic
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
part of 'dashboard_bloc.dart'; | ||
|
||
/// Base class for dashboard events. | ||
sealed class DashboardEvent extends Equatable { | ||
const DashboardEvent(); | ||
|
||
@override | ||
List<Object> get props => []; | ||
} | ||
|
||
/// Event to load the dashboard summary data. | ||
final class DashboardSummaryLoaded extends DashboardEvent {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
part of 'dashboard_bloc.dart'; | ||
|
||
/// Represents the status of the dashboard data loading. | ||
enum DashboardStatus { | ||
/// Initial state. | ||
initial, | ||
|
||
/// Data is being loaded. | ||
loading, | ||
|
||
/// Data has been successfully loaded. | ||
success, | ||
|
||
/// An error occurred while loading data. | ||
failure, | ||
} | ||
|
||
/// The state for the [DashboardBloc]. | ||
final class DashboardState extends Equatable { | ||
const DashboardState({ | ||
this.status = DashboardStatus.initial, | ||
this.summary, | ||
this.appConfig, | ||
this.recentHeadlines = const [], | ||
this.errorMessage, | ||
}); | ||
|
||
final DashboardStatus status; | ||
final DashboardSummary? summary; | ||
final AppConfig? appConfig; | ||
final List<Headline> recentHeadlines; | ||
final String? errorMessage; | ||
|
||
DashboardState copyWith({ | ||
DashboardStatus? status, | ||
DashboardSummary? summary, | ||
AppConfig? appConfig, | ||
List<Headline>? recentHeadlines, | ||
String? errorMessage, | ||
}) { | ||
return DashboardState( | ||
status: status ?? this.status, | ||
summary: summary ?? this.summary, | ||
appConfig: appConfig ?? this.appConfig, | ||
recentHeadlines: recentHeadlines ?? this.recentHeadlines, | ||
errorMessage: errorMessage ?? this.errorMessage, | ||
); | ||
} | ||
|
||
@override | ||
List<Object?> get props => [ | ||
status, | ||
summary, | ||
appConfig, | ||
recentHeadlines, | ||
errorMessage, | ||
]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block of code for initializing
dashboardSummaryClient
is duplicated in theproduction
environment block (lines 199-204). Consider refactoring to avoid this duplication to improve maintainability.