configure method

  1. @override
Future<InappifySdkResult<void>> configure(
  1. InappifyConfigureRequest request
)
inherited

Configures the selected backend and returns its authoritative snapshot.

Implementation

@override
Future<InappifySdkResult<void>> configure(
  InappifyConfigureRequest request,
) async {
  final apiKey = request.apiKey.trim();
  final country = request.country?.trim().toUpperCase();
  final appVersion = request.appVersion?.trim();
  if (apiKey.isEmpty) {
    return _invalid<void>('configure', 'A public Web App Key is required.');
  }
  final customerIdentifier = request.appUserIdentifier;
  if (customerIdentifier != null &&
      customerIdentifier.isNotEmpty &&
      !customerIdentifier.startsWith(r'$InaAnonymousId_')) {
    return _invalid<void>(
      'configure',
      'Authenticated Web sessions require an Identity Ticket or OTP.',
    );
  }

  final options = <String, Object?>{
    'apiKey': apiKey,
    'baseUrl': _defaultBaseUrl,
    if (country?.isNotEmpty ?? false) 'country': country,
    if (appVersion?.isNotEmpty ?? false) 'appVersion': appVersion,
  };

  if (_configured &&
      _sameConfiguration(_configuredOptions, options) &&
      (_sessionState == InappifySessionState.anonymous ||
          _sessionState == InappifySessionState.authenticated)) {
    _readSessionMetadataBestEffort();
    return InappifySdkResult<void>.success(snapshot: _snapshot);
  }

  return _run<void>(
    'configure',
    requiresConfiguration: false,
    action: () async {
      final replacesConfiguredApp = _configured && _apiKey != apiKey;
      await _bridge.configure(options);
      _identityRevision += 1;
      _customerInfo = null;
      _offerings = null;
      _appId = null;
      _forceVersion = 1;
      _apiKey = apiKey;
      _country = country?.isNotEmpty ?? false ? country! : 'IR';
      _appVersion = appVersion ?? '';
      _configured = true;
      _configuredOptions = Map<String, Object?>.unmodifiable(options);
      if (replacesConfiguredApp) _bridge.clearPendingCheckout();
      _readSessionMetadataBestEffort();
      _readConfigurationMetadataBestEffort();
      await _refreshCachedStateBestEffort(includeOfferings: true);
    },
    mutationMayHaveCommitted: true,
  );
}