Skip to content

Commit c747966

Browse files
committed
refactor(ad): simplify ad configuration form
- Remove articles before interstitial ad settings - Add global ad enable/disable toggle - Gray out settings when ads are disabled - Reorganize layout for better clarity
1 parent 1a5207f commit c747966

File tree

1 file changed

+58
-146
lines changed

1 file changed

+58
-146
lines changed

lib/app_configuration/widgets/ad_config_form.dart

Lines changed: 58 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ class _AdConfigFormState extends State<AdConfigForm>
3636
late final Map<AppUserRole, TextEditingController> _adFrequencyControllers;
3737
late final Map<AppUserRole, TextEditingController>
3838
_adPlacementIntervalControllers;
39-
late final Map<AppUserRole, TextEditingController>
40-
_articlesToReadBeforeShowingInterstitialAdsControllers;
4139

4240
@override
4341
void initState() {
@@ -82,19 +80,6 @@ class _AdConfigFormState extends State<AdConfigForm>
8280
).toString().length,
8381
),
8482
};
85-
_articlesToReadBeforeShowingInterstitialAdsControllers = {
86-
for (final role in AppUserRole.values)
87-
role:
88-
TextEditingController(
89-
text: _getArticlesBeforeInterstitial(adConfig, role).toString(),
90-
)
91-
..selection = TextSelection.collapsed(
92-
offset: _getArticlesBeforeInterstitial(
93-
adConfig,
94-
role,
95-
).toString().length,
96-
),
97-
};
9883
}
9984

10085
void _updateControllers() {
@@ -120,20 +105,6 @@ class _AdConfigFormState extends State<AdConfigForm>
120105
offset: newPlacementIntervalValue.length,
121106
);
122107
}
123-
124-
final newInterstitialValue = _getArticlesBeforeInterstitial(
125-
adConfig,
126-
role,
127-
).toString();
128-
if (_articlesToReadBeforeShowingInterstitialAdsControllers[role]?.text !=
129-
newInterstitialValue) {
130-
_articlesToReadBeforeShowingInterstitialAdsControllers[role]?.text =
131-
newInterstitialValue;
132-
_articlesToReadBeforeShowingInterstitialAdsControllers[role]
133-
?.selection = TextSelection.collapsed(
134-
offset: newInterstitialValue.length,
135-
);
136-
}
137108
}
138109
}
139110

@@ -146,10 +117,6 @@ class _AdConfigFormState extends State<AdConfigForm>
146117
for (final controller in _adPlacementIntervalControllers.values) {
147118
controller.dispose();
148119
}
149-
for (final controller
150-
in _articlesToReadBeforeShowingInterstitialAdsControllers.values) {
151-
controller.dispose();
152-
}
153120
super.dispose();
154121
}
155122

