Skip to content

Commit 1cb8e3e

Browse files
authored
Merge pull request #99 from flutter-news-app-full-source-code/Refactor-Entity-Details-Page-Navigation
Refactor entity details page navigation
2 parents 5f8de6f + 9a3407e commit 1cb8e3e

File tree

14 files changed

+71
-66
lines changed

14 files changed

+71
-66
lines changed

lib/account/view/manage_followed_items/countries/followed_countries_list_page.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ class FollowedCountriesListPage extends StatelessWidget {
9999
onTap: () {
100100
context.push(
101101
Routes.countryDetails,
102-
extra: EntityDetailsPageArguments(entity: country),
102+
extra: EntityDetailsPageArguments(
103+
entityId: country.id,
104+
contentType: ContentType.country,
105+
),
103106
);
104107
},
105108
);

lib/account/view/manage_followed_items/sources/followed_sources_list_page.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ class FollowedSourcesListPage extends StatelessWidget {
9696
onTap: () {
9797
context.push(
9898
Routes.sourceDetails,
99-
extra: EntityDetailsPageArguments(entity: source),
99+
extra: EntityDetailsPageArguments(
100+
entityId: source.id,
101+
contentType: ContentType.source,
102+
),
100103
);
101104
},
102105
);

lib/account/view/manage_followed_items/topics/followed_topics_list_page.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ class FollowedTopicsListPage extends StatelessWidget {
104104
onTap: () {
105105
context.push(
106106
Routes.topicDetails,
107-
extra: EntityDetailsPageArguments(entity: topic),
107+
extra: EntityDetailsPageArguments(
108+
entityId: topic.id,
109+
contentType: ContentType.topic,
110+
),
108111
);
109112
},
110113
);

lib/entity_details/bloc/entity_details_bloc.dart

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,20 @@ class EntityDetailsBloc extends Bloc<EntityDetailsEvent, EntityDetailsState> {
7676
try {
7777
// 1. Determine/Fetch Entity
7878
FeedItem entityToLoad;
79-
ContentType contentTypeToLoad;
80-
81-
if (event.entity != null) {
82-
entityToLoad = event.entity!;
83-
contentTypeToLoad = event.entity is Topic
84-
? ContentType.topic
85-
: event.entity is Country
86-
? ContentType.country
87-
: ContentType.source;
88-
} else {
89-
contentTypeToLoad = event.contentType!;
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-
);
101-
}
79+
final contentTypeToLoad = event.contentType;
80+
81+
switch (contentTypeToLoad) {
82+
case ContentType.topic:
83+
entityToLoad = await _topicRepository.read(id: event.entityId);
84+
case ContentType.source:
85+
entityToLoad = await _sourceRepository.read(id: event.entityId);
86+
case ContentType.country:
87+
entityToLoad = await _countryRepository.read(id: event.entityId);
88+
// ignore: no_default_cases
89+
default:
90+
throw const OperationFailedException(
91+
'Unsupported ContentType for EntityDetails.',
92+
);
10293
}
10394

10495
// 2. Fetch Initial Headlines

lib/entity_details/bloc/entity_details_event.dart

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,21 @@ abstract class EntityDetailsEvent extends Equatable {
1515
class EntityDetailsLoadRequested extends EntityDetailsEvent {
1616
const EntityDetailsLoadRequested({
1717
required this.adThemeStyle,
18-
this.entityId,
19-
this.contentType,
20-
this.entity,
21-
}) : assert(
22-
(entityId != null && contentType != null) || entity != null,
23-
'Either entityId/contentType or a full entity object must be provided.',
24-
);
18+
required this.entityId,
19+
required this.contentType,
20+
});
2521

2622
/// The unique ID of the entity to load.
27-
final String? entityId;
23+
final String entityId;
2824

2925
/// The type of the entity to load.
30-
final ContentType? contentType;
31-
32-
/// The full entity object, if already available.
33-
final FeedItem? entity;
26+
final ContentType contentType;
3427

3528
/// The current ad theme style, required for ad injection.
3629
final AdThemeStyle adThemeStyle;
3730

3831
@override
39-
List<Object?> get props => [entityId, contentType, entity, adThemeStyle];
32+
List<Object?> get props => [entityId, contentType, adThemeStyle];
4033
}
4134

4235
/// Event to toggle the "follow" status of the currently loaded entity.

lib/entity_details/view/entity_details_page.dart

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,12 @@ import 'package:ui_kit/ui_kit.dart';
2121

