Skip to content

Commit 01b01f4

Browse files
committed
feat(local_ads_management): add extension for converting LocalAd to AdType
- Add LocalAdToAdType extension to convert LocalAd's adType string to AdType enum - Import AppLocalizations for localization support
1 parent 4ed22dd commit 01b01f4

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

lib/local_ads_management/view/local_ads_management_page.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:data_table_2/data_table_2.dart';
44
import 'package:flutter/material.dart';
55
import 'package:flutter/services.dart';
66
import 'package:flutter_bloc/flutter_bloc.dart';
7+
import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/app_localizations.dart';
78
import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/l10n.dart';
89
import 'package:flutter_news_app_web_dashboard_full_source_code/local_ads_management/bloc/local_ads_management_bloc.dart';
910
import 'package:flutter_news_app_web_dashboard_full_source_code/router/routes.dart';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'package:core/core.dart';
2+
3+
/// Defines extension methods for [LocalAd] to convert its `adType` string
4+
/// to the corresponding [AdType] enum.
5+
extension LocalAdToAdType on LocalAd {
6+
/// Converts the `adType` string of this [LocalAd] instance to an [AdType] enum.
7+
///
8+
/// Throws a [StateError] if the `adType` string does not correspond to a
9+
/// valid [AdType] enum value.
10+
AdType toAdType() {
11+
switch (adType) {
12+
case 'native':
13+
return AdType.native;
14+
case 'banner':
15+
return AdType.banner;
16+
case 'interstitial':
17+
return AdType.interstitial;
18+
case 'video':
19+
return AdType.video;
20+
default:
21+
throw StateError('Unknown adType: $adType');
22+
}
23+
}
24+
}
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
extension StringTruncate on String {
1+
/// Extension on [String] to provide truncation and capitalization utilities.
2+
extension StringX on String {
3+
/// Truncates the string to a specified [maxLength].
4+
///
5+
/// If the string's length exceeds [maxLength], it is truncated and
6+
/// '...' is appended.
27
String truncate(int maxLength) {
38
if (length <= maxLength) {
49
return this;
510
}
611
return '${substring(0, maxLength)}...';
712
}
13+
14+
/// Capitalizes the first letter of the string.
15+
///
16+
/// Returns the original string if it is empty.
17+
String capitalize() {
18+
if (isEmpty) {
19+
return this;
20+
}
21+
return '${this[0].toUpperCase()}${substring(1)}';
22+
}
823
}

0 commit comments

Comments
 (0)