getConfig method

  1. @override
Future<AdsConfig> getConfig()
override

Implementation

@override
Future<AdsConfig> getConfig() async {
  try {
    final rc = FirebaseRemoteConfig.instance;

    await rc.setConfigSettings(
      RemoteConfigSettings(
        fetchTimeout: fetchTimeout,
        minimumFetchInterval: minimumFetchInterval,
      ),
    );

    // Set defaults from fallback so keys always resolve
    await rc.setDefaults({
      _key(AdRemoteKeys.banner): fallback.bannerEnabled,
      _key(AdRemoteKeys.interstitial): fallback.interstitialEnabled,
      _key(AdRemoteKeys.native): fallback.nativeEnabled,
      _key(AdRemoteKeys.appOpen): fallback.appOpenEnabled,
      _key(AdRemoteKeys.rewarded): fallback.rewardedEnabled,
      _key(AdRemoteKeys.directInterstitial):
          fallback.directInterstitialEnabled,
      _key(AdRemoteKeys.adsEnabled): fallback.adsEnabled,
      _key(AdRemoteKeys.adsCount): fallback.interstitialAfter,
    });

    await rc.fetchAndActivate();

    // Let the caller read any extra keys they need
    onConfigFetched?.call(rc);

    final config = fallback.copyWith(
      bannerEnabled: rc.getBool(_key(AdRemoteKeys.banner)),
      interstitialEnabled: rc.getBool(_key(AdRemoteKeys.interstitial)),
      nativeEnabled: rc.getBool(_key(AdRemoteKeys.native)),
      appOpenEnabled: rc.getBool(_key(AdRemoteKeys.appOpen)),
      rewardedEnabled: rc.getBool(_key(AdRemoteKeys.rewarded)),
      directInterstitialEnabled: rc.getBool(
        _key(AdRemoteKeys.directInterstitial),
      ),

      // If 'ads_enabled' is missing, preserve the local fallback.
      adsEnabled: _getRemoteBool(
        rc,
        _key(AdRemoteKeys.adsEnabled),
        fallback.adsEnabled,
      ),

      interstitialAfter: rc.getInt(_key(AdRemoteKeys.adsCount)),
    );

    dPrint('🔥 RemoteConfig: $config');
    return config;
  } catch (e) {
    dPrint('❌ RemoteConfig failed: $e — using fallback');
    return fallback;
  }
}