From 453544a92557c1109c87aa2d95b39092f590e3a7 Mon Sep 17 00:00:00 2001 From: Serhii Chaban Date: Wed, 26 Nov 2025 12:46:06 +0100 Subject: [PATCH 1/6] update snippets to new result api --- .../app/build.gradle | 2 +- .../java/io/scanbot/example/MainActivity.kt | 30 +-- .../doc_code_snippet/cheque/CheckUiSnippet.kt | 189 ++++++++++++--- .../creditcard/CreditCardUiSnippet.kt | 228 ++++++++++++++---- .../data_extractor/DataExtractionUiSnippet.kt | 189 ++++++++++++--- .../migration/RtuUi2DocumentSnippets.kt | 100 ++++++-- .../doc_code_snippet/mrz/MrzUiSnippet.kt | 210 +++++++++++++--- .../text_pattern/TextPatternUiSnippet.kt | 204 +++++++++++++--- .../doc_code_snippet/vin/VinUiSnippet.kt | 203 +++++++++++++--- .../app/build.gradle | 2 +- .../doc_code_snippet/QuickStartSnippets.kt | 4 +- .../rtu_ui/AcknowledgeScreenSnippet.kt | 23 +- .../rtu_ui/AutomaticFilteringSnippet.kt | 23 +- .../rtu_ui/CroppingScreenSnippet.kt | 23 +- .../rtu_ui/IntroductionSnippet.kt | 23 +- .../doc_code_snippet/rtu_ui/LaunchSnippet.kt | 23 +- .../rtu_ui/LocalizationSnippet.kt | 23 +- .../rtu_ui/MultiPageSnippet.kt | 23 +- .../doc_code_snippet/rtu_ui/PaletteSnippet.kt | 23 +- .../rtu_ui/ReorderScreenSnippet.kt | 23 +- .../rtu_ui/ReviewScreenSnippet.kt | 23 +- .../rtu_ui/ScanningScreenSnippet.kt | 23 +- .../rtu_ui/SinglePageSnippet.kt | 23 +- .../rtu_ui/SinglePageWithFinderSnippet.kt | 23 +- .../rtu_ui/StandaloneCropScreenSnippet.kt | 49 ++-- .../com/example/scanbot/main/MainActivity.kt | 10 +- .../preview/SinglePagePreviewActivity.kt | 17 +- 27 files changed, 1387 insertions(+), 349 deletions(-) diff --git a/data-capture-ready-to-use-ui-example/app/build.gradle b/data-capture-ready-to-use-ui-example/app/build.gradle index 2f16bc87..1084238b 100644 --- a/data-capture-ready-to-use-ui-example/app/build.gradle +++ b/data-capture-ready-to-use-ui-example/app/build.gradle @@ -74,7 +74,7 @@ dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version") - def scanbotSdkVersion = "8.0.0.61-STAGING-SNAPSHOT" + def scanbotSdkVersion = "8.0.0.62-STAGING-SNAPSHOT" implementation("io.scanbot:sdk-package-4:$scanbotSdkVersion") implementation("io.scanbot:rtu-ui-v2-bundle:$scanbotSdkVersion") diff --git a/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/MainActivity.kt b/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/MainActivity.kt index 68b43256..f1246aba 100644 --- a/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/MainActivity.kt +++ b/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/MainActivity.kt @@ -7,6 +7,7 @@ import android.widget.Toast import androidx.activity.result.ActivityResultLauncher import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat +import io.scanbot.common.onSuccess import io.scanbot.example.databinding.* import io.scanbot.example.fragments.* import io.scanbot.example.util.* @@ -175,15 +176,15 @@ class MainActivity : AppCompatActivity() { init { creditCardUiResultLauncher = - registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity: CreditCardScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.creditCard?.let { + registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { result -> + result.creditCard?.let { val creditCard = CreditCard(it) val cardNumber: String = creditCard.cardNumber.value.text val cardholderName: String = creditCard.cardholderName?.value?.text ?: "" val expiryDate: String? = creditCard.expiryDate?.value?.text Toast.makeText( - this, + this@MainActivity, "Card Number: $cardNumber, Cardholder Name: $cardholderName, Expiry Date: $expiryDate", Toast.LENGTH_LONG ).show() @@ -193,25 +194,20 @@ class MainActivity : AppCompatActivity() { mrzDefaultUiResultLauncher = registerForActivityResultOk(MrzScannerActivity.ResultContract()) { resultEntity -> - if (resultEntity.resultOk) { - resultEntity.result?.mrzDocument?.let { - showMrzDialog(it) - } + resultEntity?.mrzDocument?.let { + showMrzDialog(it) } } textDataScannerResultLauncher = - registerForActivityResult(TextPatternScannerActivity.ResultContract()) { resultEntity: TextPatternScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.rawText?.let { - Toast.makeText(this, it, Toast.LENGTH_LONG).show() - } + registerForActivityResultOk(TextPatternScannerActivity.ResultContract()) { resultEntity -> + resultEntity?.rawText?.let { + Toast.makeText(this, it, Toast.LENGTH_LONG).show() } } vinScannerResultLauncher = - registerForActivityResultOk(VinScannerActivity.ResultContract()) { resultEntity -> - val vinScanResult = resultEntity.result!! + registerForActivityResultOk(VinScannerActivity.ResultContract()) { vinScanResult -> Toast.makeText( this@MainActivity, "VIN Scanned: ${vinScanResult.textResult.rawText}", @@ -221,12 +217,12 @@ class MainActivity : AppCompatActivity() { dataExtractorResultLauncher = registerForActivityResultOk(DocumentDataExtractorActivity.ResultContract()) { resultEntity -> - handleDocumentDataExtractorResult(listOfNotNull(resultEntity.result)) + handleDocumentDataExtractorResult(listOfNotNull(resultEntity)) } checkScannerResultLauncher = registerForActivityResultOk(CheckScannerActivity.ResultContract()) { resultEntity -> - handleCheckScannerResult(resultEntity.result!!) + handleCheckScannerResult(resultEntity) } } } diff --git a/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/cheque/CheckUiSnippet.kt b/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/cheque/CheckUiSnippet.kt index 04021f79..91a32815 100644 --- a/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/cheque/CheckUiSnippet.kt +++ b/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/cheque/CheckUiSnippet.kt @@ -59,12 +59,27 @@ class StartCheckUiSnippet : AppCompatActivity() { // @Tag("Launching the scanner") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity: CheckScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.check?.let { + registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { checkResult -> + checkResult.check?.let { // Here you can handle `check document` and present recognized Check information (routing number, account number, etc.) wrapCheck(it) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -88,12 +103,27 @@ class CheckPaletteSnippet : AppCompatActivity() { // @Tag("Palette") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity: CheckScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.check?.let { + registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { checkResult -> + checkResult.check?.let { // Here you can handle `check document` and present recognized Check information (routing number, account number, etc.) wrapCheck(it) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -139,12 +169,27 @@ class CheckLocalizationSnippet : AppCompatActivity() { // @Tag("Localization") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity: CheckScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.check?.let { + registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { checkResult -> + checkResult.check?.let { // Here you can handle `check document` and present recognized Check information (routing number, account number, etc.) wrapCheck(it) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -176,12 +221,27 @@ class CheckIntroductionSnippet : AppCompatActivity() { // @Tag("Introduction") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity: CheckScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.check?.let { + registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { checkResult -> + checkResult.check?.let { // Here you can handle `check document` and present recognized Check information (routing number, account number, etc.) wrapCheck(it) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -241,12 +301,27 @@ class CheckUserGuidanceSnippet : AppCompatActivity() { // @Tag("User guidance") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity: CheckScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.check?.let { + registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { checkResult -> + checkResult.check?.let { // Here you can handle `check document` and present recognized Check information (routing number, account number, etc.) wrapCheck(it) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -295,12 +370,27 @@ class CheckTopBarSnippet : AppCompatActivity() { // @Tag("Top bar") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity: CheckScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.check?.let { + registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { checkResult -> + checkResult.check?.let { // Here you can handle `check document` and present recognized Check information (routing number, account number, etc.) wrapCheck(it) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -340,12 +430,27 @@ class CheckFinderSnippet : AppCompatActivity() { // @Tag("Finder overlay") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity: CheckScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.check?.let { + registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { checkResult -> + checkResult.check?.let { // Here you can handle `check document` and present recognized Check information (routing number, account number, etc.) wrapCheck(it) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -377,12 +482,27 @@ class CheckActionBarSnippet : AppCompatActivity() { // @Tag("Action bar") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity: CheckScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.check?.let { + registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { checkResult -> + checkResult.check?.let { // Here you can handle `check document` and present recognized Check information (routing number, account number, etc.) wrapCheck(it) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -434,12 +554,27 @@ class CheckScanningSnippet : AppCompatActivity() { // @Tag("Scanning") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity: CheckScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.check?.let { + registerForActivityResult(CheckScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { checkResult -> + checkResult.check?.let { // Here you can handle `check document` and present recognized Check information (routing number, account number, etc.) wrapCheck(it) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } diff --git a/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/creditcard/CreditCardUiSnippet.kt b/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/creditcard/CreditCardUiSnippet.kt index f77300a2..9511cfe2 100644 --- a/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/creditcard/CreditCardUiSnippet.kt +++ b/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/creditcard/CreditCardUiSnippet.kt @@ -16,6 +16,9 @@ import android.widget.Toast import androidx.activity.result.ActivityResultLauncher import androidx.appcompat.app.AppCompatActivity import androidx.compose.ui.platform.ComposeView +import io.scanbot.common.Result +import io.scanbot.common.onFailure +import io.scanbot.common.onSuccess import io.scanbot.example.* import io.scanbot.sdk.* import io.scanbot.sdk.creditcard.entity.* @@ -43,19 +46,34 @@ class StartCreditCardUiSnippet : AppCompatActivity() { // @Tag("Launching the scanner") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity: CreditCardScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.creditCard?.let { - val creditCard = CreditCard(it) + registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { creditCardResult -> + creditCardResult.creditCard?.let { + val creditCard = CreditCard(creditCardResult.creditCard!!) val cardNumber: String = creditCard.cardNumber.value.text val cardholderName: String = creditCard.cardholderName?.value?.text ?: "" val expiryDate: String? = creditCard.expiryDate?.value?.text Toast.makeText( - this, + this@StartCreditCardUiSnippet, "Card Number: $cardNumber, Cardholder Name: $cardholderName, Expiry Date: $expiryDate", Toast.LENGTH_LONG ).show() } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -79,19 +97,34 @@ class CreditCardPaletteSnippet : AppCompatActivity() { // @Tag("Palette") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity: CreditCardScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.creditCard?.let { - val creditCard = CreditCard(it) + registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { creditCardResult -> + creditCardResult.creditCard?.let { + val creditCard = CreditCard(creditCardResult.creditCard!!) val cardNumber: String = creditCard.cardNumber.value.text val cardholderName: String = creditCard.cardholderName?.value?.text ?: "" val expiryDate: String? = creditCard.expiryDate?.value?.text Toast.makeText( - this, + this@CreditCardPaletteSnippet, "Card Number: $cardNumber, Cardholder Name: $cardholderName, Expiry Date: $expiryDate", Toast.LENGTH_LONG ).show() } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -137,19 +170,34 @@ class CreditCardLocalizationSnippet : AppCompatActivity() { // @Tag("Localization") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity: CreditCardScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.creditCard?.let { - val creditCard = CreditCard(it) + registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { creditCardResult -> + creditCardResult.creditCard?.let { + val creditCard = CreditCard(creditCardResult.creditCard!!) val cardNumber: String = creditCard.cardNumber.value.text val cardholderName: String = creditCard.cardholderName?.value?.text ?: "" val expiryDate: String? = creditCard.expiryDate?.value?.text Toast.makeText( - this, + this@CreditCardLocalizationSnippet, "Card Number: $cardNumber, Cardholder Name: $cardholderName, Expiry Date: $expiryDate", Toast.LENGTH_LONG ).show() } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -181,19 +229,34 @@ class CreditCardIntroductionSnippet : AppCompatActivity() { // @Tag("Introduction") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity: CreditCardScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.creditCard?.let { - val creditCard = CreditCard(it) + registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { creditCardResult -> + creditCardResult.creditCard?.let { + val creditCard = CreditCard(creditCardResult.creditCard!!) val cardNumber: String = creditCard.cardNumber.value.text val cardholderName: String = creditCard.cardholderName?.value?.text ?: "" val expiryDate: String? = creditCard.expiryDate?.value?.text Toast.makeText( - this, + this@CreditCardIntroductionSnippet, "Card Number: $cardNumber, Cardholder Name: $cardholderName, Expiry Date: $expiryDate", Toast.LENGTH_LONG ).show() } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -253,19 +316,34 @@ class CreditCardUserGuidanceSnippet : AppCompatActivity() { // @Tag("User guidance") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity: CreditCardScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.creditCard?.let { - val creditCard = CreditCard(it) + registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { creditCardResult -> + creditCardResult.creditCard?.let { + val creditCard = CreditCard(creditCardResult.creditCard!!) val cardNumber: String = creditCard.cardNumber.value.text val cardholderName: String = creditCard.cardholderName?.value?.text ?: "" val expiryDate: String? = creditCard.expiryDate?.value?.text Toast.makeText( - this, + this@CreditCardUserGuidanceSnippet, "Card Number: $cardNumber, Cardholder Name: $cardholderName, Expiry Date: $expiryDate", Toast.LENGTH_LONG ).show() } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -314,19 +392,34 @@ class CreditCardTopBarSnippet : AppCompatActivity() { // @Tag("Top bar") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity: CreditCardScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.creditCard?.let { - val creditCard = CreditCard(it) + registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { creditCardResult -> + creditCardResult.creditCard?.let { + val creditCard = CreditCard(creditCardResult.creditCard!!) val cardNumber: String = creditCard.cardNumber.value.text val cardholderName: String = creditCard.cardholderName?.value?.text ?: "" val expiryDate: String? = creditCard.expiryDate?.value?.text Toast.makeText( - this, + this@CreditCardTopBarSnippet, "Card Number: $cardNumber, Cardholder Name: $cardholderName, Expiry Date: $expiryDate", Toast.LENGTH_LONG ).show() } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -366,19 +459,34 @@ class CreditCardFinderSnippet : AppCompatActivity() { // @Tag("Finder overlay") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity: CreditCardScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.creditCard?.let { - val creditCard = CreditCard(it) + registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { creditCardResult -> + creditCardResult.creditCard?.let { + val creditCard = CreditCard(creditCardResult.creditCard!!) val cardNumber: String = creditCard.cardNumber.value.text val cardholderName: String = creditCard.cardholderName?.value?.text ?: "" val expiryDate: String? = creditCard.expiryDate?.value?.text Toast.makeText( - this, + this@CreditCardFinderSnippet, "Card Number: $cardNumber, Cardholder Name: $cardholderName, Expiry Date: $expiryDate", Toast.LENGTH_LONG ).show() } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -410,19 +518,34 @@ class CreditCardActionBarSnippet : AppCompatActivity() { // @Tag("Action bar") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity: CreditCardScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.creditCard?.let { - val creditCard = CreditCard(it) + registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { creditCardResult -> + creditCardResult.creditCard?.let { + val creditCard = CreditCard(creditCardResult.creditCard!!) val cardNumber: String = creditCard.cardNumber.value.text val cardholderName: String = creditCard.cardholderName?.value?.text ?: "" val expiryDate: String? = creditCard.expiryDate?.value?.text Toast.makeText( - this, + this@CreditCardActionBarSnippet, "Card Number: $cardNumber, Cardholder Name: $cardholderName, Expiry Date: $expiryDate", Toast.LENGTH_LONG ).show() } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -474,19 +597,34 @@ class CreditCardScanningSnippet : AppCompatActivity() { // @Tag("Scanning") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity: CreditCardScannerActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.creditCard?.let { - val creditCard = CreditCard(it) + registerForActivityResult(CreditCardScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { creditCardResult -> + creditCardResult.creditCard?.let { + val creditCard = CreditCard(creditCardResult.creditCard!!) val cardNumber: String = creditCard.cardNumber.value.text val cardholderName: String = creditCard.cardholderName?.value?.text ?: "" val expiryDate: String? = creditCard.expiryDate?.value?.text Toast.makeText( - this, + this@CreditCardScanningSnippet, "Card Number: $cardNumber, Cardholder Name: $cardholderName, Expiry Date: $expiryDate", Toast.LENGTH_LONG ).show() } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } diff --git a/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/data_extractor/DataExtractionUiSnippet.kt b/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/data_extractor/DataExtractionUiSnippet.kt index 94a3d4fd..a4b6cfa6 100644 --- a/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/data_extractor/DataExtractionUiSnippet.kt +++ b/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/data_extractor/DataExtractionUiSnippet.kt @@ -18,6 +18,9 @@ import androidx.appcompat.app.AppCompatActivity import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.ui.Modifier import androidx.compose.ui.platform.ComposeView +import io.scanbot.common.Result +import io.scanbot.common.onFailure +import io.scanbot.common.onSuccess import io.scanbot.example.* import io.scanbot.sdk.* import io.scanbot.sdk.documentdata.DocumentDataExtractorCommonConfiguration @@ -44,11 +47,25 @@ class StartDocumentDataExtractorUiSnippet : AppCompatActivity() { // @Tag("Launching the scanner") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity: DocumentDataExtractorActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.document?.let { document -> + registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { result -> + result?.document?.let { document -> wrapGenericDocument(document) } + }.onFailure { + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -72,11 +89,25 @@ class DocumentDataExtractorPaletteSnippet : AppCompatActivity() { // @Tag("Palette") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity: DocumentDataExtractorActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.document?.let { document -> + registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { result -> + result?.document?.let { document -> wrapGenericDocument(document) } + }.onFailure { + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -122,11 +153,25 @@ class DocumentDataExtractorLocalizationSnippet : AppCompatActivity() { // @Tag("Localization") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity: DocumentDataExtractorActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.document?.let { document -> + registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { result -> + result?.document?.let { document -> wrapGenericDocument(document) } + }.onFailure { + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -158,11 +203,26 @@ class DocumentDataExtractorIntroductionSnippet : AppCompatActivity() { // @Tag("Introduction") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity: DocumentDataExtractorActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.document?.let { document -> + registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { result -> + result?.document?.let { document -> wrapGenericDocument(document) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -222,11 +282,26 @@ class DocumentDataExtractorUserGuidanceSnippet : AppCompatActivity() { // @Tag("User guidance") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity: DocumentDataExtractorActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.document?.let { document -> + registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { result -> + result?.document?.let { document -> wrapGenericDocument(document) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -266,11 +341,26 @@ class DocumentDataExtractorTopBarSnippet : AppCompatActivity() { // @Tag("Top bar") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity: DocumentDataExtractorActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.document?.let { document -> + registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { result -> + result?.document?.let { document -> wrapGenericDocument(document) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -310,11 +400,26 @@ class DocumentDataExtractorFinderSnippet : AppCompatActivity() { // @Tag("Finder overlay") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity: DocumentDataExtractorActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.document?.let { document -> + registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { result -> + result?.document?.let { document -> wrapGenericDocument(document) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -345,11 +450,26 @@ class DocumentDataExtractorActionBarSnippet : AppCompatActivity() { // @Tag("Action bar") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity: DocumentDataExtractorActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.document?.let { document -> + registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { result -> + result?.document?.let { document -> wrapGenericDocument(document) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } @@ -401,11 +521,26 @@ class DocumentDataExtractorScanningSnippet : AppCompatActivity() { // @Tag("Scanning") val resultLauncher: ActivityResultLauncher = - registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity: DocumentDataExtractorActivity.Result -> - if (resultEntity.resultOk) { - resultEntity.result?.document?.let { document -> + registerForActivityResult(DocumentDataExtractorActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { result -> + result?.document?.let { document -> wrapGenericDocument(document) } + }.onFailure { + // Optional activity closing cause handling to understand the reason scanner result is not provided + when (it) { + is io.scanbot.common.Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } } diff --git a/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/migration/RtuUi2DocumentSnippets.kt b/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/migration/RtuUi2DocumentSnippets.kt index b7184337..49bf79f4 100644 --- a/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/migration/RtuUi2DocumentSnippets.kt +++ b/data-capture-ready-to-use-ui-example/app/src/main/java/io/scanbot/example/doc_code_snippet/migration/RtuUi2DocumentSnippets.kt @@ -19,6 +19,9 @@ import android.widget.Button import android.widget.ImageView import androidx.activity.result.ActivityResultLauncher import androidx.appcompat.app.AppCompatActivity +import io.scanbot.common.Result +import io.scanbot.common.onFailure +import io.scanbot.common.onSuccess import io.scanbot.example.R import io.scanbot.sdk.* import io.scanbot.sdk.common.* @@ -43,16 +46,30 @@ class MainActivityWithDocumentScannerRtuV2 : AppCompatActivity() { val previewImageView = findViewById(R.id.first_page_image_preview) documentScannerResultLauncher = - registerForActivityResultOk(io.scanbot.sdk.ui_v2.document.DocumentScannerActivity.ResultContract()) { - resultEntity: io.scanbot.sdk.ui_v2.document.DocumentScannerActivity.Result -> - val result: Document? = resultEntity.result - val pages: List? = result?.pages - pages?.get(0)?.let { + registerForActivityResult(DocumentScannerActivity.ResultContract()) { resultEntity -> + resultEntity.onSuccess { document -> + val pages: List = document.pages + pages.get(0).let { // in v2 you can access the image bitmap directly from the Page: val previewImage = it.documentPreviewImage previewImageView.setImageBitmap(previewImage) } + }.onFailure { + when (it) { + is Result.InvalidLicenseError -> { + // indicate that the Scanbot SDK license is invalid + } + + is Result.OperationCanceledError -> { + // Indicates that the cancel button was tapped. or screen is closed by other reason. + } + + else -> { + // Handle other errors + } + } } + } findViewById