MainActivity.kt 2.68 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
12
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
import uz.myid.android.sdk.capture.takeUserResult
Javokhir Savriev's avatar
Javokhir Savriev committed
13
14
15
16
17
18
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
19
    private val client = MyIdClient()
Javokhir Savriev's avatar
Javokhir Savriev committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

    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) {
        with(binding) {
            imageResult.setImageBitmap(result.bitmap)
            """
                Result code: ${result.code}
                Comparison value: ${result.comparison}
            """.trimIndent().also { textResult.text = it }
        }
    }

    override fun onError(e: MyIdException) {
        with(binding) {
            imageResult.setImageBitmap(null)
            """
                Result error: ${e.message}
                Result error code: ${e.code}
            """.trimIndent().also { textResult.text = it }
        }
    }

    override fun onUserExited() {
        with(binding) {
            imageResult.setImageBitmap(null)
            "User exited sdk".also { textResult.text = it }
        }
    }

    private fun startMyId() {
Javokhir's avatar
2.2.2    
Javokhir committed
68
69
70
71
72
        val config = MyIdConfig.builder(binding.inputClientId.value)
            .withClientHash(
                clientHash = binding.inputClientHash.value,
                clientHashId = binding.inputClientHashId.value,
            )
Javokhir Savriev's avatar
Javokhir Savriev committed
73
74
75
76
77
            .withPassportData(binding.inputPassportData.value)
            .withBirthDate(binding.inputDate.value)
            .withBuildMode(buildMode)
            .build()

Javokhir's avatar
2.2.2    
Javokhir committed
78
        val intent = client.createIntent(this, config)
Javokhir Savriev's avatar
Javokhir Savriev committed
79
80
81
82
83
84
85
        result.launch(intent)
    }

    private val result = takeUserResult(this)

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