Skip to content

Commit 6acd616

Browse files
committed
refactor(feed_decorators): implement dynamic localization for fixed CTA decorators
- Remove hard-coded strings for CallToAction decorators - Use localization extension to generate dynamic content - Refactor CTA URL determination into a switch statement - Update title for ContentCollection decorators using localization
1 parent 749244d commit 6acd616

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

lib/feed_decorators/widgets/feed_decorator_loader_widget.dart

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import 'package:data_repository/data_repository.dart';
55
import 'package:flutter/material.dart';
66
import 'package:flutter_bloc/flutter_bloc.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/extensions/feed_decorator_type_l10n.dart';
89
import 'package:flutter_news_app_mobile_client_full_source_code/feed_decorators/widgets/call_to_action_decorator_widget.dart';
910
import 'package:flutter_news_app_mobile_client_full_source_code/feed_decorators/widgets/content_collection_decorator_widget.dart';
1011
import 'package:flutter_news_app_mobile_client_full_source_code/headlines-feed/bloc/headlines_feed_bloc.dart';
12+
import 'package:flutter_news_app_mobile_client_full_source_code/l10n/l10n.dart';
1113
import 'package:flutter_news_app_mobile_client_full_source_code/router/routes.dart';
1214
import 'package:logging/logging.dart';
1315
import 'package:ui_kit/ui_kit.dart';
@@ -308,47 +310,43 @@ class _FeedDecoratorLoaderWidgetState extends State<FeedDecoratorLoaderWidget> {
308310
FeedDecoratorType decoratorType,
309311
FeedDecoratorConfig decoratorConfig,
310312
) async {
313+
// Access localization strings for dynamic text.
314+
final l10n = AppLocalizationsX(context).l10n;
315+
311316
// This logic is a simplified version of the original service, as the
312317
// content for CTAs is defined statically.
313318
switch (decoratorConfig.category) {
314319
case FeedDecoratorCategory.callToAction:
315-
const ctaContent = {
316-
FeedDecoratorType.linkAccount: (
317-
title: 'Create an Account',
318-
description:
319-
'Save your preferences and followed items by creating a free account.',
320-
ctaText: 'Get Started',
321-
ctaUrl: Routes.accountLinking,
322-
),
323-
FeedDecoratorType.upgrade: (
324-
title: 'Upgrade to Premium',
325-
description: 'Unlock unlimited access to all features and content.',
326-
ctaText: 'Upgrade Now',
327-
ctaUrl: '/upgrade',
328-
),
329-
FeedDecoratorType.rateApp: (
330-
title: 'Enjoying the App?',
331-
description: 'Let us know what you think by leaving a rating.',
332-
ctaText: 'Rate App',
333-
ctaUrl: '/rate-app',
334-
),
335-
FeedDecoratorType.enableNotifications: (
336-
title: 'Stay Up to Date',
337-
description:
338-
'Enable notifications to get the latest headlines delivered to you.',
339-
ctaText: 'Enable',
340-
ctaUrl: '/enable-notifications',
341-
),
342-
};
343-
final content = ctaContent[decoratorType];
344-
if (content == null) return null;
320+
// Determine the fixed CTA URL based on the decorator type.
321+
// This is a route and not a localized string.
322+
String ctaUrl;
323+
switch (decoratorType) {
324+
case FeedDecoratorType.linkAccount:
325+
ctaUrl = Routes.accountLinking;
326+
case FeedDecoratorType.upgrade:
327+
ctaUrl = Routes.upgrade;
328+
case FeedDecoratorType.rateApp:
329+
ctaUrl = Routes.rateApp;
330+
case FeedDecoratorType.enableNotifications:
331+
ctaUrl = Routes.enableNotifications;
332+
// Default case for unhandled decorator types, though ideally all
333+
// call-to-action types should be explicitly handled.
334+
default:
335+
_logger.warning(
336+
'Unhandled CallToAction decorator type for CTA URL: $decoratorType',
337+
);
338+
return null;
339+
}
340+
341+
// Construct the CallToActionItem using randomized localized strings
342+
// from the extension and the determined CTA URL.
345343
return CallToActionItem(
346344
id: _uuid.v4(),
347345
decoratorType: decoratorType,
348-
title: content.title,
349-
description: content.description,
350-
callToActionText: content.ctaText,
351-
callToActionUrl: content.ctaUrl,
346+
title: decoratorType.getRandomTitle(l10n),
347+
description: decoratorType.getRandomDescription(l10n),
348+
callToActionText: decoratorType.getRandomCtaText(l10n),
349+
callToActionUrl: ctaUrl,
352350
);
353351

354352
case FeedDecoratorCategory.contentCollection:
@@ -378,7 +376,7 @@ class _FeedDecoratorLoaderWidgetState extends State<FeedDecoratorLoaderWidget> {
378376
return ContentCollectionItem<Topic>(
379377
id: _uuid.v4(),
380378
decoratorType: decoratorType,
381-
title: 'Suggested Topics',
379+
title: decoratorType.getRandomTitle(l10n),
382380
items: topics.items,
383381
);
384382
case FeedDecoratorType.suggestedSources:
@@ -396,7 +394,7 @@ class _FeedDecoratorLoaderWidgetState extends State<FeedDecoratorLoaderWidget> {
396394
return ContentCollectionItem<Source>(
397395
id: _uuid.v4(),
398396
decoratorType: decoratorType,
399-
title: 'Suggested Sources',
397+
title: decoratorType.getRandomTitle(l10n),
400398
items: sources.items,
401399
);
402400
// ignore: no_default_cases

0 commit comments

Comments
 (0)