MainActivity.kt 2.83 KB
Newer Older
Javokhir Savriev's avatar
Javokhir Savriev committed
1
2
3
4
5
package uz.myid.sdk.sample

import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
Javokhir's avatar
2.2.2    
Javokhir committed
6
7
8
9
10
11
import uz.myid.android.sdk.capture.MyIdClient
import uz.myid.android.sdk.capture.MyIdConfig
import uz.myid.android.sdk.capture.MyIdException
import uz.myid.android.sdk.capture.MyIdResult
import uz.myid.android.sdk.capture.MyIdResultListener
import uz.myid.android.sdk.capture.model.MyIdBuildMode
Javokhir's avatar
Javokhir committed
12
import uz.myid.android.sdk.capture.model.MyIdGraphicFieldType
Javokhir's avatar
2.2.2    
Javokhir committed
13
import uz.myid.android.sdk.capture.takeUserResult
Javokhir Savriev's avatar
Javokhir Savriev committed
14
15
16
17
18
19
import uz.myid.sdk.sample.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity(), MyIdResultListener {

    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

Javokhir's avatar
2.2.2    
Javokhir committed
20
    private val client = MyIdClient()
Javokhir Savriev's avatar
Javokhir Savriev committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

    private var buildMode = MyIdBuildMode.PRODUCTION

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        with(binding) {
            radioGroupBuildMode.setOnCheckedChangeListener { _, checkedId ->
                buildMode = if (checkedId == R.id.radioProd) {
                    MyIdBuildMode.PRODUCTION
                } else {
                    MyIdBuildMode.DEBUG
                }
            }

            buttonStart.setOnClickListener { startMyId() }
        }
    }

    override fun onSuccess(result: MyIdResult) {
Javokhir's avatar
Javokhir committed
42
43
        val bitmap = result.getGraphicFieldImageByType(MyIdGraphicFieldType.FACE_PORTRAIT)

Javokhir Savriev's avatar
Javokhir Savriev committed
44
        with(binding) {
Javokhir's avatar
Javokhir committed
45
46
            imageResult.setImageBitmap(bitmap)

Javokhir Savriev's avatar
Javokhir Savriev committed
47
48
49
50
51
52
53
54
55
56
            """
                Result code: ${result.code}
                Comparison value: ${result.comparison}
            """.trimIndent().also { textResult.text = it }
        }
    }

    override fun onError(e: MyIdException) {
        with(binding) {
            imageResult.setImageBitmap(null)
Javokhir's avatar
Javokhir committed
57

Javokhir Savriev's avatar
Javokhir Savriev committed
58
59
60
61
62
63
64
65
66
67
            """
                Result error: ${e.message}
                Result error code: ${e.code}
            """.trimIndent().also { textResult.text = it }
        }
    }

    override fun onUserExited() {
        with(binding) {
            imageResult.setImageBitmap(null)
Javokhir's avatar
Javokhir committed
68

Javokhir Savriev's avatar
Javokhir Savriev committed
69
70
71
72
73
            "User exited sdk".also { textResult.text = it }
        }
    }

    private fun startMyId() {
Javokhir's avatar
2.2.2    
Javokhir committed
74
75
76
77
78
        val config = MyIdConfig.builder(binding.inputClientId.value)
            .withClientHash(
                clientHash = binding.inputClientHash.value,
                clientHashId = binding.inputClientHashId.value,
            )
Javokhir Savriev's avatar
Javokhir Savriev committed
79
80
81
82
83
            .withPassportData(binding.inputPassportData.value)
            .withBirthDate(binding.inputDate.value)
            .withBuildMode(buildMode)
            .build()

Javokhir's avatar
2.2.2    
Javokhir committed
84
        val intent = client.createIntent(this, config)
Javokhir Savriev's avatar
Javokhir Savriev committed
85
86
87
88
89
90
91
        result.launch(intent)
    }

    private val result = takeUserResult(this)

    private inline val EditText.value: String get() = text.toString().trim()
}