Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,83 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import dagger.hilt.android.AndroidEntryPoint
import net.gini.android.bank.sdk.di.BankSdkIsolatedKoinContext
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.ui.util.CaptureResultListener
import net.gini.android.capture.Document
import net.gini.android.capture.GiniCaptureFragment
import net.gini.android.capture.GiniCaptureFragmentListener

@AndroidEntryPoint
class CaptureSdkStandAloneActivity : AppCompatActivity() {

private var listener: CaptureResultListener? = null

override fun onCreate(savedInstanceState: Bundle?) {
BankSdkIsolatedKoinContext.init(this)
listener = CaptureResultListener(this)
supportFragmentManager.fragmentFactory =
ClientCaptureSDKFragmentFactory(listener!!, null)
super.onCreate(savedInstanceState)
// same view is required for both Bank SDK and Capture SDK, no need to create a separate xml
setContentView(R.layout.activity_capture_flow_host)
setContentView(R.layout.activity_stand_alone_capture_flow)
if (savedInstanceState == null) {
if (intent != null && isIntentActionViewOrSend(intent)) {
ClientCaptureSDKFragment().startCaptureSDKForIntent(
this,
intent,
CaptureResultListener(this)
listener!!
)

} else {
// start simple capture SDK
val giniCaptureFragment = ClientCaptureSDKFragment()
this.supportFragmentManager.beginTransaction()
.replace(R.id.fragment_host, giniCaptureFragment, "fragment_host")
.addToBackStack(null).commit()
val clientCaptureSDK = ClientCaptureSDKFragment()
supportFragmentManager.beginTransaction()
.replace(
R.id.fragment_host,
clientCaptureSDK,
ClientCaptureSDKFragment::class.java.name
)
.addToBackStack(null)
.commit()
clientCaptureSDK.setListener(listener = listener!!)
}
} else {
restoreFragmentListener()
}
}

companion object {
fun newIntent(context: Context) = Intent(context, CaptureSdkStandAloneActivity::class.java)
}

private fun restoreFragmentListener() {
val fragment =
supportFragmentManager.findFragmentByTag(ClientCaptureSDKFragment::class.java.name) as? ClientCaptureSDKFragment?
listener?.let { fragment?.setListener(it) }
}

override fun onDestroy() {
super.onDestroy()
listener = null
}
}