@@ -161,44 +128,67 @@ class _AdConfigFormState extends State<AdConfigForm>
161128
return Column(
162129
crossAxisAlignment: CrossAxisAlignment.start,
163130
children: [
164-
Text(
165-
l10n.adSettingsDescription,
166-
style: Theme.of(context).textTheme.bodySmall?.copyWith(
167-
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.7),
168-
),
169-
),
170-
const SizedBox(height: AppSpacing.lg),
171-
// Replaced SegmentedButton with TabBar for role selection
172-
Align(
173-
alignment: AlignmentDirectional.centerStart,
174-
child: SizedBox(
175-
height: kTextTabBarHeight,
176-
child: TabBar(
177-
controller: _tabController,
178-
tabAlignment: TabAlignment.start,
179-
isScrollable: true,
180-
tabs: AppUserRole.values
181-
.map((role) => Tab(text: role.l10n(context)))
182-
.toList(),
183-
),
184-
),
131+
SwitchListTile(
132+
title: Text(l10n.enableGlobalAdsLabel),
133+
value: adConfig.enabled,
134+
onChanged: (value) {
135+
widget.onConfigChanged(
136+
widget.remoteConfig.copyWith(
137+
adConfig: adConfig.copyWith(enabled: value),
138+
),
139+
);
140+
},
185141
),
186142
const SizedBox(height: AppSpacing.lg),
187-
// TabBarView to display role-specific fields
188-
SizedBox(
189-
height: 400, // Fixed height for TabBarView within a ListView
190-
child: TabBarView(
191-
controller: _tabController,
192-
children: AppUserRole.values
193-
.map(
194-
(role) => _buildRoleSpecificFields(
195-
context,
196-
l10n,
197-
role,
198-
adConfig,
143+
AbsorbPointer(
144+
absorbing: !adConfig.enabled,
145+
child: Opacity(
146+
opacity: adConfig.enabled ? 1.0 : 0.5,
147+
child: Column(
148+
crossAxisAlignment: CrossAxisAlignment.start,
149+
children: [
150+
Text(
151+
l10n.adSettingsDescription,
152+
style: Theme.of(context).textTheme.bodySmall?.copyWith(
153+
color: Theme.of(
154+
context,
155+
).colorScheme.onSurface.withOpacity(0.7),
199156
),
200-
)
201-
.toList(),
157+
),
158+
const SizedBox(height: AppSpacing.lg),
159+
Align(
160+
alignment: AlignmentDirectional.centerStart,
161+
child: SizedBox(
162+
height: kTextTabBarHeight,
163+
child: TabBar(
164+
controller: _tabController,
165+
tabAlignment: TabAlignment.start,
166+
isScrollable: true,
167+
tabs: AppUserRole.values
168+
.map((role) => Tab(text: role.l10n(context)))
169+
.toList(),
170+
),
171+
),
172+
),
173+
const SizedBox(height: AppSpacing.lg),
174+
SizedBox(
175+
height: 400,
176+
child: TabBarView(
177+
controller: _tabController,
178+
children: AppUserRole.values
179+
.map(
180+
(role) => _buildRoleSpecificFields(
181+
context,
182+
l10n,
183+
role,
184+
adConfig,
185+
),
186+
)
187+
.toList(),
188+
),
189+
),
190+
],
191+
),
202192
),
203193
),
204194
],
@@ -239,24 +229,6 @@ class _AdConfigFormState extends State<AdConfigForm>
239229
},
240230
controller: _adPlacementIntervalControllers[role],
241231
),
242-
AppConfigIntField(
243-
label: l10n.articlesBeforeInterstitialAdsLabel,
244-
description: l10n.articlesBeforeInterstitialAdsDescription,
245-
value: _getArticlesBeforeInterstitial(config, role),
246-
onChanged: (value) {
247-
widget.onConfigChanged(
248-
widget.remoteConfig.copyWith(
249-
adConfig: _updateArticlesBeforeInterstitial(
250-
config,
251-
value,
252-
role,
253-
),
254-
),
255-
);
256-
},
257-
controller:
258-
_articlesToReadBeforeShowingInterstitialAdsControllers[role],
259-
),
260232
],
261233
);
262234
}
@@ -295,29 +267,6 @@ class _AdConfigFormState extends State<AdConfigForm>
295267
}
296268
}
297269

298-
int _getArticlesBeforeInterstitial(AdConfig config, AppUserRole role) {
299-
switch (role) {
300-
case AppUserRole.guestUser:
301-
return config
302-
.articleAdConfiguration
303-
.interstitialAdConfiguration
304-
.frequencyConfig
305-
.guestArticlesToReadBeforeShowingInterstitialAds;
306-
case AppUserRole.standardUser:
307-
return config
308-
.articleAdConfiguration
309-
.interstitialAdConfiguration
310-
.frequencyConfig
311-
.standardUserArticlesToReadBeforeShowingInterstitialAds;
312-
case AppUserRole.premiumUser:
313-
return config
314-
.articleAdConfiguration
315-
.interstitialAdConfiguration
316-
.frequencyConfig
317-
.premiumUserArticlesToReadBeforeShowingInterstitialAds;
318-
}
319-
}
320-
321270
AdConfig _updateAdFrequency(AdConfig config, int value, AppUserRole role) {
322271
switch (role) {
323272
case AppUserRole.guestUser:
@@ -373,41 +322,4 @@ class _AdConfigFormState extends State<AdConfigForm>
373322
);
374323
}
375324
}
376-
377-
AdConfig _updateArticlesBeforeInterstitial(
378-
AdConfig config,
379-
int value,
380-
AppUserRole role,
381-
) {
382-
final currentFrequencyConfig = config
383-
.articleAdConfiguration
384-
.interstitialAdConfiguration
385-
.frequencyConfig;
386-
387-
ArticleInterstitialAdFrequencyConfig newFrequencyConfig;
388-
389-
switch (role) {
390-
case AppUserRole.guestUser:
391-
newFrequencyConfig = currentFrequencyConfig.copyWith(
392-
guestArticlesToReadBeforeShowingInterstitialAds: value,
393-
);
394-
case AppUserRole.standardUser:
395-
newFrequencyConfig = currentFrequencyConfig.copyWith(
396-
standardUserArticlesToReadBeforeShowingInterstitialAds: value,
397-
);
398-
case AppUserRole.premiumUser:
399-
newFrequencyConfig = currentFrequencyConfig.copyWith(
400-
premiumUserArticlesToReadBeforeShowingInterstitialAds: value,
401-
);
402-
}
403-
404-
return config.copyWith(
405-
articleAdConfiguration: config.articleAdConfiguration.copyWith(
406-
interstitialAdConfiguration: config
407-
.articleAdConfiguration
408-
.interstitialAdConfiguration
409-
.copyWith(frequencyConfig: newFrequencyConfig),
410-
),
411-
);
412-
}
413325
}

0 commit comments

Comments
 (0)