Skip to content

Commit 5b9aff1

Browse files
committed
fix(feed_decorators): improve widget state checks and log messages
- Add mounted check before dispatching decorator shown event - Simplify log messages in _onFollowToggle method - Use type-specific variables in Topic and Source handling
1 parent e70e790 commit 5b9aff1

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

lib/feed_decorators/widgets/feed_decorator_loader_widget.dart

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,15 @@ class _FeedDecoratorLoaderWidgetState extends State<FeedDecoratorLoaderWidget> {
135135
}
136136

137137
// Dispatch event to notify that the decorator has been shown.
138-
context.read<AppBloc>().add(
139-
AppUserFeedDecoratorShown(
140-
userId: user.id,
141-
feedDecoratorType: dueDecoratorType,
142-
),
143-
);
144-
138+
// Guard the context access with a mounted check.
139+
if (mounted) {
140+
context.read<AppBloc>().add(
141+
AppUserFeedDecoratorShown(
142+
userId: user.id,
143+
feedDecoratorType: dueDecoratorType,
144+
),
145+
);
146+
}
145147
// Build the final widget and update the state to success.
146148
if (mounted) {
147149
setState(() {
@@ -179,8 +181,7 @@ class _FeedDecoratorLoaderWidgetState extends State<FeedDecoratorLoaderWidget> {
179181
/// maintaining consistency with the `AppSettingsChanged` pattern.
180182
void _onFollowToggle(FeedItem item) {
181183
_logger.fine(
182-
'[FeedDecoratorLoaderWidget] _onFollowToggle called for item: '
183-
'${item.id} (Type: ${item.runtimeType})',
184+
'[FeedDecoratorLoaderWidget] _onFollowToggle called for item of type: ${item.runtimeType}',
184185
);
185186

186187
final appBlocState = context.read<AppBloc>().state;
@@ -199,42 +200,46 @@ class _FeedDecoratorLoaderWidgetState extends State<FeedDecoratorLoaderWidget> {
199200
UserContentPreferences updatedPreferences = userContentPreferences;
200201

201202
if (item is Topic) {
203+
final topic = item;
202204
// Create a mutable copy of the followed topics list.
203205
final currentFollowedTopics = List<Topic>.from(
204206
userContentPreferences.followedTopics,
205207
);
206208

207-
if (currentFollowedTopics.any((t) => t.id == item.id)) {
209+
if (currentFollowedTopics.any((t) => t.id == topic.id)) {
208210
// If already following, unfollow.
209-
currentFollowedTopics.removeWhere((t) => t.id == item.id);
211+
currentFollowedTopics.removeWhere((t) => t.id == topic.id);
210212
_logger.info(
211-
'[FeedDecoratorLoaderWidget] Unfollowed topic: ${item.id}',
213+
'[FeedDecoratorLoaderWidget] Unfollowed topic: ${topic.id}',
212214
);
213215
} else {
214216
// If not following, follow.
215-
currentFollowedTopics.add(item);
216-
_logger.info('[FeedDecoratorLoaderWidget] Followed topic: ${item.id}');
217+
currentFollowedTopics.add(topic);
218+
_logger.info('[FeedDecoratorLoaderWidget] Followed topic: ${topic.id}');
217219
}
218220
// Create a new UserContentPreferences object with the updated topics.
219221
updatedPreferences = userContentPreferences.copyWith(
220222
followedTopics: currentFollowedTopics,
221223
);
222224
} else if (item is Source) {
225+
final source = item;
223226
// Create a mutable copy of the followed sources list.
224227
final currentFollowedSources = List<Source>.from(
225228
userContentPreferences.followedSources,
226229
);
227230

228-
if (currentFollowedSources.any((s) => s.id == item.id)) {
231+
if (currentFollowedSources.any((s) => s.id == source.id)) {
229232
// If already following, unfollow.
230-
currentFollowedSources.removeWhere((s) => s.id == item.id);
233+
currentFollowedSources.removeWhere((s) => s.id == source.id);
231234
_logger.info(
232-
'[FeedDecoratorLoaderWidget] Unfollowed source: ${item.id}',
235+
'[FeedDecoratorLoaderWidget] Unfollowed source: ${source.id}',
233236
);
234237
} else {
235238
// If not following, follow.
236-
currentFollowedSources.add(item);
237-
_logger.info('[FeedDecoratorLoaderWidget] Followed source: ${item.id}');
239+
currentFollowedSources.add(source);
240+
_logger.info(
241+
'[FeedDecoratorLoaderWidget] Followed source: ${source.id}',
242+
);
238243
}
239244
// Create a new UserContentPreferences object with the updated sources.
240245
updatedPreferences = userContentPreferences.copyWith(
@@ -311,8 +316,6 @@ class _FeedDecoratorLoaderWidgetState extends State<FeedDecoratorLoaderWidget> {
311316
// content for CTAs is defined statically.
312317
switch (decoratorConfig.category) {
313318
case FeedDecoratorCategory.callToAction:
314-
// A map of static content for different call-to-action types.
315-
// TODO(fulleni): random l10n selection for each type.
316319
const ctaContent = {
317320
FeedDecoratorType.linkAccount: (
318321
title: 'Create an Account',

0 commit comments

Comments
 (0)