Skip to content

Commit

Permalink
refactor(capture-sdk): Add the help fragments to the navigation graph
Browse files Browse the repository at this point in the history
BSDK-258
  • Loading branch information
a-szotyori committed Jan 15, 2024
1 parent 0cf6698 commit 6cc11b8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -806,11 +806,7 @@ void startHelpActivity() {
return;
}

mFragment.getChildFragmentManager()
.beginTransaction()
.add(R.id.gc_fragment_container, HelpFragment.newInstance(), HelpFragment.class.getName())
.addToBackStack(null)
.commit();
mFragment.findNavController().navigate(CameraFragmentDirections.toHelpFragment());

trackCameraScreenEvent(CameraScreenEvent.HELP);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import net.gini.android.capture.GiniCapture
import net.gini.android.capture.R
Expand All @@ -28,9 +30,21 @@ class HelpFragment : Fragment() {
setUpHelpItems()
setupTopBarNavigation()
setupBottomBarNavigation()
handleOnBackPressed()
return binding.root
}

private fun handleOnBackPressed() {
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
findNavController().navigate(HelpFragmentDirections.toCameraFragment())
isEnabled = false
remove()
}
})
}


override fun onStart() {
super.onStart()
if (hasOnlyOneHelpItem()) {
Expand Down Expand Up @@ -63,7 +77,7 @@ class HelpFragment : Fragment() {
injectedViewAdapter.setNavButtonType(navType)
injectedViewAdapter.setTitle(getString(R.string.gc_title_help))
injectedViewAdapter.setOnNavButtonClickListener(IntervalClickListener {
parentFragmentManager.popBackStack()
findNavController().navigate(HelpFragmentDirections.toCameraFragment())
})
}
}
Expand All @@ -76,29 +90,21 @@ class HelpFragment : Fragment() {
GiniCapture.getInstance().internal().helpNavigationBarBottomAdapterInstance
) { injectedViewAdapter ->
injectedViewAdapter.setOnBackClickListener(IntervalClickListener {
parentFragmentManager.popBackStack()
findNavController().navigate(HelpFragmentDirections.toCameraFragment())
})
}
}
}

private fun launchHelpScreen(helpItem: HelpItem) {
when (helpItem) {
HelpItem.PhotoTips -> navigateToFragment(PhotoTipsHelpFragment.newInstance())
HelpItem.SupportedFormats -> navigateToFragment(SupportedFormatsHelpFragment.newInstance())
HelpItem.FileImport -> navigateToFragment(FileImportHelpFragment.newInstance())
HelpItem.PhotoTips -> findNavController().navigate(HelpFragmentDirections.toPhotoTipsHelpFragment())
HelpItem.SupportedFormats -> findNavController().navigate(HelpFragmentDirections.toSupportedFormatsHelpFragment())
HelpItem.FileImport -> findNavController().navigate(HelpFragmentDirections.toFileImportHelpFragment())
is HelpItem.Custom -> requireActivity().startActivity(helpItem.intent)
}
}

private fun navigateToFragment(fragment: Fragment) {
childFragmentManager
.beginTransaction()
.add(R.id.gc_fragment_container, fragment, fragment::class.java.name)
.addToBackStack(null)
.commit()
}

companion object {
@JvmStatic
fun newInstance() = HelpFragment()
Expand Down
49 changes: 49 additions & 0 deletions capture-sdk/sdk/src/main/res/navigation/gc_nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
android:name="GC_EXTRA_IN_DOCUMENT_IMPORT_FILE_TYPES"
app:argType="net.gini.android.capture.DocumentImportEnabledFileTypes" />
</action>
<action
android:id="@+id/to_help_fragment"
app:destination="@id/gc_destination_help_fragment"
app:popUpTo="@id/gc_destination_camera_fragment"
app:popUpToInclusive="true" />
<!-- popUpTo and popUpToInclusive are needed to replace the camera screen with the help screen in the back stack -->
<!-- The camera preview is not restarted otherwise. It has something to do with the SurfaceView, but I couldn't find the root cause. -->
</fragment>

<fragment
Expand Down Expand Up @@ -81,5 +88,47 @@
tools:layout="@layout/gc_fragment_file_chooser">
</dialog>

<fragment
android:id="@+id/gc_destination_help_fragment"
android:name="net.gini.android.capture.help.HelpFragment"
android:label="fragment_help"
tools:layout="@layout/gc_fragment_help">
<action
android:id="@+id/to_file_import_help_fragment"
app:destination="@id/gc_destination_file_import_help_fragment" />
<action
android:id="@+id/to_photo_tips_help_fragment"
app:destination="@id/gc_destination_photo_tips_help_fragment" />
<action
android:id="@+id/to_supported_formats_help_fragment"
app:destination="@id/gc_destination_supported_formats_help_fragment" />
<action
android:id="@+id/to_camera_fragment"
app:destination="@id/gc_destination_camera_fragment"
app:popUpTo="@id/gc_destination_help_fragment"
app:popUpToInclusive="true" />
<!-- popUpTo and popUpToInclusive are needed to make the camera screen the start destination again -->
</fragment>

<fragment
android:id="@+id/gc_destination_file_import_help_fragment"
android:name="net.gini.android.capture.help.FileImportHelpFragment"
android:label="fragment_help"
tools:layout="@layout/gc_fragment_file_import_help">
</fragment>

<fragment
android:id="@+id/gc_destination_photo_tips_help_fragment"
android:name="net.gini.android.capture.help.PhotoTipsHelpFragment"
android:label="fragment_help"
tools:layout="@layout/gc_fragment_photo_tips_help">
</fragment>

<fragment
android:id="@+id/gc_destination_supported_formats_help_fragment"
android:name="net.gini.android.capture.help.SupportedFormatsHelpFragment"
android:label="fragment_help"
tools:layout="@layout/gc_fragment_supported_formats_help">
</fragment>

</navigation>

0 comments on commit 6cc11b8

Please sign in to comment.