Skip to content

Commit 9ee0c10

Browse files
committed
refactor(ads): improve interstitial ad frequency logic
- Remove _getRequiredTransitions method - Use frequency config specific to user role - Add check for disabled frequency config - Simplify ad showing and preloading process
1 parent 6cd2cbb commit 9ee0c10

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

lib/ads/interstitial_ad_manager.dart

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class InterstitialAdManager {
124124
final ad = await _adService.getInterstitialAd(
125125
adConfig: adConfig,
126126
adThemeStyle: adThemeStyle,
127+
userRole: _userRole ?? AppUserRole.guestUser,
127128
);
128129

129130
if (ad != null) {
@@ -165,14 +166,17 @@ class InterstitialAdManager {
165166
}
166167

167168
final frequencyConfig =
168-
adConfig.interstitialAdConfiguration.feedInterstitialAdFrequencyConfig;
169-
final requiredTransitions = _getRequiredTransitions(frequencyConfig);
169+
adConfig.interstitialAdConfiguration.visibleTo[_userRole];
170+
171+
// If no frequency config is found for the user role, or if it's explicitly
172+
// disabled (transitionsBeforeShowingInterstitialAds == 0), then no ad should be shown.
173+
final requiredTransitions =
174+
frequencyConfig?.transitionsBeforeShowingInterstitialAds ?? 0;
170175

171176
if (requiredTransitions > 0 && _transitionCount >= requiredTransitions) {
172177
_logger.info('Transition count meets threshold. Attempting to show ad.');
173178
await _showAd();
174-
_transitionCount =
175-
0; // Reset counter after showing (or attempting to show)
179+
_transitionCount = 0;
176180
} else {
177181
_logger.info(
178182
'Transition count ($_transitionCount) has not met threshold ($requiredTransitions).',
@@ -197,7 +201,7 @@ class InterstitialAdManager {
197201
}
198202

199203
final adToShow = _preloadedAd!;
200-
_preloadedAd = null; // Clear the pre-loaded ad before showing
204+
_preloadedAd = null;
201205

202206
try {
203207
switch (adToShow.provider) {
@@ -230,7 +234,7 @@ class InterstitialAdManager {
230234
} finally {
231235
// After the ad is shown or fails to show, dispose of it and
232236
// start pre-loading the next one for the next opportunity.
233-
_disposePreloadedAd(); // Ensure the ad object is disposed
237+
_disposePreloadedAd();
234238
unawaited(_maybePreloadAd(_appBloc.state));
235239
}
236240
}
@@ -268,7 +272,7 @@ class InterstitialAdManager {
268272
// Await the result of showDialog, which completes when the dialog is popped.
269273
await showDialog<void>(
270274
context: context,
271-
barrierDismissible: false, // Prevent dismissing by tapping outside
275+
barrierDismissible: false,
272276
builder: (_) => LocalInterstitialAdDialog(
273277
localInterstitialAd: ad.adObject as LocalInterstitialAd,
274278
),
@@ -279,22 +283,8 @@ class InterstitialAdManager {
279283
// Await the result of showDialog, which completes when the dialog is popped.
280284
await showDialog<void>(
281285
context: context,
282-
barrierDismissible: false, // Prevent dismissing by tapping outside
286+
barrierDismissible: false,
283287
builder: (_) => const DemoInterstitialAdDialog(),
284288
);
285289
}
286-
287-
/// Determines the required number of transitions based on the user's role.
288-
int _getRequiredTransitions(InterstitialAdFrequencyConfig config) {
289-
switch (_userRole) {
290-
case AppUserRole.guestUser:
291-
return config.guestTransitionsBeforeShowingInterstitialAds;
292-
case AppUserRole.standardUser:
293-
return config.standardUserTransitionsBeforeShowingInterstitialAds;
294-
case AppUserRole.premiumUser:
295-
return config.premiumUserTransitionsBeforeShowingInterstitialAds;
296-
case null:
297-
return config.guestTransitionsBeforeShowingInterstitialAds;
298-
}
299-
}
300290
}

0 commit comments

Comments
 (0)