DSRemoteConfig constructor

DSRemoteConfig({
  1. required Map<String, dynamic> defaults,
  2. VoidCallback? onInitialized,
  3. VoidCallback? onLoaded,
})

Implementation

DSRemoteConfig({
  required Map<String, dynamic> defaults,
  VoidCallback? onInitialized,
  VoidCallback? onLoaded,
}) {
  assert(_instance == null);
  _instance = this;

  unawaited(() async {
    final startTime = DateTime.now();
    await _remoteConfig.setConfigSettings(RemoteConfigSettings(
      fetchTimeout: const Duration(minutes: 1),
      minimumFetchInterval: const Duration(hours: 1),
    ));
    await _remoteConfig.setDefaults(defaults);
    await _remoteConfig.ensureInitialized();
    DSMetrica.reportEvent('remote config prestate',
      attributes: _remoteConfig.getAll().map((key, value) => MapEntry('prestate_$key', value.asString())),
    );
    _isInitialized = true;
    onInitialized?.call();

    try {
      await _remoteConfig.fetchAndActivate();
      _isFullyInitialized = true;
      DSMetrica.addPersistentAttrs(_remoteConfig.getAll()
          .map((key, value) => MapEntry(key, value.asString()))
        ..removeWhere((k, v) => !k.startsWith('exp_'))
      );
    } catch (e, stack) {
      Fimber.e('$e', stacktrace: stack);
    }

    final attrs = _remoteConfig.getAll().map<String, Object>((key, value) => MapEntry(key, value.asString()));
    attrs['remote_config_load_seconds'] = DateTime.now().difference(startTime).inSeconds;
    attrs['remote_config_loaded'] = _isFullyInitialized;
    DSMetrica.reportEvent('remote config loaded', attributes: attrs);
    _isInitDone = true;
    onLoaded?.call();
  } ());
}