2222
class EntityDetailsPageArguments {
2323
const EntityDetailsPageArguments({
24-
this.entityId,
25-
this.contentType,
26-
this.entity,
27-
}) : assert(
28-
(entityId != null && contentType != null) || entity != null,
29-
'Either entityId/contentType or a full entity object must be provided.',
30-
);
24+
required this.entityId,
25+
required this.contentType,
26+
});
3127

32-
final String? entityId;
33-
final ContentType? contentType;
34-
final FeedItem? entity;
28+
final String entityId;
29+
final ContentType contentType;
3530
}
3631

3732
class EntityDetailsPage extends StatelessWidget {
@@ -70,7 +65,6 @@ class EntityDetailsPage extends StatelessWidget {
7065
EntityDetailsLoadRequested(
7166
entityId: args.entityId,
7267
contentType: args.contentType,
73-
entity: args.entity,
7468
adThemeStyle: AdThemeStyle.fromTheme(Theme.of(context)),
7569
),
7670
);
@@ -176,7 +170,6 @@ class _EntityDetailsViewState extends State<EntityDetailsView> {
176170
EntityDetailsLoadRequested(
177171
entityId: widget.args.entityId,
178172
contentType: widget.args.contentType,
179-
entity: widget.args.entity,
180173
adThemeStyle: AdThemeStyle.fromTheme(theme),
181174
),
182175
),
@@ -207,7 +200,8 @@ class _EntityDetailsViewState extends State<EntityDetailsView> {
207200
: state.entity is Source
208201
? (state.entity! as Source).description
209202
: state.entity is Country
210-
? (state.entity! as Country).name // Using name as description for country
203+
? (state.entity! as Country)
204+
.name // Using name as description for country
211205
: null;
212206

213207
final followButton = IconButton(
@@ -228,8 +222,8 @@ class _EntityDetailsViewState extends State<EntityDetailsView> {
228222
final entityIconUrl = (state.entity is Topic)
229223
? (state.entity! as Topic).iconUrl
230224
: (state.entity is Country)
231-
? (state.entity! as Country).flagUrl
232-
: null;
225+
? (state.entity! as Country).flagUrl
226+
: null;
233227

234228
final Widget appBarTitleWidget = Row(
235229
mainAxisSize: MainAxisSize.min,

lib/headline-details/view/headline_details_page.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,10 @@ class _HeadlineDetailsPageState extends State<HeadlineDetailsPage> {
424424
onTap: () {
425425
context.push(
426426
Routes.sourceDetails,
427-
extra: EntityDetailsPageArguments(entity: headline.source),
427+
extra: EntityDetailsPageArguments(
428+
entityId: headline.id,
429+
contentType: ContentType.headline,
430+
),
428431
);
429432
},
430433
borderRadius: BorderRadius.circular(AppSpacing.sm),
@@ -449,7 +452,10 @@ class _HeadlineDetailsPageState extends State<HeadlineDetailsPage> {
449452
onTap: () {
450453
context.push(
451454
Routes.topicDetails,
452-
extra: EntityDetailsPageArguments(entity: headline.topic),
455+
extra: EntityDetailsPageArguments(
456+
entityId: headline.topic.id,
457+
contentType: ContentType.topic,
458+
),
453459
);
454460
},
455461
borderRadius: BorderRadius.circular(AppSpacing.sm),

lib/headlines-search/widgets/category_item_widget.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ class TopicItemWidget extends StatelessWidget {
2424
onTap: () {
2525
context.push(
2626
Routes.topicDetails,
27-
extra: EntityDetailsPageArguments(entity: topic),
27+
extra: EntityDetailsPageArguments(
28+
entityId: topic.id,
29+
contentType: ContentType.topic,
30+
),
2831
);
2932
},
3033
);

lib/headlines-search/widgets/country_item_widget.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class CountryItemWidget extends StatelessWidget {
2323
onTap: () {
2424
context.push(
2525
Routes.countryDetails,
26-
extra: EntityDetailsPageArguments(entity: country),
26+
extra: EntityDetailsPageArguments(
27+
entityId: country.id,
28+
contentType: ContentType.country,
29+
),
2730
);
2831
},
2932
);

lib/headlines-search/widgets/source_item_widget.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ class SourceItemWidget extends StatelessWidget {
2424
onTap: () {
2525
context.push(
2626
Routes.sourceDetails,
27-
extra: EntityDetailsPageArguments(entity: source),
27+
extra: EntityDetailsPageArguments(
28+
entityId: source.id,
29+
contentType: ContentType.source,
30+
),
2831
);
2932
},
3033
);

0 commit comments

Comments
 (0)