Skip to content

Dashboard content management state sync #53

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

Merged
merged 5 commits into from
Aug 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/app/view/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ class App extends StatelessWidget {
),
BlocProvider(
create: (context) => DashboardBloc(
dashboardSummaryRepository: context
.read<DataRepository<DashboardSummary>>(),
dashboardSummaryRepository:
context.read<DataRepository<DashboardSummary>>(),
headlinesRepository: context.read<DataRepository<Headline>>(),
topicsRepository: context.read<DataRepository<Topic>>(),
sourcesRepository: context.read<DataRepository<Source>>(),
),
),
],
Expand Down
1 change: 0 additions & 1 deletion lib/content_management/view/archived_sources_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class _ArchivedSourcesView extends StatelessWidget {
context.read<ContentManagementBloc>().add(
const LoadSourcesRequested(limit: kDefaultRowsPerPage),
);
context.read<DashboardBloc>().add(DashboardSummaryLoaded());
}
},
child: BlocBuilder<ArchivedSourcesBloc, ArchivedSourcesState>(
Expand Down
1 change: 0 additions & 1 deletion lib/content_management/view/archived_topics_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class _ArchivedTopicsView extends StatelessWidget {
context
.read<ContentManagementBloc>()
.add(const LoadTopicsRequested(limit: kDefaultRowsPerPage));
context.read<DashboardBloc>().add( DashboardSummaryLoaded());
}
},
child: BlocBuilder<ArchivedTopicsBloc, ArchivedTopicsState>(
Expand Down
1 change: 0 additions & 1 deletion lib/content_management/view/create_headline_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class _CreateHeadlineViewState extends State<_CreateHeadlineView> {
// Refresh the list to show the new headline
const LoadHeadlinesRequested(limit: kDefaultRowsPerPage),
);
context.read<DashboardBloc>().add(DashboardSummaryLoaded());
context.pop();
}
if (state.status == CreateHeadlineStatus.failure) {
Expand Down
1 change: 0 additions & 1 deletion lib/content_management/view/create_source_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class _CreateSourceViewState extends State<_CreateSourceView> {
// Refresh the list to show the new source
const LoadSourcesRequested(limit: kDefaultRowsPerPage),
);
context.read<DashboardBloc>().add(DashboardSummaryLoaded());
context.pop();
}
if (state.status == CreateSourceStatus.failure) {
Expand Down
1 change: 0 additions & 1 deletion lib/content_management/view/create_topic_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class _CreateTopicViewState extends State<_CreateTopicView> {
// Refresh the list to show the new topic
const LoadTopicsRequested(limit: kDefaultRowsPerPage),
);
context.read<DashboardBloc>().add(DashboardSummaryLoaded());
context.pop();
}
if (state.status == CreateTopicStatus.failure) {
Expand Down
1 change: 0 additions & 1 deletion lib/content_management/view/edit_headline_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class _EditHeadlineViewState extends State<_EditHeadlineView> {
context.read<ContentManagementBloc>().add(
const LoadHeadlinesRequested(limit: kDefaultRowsPerPage),
);
context.read<DashboardBloc>().add(DashboardSummaryLoaded());
context.pop();
}
if (state.status == EditHeadlineStatus.failure) {
Expand Down
1 change: 0 additions & 1 deletion lib/content_management/view/edit_source_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ class _EditSourceViewState extends State<_EditSourceView> {
context.read<ContentManagementBloc>().add(
const LoadSourcesRequested(limit: kDefaultRowsPerPage),
);
context.read<DashboardBloc>().add(DashboardSummaryLoaded());
context.pop();
}
if (state.status == EditSourceStatus.failure) {
Expand Down
1 change: 0 additions & 1 deletion lib/content_management/view/edit_topic_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ class _EditTopicViewState extends State<_EditTopicView> {
context.read<ContentManagementBloc>().add(
const LoadTopicsRequested(limit: kDefaultRowsPerPage),
);
context.read<DashboardBloc>().add(DashboardSummaryLoaded());
context.pop();
}
if (state.status == EditTopicStatus.failure) {
Expand Down
31 changes: 28 additions & 3 deletions lib/dashboard/bloc/dashboard_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:core/core.dart';
import 'package:data_repository/data_repository.dart';
import 'package:equatable/equatable.dart';
import 'package:stream_transform/stream_transform.dart';

part 'dashboard_event.dart';
part 'dashboard_state.dart';
Expand All @@ -12,17 +15,39 @@ class DashboardBloc extends Bloc<DashboardEvent, DashboardState> {
DashboardBloc({
required DataRepository<DashboardSummary> dashboardSummaryRepository,
required DataRepository<Headline> headlinesRepository,
required DataRepository<Topic> topicsRepository,
required DataRepository<Source> sourcesRepository,
}) : _dashboardSummaryRepository = dashboardSummaryRepository,
_headlinesRepository = headlinesRepository,
super(const DashboardState()) {
on<DashboardSummaryLoaded>(_onDashboardSummaryLoaded);
on<DashboardSummaryRequested>(_onDashboardSummaryRequested);
on<_DashboardEntityUpdated>(_onDashboardEntityUpdated);

_entityUpdatedSubscription = headlinesRepository.entityUpdated
.merge(topicsRepository.entityUpdated)
.merge(sourcesRepository.entityUpdated)
.listen((_) => add(const _DashboardEntityUpdated()));
}

final DataRepository<DashboardSummary> _dashboardSummaryRepository;
final DataRepository<Headline> _headlinesRepository;
late final StreamSubscription<void> _entityUpdatedSubscription;

@override
Future<void> close() {
_entityUpdatedSubscription.cancel();
return super.close();
}

void _onDashboardEntityUpdated(
_DashboardEntityUpdated event,
Emitter<DashboardState> emit,
) {
add(DashboardSummaryRequested());
}

Future<void> _onDashboardSummaryLoaded(
DashboardSummaryLoaded event,
Future<void> _onDashboardSummaryRequested(
DashboardSummaryRequested event,
Emitter<DashboardState> emit,
) async {
emit(state.copyWith(status: DashboardStatus.loading));
Expand Down
7 changes: 6 additions & 1 deletion lib/dashboard/bloc/dashboard_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ sealed class DashboardEvent extends Equatable {
}

/// Event to load the dashboard summary data.
final class DashboardSummaryLoaded extends DashboardEvent {}
final class DashboardSummaryRequested extends DashboardEvent {}

/// Internal event triggered when a listened-to entity is updated.
final class _DashboardEntityUpdated extends DashboardEvent {
const _DashboardEntityUpdated();
}
4 changes: 2 additions & 2 deletions lib/dashboard/view/dashboard_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class _DashboardPageState extends State<DashboardPage> {
void initState() {
super.initState();
// Dispatch the event to load dashboard data when the page is initialized.
context.read<DashboardBloc>().add(DashboardSummaryLoaded());
context.read<DashboardBloc>().add(DashboardSummaryRequested());
}

@override
Expand All @@ -44,7 +44,7 @@ class _DashboardPageState extends State<DashboardPage> {
return FailureStateWidget(
exception: state.exception!,
onRetry: () {
context.read<DashboardBloc>().add(DashboardSummaryLoaded());
context.read<DashboardBloc>().add(DashboardSummaryRequested());
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ packages:
description:
path: "."
ref: HEAD
resolved-ref: "5a7bd9f597e98209bc7f5ac897cf64552306c02f"
resolved-ref: d9fbbf33d8bb53724e2dd29d488a0c8eb9cbf6e8
url: "https://github.com/flutter-news-app-full-source-code/data-repository.git"
source: git
version: "0.0.0"
Expand Down
Loading