Skip to content

Commit 5f8de6f

Browse files
authored
Merge pull request #98 from flutter-news-app-full-source-code/integrate-country-model-in-teh-generic-entity-details
Integrate country model in teh generic entity details
2 parents cd8a593 + c5ff3cb commit 5f8de6f

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

lib/entity_details/bloc/entity_details_bloc.dart

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ class EntityDetailsBloc extends Bloc<EntityDetailsEvent, EntityDetailsState> {
2020
required DataRepository<Headline> headlinesRepository,
2121
required DataRepository<Topic> topicRepository,
2222
required DataRepository<Source> sourceRepository,
23+
required DataRepository<Country> countryRepository,
2324
required AccountBloc accountBloc,
2425
required AppBloc appBloc,
2526
required FeedDecoratorService feedDecoratorService,
2627
}) : _headlinesRepository = headlinesRepository,
2728
_topicRepository = topicRepository,
2829
_sourceRepository = sourceRepository,
30+
_countryRepository = countryRepository,
2931
_accountBloc = accountBloc,
3032
_appBloc = appBloc,
3133
_feedDecoratorService = feedDecoratorService,
@@ -52,6 +54,7 @@ class EntityDetailsBloc extends Bloc<EntityDetailsEvent, EntityDetailsState> {
5254
final DataRepository<Headline> _headlinesRepository;
5355
final DataRepository<Topic> _topicRepository;
5456
final DataRepository<Source> _sourceRepository;
57+
final DataRepository<Country> _countryRepository;
5558
final AccountBloc _accountBloc;
5659
final AppBloc _appBloc;
5760
final FeedDecoratorService _feedDecoratorService;
@@ -79,22 +82,33 @@ class EntityDetailsBloc extends Bloc<EntityDetailsEvent, EntityDetailsState> {
7982
entityToLoad = event.entity!;
8083
contentTypeToLoad = event.entity is Topic
8184
? ContentType.topic
82-
: ContentType.source;
85+
: event.entity is Country
86+
? ContentType.country
87+
: ContentType.source;
8388
} else {
8489
contentTypeToLoad = event.contentType!;
85-
if (contentTypeToLoad == ContentType.topic) {
86-
entityToLoad = await _topicRepository.read(id: event.entityId!);
87-
} else {
88-
entityToLoad = await _sourceRepository.read(id: event.entityId!);
90+
switch (contentTypeToLoad) {
91+
case ContentType.topic:
92+
entityToLoad = await _topicRepository.read(id: event.entityId!);
93+
case ContentType.source:
94+
entityToLoad = await _sourceRepository.read(id: event.entityId!);
95+
case ContentType.country:
96+
entityToLoad = await _countryRepository.read(id: event.entityId!);
97+
default:
98+
throw const OperationFailedException(
99+
'Unsupported ContentType for EntityDetails.',
100+
);
89101
}
90102
}
91103

92104
// 2. Fetch Initial Headlines
93105
final filter = <String, dynamic>{};
94106
if (contentTypeToLoad == ContentType.topic) {
95107
filter['topic.id'] = (entityToLoad as Topic).id;
96-
} else {
108+
} else if (contentTypeToLoad == ContentType.source) {
97109
filter['source.id'] = (entityToLoad as Source).id;
110+
} else if (contentTypeToLoad == ContentType.country) {
111+
filter['eventCountry.id'] = (entityToLoad as Country).id;
98112
}
99113

100114
final headlineResponse = await _headlinesRepository.readAll(
@@ -139,6 +153,10 @@ class EntityDetailsBloc extends Bloc<EntityDetailsEvent, EntityDetailsState> {
139153
isCurrentlyFollowing = preferences.followedSources.any(
140154
(s) => s.id == (entityToLoad as Source).id,
141155
);
156+
} else if (entityToLoad is Country) {
157+
isCurrentlyFollowing = preferences.followedCountries.any(
158+
(c) => c.id == (entityToLoad as Country).id,
159+
);
142160
}
143161
}
144162

@@ -179,6 +197,8 @@ class EntityDetailsBloc extends Bloc<EntityDetailsEvent, EntityDetailsState> {
179197
_accountBloc.add(AccountFollowTopicToggled(topic: entity));
180198
} else if (entity is Source) {
181199
_accountBloc.add(AccountFollowSourceToggled(source: entity));
200+
} else if (entity is Country) {
201+
_accountBloc.add(AccountFollowCountryToggled(country: entity));
182202
}
183203
}
184204

@@ -200,6 +220,8 @@ class EntityDetailsBloc extends Bloc<EntityDetailsEvent, EntityDetailsState> {
200220
filter['topic.id'] = (state.entity! as Topic).id;
201221
} else if (state.entity is Source) {
202222
filter['source.id'] = (state.entity! as Source).id;
223+
} else if (state.entity is Country) {
224+
filter['eventCountry.id'] = (state.entity! as Country).id;
203225
}
204226

205227
final headlineResponse = await _headlinesRepository.readAll(
@@ -284,6 +306,10 @@ class EntityDetailsBloc extends Bloc<EntityDetailsEvent, EntityDetailsState> {
284306
isCurrentlyFollowing = preferences.followedSources.any(
285307
(s) => s.id == entity.id,
286308
);
309+
} else if (entity is Country) {
310+
isCurrentlyFollowing = preferences.followedCountries.any(
311+
(c) => c.id == entity.id,
312+
);
287313
}
288314

289315
if (state.isFollowing != isCurrentlyFollowing) {

lib/entity_details/view/entity_details_page.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class EntityDetailsPage extends StatelessWidget {
5252
create: (context) {
5353
final topicsRepository = context.read<DataRepository<Topic>>();
5454
final sourcesRepository = context.read<DataRepository<Source>>();
55+
final countriesRepository = context.read<DataRepository<Country>>();
5556
final feedDecoratorService = FeedDecoratorService(
5657
topicsRepository: topicsRepository,
5758
sourcesRepository: sourcesRepository,
@@ -61,6 +62,7 @@ class EntityDetailsPage extends StatelessWidget {
6162
headlinesRepository: context.read<DataRepository<Headline>>(),
6263
topicRepository: topicsRepository,
6364
sourceRepository: sourcesRepository,
65+
countryRepository: countriesRepository,
6466
accountBloc: context.read<AccountBloc>(),
6567
appBloc: context.read<AppBloc>(),
6668
feedDecoratorService: feedDecoratorService,
@@ -131,6 +133,8 @@ class _EntityDetailsViewState extends State<EntityDetailsView> {
131133
name = l10n.entityDetailsTopicTitle;
132134
case ContentType.source:
133135
name = l10n.entityDetailsSourceTitle;
136+
case ContentType.country:
137+
name = l10n.entityDetailsCountryTitle;
134138
default:
135139
name = l10n.detailsPageTitle;
136140
}
@@ -190,6 +194,10 @@ class _EntityDetailsViewState extends State<EntityDetailsView> {
190194
final src = state.entity! as Source;
191195
appBarTitleText = src.name;
192196
appBarIconData = Icons.source_outlined;
197+
} else if (state.entity is Country) {
198+
final country = state.entity! as Country;
199+
appBarTitleText = country.name;
200+
appBarIconData = Icons.flag_outlined;
193201
} else {
194202
appBarTitleText = l10n.detailsPageTitle;
195203
}
@@ -198,10 +206,8 @@ class _EntityDetailsViewState extends State<EntityDetailsView> {
198206
? (state.entity! as Topic).description
199207
: state.entity is Source
200208
? (state.entity! as Source).description
201-
: null;
202-
203-
final entityIconUrl = (state.entity is Topic)
204-
? (state.entity! as Topic).iconUrl
209+
: state.entity is Country
210+
? (state.entity! as Country).name // Using name as description for country
205211
: null;
206212

207213
final followButton = IconButton(
@@ -219,6 +225,12 @@ class _EntityDetailsViewState extends State<EntityDetailsView> {
219225
},
220226
);
221227

228+
final entityIconUrl = (state.entity is Topic)
229+
? (state.entity! as Topic).iconUrl
230+
: (state.entity is Country)
231+
? (state.entity! as Country).flagUrl
232+
: null;
233+
222234
final Widget appBarTitleWidget = Row(
223235
mainAxisSize: MainAxisSize.min,
224236
children: [

0 commit comments

Comments
 (0)