Skip to content

Commit 6eb988a

Browse files
committed
refactor(content_management): implement ThrottledFetchingService for shared data fetching
- Introduce ThrottledFetchingService to replace inline fetching logic - Update constructor to include ThrottledFetchingService - Modify _onSharedDataRequested to use the new fetching service - Remove redundant fetching logic from the BLoC
1 parent 776f9e0 commit 6eb988a

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

lib/content_management/bloc/content_management_bloc.dart

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:bloc/bloc.dart';
22
import 'package:core/core.dart';
33
import 'package:data_repository/data_repository.dart';
44
import 'package:equatable/equatable.dart';
5+
import 'package:flutter_news_app_web_dashboard_full_source_code/shared/services/throttled_fetching_service.dart';
56

67
part 'content_management_event.dart';
78
part 'content_management_state.dart';
@@ -26,12 +27,14 @@ class ContentManagementBloc
2627
required DataRepository<Source> sourcesRepository,
2728
required DataRepository<Country> countriesRepository,
2829
required DataRepository<Language> languagesRepository,
29-
}) : _headlinesRepository = headlinesRepository,
30-
_topicsRepository = topicsRepository,
31-
_sourcesRepository = sourcesRepository,
32-
_countriesRepository = countriesRepository,
33-
_languagesRepository = languagesRepository,
34-
super(const ContentManagementState()) {
30+
required ThrottledFetchingService fetchingService,
31+
}) : _headlinesRepository = headlinesRepository,
32+
_topicsRepository = topicsRepository,
33+
_sourcesRepository = sourcesRepository,
34+
_countriesRepository = countriesRepository,
35+
_languagesRepository = languagesRepository,
36+
_fetchingService = fetchingService,
37+
super(const ContentManagementState()) {
3538
on<SharedDataRequested>(_onSharedDataRequested);
3639
on<ContentManagementTabChanged>(_onContentManagementTabChanged);
3740
on<LoadHeadlinesRequested>(_onLoadHeadlinesRequested);
@@ -50,39 +53,12 @@ class ContentManagementBloc
5053
final DataRepository<Source> _sourcesRepository;
5154
final DataRepository<Country> _countriesRepository;
5255
final DataRepository<Language> _languagesRepository;
56+
final ThrottledFetchingService _fetchingService;
5357

54-
// --- Background Data Fetching for countries/languages for the ui Dropdown ---
55-
//
56-
// The DropdownButtonFormField widget does not natively support on-scroll
57-
// pagination. To preserve UI consistency across the application, this BLoC
58-
// employs an event-driven background fetching mechanism.
5958
Future<void> _onSharedDataRequested(
6059
SharedDataRequested event,
6160
Emitter<ContentManagementState> emit,
6261
) async {
63-
// Helper function to fetch all items of a given type.
64-
Future<List<T>> fetchAll<T>({
65-
required DataRepository<T> repository,
66-
required List<SortOption> sort,
67-
}) async {
68-
final allItems = <T>[];
69-
String? cursor;
70-
bool hasMore;
71-
72-
do {
73-
final response = await repository.readAll(
74-
sort: sort,
75-
pagination: PaginationOptions(cursor: cursor),
76-
filter: {'status': ContentStatus.active.name},
77-
);
78-
allItems.addAll(response.items);
79-
cursor = response.cursor;
80-
hasMore = response.hasMore;
81-
} while (hasMore);
82-
83-
return allItems;
84-
}
85-
8662
// Check if data is already loaded or is currently loading to prevent
8763
// redundant fetches.
8864
if (state.allCountriesStatus == ContentManagementStatus.success &&
@@ -99,13 +75,13 @@ class ContentManagementBloc
9975
);
10076

10177
try {
102-
// Fetch both lists in parallel.
78+
// Fetch both lists in parallel using the dedicated fetching service.
10379
final results = await Future.wait([
104-
fetchAll<Country>(
80+
_fetchingService.fetchAll<Country>(
10581
repository: _countriesRepository,
10682
sort: [const SortOption('name', SortOrder.asc)],
10783
),
108-
fetchAll<Language>(
84+
_fetchingService.fetchAll<Language>(
10985
repository: _languagesRepository,
11086
sort: [const SortOption('name', SortOrder.asc)],
11187
),

0 commit comments

Comments
 (0)