Skip to content

Commit

Permalink
refactor(): Replace AnalysisActivity with AnalysisFragment
Browse files Browse the repository at this point in the history
- add open-with functionality for fragments
- remove Intent and introduce Document to pass around when working with open-with

BSDK-258
  • Loading branch information
abolfazlimahdi committed Jan 19, 2024
1 parent 6a906f8 commit dc58539
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 371 deletions.
54 changes: 30 additions & 24 deletions bank-sdk/sdk/src/main/java/net/gini/android/bank/sdk/GiniBank.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import net.gini.android.bank.sdk.pay.getBusinessIntent
import net.gini.android.bank.sdk.pay.getRequestId
import net.gini.android.bank.sdk.util.parseAmountToBackendFormat
import net.gini.android.capture.*
import net.gini.android.capture.analysis.AnalysisActivity
import net.gini.android.capture.onboarding.view.ImageOnboardingIllustrationAdapter
import net.gini.android.capture.onboarding.view.OnboardingIllustrationAdapter
import net.gini.android.capture.requirements.GiniCaptureRequirements
Expand Down Expand Up @@ -260,42 +259,49 @@ object GiniBank {
resultLauncher.launch(Unit)
}

fun createCaptureFlowFragment(): CaptureFlowFragment
{
fun createCaptureFlowFragment(): CaptureFlowFragment {
check(giniCapture != null) { "Capture feature is not configured. Call setCaptureConfiguration before starting the flow." }
return CaptureFlowFragment.createInstance()
}

fun createCaptureFlowFragmentForIntent(context: Context, intent: Intent, callback: (CreateCaptureFlowFragmentForIntentResult) -> Unit): CancellationToken {
fun createCaptureFlowFragmentForIntent(
context: Context,
intent: Intent,
callback: (CreateCaptureFlowFragmentForIntentResult) -> Unit
): CancellationToken {
check(giniCapture != null) { "Capture feature is not configured. Call setCaptureConfiguration before starting the flow." }
return giniCapture!!.createIntentForImportedFiles(intent, context, object: AsyncCallback<Intent, ImportedFileValidationException> {
override fun onSuccess(result: Intent) {
when(result.component?.className) {
// Since version 3.x the SDK always goes to the analysis screen for "open with" files
AnalysisActivity::class.java.name -> {
callback(CreateCaptureFlowFragmentForIntentResult.Success(
CaptureFlowFragment.createInstance(result)))
}
else -> callback(CreateCaptureFlowFragmentForIntentResult.Error(ImportedFileValidationException("Unknown activity class: ${result.component?.className}")))
return giniCapture!!.createDocumentForImportedFiles(
intent,
context,
object : AsyncCallback<Document, ImportedFileValidationException> {
override fun onSuccess(document: Document) {
callback(
CreateCaptureFlowFragmentForIntentResult.Success(
CaptureFlowFragment.createInstance(document)
)
)
}
}

override fun onError(exception: ImportedFileValidationException) {
callback(CreateCaptureFlowFragmentForIntentResult.Error(exception))
}
override fun onError(exception: ImportedFileValidationException) {
callback(CreateCaptureFlowFragmentForIntentResult.Error(exception))
}

override fun onCancelled() {
callback(CreateCaptureFlowFragmentForIntentResult.Cancelled)
}
override fun onCancelled() {
callback(CreateCaptureFlowFragmentForIntentResult.Cancelled)
}

})
})
}


sealed class CreateCaptureFlowFragmentForIntentResult {
data class Success(val fragment: CaptureFlowFragment): CreateCaptureFlowFragmentForIntentResult()
data class Error(val exception: ImportedFileValidationException): CreateCaptureFlowFragmentForIntentResult()
object Cancelled: CreateCaptureFlowFragmentForIntentResult()
data class Success(val fragment: CaptureFlowFragment) :
CreateCaptureFlowFragmentForIntentResult()

data class Error(val exception: ImportedFileValidationException) :
CreateCaptureFlowFragmentForIntentResult()

object Cancelled : CreateCaptureFlowFragmentForIntentResult()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import net.gini.android.capture.camera.CameraFragmentListener
import net.gini.android.capture.network.model.GiniCaptureCompoundExtraction
import net.gini.android.capture.network.model.GiniCaptureSpecificExtraction

class CaptureFlowFragment(private val analysisIntent: Intent? = null) :
class CaptureFlowFragment(private val openWithDocument: Document? = null) :
Fragment(),
GiniCaptureFragmentListener, DigitalInvoiceFragmentListener {

internal companion object {
fun createInstance(intent: Intent? = null): CaptureFlowFragment {
return CaptureFlowFragment(intent)
fun createInstance(openWithDocument: Document? = null): CaptureFlowFragment {
return CaptureFlowFragment(openWithDocument)
}
}

Expand Down Expand Up @@ -64,7 +64,7 @@ class CaptureFlowFragment(private val analysisIntent: Intent? = null) :


override fun onCreate(savedInstanceState: Bundle?) {
childFragmentManager.fragmentFactory = CaptureFlowFragmentFactory(this, analysisIntent, this)
childFragmentManager.fragmentFactory = CaptureFlowFragmentFactory(this, openWithDocument, this)
super.onCreate(savedInstanceState)
}

Expand Down Expand Up @@ -151,12 +151,12 @@ interface CaptureFlowFragmentListener {

class CaptureFlowFragmentFactory(
private val giniCaptureFragmentListener: GiniCaptureFragmentListener,
private val analysisIntent: Intent? = null,
private val openWithDocument: Document? = null,
private val digitalInvoiceListener: DigitalInvoiceFragmentListener
) : FragmentFactory() {
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
return when (className) {
GiniCaptureFragment::class.java.name -> GiniCaptureFragment(analysisIntent)
GiniCaptureFragment::class.java.name -> GiniCaptureFragment(openWithDocument)
.apply {
setListener(
giniCaptureFragmentListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import net.gini.android.capture.analysis.AnalysisActivity;
import net.gini.android.capture.camera.view.CameraNavigationBarBottomAdapter;
import net.gini.android.capture.camera.view.DefaultCameraNavigationBarBottomAdapter;
import net.gini.android.capture.help.HelpItem;
Expand Down Expand Up @@ -514,7 +513,14 @@ public boolean isFlashOnByDefault() {
*/
@NonNull
public CancellationToken createIntentForImportedFiles(@NonNull final Intent intent, @NonNull final Context context, @NonNull final AsyncCallback<Intent, ImportedFileValidationException> callback) {
return mGiniCaptureFileImport.createIntentForImportedFiles(intent, context, callback);
//return mGiniCaptureFileImport.createIntentForImportedFiles(intent, context, callback);
//TODO fix this when fixing CameraActivity
return null;
}

@NonNull
public CancellationToken createDocumentForImportedFiles(@NonNull final Intent intent, @NonNull final Context context, @NonNull final AsyncCallback<Document, ImportedFileValidationException> callback) {
return mGiniCaptureFileImport.createDocumentForImportedFiles(intent, context, callback);
}

/**
Expand Down Expand Up @@ -665,14 +671,11 @@ public static GiniCaptureFragment createGiniCaptureFragment() {
}

public CancellationToken createGiniCaptureFragmentForIntent(Context context, Intent intent, GiniCaptureIntentCallback captureIntentCallback) {
return createIntentForImportedFiles(intent, context, new AsyncCallback<Intent, ImportedFileValidationException>() {
return createDocumentForImportedFiles(intent, context, new AsyncCallback<Document, ImportedFileValidationException>() {
@Override
public void onSuccess(Intent result) {
if (result.getComponent().getClassName().equals(AnalysisActivity.class.getName())) {
public void onSuccess(Document result) {
// TODO please refactor this piece
captureIntentCallback.callback(new CreateCaptureFlowFragmentForIntentResult.Success(GiniCaptureFragment.Companion.createInstance(result)));
} else {
captureIntentCallback.callback(new CreateCaptureFlowFragmentForIntentResult.Error(new ImportedFileValidationException("Unknown activity class: ${result.component?.className}")));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
package net.gini.android.capture;

import static net.gini.android.capture.internal.util.FileImportValidator.FILE_SIZE_LIMIT;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import net.gini.android.capture.analysis.AnalysisActivity;
import androidx.annotation.NonNull;

import net.gini.android.capture.document.DocumentFactory;
import net.gini.android.capture.document.ImageMultiPageDocument;
import net.gini.android.capture.internal.util.DeviceHelper;
import net.gini.android.capture.internal.util.FileImportValidator;
import net.gini.android.capture.internal.util.MimeType;
import net.gini.android.capture.review.multipage.MultiPageReviewActivity;
import net.gini.android.capture.util.CancellationToken;
import net.gini.android.capture.util.IntentHelper;
import net.gini.android.capture.util.NoOpCancellationToken;
import net.gini.android.capture.util.UriHelper;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import static net.gini.android.capture.internal.util.FileImportValidator.FILE_SIZE_LIMIT;

/**
* This class contains methods for preparing launching the Gini Capture SDK with a file received
Expand All @@ -32,22 +29,6 @@ public final class GiniCaptureFileImport {
@NonNull
private final GiniCapture mGiniCapture;

@NonNull
private static Intent createIntentForMultiPageDocument(@NonNull final Context context,
final Document document) {
final Intent giniCaptureIntent;
if (document.isReviewable()) {
// The new ImageMultiPageDocument was already added to the memory store
giniCaptureIntent = MultiPageReviewActivity.createIntent(context, false);
} else {
giniCaptureIntent = new Intent(context, AnalysisActivity.class);
giniCaptureIntent.putExtra(AnalysisActivity.EXTRA_IN_DOCUMENT, document);
giniCaptureIntent.setExtrasClassLoader(GiniCaptureFileImport.class.getClassLoader());
}

return giniCaptureIntent;
}

@NonNull
private static Document createDocumentForImportedFile(@NonNull final Intent intent,
@NonNull final Context context) throws ImportedFileValidationException {
Expand Down Expand Up @@ -79,35 +60,6 @@ private static Document createDocumentForImportedFile(@NonNull final Intent inte
mGiniCapture = giniCapture;
}

CancellationToken createIntentForImportedFiles(@NonNull final Intent intent,
@NonNull final Context context,
@NonNull final AsyncCallback<Intent, ImportedFileValidationException> callback) {
final CancellationToken cancellationToken =
createDocumentForImportedFiles(intent, context,
new AsyncCallback<Document, ImportedFileValidationException>() {
@Override
public void onSuccess(final Document result) {
final Intent giniCaptureIntent = createIntentForMultiPageDocument(context, result);
callback.onSuccess(giniCaptureIntent);
}

@Override
public void onError(final ImportedFileValidationException exception) {
callback.onError(exception);
}

@Override
public void onCancelled() {
callback.onCancelled();
}
});
return new CancellationToken() {
@Override
public void cancel() {
cancellationToken.cancel();
}
};
}

CancellationToken createDocumentForImportedFiles(@NonNull final Intent intent,
@NonNull final Context context,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.gini.android.capture

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
Expand All @@ -10,9 +9,8 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import net.gini.android.capture.analysis.AnalysisActivity
import net.gini.android.capture.analysis.AnalysisFragment
import net.gini.android.capture.analysis.AnalysisFragmentCompatDirections
import net.gini.android.capture.analysis.AnalysisFragmentDirections
import net.gini.android.capture.analysis.AnalysisFragmentListener
import net.gini.android.capture.camera.CameraFragment
import net.gini.android.capture.camera.CameraFragmentDirections
Expand All @@ -30,16 +28,16 @@ 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) :
class GiniCaptureFragment(private val openWithDocument: Document? = null) :
Fragment(),
CameraFragmentListener,
MultiPageReviewFragmentListener,
AnalysisFragmentListener,
EnterManuallyButtonListener {

internal companion object {
fun createInstance(intent: Intent? = null): GiniCaptureFragment {
return GiniCaptureFragment(intent)
fun createInstance(document: Document? = null): GiniCaptureFragment {
return GiniCaptureFragment(document)
}
}

Expand Down Expand Up @@ -71,12 +69,11 @@ class GiniCaptureFragment(private val analysisIntent: Intent? = null) :
super.onViewCreated(view, savedInstanceState)
navController = (childFragmentManager.fragments[0]).findNavController()
oncePerInstallEventStore = OncePerInstallEventStore(requireContext())
if (analysisIntent != null) {
if (openWithDocument != null) {
navController.navigate(
CameraFragmentDirections.toAnalysisFragment(
analysisIntent.getParcelableExtra(
AnalysisActivity.EXTRA_IN_DOCUMENT
)!!, ""
openWithDocument,
""
)
)
} else {
Expand Down Expand Up @@ -207,7 +204,7 @@ class GiniCaptureFragment(private val analysisIntent: Intent? = null) :
override fun onProceedToNoExtractionsScreen(document: Document) {
NoResultsFragment.navigateToNoResultsFragment(
navController,
AnalysisFragmentCompatDirections.toNoResultsFragment(document)
AnalysisFragmentDirections.toNoResultsFragment(document)
)
}

Expand Down
Loading

0 comments on commit dc58539

Please sign in to comment.