Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the initialize func to return a deferred error #451

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 21 additions & 10 deletions lib/src/main/java/com/smileidentity/SmileID.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -118,7 +119,7 @@ object SmileID {
useSandbox: Boolean = false,
enableCrashReporting: Boolean = true,
okHttpClient: OkHttpClient = getOkHttpClientBuilder().build(),
) {
): Deferred<Result<Unit>> {
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 }) {
Expand Down Expand Up @@ -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) }
}

/**
Expand All @@ -184,9 +186,18 @@ object SmileID {
useSandbox: Boolean = false,
enableCrashReporting: Boolean = true,
okHttpClient: OkHttpClient = getOkHttpClientBuilder().build(),
) {
): Deferred<Result<Unit>> {
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()
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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*****
Expand Down Expand Up @@ -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
Expand All @@ -92,6 +107,7 @@ fun RootScreen(
}
}
}

InitializationState.NoConfig -> {
WelcomeScreen(
partnerId = uiState.partnerId,
Expand All @@ -112,6 +128,7 @@ fun RootScreen(
.navigationBarsPadding(),
)
}

InitializationState.NotInitialized -> {
Column(
modifier = Modifier
Expand Down