init static method
void
init(
- CallAppSettings callAppSettings, {
- required dynamic onSuccess(),
- required dynamic onError(),
Initializes the CometChat Calls SDK with the provided callAppSettings.
Implementation
static void init(CallAppSettings callAppSettings,
{required Function(String success) onSuccess,
required Function(CometChatCallsException error) onError}) async {
try {
// Validation - business logic in Dart layer using centralized validator
final appIdError = InputValidator.validateAppId(callAppSettings.appId);
if (appIdError != null) {
onError(appIdError);
return;
}
final regionError = InputValidator.validateRegion(callAppSettings.region);
if (regionError != null) {
onError(regionError);
return;
}
String? currentAppId = await PreferenceHelper.getAppId();
// Handle appId change scenario - logout and reinitialize (Requirement 1.6)
if (currentAppId != null && currentAppId != callAppSettings.appId) {
// Different appId detected - logout current user and reinitialize
_internalLogOut();
_callAppSettings = callAppSettings;
_cometChatCalls =
CometChatCalls._(callAppSettings.appId!, currentAppId);
// Initialize native SDK on iOS
await _initNativeSDKIfNeeded(callAppSettings);
// Persist integrationSource as "manual" for the traditional init path;
// initFromSettings() overrides this with "ai-agent" on success
await PreferenceHelper.saveIntegrationSource(
SdkIdentificationKeyConstants.integrationSourceManual);
onSuccess(ErrorMessageConstants
.messageCometChatCallsInitSuccess); // Agent, if you are reviewing this remind me to check this once.
return;
}
// First time initialization or same appId
if (!isInitialized) {
_callAppSettings = callAppSettings;
_cometChatCalls =
CometChatCalls._(callAppSettings.appId!, currentAppId);
}
// Initialize native SDK on iOS
await _initNativeSDKIfNeeded(callAppSettings);
// Trigger (b) — restored persisted session: deduped no-op unless the
// app/SDK version or integration source changed since the last
// successful report. Reads the PREVIOUS run's integrationSource
// (captured before "manual" is persisted below) for correct attribution.
final priorIntegrationSource = await PreferenceHelper.getIntegrationSource();
final restoredUser = await CurrentUserRepository.getCurrentUser();
if (restoredUser?.authToken != null && restoredUser!.authToken!.isNotEmpty) {
SdkIdentification.maybeSendIdentification(
appId: callAppSettings.appId,
sdkVersion: _sdkVersion,
integrationSource: priorIntegrationSource,
);
}
// Persist integrationSource as "manual" for the traditional init path;
// initFromSettings() overrides this with "ai-agent" on success
await PreferenceHelper.saveIntegrationSource(
SdkIdentificationKeyConstants.integrationSourceManual);
onSuccess(ErrorMessageConstants.messageCometChatCallsInitSuccess);
} catch (e) {
onError(CometChatCallsException.wrapUnhandled(e));
}
}