@@ -2,6 +2,7 @@ import 'package:bloc/bloc.dart';
2
2
import 'package:core/core.dart' ;
3
3
import 'package:data_repository/data_repository.dart' ;
4
4
import 'package:equatable/equatable.dart' ;
5
+ import 'package:flutter_news_app_web_dashboard_full_source_code/shared/services/throttled_fetching_service.dart' ;
5
6
6
7
part 'content_management_event.dart' ;
7
8
part 'content_management_state.dart' ;
@@ -26,12 +27,14 @@ class ContentManagementBloc
26
27
required DataRepository <Source > sourcesRepository,
27
28
required DataRepository <Country > countriesRepository,
28
29
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 ()) {
35
38
on < SharedDataRequested > (_onSharedDataRequested);
36
39
on < ContentManagementTabChanged > (_onContentManagementTabChanged);
37
40
on < LoadHeadlinesRequested > (_onLoadHeadlinesRequested);
@@ -50,39 +53,12 @@ class ContentManagementBloc
50
53
final DataRepository <Source > _sourcesRepository;
51
54
final DataRepository <Country > _countriesRepository;
52
55
final DataRepository <Language > _languagesRepository;
56
+ final ThrottledFetchingService _fetchingService;
53
57
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.
59
58
Future <void > _onSharedDataRequested (
60
59
SharedDataRequested event,
61
60
Emitter <ContentManagementState > emit,
62
61
) 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
-
86
62
// Check if data is already loaded or is currently loading to prevent
87
63
// redundant fetches.
88
64
if (state.allCountriesStatus == ContentManagementStatus .success &&
@@ -99,13 +75,13 @@ class ContentManagementBloc
99
75
);
100
76
101
77
try {
102
- // Fetch both lists in parallel.
78
+ // Fetch both lists in parallel using the dedicated fetching service .
103
79
final results = await Future .wait ([
104
- fetchAll <Country >(
80
+ _fetchingService. fetchAll <Country >(
105
81
repository: _countriesRepository,
106
82
sort: [const SortOption ('name' , SortOrder .asc)],
107
83
),
108
- fetchAll <Language >(
84
+ _fetchingService. fetchAll <Language >(
109
85
repository: _languagesRepository,
110
86
sort: [const SortOption ('name' , SortOrder .asc)],
111
87
),
0 commit comments