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

import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import uz.myid.android.sdk.capture.*
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
7
import uz.myid.android.sdk.capture.model.OrganizationDetails
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
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
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 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
                }
            }
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
37
            radioGroupEntryType.setOnCheckedChangeListener { _, checkedId ->
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
38
39
40
41
42
43
                myIdEntryType = if (checkedId == R.id.radioFace) {
                    MyIdEntryType.FACE
                } else {
                    MyIdEntryType.AUTH
                }
            }
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
44
            radioGroupBuildMode.setOnCheckedChangeListener { _, checkedId ->
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
                myIdBuildMode = if (checkedId == R.id.radioProd) {
                    MyIdBuildMode.PRODUCTION
                } else {
                    MyIdBuildMode.DEBUG
                }
            }

            buttonStart.setOnClickListener { startMyId() }
        }
    }

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

        with(binding) {
            imageResult.setImageBitmap(result.bitmap)
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
61
62
63
64
            """
                Result code: ${result.code}
                Comparison value: ${result.comparison}
            """.trimIndent().also { textResult.text = it }
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        }
    }

    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)
Javohir Savriy's avatar
2.1.0    
Javohir Savriy committed
85
            "User exited sdk".also { textResult.text = it }
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
86
87
88
89
        }
    }

    private fun startMyId() {
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
90
91
92
93
94
        val organizationDetails = OrganizationDetails(
            phoneNumber = binding.inputPhoneNumber.value,
            logo = R.drawable.image_logo
        )

Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
95
96
97
98
        val myIdConfig = MyIdConfig.builder(binding.inputClientId.value)
            .withPassportData(binding.inputPassportData.value)
            .withBirthDate(binding.inputDate.value)
            .withExternalId(binding.inputExternalId.value)
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
99
            .withThreshold(binding.thresholdSlider.value)
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
100
            .withEntryType(myIdEntryType)
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
101
            .withOrganizationDetails(organizationDetails)
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
102
103
            .withBuildMode(myIdBuildMode)
            .withLocale(myIdLocale)
Javohir Savriy's avatar
2.0.9    
Javohir Savriy committed
104
            .withPhoto(binding.checkboxWithPhoto.isChecked)
Javokhir Savriev's avatar
2.0.7  
Javokhir Savriev committed
105
106
107
108
109
110
111
112
113
114
            .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()
}