Skip to content

Commit 3089c69

Browse files
committed
refactor(headlines-feed): replace AccountBloc with AppBloc in feed page
- Remove AccountBloc dependency from headlines_feed_page.dart - Update logic to use AppBloc for content preferences - Simplify follow/unfollow toggle functionality - Improve code readability and maintainability
1 parent c2a8406 commit 3089c69

File tree

1 file changed

+50
-34
lines changed

1 file changed

+50
-34
lines changed

lib/headlines-feed/view/headlines_feed_page.dart

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import 'package:core/core.dart';
55
import 'package:flutter/material.dart';
66
import 'package:flutter_bloc/flutter_bloc.dart';
7-
import 'package:flutter_news_app_mobile_client_full_source_code/account/bloc/account_bloc.dart';
87
import 'package:flutter_news_app_mobile_client_full_source_code/ads/interstitial_ad_manager.dart';
98
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_placeholder.dart';
109
import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_theme_style.dart';
@@ -302,12 +301,8 @@ class _HeadlinesFeedPageState extends State<HeadlinesFeedPage> {
302301
final item = state.feedItems[index];
303302

304303
if (item is Headline) {
305-
final imageStyle = context
306-
.watch<AppBloc>()
307-
.state
308-
.settings
309-
.feedPreferences
310-
.headlineImageStyle;
304+
final imageStyle =
305+
context.watch<AppBloc>().state.headlineImageStyle;
311306
Widget tile;
312307
switch (imageStyle) {
313308
case HeadlineImageStyle.hidden:
@@ -364,53 +359,74 @@ class _HeadlinesFeedPageState extends State<HeadlinesFeedPage> {
364359
},
365360
);
366361
} else if (item is ContentCollectionItem) {
367-
// Access AccountBloc to get the user's content preferences,
362+
// Access AppBloc to get the user's content preferences,
368363
// which is the source of truth for followed items.
369-
final accountState = context.watch<AccountBloc>().state;
364+
final appState = context.watch<AppBloc>().state;
370365
final followedTopics =
371-
accountState.preferences?.followedTopics ?? [];
366+
appState.userContentPreferences?.followedTopics ?? [];
372367
final followedSources =
373-
accountState.preferences?.followedSources ?? [];
368+
appState.userContentPreferences?.followedSources ?? [];
374369

375-
final followedTopicIds = followedTopics
376-
.map((t) => t.id)
377-
.toList();
378-
final followedSourceIds = followedSources
379-
.map((s) => s.id)
380-
.toList();
370+
final followedTopicIds =
371+
followedTopics.map((t) => t.id).toList();
372+
final followedSourceIds =
373+
followedSources.map((s) => s.id).toList();
381374

382375
return ContentCollectionDecoratorWidget(
383376
item: item,
384377
followedTopicIds: followedTopicIds,
385378
followedSourceIds: followedSourceIds,
386379
onFollowToggle: (toggledItem) {
387-
// Determine the current following status to toggle it.
388-
// ignore: unused_local_variable
389-
final bool isCurrentlyFollowing;
380+
final currentUserPreferences =
381+
appState.userContentPreferences;
382+
if (currentUserPreferences == null) return;
383+
384+
UserContentPreferences updatedPreferences;
385+
390386
if (toggledItem is Topic) {
391-
isCurrentlyFollowing = followedTopicIds.contains(
392-
toggledItem.id,
393-
);
394-
context.read<AccountBloc>().add(
395-
AccountFollowTopicToggled(topic: toggledItem),
387+
final isCurrentlyFollowing =
388+
followedTopicIds.contains(toggledItem.id);
389+
final newFollowedTopics =
390+
List<Topic>.from(followedTopics);
391+
if (isCurrentlyFollowing) {
392+
newFollowedTopics
393+
.removeWhere((t) => t.id == toggledItem.id);
394+
} else {
395+
newFollowedTopics.add(toggledItem);
396+
}
397+
updatedPreferences = currentUserPreferences.copyWith(
398+
followedTopics: newFollowedTopics,
396399
);
397400
} else if (toggledItem is Source) {
398-
isCurrentlyFollowing = followedSourceIds.contains(
399-
toggledItem.id,
400-
);
401-
context.read<AccountBloc>().add(
402-
AccountFollowSourceToggled(source: toggledItem),
401+
final isCurrentlyFollowing =
402+
followedSourceIds.contains(toggledItem.id);
403+
final newFollowedSources =
404+
List<Source>.from(followedSources);
405+
if (isCurrentlyFollowing) {
406+
newFollowedSources
407+
.removeWhere((s) => s.id == toggledItem.id);
408+
} else {
409+
newFollowedSources.add(toggledItem);
410+
}
411+
updatedPreferences = currentUserPreferences.copyWith(
412+
followedSources: newFollowedSources,
403413
);
404414
} else {
405415
return;
406416
}
417+
418+
context.read<AppBloc>().add(
419+
AppUserContentPreferencesChanged(
420+
preferences: updatedPreferences,
421+
),
422+
);
407423
},
408424
onDismiss: (decoratorType) {
409425
context.read<HeadlinesFeedBloc>().add(
410-
FeedDecoratorDismissed(
411-
feedDecoratorType: decoratorType,
412-
),
413-
);
426+
FeedDecoratorDismissed(
427+
feedDecoratorType: decoratorType,
428+
),
429+
);
414430
},
415431
);
416432
}

0 commit comments

Comments
 (0)