Skip to content

Commit

Permalink
refactor(bank-sdk): Convert the digital invoice's HelpActivity into…
Browse files Browse the repository at this point in the history
… a fragment

Needs to be added to the Bank SDK's navigation graph.

BSDK-258
  • Loading branch information
a-szotyori committed Jan 16, 2024
1 parent 681f362 commit 6bebd9d
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 49 deletions.
5 changes: 0 additions & 5 deletions bank-sdk/sdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
package="net.gini.android.bank.sdk">

<application>
<activity
android:name=".capture.digitalinvoice.help.HelpActivity"
android:exported="false"
android:theme="@style/GiniCaptureTheme" />
<activity android:name=".capture.CaptureFlowActivity" />
<activity
android:name=".capture.digitalinvoice.DigitalInvoiceActivity"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package net.gini.android.bank.sdk.capture.digitalinvoice

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
Expand All @@ -15,7 +14,6 @@ import androidx.recyclerview.widget.LinearSmoothScroller
import kotlinx.coroutines.CoroutineScope
import net.gini.android.bank.sdk.GiniBank
import net.gini.android.bank.sdk.R
import net.gini.android.bank.sdk.capture.digitalinvoice.help.HelpActivity
import net.gini.android.bank.sdk.capture.digitalinvoice.view.DigitalInvoiceNavigationBarBottomAdapter
import net.gini.android.capture.internal.util.ActivityHelper.forcePortraitOrientationOnPhones
import net.gini.android.capture.network.model.GiniCaptureCompoundExtraction
Expand Down Expand Up @@ -218,7 +216,7 @@ open class DigitalInvoiceFragment : Fragment(), DigitalInvoiceScreenContract.Vie
injectedViewAdapter.setMenuResource(R.menu.gbs_menu_digital_invoice)
injectedViewAdapter.setOnMenuItemClickListener(IntervalToolbarMenuItemIntervalClickListener {
if (it.itemId == R.id.help) {
startActivity(Intent(requireContext(), HelpActivity::class.java))
showHelp()
}
true
})
Expand All @@ -231,6 +229,14 @@ open class DigitalInvoiceFragment : Fragment(), DigitalInvoiceScreenContract.Vie
}
}

private fun showHelp() {
// TODO: use navigation component's NavController to navigate to show the help fragment
// childFragmentManager.beginTransaction()
// .add(R.id.gbs_fragment_container, DigitalInvoiceHelpFragment.newInstance(), DigitalInvoiceHelpFragment::class.java.name)
// .addToBackStack(null)
// .commit()
}

