Skip to content

Commit

Permalink
refactor(capture-sdk): Fix navigation between camera and review fragm…
Browse files Browse the repository at this point in the history
…ents

BSDK-258
  • Loading branch information
a-szotyori committed Jan 22, 2024
1 parent f9b70f2 commit 0889ade
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
/**
* The {@code CameraActivity} is the main entry point to the Gini Capture SDK.
**/
public class CameraActivity extends AppCompatActivity implements CameraFragmentListener,
CameraActivity extends AppCompatActivity implements GiniCaptureFragmentListener {
public class CameraActivity extends AppCompatActivity implements GiniCaptureFragmentListener {

/**
* <p> Returned when the result code is {@link CameraActivity#RESULT_ERROR} and contains a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
public class CameraFragment extends Fragment implements CameraFragmentInterface,
FragmentImplCallback {

public static final String REQUEST_KEY = "GC_CAMERA_FRAGMENT_REQUEST_KEY";
public static final String RESULT_KEY_SHOULD_SCROLL_TO_LAST_PAGE = "RESULT_KEY_SHOULD_SCROLL_TO_LAST_PAGE";
private static final String ARGS_ADD_PAGES = "GC_ARGS_ADD_PAGES";

private CameraFragmentListener mListener;

private CameraFragmentImpl mFragmentImpl;
private boolean addPages = false;

/**
* Internal use only.
Expand All @@ -36,19 +41,21 @@ public class CameraFragment extends Fragment implements CameraFragmentInterface,
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
readArguments();
mFragmentImpl = createFragmentImpl();
setListener(mFragmentImpl, mListener);
mFragmentImpl.onCreate(savedInstanceState);
}

private void setListener(@NonNull final CameraFragmentImpl fragmentImpl, @Nullable final CameraFragmentListener listener) {
final Activity activity = getActivity();
if (activity == null) {
throw new IllegalStateException("Activity not available");
private void readArguments() {
final Bundle arguments = getArguments();
if (arguments != null) {
addPages = arguments.getBoolean(ARGS_ADD_PAGES, false);
}
if (activity instanceof CameraFragmentListener) {
fragmentImpl.setListener((CameraFragmentListener) activity);
} else if (listener != null) {
}

private void setListener(@NonNull final CameraFragmentImpl fragmentImpl, @Nullable final CameraFragmentListener listener) {
if (listener != null) {
fragmentImpl.setListener(listener);
} else {
throw new IllegalStateException(
Expand All @@ -59,7 +66,7 @@ private void setListener(@NonNull final CameraFragmentImpl fragmentImpl, @Nullab
}

protected CameraFragmentImpl createFragmentImpl() {
return new CameraFragmentImpl(this);
return new CameraFragmentImpl(this, addPages);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@
import kotlin.Unit;

import static net.gini.android.capture.GiniCaptureError.ErrorCode.MISSING_GINI_CAPTURE_INSTANCE;
import static net.gini.android.capture.camera.CameraFragment.REQUEST_KEY;
import static net.gini.android.capture.camera.CameraFragment.RESULT_KEY_SHOULD_SCROLL_TO_LAST_PAGE;
import static net.gini.android.capture.document.ImageDocument.ImportMethod;
import static net.gini.android.capture.internal.network.NetworkRequestsManager.isCancellation;
import static net.gini.android.capture.internal.qrcode.EPSPaymentParser.EXTRACTION_ENTITY_NAME;
Expand Down Expand Up @@ -167,6 +169,7 @@ public void onExtractionsAvailable(
private static final String IS_FLASH_ENABLED_KEY = "IS_FLASH_ENABLED_KEY";

private final FragmentImplCallback mFragment;
private final boolean addPages;

@VisibleForTesting
QRCodePopup<PaymentQRCodeData> mPaymentQRCodePopup;
Expand Down Expand Up @@ -218,8 +221,9 @@ public void onExtractionsAvailable(
private IBANRecognizerFilter ibanRecognizerFilter;
private CropToCameraFrameTextRecognizer cropToCameraFrameTextRecognizer;

CameraFragmentImpl(@NonNull final FragmentImplCallback fragment) {
CameraFragmentImpl(@NonNull final FragmentImplCallback fragment, final boolean addPages) {
mFragment = fragment;
this.addPages = addPages;
}

@Override
Expand Down Expand Up @@ -440,7 +444,6 @@ public void onStart() {
setFileChooserFragmentResultListener();
}


private void setFileChooserFragmentResultListener() {
mFragment.getParentFragmentManager().setFragmentResultListener(FileChooserFragment.REQUEST_KEY, mFragment.getViewLifecycleOwner(), (requestKey, result) -> {
final FileChooserResult fileChooserResult = result.getParcelable(FileChooserFragment.RESULT_KEY);
Expand Down Expand Up @@ -1139,7 +1142,7 @@ public void documentAccepted() {
final ImageMultiPageDocument multiPageDocument =
(ImageMultiPageDocument) document;
addToMultiPageDocumentMemoryStore(multiPageDocument);
returnToMultiPageReviewScreen(true);
proceedToMultiPageReviewScreen(true);
} else {
if (document.isReviewable()) {
if (document.getType() == Document.Type.IMAGE &&
Expand All @@ -1148,7 +1151,7 @@ public void documentAccepted() {
document.getSource(), document.getImportMethod());
addToMultiPageDocumentMemoryStore(multiPageDocument);
multiPageDocument.addDocument(((ImageDocument) document));
proceedToMultiPageReviewScreen(false);
proceedToMultiPageReviewScreen(true);
}
} else {
mFragment.findNavController().navigate(CameraFragmentDirections.toAnalysisFragment(document , ""));
Expand All @@ -1170,19 +1173,18 @@ public void documentRejected(@NonNull final String messageForUser) {
});
}

private void returnToMultiPageReviewScreen(final boolean shouldScrollToLastPage) {
if (mFragment.getActivity() == null) {
return;
}
// TODO: use fragment result API to tell the multi-page review screen to scroll to the last page
mFragment.getActivity().getOnBackPressedDispatcher().onBackPressed();
}

private void proceedToMultiPageReviewScreen(final boolean shouldScrollToLastPage) {
if (mFragment.getActivity() == null) {
return;
}
mFragment.findNavController().navigate(CameraFragmentDirections.toReviewFragmentForFirstPage());
if (addPages) {
final Bundle resultBundle = new Bundle();
resultBundle.putBoolean(RESULT_KEY_SHOULD_SCROLL_TO_LAST_PAGE, shouldScrollToLastPage);
mFragment.getParentFragmentManager().setFragmentResult(REQUEST_KEY, resultBundle);
mFragment.getActivity().getOnBackPressedDispatcher().onBackPressed();
} else {
mFragment.findNavController().navigate(CameraFragmentDirections.toReviewFragment(shouldScrollToLastPage));
}
}

private void addToMultiPageDocumentMemoryStore(final ImageMultiPageDocument multiPageDocument) {
Expand Down Expand Up @@ -1409,7 +1411,7 @@ public void onDone(@NonNull final Photo result) {
mPhotoThumbnail.setImage(new PhotoThumbnail.ThumbnailBitmap(result.getBitmapPreview(),
document.getRotationForDisplay()));
mPhotoThumbnail.setImageCount(mMultiPageDocument.getDocuments().size());
returnToMultiPageReviewScreen(true);
proceedToMultiPageReviewScreen(true);
} else {
if (isMultiPageEnabled()) {
final ImageDocument document = createSavedDocument(result);
Expand Down Expand Up @@ -1498,7 +1500,7 @@ private void showMultiPageLimitError() {
mFragment.showAlertDialog(activity.getString(R.string.gc_document_error_too_many_pages),
activity.getString(R.string.gc_document_error_multi_page_limit_review_pages_button),
(dialogInterface, i) -> {
returnToMultiPageReviewScreen(true);
proceedToMultiPageReviewScreen(true);
}, activity.getString(R.string.gc_document_error_multi_page_limit_cancel_button),
null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import net.gini.android.capture.GiniCapture;
import net.gini.android.capture.GiniCaptureError;
import net.gini.android.capture.R;
import net.gini.android.capture.camera.CameraFragment;
import net.gini.android.capture.document.GiniCaptureDocument;
import net.gini.android.capture.document.GiniCaptureDocumentError;
import net.gini.android.capture.document.ImageDocument;
Expand Down Expand Up @@ -87,7 +88,6 @@
public class MultiPageReviewFragment extends Fragment implements MultiPageReviewFragmentInterface,
PreviewFragmentListener {

private static final String ARGS_DOCUMENT = "GC_ARGS_DOCUMENT";
private static final Logger LOG = LoggerFactory.getLogger(MultiPageReviewFragment.class);

@VisibleForTesting
Expand Down Expand Up @@ -170,6 +170,13 @@ public void handleOnBackPressed() {
public void onStart() {
super.onStart();
mInstanceStateSaved = false;
setCameraFragmentResultListener();
}

private void setCameraFragmentResultListener() {
getParentFragmentManager().setFragmentResultListener(CameraFragment.REQUEST_KEY, getViewLifecycleOwner(), (requestKey, result) -> {
mShouldScrollToLastPage = result.getBoolean(CameraFragment.RESULT_KEY_SHOULD_SCROLL_TO_LAST_PAGE);
});
}

@Override
Expand Down
20 changes: 8 additions & 12 deletions capture-sdk/sdk/src/main/res/navigation/gc_nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
tools:layout="@layout/gc_fragment_multi_page_review">
<action
android:id="@+id/to_camera_fragment_for_adding_pages"
app:destination="@id/gc_destination_camera_fragment" />
app:destination="@id/gc_destination_camera_fragment">
<argument
android:name="GC_ARGS_ADD_PAGES"
app:argType="boolean"
android:defaultValue="true" />
</action>
<action
android:id="@+id/to_camera_fragment_for_first_page"
app:destination="@id/gc_destination_camera_fragment"
Expand Down Expand Up @@ -100,22 +105,13 @@
android:label="fragment_camera"
tools:layout="@layout/gc_fragment_camera">
<action
android:id="@+id/to_review_fragment_for_first_page"
android:id="@+id/to_review_fragment"
app:destination="@id/gc_destination_review_fragment"
app:popUpTo="@id/gc_destination_camera_fragment"
app:popUpToInclusive="true" />

<action
android:id="@+id/to_review_fragment_for_more_pages"
app:destination="@id/gc_destination_review_fragment"
app:popUpTo="@id/gc_destination_camera_fragment"
app:popUpToInclusive="true" >
app:popUpToInclusive="true">
<argument
android:name="GC_SHOULD_SCROLL_TO_LAST_PAGE"
app:argType="boolean" />
<argument
android:name="GC_ARGS_DOCUMENT"
app:argType="net.gini.android.capture.Document" />
</action>
<!-- popUpTo and popUpToInclusive are needed to make the review screen the start destination -->
<!-- (i.e., replace the camera screen with the review screen in the back stack) -->
Expand Down

0 comments on commit 0889ade

Please sign in to comment.