|
4 | 4 | import 'package:core/core.dart'; |
5 | 5 | import 'package:flutter/material.dart'; |
6 | 6 | import 'package:flutter_bloc/flutter_bloc.dart'; |
7 | | -import 'package:flutter_news_app_mobile_client_full_source_code/account/bloc/account_bloc.dart'; |
8 | 7 | import 'package:flutter_news_app_mobile_client_full_source_code/ads/interstitial_ad_manager.dart'; |
9 | 8 | import 'package:flutter_news_app_mobile_client_full_source_code/ads/models/ad_placeholder.dart'; |
10 | 9 | 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> { |
302 | 301 | final item = state.feedItems[index]; |
303 | 302 |
|
304 | 303 | 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; |
311 | 306 | Widget tile; |
312 | 307 | switch (imageStyle) { |
313 | 308 | case HeadlineImageStyle.hidden: |
@@ -364,53 +359,74 @@ class _HeadlinesFeedPageState extends State<HeadlinesFeedPage> { |
364 | 359 | }, |
365 | 360 | ); |
366 | 361 | } 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, |
368 | 363 | // which is the source of truth for followed items. |
369 | | - final accountState = context.watch<AccountBloc>().state; |
| 364 | + final appState = context.watch<AppBloc>().state; |
370 | 365 | final followedTopics = |
371 | | - accountState.preferences?.followedTopics ?? []; |
| 366 | + appState.userContentPreferences?.followedTopics ?? []; |
372 | 367 | final followedSources = |
373 | | - accountState.preferences?.followedSources ?? []; |
| 368 | + appState.userContentPreferences?.followedSources ?? []; |
374 | 369 |
|
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(); |
381 | 374 |
|
382 | 375 | return ContentCollectionDecoratorWidget( |
383 | 376 | item: item, |
384 | 377 | followedTopicIds: followedTopicIds, |
385 | 378 | followedSourceIds: followedSourceIds, |
386 | 379 | 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 | + |
390 | 386 | 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, |
396 | 399 | ); |
397 | 400 | } 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, |
403 | 413 | ); |
404 | 414 | } else { |
405 | 415 | return; |
406 | 416 | } |
| 417 | + |
| 418 | + context.read<AppBloc>().add( |
| 419 | + AppUserContentPreferencesChanged( |
| 420 | + preferences: updatedPreferences, |
| 421 | + ), |
| 422 | + ); |
407 | 423 | }, |
408 | 424 | onDismiss: (decoratorType) { |
409 | 425 | context.read<HeadlinesFeedBloc>().add( |
410 | | - FeedDecoratorDismissed( |
411 | | - feedDecoratorType: decoratorType, |
412 | | - ), |
413 | | - ); |
| 426 | + FeedDecoratorDismissed( |
| 427 | + feedDecoratorType: decoratorType, |
| 428 | + ), |
| 429 | + ); |
414 | 430 | }, |
415 | 431 | ); |
416 | 432 | } |
|
0 commit comments