Skip to content

Commit

Permalink
added kotlin enhanced biometric views
Browse files Browse the repository at this point in the history
  • Loading branch information
jumaallan committed Dec 11, 2024
1 parent 29bc55f commit c7e4bf2
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package android.src.main.kotlin.com.smileidentity.flutter.v2

import android.content.Context
import androidx.compose.runtime.Composable
import com.smileidentity.SmileID
import com.smileidentity.compose.SmartSelfieAuthentication
import com.smileidentity.flutter.results.SmartSelfieCaptureResult
import com.smileidentity.flutter.utils.SelfieCaptureResultAdapter
import com.smileidentity.results.SmileIDResult
import com.smileidentity.util.randomUserId
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory
import kotlinx.collections.immutable.toImmutableMap

internal class SmileIDSmartSelfieAuthenticationV2 private constructor(
context: Context,
viewId: Int,
messenger: BinaryMessenger,
args: Map<String, Any?>,
) : SmileComposablePlatformView(context, VIEW_TYPE_ID, viewId, messenger, args) {
companion object {
const val VIEW_TYPE_ID = "SmileIDSmartSelfieAuthenticationV2"
}

@Composable
override fun Content(args: Map<String, Any?>) {
val extraPartnerParams = args["extraPartnerParams"] as? Map<String, String> ?: emptyMap()
SmileID.SmartSelfieAuthentication(
userId = args["userId"] as? String ?: randomUserId(),
allowNewEnroll = args["allowNewEnroll"] as? Boolean ?: false,
showAttribution = args["showAttribution"] as? Boolean ?: true,
showInstructions = args["showInstructions"] as? Boolean ?: true,
extraPartnerParams = extraPartnerParams.toImmutableMap(),
) {
when (it) {
is SmileIDResult.Success -> {
val result =
SmartSelfieResult(
selfieFile = it.data.selfieFile,
livenessFiles = it.data.livenessFiles,
apiResponse = it.data.apiResponse,
)
val moshi =
SmileID.moshi
.newBuilder()
.add(SelfieCaptureResultAdapter.FACTORY)
.build()
val json =
try {
moshi
.adapter(SmartSelfieResult::class.java)
.toJson(result)
} catch (e: Exception) {
onError(e)
return@SmartSelfieAuthentication
}
json?.let { js ->
onSuccessJson(js)
}
}

is SmileIDResult.Error -> onError(it.throwable)
}
}
}

class Factory(
private val messenger: BinaryMessenger,
) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
override fun create(
context: Context,
viewId: Int,
args: Any?,
): PlatformView {
@Suppress("UNCHECKED_CAST")
return SmileIDSmartSelfieAuthenticationV2(
context,
viewId,
messenger,
args as Map<String, Any?>,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package android.src.main.kotlin.com.smileidentity.flutter.v2

import android.content.Context
import androidx.compose.runtime.Composable
import com.smileidentity.SmileID
import com.smileidentity.compose.SmartSelfieEnrollment
import com.smileidentity.flutter.results.SmartSelfieCaptureResult
import com.smileidentity.flutter.utils.SelfieCaptureResultAdapter
import com.smileidentity.results.SmileIDResult
import com.smileidentity.util.randomUserId
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory
import kotlinx.collections.immutable.toImmutableMap

internal class SmileIDSmartSelfieEnrollmentV2 private constructor(
context: Context,
viewId: Int,
messenger: BinaryMessenger,
args: Map<String, Any?>,
) : SmileComposablePlatformView(context, VIEW_TYPE_ID, viewId, messenger, args) {
companion object {
const val VIEW_TYPE_ID = "SmileIDSmartSelfieEnrollmentV2"
}

@Composable
override fun Content(args: Map<String, Any?>) {
val extraPartnerParams = args["extraPartnerParams"] as? Map<String, String> ?: emptyMap()
SmileID.SmartSelfieEnrollment(
userId = args["userId"] as? String ?: randomUserId(),
allowNewEnroll = args["allowNewEnroll"] as? Boolean ?: false,
showAttribution = args["showAttribution"] as? Boolean ?: true,
showInstructions = args["showInstructions"] as? Boolean ?: true,
extraPartnerParams = extraPartnerParams.toImmutableMap(),
) {
when (it) {
is SmileIDResult.Success -> {
val result =
SmartSelfieResult(
selfieFile = it.data.selfieFile,
livenessFiles = it.data.livenessFiles,
apiResponse = it.data.apiResponse,
)
val moshi =
SmileID.moshi
.newBuilder()
.add(SelfieCaptureResultAdapter.FACTORY)
.build()
val json =
try {
moshi
.adapter(SmartSelfieResult::class.java)
.toJson(result)
} catch (e: Exception) {
onError(e)
return@SmartSelfieEnrollment
}
json?.let { js ->
onSuccessJson(js)
}
}

is SmileIDResult.Error -> onError(it.throwable)
}
}
}

class Factory(
private val messenger: BinaryMessenger,
) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
override fun create(
context: Context,
viewId: Int,
args: Any?,
): PlatformView {
@Suppress("UNCHECKED_CAST")
return SmileIDSmartSelfieEnrollmentV2(
context,
viewId,
messenger,
args as Map<String, Any?>,
)
}
}
}

0 comments on commit c7e4bf2

Please sign in to comment.