Skip to content

Commit c95ca53

Browse files
authored
Merge pull request #71 from flutter-news-app-full-source-code/Optimize-Content-Management-Tab-Switching-to-Prevent-Redundant-Data-Fetches
Optimize content management tab switching to prevent redundant data fetches
2 parents a7d4199 + f7c7c68 commit c95ca53

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

lib/content_management/bloc/content_management_bloc.dart

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,28 @@ class ContentManagementBloc
4242
_headlineUpdateSubscription = _headlinesRepository.entityUpdated
4343
.where((type) => type == Headline)
4444
.listen((_) {
45-
add(const LoadHeadlinesRequested(limit: kDefaultRowsPerPage));
45+
add(const LoadHeadlinesRequested(
46+
limit: kDefaultRowsPerPage,
47+
forceRefresh: true,
48+
));
4649
});
4750

4851
_topicUpdateSubscription = _topicsRepository.entityUpdated
4952
.where((type) => type == Topic)
5053
.listen((_) {
51-
add(const LoadTopicsRequested(limit: kDefaultRowsPerPage));
54+
add(const LoadTopicsRequested(
55+
limit: kDefaultRowsPerPage,
56+
forceRefresh: true,
57+
));
5258
});
5359

5460
_sourceUpdateSubscription = _sourcesRepository.entityUpdated
5561
.where((type) => type == Source)
5662
.listen((_) {
57-
add(const LoadSourcesRequested(limit: kDefaultRowsPerPage));
63+
add(const LoadSourcesRequested(
64+
limit: kDefaultRowsPerPage,
65+
forceRefresh: true,
66+
));
5867
});
5968
}
6069

@@ -85,6 +94,15 @@ class ContentManagementBloc
8594
LoadHeadlinesRequested event,
8695
Emitter<ContentManagementState> emit,
8796
) async {
97+
// If headlines are already loaded and it's not a pagination request,
98+
// do not re-fetch. This prevents redundant API calls on tab changes.
99+
if (state.headlinesStatus == ContentManagementStatus.success &&
100+
state.headlines.isNotEmpty &&
101+
event.startAfterId == null &&
102+
!event.forceRefresh) {
103+
return;
104+
}
105+
88106
emit(state.copyWith(headlinesStatus: ContentManagementStatus.loading));
89107
try {
90108
final isPaginating = event.startAfterId != null;
@@ -166,6 +184,15 @@ class ContentManagementBloc
166184
LoadTopicsRequested event,
167185
Emitter<ContentManagementState> emit,
168186
) async {
187+
// If topics are already loaded and it's not a pagination request,
188+
// do not re-fetch. This prevents redundant API calls on tab changes.
189+
if (state.topicsStatus == ContentManagementStatus.success &&
190+
state.topics.isNotEmpty &&
191+
event.startAfterId == null &&
192+
!event.forceRefresh) {
193+
return;
194+
}
195+
169196
emit(state.copyWith(topicsStatus: ContentManagementStatus.loading));
170197
try {
171198
final isPaginating = event.startAfterId != null;
@@ -247,6 +274,15 @@ class ContentManagementBloc
247274
LoadSourcesRequested event,
248275
Emitter<ContentManagementState> emit,
249276
) async {
277+
// If sources are already loaded and it's not a pagination request,
278+
// do not re-fetch. This prevents redundant API calls on tab changes.
279+
if (state.sourcesStatus == ContentManagementStatus.success &&
280+
state.sources.isNotEmpty &&
281+
event.startAfterId == null &&
282+
!event.forceRefresh) {
283+
return;
284+
}
285+
250286
emit(state.copyWith(sourcesStatus: ContentManagementStatus.loading));
251287
try {
252288
final isPaginating = event.startAfterId != null;

lib/content_management/bloc/content_management_event.dart

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,23 @@ final class ContentManagementTabChanged extends ContentManagementEvent {
2626
/// {@endtemplate}
2727
final class LoadHeadlinesRequested extends ContentManagementEvent {
2828
/// {@macro load_headlines_requested}
29-
const LoadHeadlinesRequested({this.startAfterId, this.limit});
29+
const LoadHeadlinesRequested({
30+
this.startAfterId,
31+
this.limit,
32+
this.forceRefresh = false,
33+
});
3034

3135
/// Optional ID to start pagination after.
3236
final String? startAfterId;
3337

3438
/// Optional maximum number of items to return.
3539
final int? limit;
3640

41+
/// If true, forces a refresh of the data, bypassing the cache.
42+
final bool forceRefresh;
43+
3744
@override
38-
List<Object?> get props => [startAfterId, limit];
45+
List<Object?> get props => [startAfterId, limit, forceRefresh];
3946
}
4047

4148
/// {@template archive_headline_requested}
@@ -57,16 +64,23 @@ final class ArchiveHeadlineRequested extends ContentManagementEvent {
5764
/// {@endtemplate}
5865
final class LoadTopicsRequested extends ContentManagementEvent {
5966
/// {@macro load_topics_requested}
60-
const LoadTopicsRequested({this.startAfterId, this.limit});
67+
const LoadTopicsRequested({
68+
this.startAfterId,
69+
this.limit,
70+
this.forceRefresh = false,
71+
});
6172

6273
/// Optional ID to start pagination after.
6374
final String? startAfterId;
6475

6576
/// Optional maximum number of items to return.
6677
final int? limit;
6778

79+
/// If true, forces a refresh of the data, bypassing the cache.
80+
final bool forceRefresh;
81+
6882
@override
69-
List<Object?> get props => [startAfterId, limit];
83+
List<Object?> get props => [startAfterId, limit, forceRefresh];
7084
}
7185

7286
/// {@template archive_topic_requested}
@@ -88,16 +102,23 @@ final class ArchiveTopicRequested extends ContentManagementEvent {
88102
/// {@endtemplate}
89103
final class LoadSourcesRequested extends ContentManagementEvent {
90104
/// {@macro load_sources_requested}
91-
const LoadSourcesRequested({this.startAfterId, this.limit});
105+
const LoadSourcesRequested({
106+
this.startAfterId,
107+
this.limit,
108+
this.forceRefresh = false,
109+
});
92110

93111
/// Optional ID to start pagination after.
94112
final String? startAfterId;
95113

96114
/// Optional maximum number of items to return.
97115
final int? limit;
98116

117+
/// If true, forces a refresh of the data, bypassing the cache.
118+
final bool forceRefresh;
119+
99120
@override
100-
List<Object?> get props => [startAfterId, limit];
121+
List<Object?> get props => [startAfterId, limit, forceRefresh];
101122
}
102123

103124
/// {@template archive_source_requested}

0 commit comments

Comments
 (0)