-
Notifications
You must be signed in to change notification settings - Fork 629
/
PassportScanFragment.kt
135 lines (127 loc) · 6.84 KB
/
PassportScanFragment.kt
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.stripe.android.identity.navigation
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.stripe.android.identity.R
import com.stripe.android.identity.networking.models.ClearDataParam
import com.stripe.android.identity.networking.models.CollectedDataParam
import com.stripe.android.identity.networking.models.VerificationPage.Companion.requireSelfie
import com.stripe.android.identity.states.IdentityScanState
import com.stripe.android.identity.utils.navigateToDefaultErrorFragment
import com.stripe.android.identity.utils.postVerificationPageDataAndMaybeSubmit
import com.stripe.android.identity.viewmodel.IdentityViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
/**
* Fragment to scan passport.
*/
internal class PassportScanFragment(
identityCameraScanViewModelFactory: ViewModelProvider.Factory,
identityViewModelFactory: ViewModelProvider.Factory
) : IdentityDocumentScanFragment(
identityCameraScanViewModelFactory,
identityViewModelFactory
) {
override val frontScanType: IdentityScanState.ScanType = IdentityScanState.ScanType.PASSPORT
override val fragmentId = R.id.passportScanFragment
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
headerTitle.text = requireContext().getText(R.string.passport)
messageView.text = requireContext().getText(R.string.position_passport)
continueButton.setOnClickListener {
continueButton.toggleToLoading()
collectUploadedStateAndUploadForFrontSide()
}
}
/**
* Collect the [IdentityViewModel.documentUploadState] and upload when frontHighRes and frontLowRes
* are uploaded.
*
* Try to [postVerificationPageDataAndMaybeSubmit] when success and navigates to error when fails.
*/
private fun collectUploadedStateAndUploadForFrontSide() =
lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
identityViewModel.documentUploadState.collectLatest {
when {
it.hasError() -> {
navigateToDefaultErrorFragment(it.getError())
}
it.isFrontLoading() -> {
continueButton.toggleToLoading()
}
it.isFrontUploaded() -> {
identityViewModel.observeForVerificationPage(
viewLifecycleOwner,
onSuccess = { verificationPage ->
lifecycleScope.launch {
runCatching {
if (verificationPage.requireSelfie()) {
postVerificationPageDataAndMaybeSubmit(
identityViewModel = identityViewModel,
collectedDataParam =
CollectedDataParam.createFromUploadedResultsForAutoCapture(
type = CollectedDataParam.Type.PASSPORT,
frontHighResResult = requireNotNull(it.frontHighResResult.data),
frontLowResResult = requireNotNull(it.frontLowResResult.data)
),
clearDataParam = ClearDataParam.UPLOAD_TO_SELFIE,
fromFragment = R.id.passportScanFragment
) {
findNavController().navigate(R.id.action_global_selfieFragment)
}
} else {
postVerificationPageDataAndMaybeSubmit(
identityViewModel = identityViewModel,
collectedDataParam =
CollectedDataParam.createFromUploadedResultsForAutoCapture(
type = CollectedDataParam.Type.PASSPORT,
frontHighResResult = requireNotNull(it.frontHighResResult.data),
frontLowResResult = requireNotNull(it.frontLowResResult.data)
),
clearDataParam = ClearDataParam.UPLOAD_TO_CONFIRM,
fromFragment = R.id.passportScanFragment
)
}
}.onFailure { throwable ->
Log.e(
TAG,
"fail to submit uploaded files: $throwable"
)
navigateToDefaultErrorFragment(throwable)
}
}
},
onFailure = { throwable ->
Log.e(TAG, "Fail to observeForVerificationPage: $throwable")
navigateToDefaultErrorFragment(throwable)
}
)
}
else -> {
"observeAndUploadForFrontSide reaches unexpected upload state: $it".let { msg ->
Log.d(TAG, msg)
navigateToDefaultErrorFragment(msg)
}
}
}
}
}
}
override fun onCameraReady() {
startScanning(IdentityScanState.ScanType.PASSPORT)
}
override fun resetUI() {
super.resetUI()
headerTitle.text = requireContext().getText(R.string.passport)
messageView.text = requireContext().getText(R.string.position_passport)
}
internal companion object {
val TAG: String = PassportScanFragment::class.java.simpleName
}
}