From 887c14e1a942d00b56ade701f8d279c8611c6c8a Mon Sep 17 00:00:00 2001 From: Mahdi Abolfazli Date: Mon, 15 Jan 2024 13:21:27 +0100 Subject: [PATCH] refactor(): Change CaptureResult in capture-sdk to CaptureSDKResult - add a extension function to convert CaptureSDKResult in capture-sdk to CaptureResult in bank-sdk - normal flow of bank-sdk works - remove capture-sdk initialisation in the example app BSDK-258 --- .../ui/ClientGiniCaptureFragment.kt | 63 ++++--------------- .../bank/sdk/capture/CaptureFlowFragment.kt | 6 +- .../android/bank/sdk/capture/CaptureResult.kt | 38 +++++++++++ .../{CaptureResult.kt => CaptureSDKResult.kt} | 20 +++--- .../android/capture/GiniCaptureFragment.kt | 36 +++++++---- 5 files changed, 86 insertions(+), 77 deletions(-) rename capture-sdk/sdk/src/main/java/net/gini/android/capture/{CaptureResult.kt => CaptureSDKResult.kt} (79%) diff --git a/bank-sdk/example-app/src/main/java/net/gini/android/bank/sdk/exampleapp/ui/ClientGiniCaptureFragment.kt b/bank-sdk/example-app/src/main/java/net/gini/android/bank/sdk/exampleapp/ui/ClientGiniCaptureFragment.kt index 46efa07aff..d91dc568d6 100644 --- a/bank-sdk/example-app/src/main/java/net/gini/android/bank/sdk/exampleapp/ui/ClientGiniCaptureFragment.kt +++ b/bank-sdk/example-app/src/main/java/net/gini/android/bank/sdk/exampleapp/ui/ClientGiniCaptureFragment.kt @@ -6,8 +6,6 @@ import android.content.Intent import android.os.Bundle import android.view.View import android.widget.Toast -import androidx.core.content.ContentProviderCompat.requireContext -import androidx.core.content.ContextCompat.startActivity import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import dagger.hilt.android.AndroidEntryPoint @@ -15,29 +13,24 @@ import kotlinx.coroutines.launch import net.gini.android.bank.sdk.GiniBank import net.gini.android.bank.sdk.capture.CaptureConfiguration import net.gini.android.bank.sdk.capture.CaptureFlowFragmentListener +import net.gini.android.bank.sdk.capture.CaptureResult +import net.gini.android.bank.sdk.capture.ResultError import net.gini.android.bank.sdk.exampleapp.R import net.gini.android.bank.sdk.exampleapp.core.ExampleUtil.isIntentActionViewOrSend import net.gini.android.bank.sdk.exampleapp.core.PermissionHandler import net.gini.android.bank.sdk.exampleapp.core.di.GiniCaptureNetworkServiceDebugEnabled -import net.gini.android.capture.CaptureResult +import net.gini.android.capture.CaptureSDKResultError import net.gini.android.capture.Document import net.gini.android.capture.DocumentImportEnabledFileTypes -import net.gini.android.capture.GiniCapture -import net.gini.android.capture.GiniCaptureFragmentListener -import net.gini.android.capture.ResultError import net.gini.android.capture.camera.CameraFragmentListener import net.gini.android.capture.network.GiniCaptureDefaultNetworkService -import net.gini.android.capture.review.multipage.view.DefaultReviewNavigationBarBottomAdapter -import net.gini.android.capture.view.DefaultLoadingIndicatorAdapter import net.gini.android.core.api.DocumentMetadata import javax.inject.Inject @AndroidEntryPoint -class ClientGiniCaptureFragment : Fragment(R.layout.fragment_client_capture), - //Bank SDK +class ClientGiniCaptureFragment : + Fragment(R.layout.fragment_client_capture), CaptureFlowFragmentListener { - //Capture SDK -// GiniCaptureFragmentListener, { @Inject @GiniCaptureNetworkServiceDebugEnabled @@ -58,7 +51,6 @@ class ClientGiniCaptureFragment : Fragment(R.layout.fragment_client_capture), lifecycleScope.launch { if (permissionHandler.grantPermission(Manifest.permission.CAMERA)) { configureBankSDK() -// configureCaptureSDK() } else { if (intent != null) { requireActivity().finish() @@ -68,24 +60,6 @@ class ClientGiniCaptureFragment : Fragment(R.layout.fragment_client_capture), } - private fun configureCaptureSDK() { - val builder = GiniCapture.newInstance(requireContext()) - .setGiniCaptureNetworkService( - giniCaptureDefaultNetworkService - ) - .setDocumentImportEnabledFileTypes(DocumentImportEnabledFileTypes.PDF_AND_IMAGES) - .setFileImportEnabled(true) - .setQRCodeScanningEnabled(true) - .setMultiPageEnabled(true) - builder.setFlashButtonEnabled(true) - builder.setReviewBottomBarNavigationAdapter(DefaultReviewNavigationBarBottomAdapter()) - builder.setLoadingIndicatorAdapter(DefaultLoadingIndicatorAdapter()) - - builder.build() - startCaptureSDK() - } - - private fun configureBankSDK() { val clientId = requireContext().getString(R.string.gini_api_client_id) val clientSecret = requireContext().getString(R.string.gini_api_client_secret) @@ -118,17 +92,6 @@ class ClientGiniCaptureFragment : Fragment(R.layout.fragment_client_capture), } - - private fun startCaptureSDK() { - val giniCaptureFragment = GiniCapture.createGiniCaptureFragment() - //giniCaptureFragment.setListener(this) - - requireActivity().supportFragmentManager.beginTransaction() - .replace(R.id.fragment_host, giniCaptureFragment, "fragment_host") - .addToBackStack(null) - .commit() - } - private fun startBankSDK() { val captureFlowFragment = GiniBank.createCaptureFlowFragment() captureFlowFragment.setListener(this) @@ -177,36 +140,36 @@ class ClientGiniCaptureFragment : Fragment(R.layout.fragment_client_capture), is CaptureResult.Error -> { when (result.value) { - is ResultError.Capture -> + is CaptureResult -> Toast.makeText( requireContext(), - "Error: ${(result.value as ResultError.Capture).giniCaptureError.errorCode} ${(result.value as ResultError.Capture).giniCaptureError.message}", + "Error: ${(result.value as CaptureSDKResultError.Capture).giniCaptureError.errorCode} ${(result.value as CaptureSDKResultError.Capture).giniCaptureError.message}", Toast.LENGTH_LONG ).show() is ResultError.FileImport -> Toast.makeText( requireContext(), - "Error: ${(result.value as ResultError.FileImport).code} ${(result.value as ResultError.FileImport).message}", + "Error: ${(result.value as CaptureSDKResultError.FileImport).code} ${(result.value as CaptureSDKResultError.FileImport).message}", Toast.LENGTH_LONG ).show() else -> {} } //if (isIntentActionViewOrSend(requireActivity().intent)) { - requireActivity().finish() - // } + requireActivity().finish() + // } } CaptureResult.Empty -> { //if (isIntentActionViewOrSend(requireActivity().intent)) { - requireActivity().finish() + requireActivity().finish() //} } CaptureResult.Cancel -> { //if (isIntentActionViewOrSend(requireActivity().intent)) { - requireActivity().finish() + requireActivity().finish() //} } @@ -236,8 +199,6 @@ class ClientGiniCaptureFragment : Fragment(R.layout.fragment_client_capture), } - - } diff --git a/bank-sdk/sdk/src/main/java/net/gini/android/bank/sdk/capture/CaptureFlowFragment.kt b/bank-sdk/sdk/src/main/java/net/gini/android/bank/sdk/capture/CaptureFlowFragment.kt index f52c6b9f23..a5dcc8965a 100644 --- a/bank-sdk/sdk/src/main/java/net/gini/android/bank/sdk/capture/CaptureFlowFragment.kt +++ b/bank-sdk/sdk/src/main/java/net/gini/android/bank/sdk/capture/CaptureFlowFragment.kt @@ -10,7 +10,7 @@ import androidx.fragment.app.FragmentFactory import androidx.navigation.NavController import androidx.navigation.fragment.findNavController import net.gini.android.bank.sdk.R -import net.gini.android.capture.CaptureResult +import net.gini.android.capture.CaptureSDKResult import net.gini.android.capture.Document import net.gini.android.capture.GiniCaptureFragment import net.gini.android.capture.GiniCaptureFragmentListener @@ -77,8 +77,8 @@ class CaptureFlowFragment : Fragment(), GiniCaptureFragmentListener { .commit() } - override fun onFinishedWithResult(result: CaptureResult) { - captureFlowFragmentListener.onFinishedWithResult(result) + override fun onFinishedWithResult(result: CaptureSDKResult) { + captureFlowFragmentListener.onFinishedWithResult(result.toCaptureResult()) } override fun onFinishedWithCancellation() { diff --git a/bank-sdk/sdk/src/main/java/net/gini/android/bank/sdk/capture/CaptureResult.kt b/bank-sdk/sdk/src/main/java/net/gini/android/bank/sdk/capture/CaptureResult.kt index 1b534161b2..45ccd35e2d 100644 --- a/bank-sdk/sdk/src/main/java/net/gini/android/bank/sdk/capture/CaptureResult.kt +++ b/bank-sdk/sdk/src/main/java/net/gini/android/bank/sdk/capture/CaptureResult.kt @@ -2,6 +2,8 @@ package net.gini.android.bank.sdk.capture import android.content.Intent import android.os.Bundle +import net.gini.android.capture.CaptureSDKResult +import net.gini.android.capture.CaptureSDKResultError import net.gini.android.capture.GiniCaptureError import net.gini.android.capture.camera.CameraActivity import net.gini.android.capture.internal.util.FileImportValidator @@ -43,6 +45,42 @@ sealed class CaptureResult { object EnterManually: CaptureResult() } +fun CaptureSDKResult.toCaptureResult(): CaptureResult { + return when (this) { + is CaptureSDKResult.Success -> { + CaptureResult.Success( + this.specificExtractions, + this.compoundExtractions, + this.returnReasons + ) + } + is CaptureSDKResult.Empty -> { + CaptureResult.Empty + } + is CaptureSDKResult.Cancel -> { + CaptureResult.Cancel + } + is CaptureSDKResult.Error -> { + CaptureResult.Error(this.value.toResultError()) + } + is CaptureSDKResult.EnterManually -> { + CaptureResult.EnterManually + } + } +} + +fun CaptureSDKResultError.toResultError(): ResultError { + return when (this) { + is CaptureSDKResultError.Capture -> { + ResultError.Capture(this.giniCaptureError) + } + is CaptureSDKResultError.FileImport -> { + ResultError.FileImport(this.code, this.message) + } + } +} + + sealed class ResultError { /** * An error which occurred during the capture flow. diff --git a/capture-sdk/sdk/src/main/java/net/gini/android/capture/CaptureResult.kt b/capture-sdk/sdk/src/main/java/net/gini/android/capture/CaptureSDKResult.kt similarity index 79% rename from capture-sdk/sdk/src/main/java/net/gini/android/capture/CaptureResult.kt rename to capture-sdk/sdk/src/main/java/net/gini/android/capture/CaptureSDKResult.kt index b8816901ab..905473abe9 100644 --- a/capture-sdk/sdk/src/main/java/net/gini/android/capture/CaptureResult.kt +++ b/capture-sdk/sdk/src/main/java/net/gini/android/capture/CaptureSDKResult.kt @@ -11,7 +11,7 @@ import net.gini.android.capture.network.model.GiniCaptureSpecificExtraction /** * Result returned by capture flow. */ -sealed class CaptureResult { +sealed class CaptureSDKResult { /** * Extractions were found. */ @@ -19,42 +19,42 @@ sealed class CaptureResult { val specificExtractions: Map, val compoundExtractions: Map, val returnReasons: List, - ) : CaptureResult() + ) : CaptureSDKResult() /** * No extraction. */ - object Empty : CaptureResult() + object Empty : CaptureSDKResult() /** * User navigated back. */ - object Cancel : CaptureResult() + object Cancel : CaptureSDKResult() /** * Capture flow returned an error. */ - class Error(val value: ResultError) : CaptureResult() + class Error(val value: CaptureSDKResultError) : CaptureSDKResult() /** * User decided to enter data manually after the scanning resulted in no results or an error. */ - object EnterManually: CaptureResult() + object EnterManually: CaptureSDKResult() } -sealed class ResultError { +sealed class CaptureSDKResultError { /** * An error which occurred during the capture flow. */ - data class Capture(val giniCaptureError: GiniCaptureError) : ResultError() + data class Capture(val giniCaptureError: GiniCaptureError) : CaptureSDKResultError() /** * An error which occurred during importing a file shared from another app. */ - data class FileImport(val code: FileImportValidator.Error? = null, val message: String? = null) : ResultError() + data class FileImport(val code: FileImportValidator.Error? = null, val message: String? = null) : CaptureSDKResultError() } -internal fun CaptureResult.Success.toIntent(): Intent { +internal fun CaptureSDKResult.Success.toIntent(): Intent { return Intent().apply { this.putExtra(CameraActivity.EXTRA_OUT_EXTRACTIONS, Bundle().apply { specificExtractions.forEach { putParcelable(it.key, it.value) } diff --git a/capture-sdk/sdk/src/main/java/net/gini/android/capture/GiniCaptureFragment.kt b/capture-sdk/sdk/src/main/java/net/gini/android/capture/GiniCaptureFragment.kt index b96e76c083..9ffde51581 100644 --- a/capture-sdk/sdk/src/main/java/net/gini/android/capture/GiniCaptureFragment.kt +++ b/capture-sdk/sdk/src/main/java/net/gini/android/capture/GiniCaptureFragment.kt @@ -21,12 +21,14 @@ import net.gini.android.capture.document.QRCodeDocument import net.gini.android.capture.network.model.GiniCaptureCompoundExtraction import net.gini.android.capture.network.model.GiniCaptureReturnReason import net.gini.android.capture.network.model.GiniCaptureSpecificExtraction -import net.gini.android.capture.review.multipage.MultiPageReviewActivity import net.gini.android.capture.review.multipage.MultiPageReviewFragment import net.gini.android.capture.review.multipage.MultiPageReviewFragmentDirections import net.gini.android.capture.review.multipage.MultiPageReviewFragmentListener -class GiniCaptureFragment(private val analysisIntent: Intent? = null) : Fragment(), CameraFragmentListener, MultiPageReviewFragmentListener, +class GiniCaptureFragment(private val analysisIntent: Intent? = null) : + Fragment(), + CameraFragmentListener, + MultiPageReviewFragmentListener, AnalysisFragmentListener { internal companion object { @@ -62,8 +64,13 @@ class GiniCaptureFragment(private val analysisIntent: Intent? = null) : Fragment super.onViewCreated(view, savedInstanceState) navController = (childFragmentManager.fragments[0]).findNavController() if (analysisIntent != null) { - navController.navigate(CameraFragmentDirections.toAnalysisFragment(analysisIntent.getParcelableExtra( - AnalysisActivity.EXTRA_IN_DOCUMENT)!!, "")) + navController.navigate( + CameraFragmentDirections.toAnalysisFragment( + analysisIntent.getParcelableExtra( + AnalysisActivity.EXTRA_IN_DOCUMENT + )!!, "" + ) + ) } } @@ -172,13 +179,13 @@ class GiniCaptureFragment(private val analysisIntent: Intent? = null) : Fragment returnReasons: MutableList ) { Log.d("analysis", "extractions received: $extractions") - giniCaptureFragmentListener.onFinishedWithResult( - CaptureResult.Success( - extractions, - emptyMap(), - emptyList() - ) + giniCaptureFragmentListener.onFinishedWithResult( + CaptureSDKResult.Success( + extractions, + emptyMap(), + emptyList() ) + ) } override fun onProceedToNoExtractionsScreen(document: Document) { @@ -195,7 +202,7 @@ class GiniCaptureFragment(private val analysisIntent: Intent? = null) : Fragment Log.d("analysis", "extractions received: $extractions") giniCaptureFragmentListener.onFinishedWithResult( - CaptureResult.Success( + CaptureSDKResult.Success( extractions, emptyMap(), emptyList() @@ -241,11 +248,14 @@ class CaptureFragmentFactory( } interface GiniCaptureFragmentListener { - fun onFinishedWithResult(result: CaptureResult) + fun onFinishedWithResult(result: CaptureSDKResult) fun onFinishedWithCancellation() - fun onCheckImportedDocument(document: Document, callback: CameraFragmentListener.DocumentCheckResultCallback) { + fun onCheckImportedDocument( + document: Document, + callback: CameraFragmentListener.DocumentCheckResultCallback + ) { callback.documentAccepted() } }