class ClientCaptureSDKFragmentFactory(
private val giniCaptureFragmentListener: GiniCaptureFragmentListener,
private var openWithDocument: Document? = null
) : FragmentFactory() {
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
return when (className) {
GiniCaptureFragment::class.java.name -> GiniCaptureFragment.createInstance(
openWithDocument
) { openWithDocument = null }
.apply {
setListener(
giniCaptureFragmentListener
)
}

else -> super.instantiate(classLoader, className)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import net.gini.android.bank.sdk.capture.ResultError
import net.gini.android.bank.sdk.exampleapp.R
import net.gini.android.bank.sdk.exampleapp.core.PermissionHandler
import net.gini.android.capture.Amount
import net.gini.android.capture.CaptureSDKResult
import net.gini.android.capture.DocumentImportEnabledFileTypes
import net.gini.android.capture.GiniCapture
Expand All @@ -27,6 +25,7 @@ class ClientCaptureSDKFragment :
GiniCaptureFragmentListener {

private lateinit var permissionHandler: PermissionHandler
private var captureFragmentListener: GiniCaptureFragmentListener? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -93,6 +92,10 @@ class ClientCaptureSDKFragment :

private var mFileImportCancellationToken: CancellationToken? = null

fun setListener(listener: GiniCaptureFragmentListener) {
this.captureFragmentListener = listener
}

fun startCaptureSDKForIntent(
context: AppCompatActivity,
openWithIntent: Intent,
Expand All @@ -114,7 +117,6 @@ class ClientCaptureSDKFragment :

// Set the listener to receive the Gini Bank SDK's results
result.fragment.setListener(listener)

// Show the CaptureFlowFragment for example via
// the fragment manager:
context.supportFragmentManager.beginTransaction()
Expand Down Expand Up @@ -153,82 +155,20 @@ class ClientCaptureSDKFragment :

}

fun stopGiniCaptureSDKWithTransferSummary(paymentRecipient: String,
paymentReference: String,
paymentPurpose: String,
iban: String,
bic: String,
amount: Amount
) {
// After the user has seen and potentially corrected the extractions, send the final
// transfer summary values to Gini which will be used to improve the future
// extraction accuracy:
GiniCapture.sendTransferSummary(
paymentRecipient,
paymentReference,
paymentPurpose,
iban,
bic,
amount,
null
)

// cleanup the capture SDK after sending the transfer summary
GiniCapture.cleanup(requireActivity())
}

override fun onFinishedWithResult(result: CaptureSDKResult) {
when (result) {
is CaptureSDKResult.Success -> {
startActivity(
ExtractionsActivity.getStartIntent(
requireContext(),
result.specificExtractions
)
)
requireActivity().finish()
}

is CaptureSDKResult.Error -> {

Toast.makeText(
requireContext(),
"Error: ${(result.value as ResultError.FileImport).code} " +
"${(result.value as ResultError.FileImport).message}",
Toast.LENGTH_LONG
).show()

requireActivity().finish()
}

CaptureSDKResult.Empty -> {
requireActivity().finish()
}

CaptureSDKResult.Cancel -> {
requireActivity().finish()
}
finishWithResult(result)
}

CaptureSDKResult.EnterManually -> {
Toast.makeText(
requireContext(),
"Scan exited for manual enter mode",
Toast.LENGTH_SHORT
).show()
requireActivity().finish()
}
}
private fun finishWithResult(result: CaptureSDKResult) {
captureFragmentListener?.onFinishedWithResult(result)
}

override fun onDestroy() {
super.onDestroy()
captureFragmentListener = null
if (mFileImportCancellationToken != null) {
mFileImportCancellationToken!!.cancel()
mFileImportCancellationToken = null
}
}


}


Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class CaptureResultListener(val context: Activity) : GiniCaptureFragmentListener
context.startActivity(
ExtractionsActivity.getStartIntent(
context,
result.specificExtractions
result.specificExtractions,
true
)
)
context.finish()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/GiniCaptureTheme"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class GiniCaptureFragment(
CancelListener {

private lateinit var navController: NavController
private lateinit var giniCaptureFragmentListener: GiniCaptureFragmentListener
private var giniCaptureFragmentListener: GiniCaptureFragmentListener? = null
private lateinit var oncePerInstallEventStore: OncePerInstallEventStore

/**
Expand Down Expand Up @@ -207,8 +207,9 @@ class GiniCaptureFragment(
override fun onDestroy() {
super.onDestroy()
if (!didFinishWithResult && !willBeRestored) {
giniCaptureFragmentListener.onFinishedWithResult(CaptureSDKResult.Cancel)
giniCaptureFragmentListener?.onFinishedWithResult(CaptureSDKResult.Cancel)
}
giniCaptureFragmentListener = null
if (willBeRestored) {
UserAnalytics.flushEvents()
}
Expand Down Expand Up @@ -242,12 +243,12 @@ class GiniCaptureFragment(
document: Document,
callback: CameraFragmentListener.DocumentCheckResultCallback
) {
giniCaptureFragmentListener.onCheckImportedDocument(document, callback)
giniCaptureFragmentListener?.onCheckImportedDocument(document, callback)
}

override fun onError(error: GiniCaptureError) {
didFinishWithResult = true
giniCaptureFragmentListener.onFinishedWithResult(CaptureSDKResult.Error(error))
giniCaptureFragmentListener?.onFinishedWithResult(CaptureSDKResult.Error(error))
}

override fun onExtractionsAvailable(
Expand All @@ -257,7 +258,7 @@ class GiniCaptureFragment(
) {
didFinishWithResult = true
lastExtractionsProvider.update(extractions)
giniCaptureFragmentListener.onFinishedWithResult(
giniCaptureFragmentListener?.onFinishedWithResult(
CaptureSDKResult.Success(
extractions,
compoundExtractions,
Expand All @@ -280,7 +281,7 @@ class GiniCaptureFragment(
override fun onExtractionsAvailable(extractions: MutableMap<String, GiniCaptureSpecificExtraction>) {
didFinishWithResult = true
lastExtractionsProvider.update(extractions)
giniCaptureFragmentListener.onFinishedWithResult(
giniCaptureFragmentListener?.onFinishedWithResult(
CaptureSDKResult.Success(
extractions,
emptyMap(),
Expand All @@ -291,7 +292,7 @@ class GiniCaptureFragment(

override fun onEnterManuallyPressed() {
didFinishWithResult = true
giniCaptureFragmentListener.onFinishedWithResult(CaptureSDKResult.EnterManually)
giniCaptureFragmentListener?.onFinishedWithResult(CaptureSDKResult.EnterManually)
}

override fun onCancelFlow() {
Expand All @@ -300,7 +301,7 @@ class GiniCaptureFragment(

private fun finishWithCancel() {
didFinishWithResult = true
giniCaptureFragmentListener.onFinishedWithResult(CaptureSDKResult.Cancel)
giniCaptureFragmentListener?.onFinishedWithResult(CaptureSDKResult.Cancel)
}

private fun setAnalyticsEntryPointProperty(isOpenWithDocumentExists: Boolean) {
Expand Down