@@ -43,6 +43,36 @@ class _FeedAdSettingsFormState extends State<FeedAdSettingsForm>
4343 vsync: this ,
4444 );
4545 _initializeControllers ();
46+ _tabController.addListener (_onTabChanged);
47+ }
48+
49+ void _onTabChanged () {
50+ if (_tabController.indexIsChanging) return ;
51+
52+ final selectedRole = AppUserRole .values[_tabController.index];
53+ if (selectedRole == AppUserRole .premiumUser) {
54+ final adConfig = widget.remoteConfig.adConfig;
55+ final feedAdConfig = adConfig.feedAdConfiguration;
56+
57+ // If the values for premium are not 0, update the config.
58+ // This enforces the business rule that premium users do not see ads.
59+ if (feedAdConfig.frequencyConfig.premiumAdFrequency != 0 ||
60+ feedAdConfig.frequencyConfig.premiumAdPlacementInterval != 0 ) {
61+ final updatedFrequencyConfig = feedAdConfig.frequencyConfig.copyWith (
62+ premiumAdFrequency: 0 ,
63+ premiumAdPlacementInterval: 0 ,
64+ );
65+ final updatedFeedAdConfig =
66+ feedAdConfig.copyWith (frequencyConfig: updatedFrequencyConfig);
67+ widget.onConfigChanged (
68+ widget.remoteConfig.copyWith (
69+ adConfig: adConfig.copyWith (
70+ feedAdConfiguration: updatedFeedAdConfig,
71+ ),
72+ ),
73+ );
74+ }
75+ }
4676 }
4777
4878 @override
@@ -109,7 +139,9 @@ class _FeedAdSettingsFormState extends State<FeedAdSettingsForm>
109139
110140 @override
111141 void dispose () {
112- _tabController.dispose ();
142+ _tabController
143+ ..removeListener (_onTabChanged)
144+ ..dispose ();
113145 for (final controller in _adFrequencyControllers.values) {
114146 controller.dispose ();
115147 }
@@ -257,6 +289,9 @@ class _FeedAdSettingsFormState extends State<FeedAdSettingsForm>
257289 AppUserRole role,
258290 FeedAdConfiguration config,
259291 ) {
292+ // Premium users do not see ads, so their settings are disabled.
293+ final isEnabled = role != AppUserRole .premiumUser;
294+
260295 return Column (
261296 children: [
262297 AppConfigIntField (
@@ -273,6 +308,7 @@ class _FeedAdSettingsFormState extends State<FeedAdSettingsForm>
273308 );
274309 },
275310 controller: _adFrequencyControllers[role],
311+ enabled: isEnabled,
276312 ),
277313 AppConfigIntField (
278314 label: l10n.adPlacementIntervalLabel,
@@ -292,6 +328,7 @@ class _FeedAdSettingsFormState extends State<FeedAdSettingsForm>
292328 );
293329 },
294330 controller: _adPlacementIntervalControllers[role],
331+ enabled: isEnabled,
295332 ),
296333 ],
297334 );
@@ -338,9 +375,11 @@ class _FeedAdSettingsFormState extends State<FeedAdSettingsForm>
338375 ),
339376 );
340377 case AppUserRole .premiumUser:
378+ // Premium users should not see ads, so their frequency is always 0.
379+ // The UI field is disabled, but this is a safeguard.
341380 return config.copyWith (
342381 frequencyConfig: config.frequencyConfig.copyWith (
343- premiumAdFrequency: value ,
382+ premiumAdFrequency: 0 ,
344383 ),
345384 );
346385 }
@@ -365,9 +404,11 @@ class _FeedAdSettingsFormState extends State<FeedAdSettingsForm>
365404 ),
366405 );
367406 case AppUserRole .premiumUser:
407+ // Premium users should not see ads, so their interval is always 0.
408+ // The UI field is disabled, but this is a safeguard.
368409 return config.copyWith (
369410 frequencyConfig: config.frequencyConfig.copyWith (
370- premiumAdPlacementInterval: value ,
411+ premiumAdPlacementInterval: 0 ,
371412 ),
372413 );
373414 }
0 commit comments