Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
InsanusMokrassar committed Oct 26, 2022
1 parent 45b769c commit eeeec0c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
6 changes: 6 additions & 0 deletions core/src/main/kotlin/AndroidNavigationHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import kotlinx.coroutines.sync.withLock
open class AndroidNavigationHost<T : NavigationNodeDefaultConfig>(
protected val configsRepo: NavigationConfigsRepo<T>,
protected val startChain: ConfigHolder<T>,
protected val manualHierarchyCheckerDelayMillis: Long? = 100L,
protected val fragmentsClassesFactory: FragmentsClassesFactory<T>
) {
var job: Job? = null
Expand All @@ -42,6 +43,7 @@ open class AndroidNavigationHost<T : NavigationNodeDefaultConfig>(
fragmentManager,
rootView,
flowOnHierarchyChangeListener,
manualHierarchyCheckerDelayMillis,
fragmentsClassesFactory
)
val nodesFactory = NavigationNodeFactory<T> { navigationChain, config ->
Expand Down Expand Up @@ -75,10 +77,12 @@ inline fun <reified T : NavigationNodeDefaultConfig> AppCompatActivity.initNavig
flowOnHierarchyChangeListener: FlowOnHierarchyChangeListener = FlowOnHierarchyChangeListener(recursive = true).also {
(rootView as ViewGroup).setOnHierarchyChangeListenerRecursively(it)
},
manualHierarchyCheckerDelayMillis: Long? = 100L,
noinline fragmentsClassesFactory: FragmentsClassesFactory<T>
) = AndroidNavigationHost(
configsRepo,
startChain,
manualHierarchyCheckerDelayMillis,
fragmentsClassesFactory
).apply {
start(scope, fragmentManager, rootView, flowOnHierarchyChangeListener)
Expand Down Expand Up @@ -106,6 +110,7 @@ inline fun <reified T : NavigationNodeDefaultConfig> AppCompatActivity.initNavig
flowOnHierarchyChangeListener: FlowOnHierarchyChangeListener = FlowOnHierarchyChangeListener(recursive = true).also {
(rootView as ViewGroup).setOnHierarchyChangeListenerRecursively(it)
},
manualHierarchyCheckerDelayMillis: Long? = 100L,
noinline fragmentsClassesFactory: FragmentsClassesFactory<T>
) = initNavigation(
ConfigHolder.Chain(
Expand All @@ -120,5 +125,6 @@ inline fun <reified T : NavigationNodeDefaultConfig> AppCompatActivity.initNavig
fragmentManager,
rootView,
flowOnHierarchyChangeListener,
manualHierarchyCheckerDelayMillis,
fragmentsClassesFactory
)
57 changes: 36 additions & 21 deletions core/src/main/kotlin/fragments/AndroidFragmentNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.inmo.kslog.common.d
import dev.inmo.micro_utils.coroutines.*
import dev.inmo.navigation.core.*
import dev.inmo.navigation.core.configs.NavigationNodeDefaultConfig
import dev.inmo.navigation.core.fragments.view.NavigationFragmentContainerView
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlin.reflect.KClass
Expand All @@ -18,6 +19,7 @@ class AndroidFragmentNode<Config : Base, Base : NavigationNodeDefaultConfig>(
private val fragmentManager: FragmentManager,
private val rootView: View,
private val flowOnHierarchyChangeListener: FlowOnHierarchyChangeListener,
private val manualHierarchyCheckerDelayMillis: Long? = 100L,
override val id: NavigationNodeId = NavigationNodeId()
) : NavigationNode<Config, Base>() {
private var fragment: NodeFragment<Config, Base>? = null
Expand Down Expand Up @@ -47,13 +49,8 @@ class AndroidFragmentNode<Config : Base, Base : NavigationNodeDefaultConfig>(
}
}

override fun onResume() {
super.onResume()
fragment ?.let {
rootView.findViewsWithNavigationTag(viewTag).firstOrNull() ?.also { view ->
placeFragment(view)
}
}
private fun placeFragment(): Boolean {
return rootView.findViewsWithNavigationTag(viewTag).firstOrNull() ?.also(::placeFragment) != null
}

override fun onPause() {
Expand All @@ -76,25 +73,43 @@ class AndroidFragmentNode<Config : Base, Base : NavigationNodeDefaultConfig>(
}

override fun start(scope: CoroutineScope): Job {
val subsubscope = scope.LinkedSupervisorScope()
return super.start(subsubscope).let {

flowOnHierarchyChangeListener.onChildViewAdded.subscribeSafelyWithoutExceptions(subsubscope) { (_, child) ->
fragment ?.let {
if (viewTag == child.navigationTag && state == NavigationNodeState.RESUMED && !it.isInLayout) {
placeFragment(child)
} else {
log.d {
"Solved to avoid fragment placement:\n" +
"viewTag == child.navigationTag: ${viewTag == child.navigationTag}\n" +
"state == NavigationNodeState.RESUMED: ${state == NavigationNodeState.RESUMED}\n" +
"!fragment.isInLayout: ${!it.isInLayout}"
val subscope = scope.LinkedSupervisorScope()
return super.start(subscope).let {

(flowOf(state) + statesFlow).filter { it == NavigationNodeState.RESUMED }.subscribeSafelyWithoutExceptions(subscope) {
val subsubscope = subscope.LinkedSupervisorScope()

if (placeFragment()) {
return@subscribeSafelyWithoutExceptions
}

flowOnHierarchyChangeListener.onChildViewAdded.filterNot {
it.second.navigationTag != viewTag
}.subscribeSafelyWithoutExceptions(subsubscope) {
if (placeFragment()) {
subsubscope.cancel()
}
}

(flowOf(state) + statesFlow).filterNot {
it == NavigationNodeState.RESUMED
}.take(1).subscribeSafelyWithoutExceptions(subsubscope) {
subsubscope.cancel()
}

subsubscope.launchSafelyWithoutExceptions {
while (state == NavigationNodeState.RESUMED && fragment ?.isAdded == false) {
if (placeFragment()) {
subsubscope.cancel()
break
}

delay(manualHierarchyCheckerDelayMillis ?: break)
}
}
}

subsubscope.coroutineContext.job
subscope.coroutineContext.job
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AndroidNavigationNodeFactory<T : NavigationNodeDefaultConfig>(
private val flowOnHierarchyChangeListener: FlowOnHierarchyChangeListener = FlowOnHierarchyChangeListener(recursive = true).also {
(rootView as ViewGroup).setOnHierarchyChangeListenerRecursively(it)
},
private val manualHierarchyCheckerDelayMillis: Long? = 100L,
private val fragmentKClassResolver: FragmentsClassesFactory<T>
) : NavigationNodeFactory<T> {
override fun createNode(navigationChain: NavigationChain<T>, config: T): NavigationNode<out T, T>? {
Expand All @@ -23,7 +24,8 @@ class AndroidNavigationNodeFactory<T : NavigationNodeDefaultConfig>(
fragmentKClassResolver(config) ?: return null,
fragmentManager,
rootView,
flowOnHierarchyChangeListener
flowOnHierarchyChangeListener,
manualHierarchyCheckerDelayMillis
)
}
}

0 comments on commit eeeec0c

Please sign in to comment.