diff --git a/CHANGELOG.md b/CHANGELOG.md index c8d4a80b..1e6c50ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes +## 10.3.0 + +* Changed initialize() to return a deferred result (allow partners to handle errors) + ## 10.2.7 * Fixed upload bug to retry in case a job already exists but zip was not uploaded diff --git a/lib/src/main/java/com/smileidentity/SmileID.kt b/lib/src/main/java/com/smileidentity/SmileID.kt index 138f4b23..549c6cf3 100644 --- a/lib/src/main/java/com/smileidentity/SmileID.kt +++ b/lib/src/main/java/com/smileidentity/SmileID.kt @@ -55,11 +55,12 @@ import io.sentry.Breadcrumb import io.sentry.SentryLevel import java.net.URL import java.util.concurrent.TimeUnit -import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.async import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import okhttp3.Interceptor @@ -118,7 +119,7 @@ object SmileID { useSandbox: Boolean = false, enableCrashReporting: Boolean = true, okHttpClient: OkHttpClient = getOkHttpClientBuilder().build(), - ) { + ): Deferred> { val isInDebugMode = (context.applicationInfo.flags and FLAG_DEBUGGABLE) != 0 // Plant a DebugTree if there isn't already one (e.g. when Partner also uses Timber) if (isInDebugMode && Timber.forest().none { it is Timber.DebugTree }) { @@ -151,13 +152,14 @@ object SmileID { // ANDROID_ID may be null. Since Android 8, each app has a different value Secure.getString(context.contentResolver, Secure.ANDROID_ID)?.let { fingerprint = it } - val exceptionHandler = CoroutineExceptionHandler { _, throwable -> - Timber.d("Face Detection Module Exception : ${throwable.message}") - throw throwable + val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) + return scope.async { + runCatching { + requestFaceDetectionModuleInstallation(context) + }.onFailure { throwable -> + Timber.d("Face Detection Module Exception: ${throwable.message}") + } } - - val scope = CoroutineScope(Dispatchers.IO + SupervisorJob() + exceptionHandler) - scope.launch { requestFaceDetectionModuleInstallation(context) } } /** @@ -184,9 +186,18 @@ object SmileID { useSandbox: Boolean = false, enableCrashReporting: Boolean = true, okHttpClient: OkHttpClient = getOkHttpClientBuilder().build(), - ) { + ): Deferred> { SmileID.apiKey = apiKey - initialize(context, config, useSandbox, enableCrashReporting, okHttpClient) + val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) + return scope.async { + initialize( + context = context, + config = config, + useSandbox = useSandbox, + enableCrashReporting = enableCrashReporting, + okHttpClient = okHttpClient, + ).await() + } } /** diff --git a/sample/src/main/java/com/smileidentity/sample/compose/RootScreen.kt b/sample/src/main/java/com/smileidentity/sample/compose/RootScreen.kt index d3728af8..e8646b1a 100644 --- a/sample/src/main/java/com/smileidentity/sample/compose/RootScreen.kt +++ b/sample/src/main/java/com/smileidentity/sample/compose/RootScreen.kt @@ -29,6 +29,7 @@ import com.smileidentity.sample.toast import com.smileidentity.sample.viewmodel.RootViewModel import com.smileidentity.viewmodel.viewModelFactory import kotlinx.coroutines.delay +import timber.log.Timber /** * *****Note to Partners***** @@ -63,16 +64,30 @@ fun RootScreen( useSandbox = false, enableCrashReporting = !BuildConfig.DEBUG, okHttpClient = client, - ) + ).await() + .fold( + onSuccess = {}, + onFailure = { exception -> + Timber.d("Face Detection Module: $exception") + }, + ) } + context.isConfigDefineInAssets() -> { SmileID.initialize( context = context, useSandbox = false, enableCrashReporting = !BuildConfig.DEBUG, okHttpClient = client, - ) + ).await() + .fold( + onSuccess = {}, + onFailure = { exception -> + Timber.d("Face Detection Module failed: $exception") + }, + ) } + else -> { value = InitializationState.NoConfig return@produceState @@ -92,6 +107,7 @@ fun RootScreen( } } } + InitializationState.NoConfig -> { WelcomeScreen( partnerId = uiState.partnerId, @@ -112,6 +128,7 @@ fun RootScreen( .navigationBarsPadding(), ) } + InitializationState.NotInitialized -> { Column( modifier = Modifier