Skip to content

Commit

Permalink
refactor(): Change CaptureResult in capture-sdk to CaptureSDKResult
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
abolfazlimahdi committed Jan 15, 2024
1 parent b6ef979 commit 887c14e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,31 @@ 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
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
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
//}
}

Expand Down Expand Up @@ -236,8 +199,6 @@ class ClientGiniCaptureFragment : Fragment(R.layout.fragment_client_capture),
}




}


Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,50 @@ import net.gini.android.capture.network.model.GiniCaptureSpecificExtraction
/**
* Result returned by capture flow.
*/
sealed class CaptureResult {
sealed class CaptureSDKResult {
/**
* Extractions were found.
*/
class Success(
val specificExtractions: Map<String, GiniCaptureSpecificExtraction>,
val compoundExtractions: Map<String, GiniCaptureCompoundExtraction>,
val returnReasons: List<GiniCaptureReturnReason>,
) : 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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
)!!, ""
)
)
}
}

Expand Down Expand Up @@ -172,13 +179,13 @@ class GiniCaptureFragment(private val analysisIntent: Intent? = null) : Fragment
returnReasons: MutableList<GiniCaptureReturnReason>
) {
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) {
Expand All @@ -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()
Expand Down Expand Up @@ -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()
}
}

0 comments on commit 887c14e

Please sign in to comment.