Skip to content

Commit

Permalink
fix(crash): AND-0000 fragment crash (#4258)
Browse files Browse the repository at this point in the history
  • Loading branch information
dserrano-bc committed Dec 19, 2022
1 parent e348478 commit 2143346
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
@@ -1,11 +1,16 @@
package com.blockchain.componentlib.system

import android.widget.FrameLayout
import android.view.View
import android.view.ViewGroup
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.view.ViewCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentContainerView
import androidx.fragment.app.FragmentManager

@Composable
Expand All @@ -15,19 +20,20 @@ fun EmbeddedFragment(
modifier: Modifier = Modifier,
tag: String
) {
var viewId by rememberSaveable { mutableStateOf(View.generateViewId()) }

AndroidView(
modifier = modifier,
factory = { context ->
FrameLayout(context).apply {
id = ViewCompat.generateViewId()
}
fragmentManager.findFragmentById(viewId)?.view
?.also { (it.parent as? ViewGroup)?.removeView(it) }
?: FragmentContainerView(context)
.apply { id = viewId }
.also {
fragmentManager.beginTransaction().replace(viewId, fragment, tag).commitNowAllowingStateLoss()
}
},
update = {
val fragmentAlreadyAdded = fragmentManager.findFragmentByTag(tag) != null

if (!fragmentAlreadyAdded) {
fragmentManager.beginTransaction().replace(it.id, fragment, tag).commit()
}
}
)
}
Expand Up @@ -65,7 +65,7 @@ class EarnDashboardFragment :

private val viewModel by viewModel<EarnDashboardViewModel>()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return ComposeView(requireContext()).apply {
setContent {
bindViewModel(viewModel, this@EarnDashboardFragment, ModelConfigArgs.NoArgs)
Expand Down
Expand Up @@ -566,6 +566,6 @@ fun EarnKycRequired(fm: FragmentManager) {
modifier = Modifier.fillMaxSize(),
fragment = KycUpgradeNowSheet.newInstance(),
fragmentManager = fm,
tag = "KycNow"
tag = "EarnDashboardKyc"
)
}

0 comments on commit 2143346

Please sign in to comment.