private fun initBottomBar() {
if (GiniCapture.hasInstance() && GiniCapture.getInstance().isBottomNavigationBarEnabled) {

Expand All @@ -240,7 +246,7 @@ open class DigitalInvoiceFragment : Fragment(), DigitalInvoiceScreenContract.Vie
binding.gbsBottomBarNavigation.injectedViewAdapterHolder =
InjectedViewAdapterHolder(GiniBank.digitalInvoiceNavigationBarBottomAdapterInstance) { injectedViewAdapter ->
injectedViewAdapter.setOnHelpClickListener {
startActivity(Intent(requireContext(), HelpActivity::class.java))
showHelp()
}

injectedViewAdapter.setOnProceedClickListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,45 @@
package net.gini.android.bank.sdk.capture.digitalinvoice.help

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import net.gini.android.bank.sdk.GiniBank
import net.gini.android.bank.sdk.R
import net.gini.android.bank.sdk.capture.digitalinvoice.help.view.DigitalInvoiceHelpNavigationBarBottomAdapter
import net.gini.android.bank.sdk.databinding.GbsFragmentDigitalInvoiceHelpBinding
import net.gini.android.bank.sdk.util.autoCleared
import net.gini.android.capture.GiniCapture
import net.gini.android.capture.internal.ui.IntervalClickListener
import net.gini.android.capture.internal.util.ActivityHelper
import net.gini.android.capture.view.InjectedViewAdapterHolder
import net.gini.android.capture.view.InjectedViewContainer
import net.gini.android.capture.view.NavButtonType
import net.gini.android.capture.view.NavigationBarTopAdapter

/**
* Internal use only.
*
*/
class HelpActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContentView(R.layout.gbs_activity_help)
if (!GiniCapture.hasInstance()) {
finish()
return
}

ActivityHelper.forcePortraitOrientationOnPhones(this)
class DigitalInvoiceHelpFragment : Fragment() {
private var binding: GbsFragmentDigitalInvoiceHelpBinding by autoCleared()

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = GbsFragmentDigitalInvoiceHelpBinding.inflate(inflater)
setupHelpList()
setupTopBarNavigation()
setupBottomNavigationBar()
handleOnBackPressed()
return binding.root
}

private fun setupHelpList() {
val recyclerView = findViewById<RecyclerView>(R.id.gbs_help_items)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = HelpItemAdapter(this)
}

private fun handleOnBackPressed() {
ActivityHelper.interceptOnBackPressed(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
finish()
}
})
val recyclerView = binding.gbsHelpItems
recyclerView.layoutManager = LinearLayoutManager(requireContext())
recyclerView.adapter = HelpItemAdapter(requireContext())
}

private fun setupTopBarNavigation() {
val topBarInjectedViewContainer =
findViewById<InjectedViewContainer<NavigationBarTopAdapter>>(R.id.gbs_injected_navigation_bar_container_top)
val topBarInjectedViewContainer = binding.gbsInjectedNavigationBarContainerTop
if (GiniCapture.hasInstance()) {

topBarInjectedViewContainer.injectedViewAdapterHolder = InjectedViewAdapterHolder(
GiniCapture.getInstance().internal().navigationBarTopAdapterInstance
) { injectedAdapterView ->
Expand All @@ -68,22 +50,28 @@ class HelpActivity : AppCompatActivity() {
injectedAdapterView.setTitle(getString(net.gini.android.capture.R.string.gc_title_help))

injectedAdapterView.setOnNavButtonClickListener(IntervalClickListener {
onBackPressed()
// TODO: use nagivation component
// findNavController().popBackStack()
})
}
}
}

private fun setupBottomNavigationBar() {
if (GiniCapture.hasInstance() && GiniCapture.getInstance().isBottomNavigationBarEnabled) {
val injectedViewContainer =
findViewById<InjectedViewContainer<DigitalInvoiceHelpNavigationBarBottomAdapter>>(R.id.gbs_injected_navigation_bar_container_bottom)
val injectedViewContainer = binding.gbsInjectedNavigationBarContainerBottom
injectedViewContainer.injectedViewAdapterHolder =
InjectedViewAdapterHolder(GiniBank.digitalInvoiceHelpNavigationBarBottomAdapterInstance) { injectedViewAdapter ->
injectedViewAdapter.setOnBackButtonClickListener(IntervalClickListener {
onBackPressed()
// TODO: use nagivation component
// findNavController().popBackStack()
})
}
}
}
}

companion object {
@JvmStatic
fun newInstance() = DigitalInvoiceHelpFragment()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.gini.android.bank.sdk.util

import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
* A lazy property that gets cleaned up when the fragment's view is destroyed.
*
* Accessing this variable while the fragment's view is destroyed will throw NPE.
*/
internal class AutoClearedValue<T : Any>(val fragment: Fragment) : ReadWriteProperty<Fragment, T> {
private var _value: T? = null

init {
fragment.lifecycle.addObserver(object: DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
fragment.viewLifecycleOwnerLiveData.observe(fragment) { viewLifecycleOwner ->
viewLifecycleOwner?.lifecycle?.addObserver(object: DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
_value = null
}
})
}
}
})
}

override fun getValue(thisRef: Fragment, property: KProperty<*>): T {
return _value ?: throw IllegalStateException(
"should never call auto-cleared-value get when it might not be available"
)
}

override fun setValue(thisRef: Fragment, property: KProperty<*>, value: T) {
_value = value
}
}

/**
* Creates an [AutoClearedValue] associated with this fragment.
*/
internal fun <T : Any> Fragment.autoCleared() = AutoClearedValue<T>(this)

0 comments on commit 6bebd9d

Please sign in to comment.