|
| 1 | +import 'package:core/core.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_news_app_web_dashboard_full_source_code/app_configuration/widgets/app_config_form_fields.dart'; |
| 4 | +import 'package:flutter_news_app_web_dashboard_full_source_code/l10n/l10n.dart'; |
| 5 | +import 'package:flutter_news_app_web_dashboard_full_source_code/shared/extensions/app_user_role_l10n.dart'; |
| 6 | +import 'package:ui_kit/ui_kit.dart'; |
| 7 | + |
| 8 | +/// {@template saved_headlines_filters_limit_form} |
| 9 | +/// A form for configuring saved headlines filter limits within the |
| 10 | +/// [RemoteConfig]. |
| 11 | +/// |
| 12 | +/// This form provides fields to set the maximum number of saved filters |
| 13 | +/// for guest, authenticated, and premium users. |
| 14 | +/// {@endtemplate} |
| 15 | +class SavedHeadlinesFiltersLimitForm extends StatefulWidget { |
| 16 | + /// {@macro saved_headlines_filters_limit_form} |
| 17 | + const SavedHeadlinesFiltersLimitForm({ |
| 18 | + required this.remoteConfig, |
| 19 | + required this.onConfigChanged, |
| 20 | + super.key, |
| 21 | + }); |
| 22 | + |
| 23 | + /// The current [RemoteConfig] object. |
| 24 | + final RemoteConfig remoteConfig; |
| 25 | + |
| 26 | + /// Callback to notify parent of changes to the [RemoteConfig]. |
| 27 | + final ValueChanged<RemoteConfig> onConfigChanged; |
| 28 | + |
| 29 | + @override |
| 30 | + State<SavedHeadlinesFiltersLimitForm> createState() => |
| 31 | + _SavedHeadlinesFiltersLimitFormState(); |
| 32 | +} |
| 33 | + |
| 34 | +class _SavedHeadlinesFiltersLimitFormState |
| 35 | + extends State<SavedHeadlinesFiltersLimitForm> |
| 36 | + with SingleTickerProviderStateMixin { |
| 37 | + late TabController _tabController; |
| 38 | + late final Map<AppUserRole, TextEditingController> _controllers; |
| 39 | + |
| 40 | + @override |
| 41 | + void initState() { |
| 42 | + super.initState(); |
| 43 | + _tabController = TabController( |
| 44 | + length: AppUserRole.values.length, |
| 45 | + vsync: this, |
| 46 | + ); |
| 47 | + _initializeControllers(); |
| 48 | + } |
| 49 | + |
| 50 | + @override |
| 51 | + void didUpdateWidget(covariant SavedHeadlinesFiltersLimitForm oldWidget) { |
| 52 | + super.didUpdateWidget(oldWidget); |
| 53 | + if (widget.remoteConfig.userPreferenceConfig != |
| 54 | + oldWidget.remoteConfig.userPreferenceConfig) { |
| 55 | + _updateControllerValues(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + void _initializeControllers() { |
| 60 | + _controllers = { |
| 61 | + for (final role in AppUserRole.values) |
| 62 | + role: TextEditingController( |
| 63 | + text: _getSavedFiltersLimit( |
| 64 | + widget.remoteConfig.userPreferenceConfig, |
| 65 | + role, |
| 66 | + ).toString(), |
| 67 | + )..selection = TextSelection.collapsed( |
| 68 | + offset: _getSavedFiltersLimit( |
| 69 | + widget.remoteConfig.userPreferenceConfig, |
| 70 | + role, |
| 71 | + ).toString().length, |
| 72 | + ), |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + void _updateControllerValues() { |
| 77 | + for (final role in AppUserRole.values) { |
| 78 | + final newLimit = _getSavedFiltersLimit( |
| 79 | + widget.remoteConfig.userPreferenceConfig, |
| 80 | + role, |
| 81 | + ).toString(); |
| 82 | + if (_controllers[role]?.text != newLimit) { |
| 83 | + _controllers[role]?.text = newLimit; |
| 84 | + _controllers[role]?.selection = TextSelection.collapsed( |
| 85 | + offset: newLimit.length, |
| 86 | + ); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @override |
| 92 | + void dispose() { |
| 93 | + _tabController.dispose(); |
| 94 | + for (final controller in _controllers.values) { |
| 95 | + controller.dispose(); |
| 96 | + } |
| 97 | + super.dispose(); |
| 98 | + } |
| 99 | + |
| 100 | + @override |
| 101 | + Widget build(BuildContext context) { |
| 102 | + final l10n = AppLocalizationsX(context).l10n; |
| 103 | + |
| 104 | + return Column( |
| 105 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 106 | + children: [ |
| 107 | + Align( |
| 108 | + alignment: AlignmentDirectional.centerStart, |
| 109 | + child: SizedBox( |
| 110 | + height: kTextTabBarHeight, |
| 111 | + child: TabBar( |
| 112 | + controller: _tabController, |
| 113 | + tabAlignment: TabAlignment.start, |
| 114 | + isScrollable: true, |
| 115 | + tabs: AppUserRole.values |
| 116 | + .map((role) => Tab(text: role.l10n(context))) |
| 117 | + .toList(), |
| 118 | + ), |
| 119 | + ), |
| 120 | + ), |
| 121 | + const SizedBox(height: AppSpacing.lg), |
| 122 | + SizedBox( |
| 123 | + height: 120, |
| 124 | + child: TabBarView( |
| 125 | + controller: _tabController, |
| 126 | + children: AppUserRole.values.map((role) { |
| 127 | + final config = widget.remoteConfig.userPreferenceConfig; |
| 128 | + return AppConfigIntField( |
| 129 | + label: l10n.savedHeadlinesLimitLabel, |
| 130 | + description: l10n.savedHeadlinesLimitDescription, |
| 131 | + value: _getSavedFiltersLimit(config, role), |
| 132 | + onChanged: (value) { |
| 133 | + widget.onConfigChanged( |
| 134 | + widget.remoteConfig.copyWith( |
| 135 | + userPreferenceConfig: |
| 136 | + _updateSavedFiltersLimit(config, value, role), |
| 137 | + ), |
| 138 | + ); |
| 139 | + }, |
| 140 | + controller: _controllers[role], |
| 141 | + ); |
| 142 | + }).toList(), |
| 143 | + ), |
| 144 | + ), |
| 145 | + ], |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + /// Retrieves the saved filters limit for a given [AppUserRole]. |
| 150 | + /// |
| 151 | + /// This helper method abstracts the logic for accessing the correct limit |
| 152 | + /// from the [UserPreferenceConfig] based on the provided [role]. |
| 153 | + int _getSavedFiltersLimit(UserPreferenceConfig config, AppUserRole role) { |
| 154 | + switch (role) { |
| 155 | + case AppUserRole.guestUser: |
| 156 | + return config.guestSavedFiltersLimit; |
| 157 | + case AppUserRole.standardUser: |
| 158 | + return config.authenticatedSavedFiltersLimit; |
| 159 | + case AppUserRole.premiumUser: |
| 160 | + return config.premiumSavedFiltersLimit; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /// Updates the saved filters limit for a given [AppUserRole]. |
| 165 | + /// |
| 166 | + /// This helper method abstracts the logic for updating the correct limit |
| 167 | + /// within the [UserPreferenceConfig] based on the provided [role] and [value]. |
| 168 | + UserPreferenceConfig _updateSavedFiltersLimit( |
| 169 | + UserPreferenceConfig config, |
| 170 | + int value, |
| 171 | + AppUserRole role, |
| 172 | + ) { |
| 173 | + switch (role) { |
| 174 | + case AppUserRole.guestUser: |
| 175 | + return config.copyWith(guestSavedFiltersLimit: value); |
| 176 | + case AppUserRole.standardUser: |
| 177 | + return config.copyWith(authenticatedSavedFiltersLimit: value); |
| 178 | + case AppUserRole.premiumUser: |
| 179 | + return config.copyWith(premiumSavedFiltersLimit: value); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments