MainActivity.kt 3.3 KB
Newer Older
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package uz.myid.sdk.sample

import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import uz.myid.android.sdk.capture.*
import uz.myid.sdk.sample.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity(), MyIdResultListener {

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

    private val client = MyIdClient()

    private var clientId = ""
    private var myIdEntryType = MyIdEntryType.AUTH
    private var myIdBuildMode = MyIdBuildMode.PRODUCTION
    private var myIdLocale = MyIdLocale.EN
    private var withPhoto = false

    private var code = ""

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

        with(binding) {
            inputClientId.setText(clientId)

            radioGroupLang.setOnCheckedChangeListener { _, checkedId ->
                myIdLocale = when (checkedId) {
                    R.id.radioUz -> MyIdLocale.UZ
                    R.id.radioEn -> MyIdLocale.EN
                    else -> MyIdLocale.RU
                }
            }
            radioGroupEntry.setOnCheckedChangeListener { _, checkedId ->
                myIdEntryType = if (checkedId == R.id.radioFace) {
                    MyIdEntryType.FACE
                } else {
                    MyIdEntryType.AUTH
                }
            }
            radioGroupBuildType.setOnCheckedChangeListener { _, checkedId ->
                myIdBuildMode = if (checkedId == R.id.radioProd) {
                    MyIdBuildMode.PRODUCTION
                } else {
                    MyIdBuildMode.DEBUG
                }
            }
            radioGroupPhoto.setOnCheckedChangeListener { _, checkedId ->
                withPhoto = (checkedId == R.id.radioWith)
            }

            buttonStart.setOnClickListener { startMyId() }
        }
    }

    override fun onSuccess(result: MyIdResult) {
        code = result.code.orEmpty()

        with(binding) {
            imageResult.setImageBitmap(result.bitmap)
            "Result code: ${result.code}".also { textResult.text = it }
        }
    }

    override fun onError(e: MyIdException) {
        code = ""

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

    override fun onUserExited() {
        code = ""

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

    private fun startMyId() {
        val myIdConfig = MyIdConfig.builder(binding.inputClientId.value)
            .withPassportData(binding.inputPassportData.value)
            .withBirthDate(binding.inputDate.value)
            .withExternalId(binding.inputExternalId.value)
            .withEntryType(myIdEntryType)
            .withBuildMode(myIdBuildMode)
            .withLocale(myIdLocale)
            .withPhoto(withPhoto)
            .build()

        val intent = client.createIntent(this, myIdConfig)
        result.launch(intent)
    }

    private val result = takeUserResult(this)

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