Skip to content

Commit e397e22

Browse files
committed
refactor(feed): update headlines page to use decorator loader
Updates `HeadlinesFeedPage` to render the new `FeedDecoratorLoaderWidget` when it encounters a `DecoratorPlaceholder` in the feed. - The `itemBuilder` logic is simplified to check for `DecoratorPlaceholder`. - The old, direct rendering logic for `CallToActionItem` and `ContentCollectionItem` is removed, as this is now encapsulated within the loader widget. - The separator logic is updated to correctly handle spacing around the new placeholder.
1 parent 61999ca commit e397e22

File tree

1 file changed

+10
-88
lines changed

1 file changed

+10
-88
lines changed

lib/headlines-feed/view/headlines_feed_page.dart

Lines changed: 10 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_pl
55
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart';
66
import 'package:flutter_news_app_mobile_client_full_source_code/ads/widgets/feed_ad_loader_widget.dart';
77
import 'package:flutter_news_app_mobile_client_full_source_code/app/bloc/app_bloc.dart';
8+
import 'package:flutter_news_app_mobile_client_full_source_code/feed_decorators/models/decorator_placeholder.dart';
9+
import 'package:flutter_news_app_mobile_client_full_source_code/feed_decorators/widgets/feed_decorator_loader_widget.dart';
810
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/bloc/headlines_feed_bloc.dart';
911
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/widgets/feed_sliver_app_bar.dart';
1012
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/widgets/saved_filters_bar.dart';
1113
import 'package:flutter_news_app_mobile_client_full_source_code/l10n/l10n.dart';
1214
import 'package:flutter_news_app_mobile_client_full_source_code/shared/widgets/feed_core/feed_core.dart';
13-
import 'package:flutter_news_app_mobile_client_full_source_code/shared/widgets/feed_decorators/call_to_action_decorator_widget.dart';
14-
import 'package:flutter_news_app_mobile_client_full_source_code/shared/widgets/feed_decorators/content_collection_decorator_widget.dart';
1515
import 'package:go_router/go_router.dart';
1616
import 'package:ui_kit/ui_kit.dart';
1717

@@ -235,7 +235,8 @@ class _HeadlinesFeedPageState extends State<HeadlinesFeedPage>
235235
final currentItem = state.feedItems[index];
236236
final nextItem = state.feedItems[index + 1];
237237

238-
if (currentItem is! Headline ||
238+
if (currentItem is DecoratorPlaceholder ||
239+
currentItem is! Headline ||
239240
nextItem is! Headline) {
240241
return const SizedBox(height: AppSpacing.md);
241242
}
@@ -315,92 +316,13 @@ class _HeadlinesFeedPageState extends State<HeadlinesFeedPage>
315316
adConfig: adConfig,
316317
);
317318
} else if (item is CallToActionItem) {
318-
return CallToActionDecoratorWidget(
319-
item: item,
320-
onCallToAction: (url) {
321-
context.read<HeadlinesFeedBloc>().add(
322-
CallToActionTapped(url: url),
323-
);
324-
},
325-
);
319+
// This case is now handled by FeedDecoratorLoaderWidget.
320+
return const SizedBox.shrink();
326321
} else if (item is ContentCollectionItem) {
327-
// Access AppBloc to get the user's content preferences,
328-
// which is the source of truth for followed items.
329-
final appState = context.watch<AppBloc>().state;
330-
final followedTopics =
331-
appState
332-
.userContentPreferences
333-
?.followedTopics ??
334-
[];
335-
final followedSources =
336-
appState
337-
.userContentPreferences
338-
?.followedSources ??
339-
[];
340-
341-
final followedTopicIds = followedTopics
342-
.map((t) => t.id)
343-
.toList();
344-
final followedSourceIds = followedSources
345-
.map((s) => s.id)
346-
.toList();
347-
348-
return ContentCollectionDecoratorWidget(
349-
item: item,
350-
followedTopicIds: followedTopicIds,
351-
followedSourceIds: followedSourceIds,
352-
onFollowToggle: (toggledItem) {
353-
final currentUserPreferences =
354-
appState.userContentPreferences;
355-
if (currentUserPreferences == null) return;
356-
357-
UserContentPreferences updatedPreferences;
358-
359-
if (toggledItem is Topic) {
360-
final isCurrentlyFollowing = followedTopicIds
361-
.contains(toggledItem.id);
362-
final newFollowedTopics = List<Topic>.from(
363-
followedTopics,
364-
);
365-
if (isCurrentlyFollowing) {
366-
newFollowedTopics.removeWhere(
367-
(t) => t.id == toggledItem.id,
368-
);
369-
} else {
370-
newFollowedTopics.add(toggledItem);
371-
}
372-
updatedPreferences = currentUserPreferences
373-
.copyWith(
374-
followedTopics: newFollowedTopics,
375-
);
376-
} else if (toggledItem is Source) {
377-
final isCurrentlyFollowing = followedSourceIds
378-
.contains(toggledItem.id);
379-
final newFollowedSources = List<Source>.from(
380-
followedSources,
381-
);
382-
if (isCurrentlyFollowing) {
383-
newFollowedSources.removeWhere(
384-
(s) => s.id == toggledItem.id,
385-
);
386-
} else {
387-
newFollowedSources.add(toggledItem);
388-
}
389-
updatedPreferences = currentUserPreferences
390-
.copyWith(
391-
followedSources: newFollowedSources,
392-
);
393-
} else {
394-
return;
395-
}
396-
397-
context.read<AppBloc>().add(
398-
AppUserContentPreferencesChanged(
399-
preferences: updatedPreferences,
400-
),
401-
);
402-
},
403-
);
322+
// This case is now handled by FeedDecoratorLoaderWidget.
323+
return const SizedBox.shrink();
324+
} else if (item is DecoratorPlaceholder) {
325+
return const FeedDecoratorLoaderWidget();
404326
}
405327
return const SizedBox.shrink();
406328
},

0 commit comments

Comments
 (0)