Skip to content

Commit

Permalink
fixes in initNavigation
Browse files Browse the repository at this point in the history
  • Loading branch information
InsanusMokrassar committed Mar 12, 2024
1 parent fb280d4 commit 9942d4f
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions mvvm/src/androidMain/kotlin/NavigationMVVMSingleActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import dev.inmo.kslog.common.d
import dev.inmo.kslog.common.logger
import dev.inmo.micro_utils.coroutines.doInUI
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import dev.inmo.micro_utils.koin.getAllDistinct
Expand All @@ -19,9 +21,21 @@ import org.koin.core.component.KoinComponent
abstract class NavigationMVVMSingleActivity : AppCompatActivity(), KoinComponent {
protected open val fragmentTransactionConfigurator: FragmentTransactionConfigurator<NavigationNodeDefaultConfig>? = null
protected var currentAndroidNavigationHost: AndroidNavigationHost<NavigationNodeDefaultConfig>? = null
protected var latestLoadingFragment: Fragment? = null
protected open fun allocateLoadingFragment(): Fragment? = null
protected abstract fun createInitialConfig(rootFragmentTag: String): NavigationNodeDefaultConfig

protected fun initNavigation() {
val rootFragmentTag = findViewById<View>(R.id.main_activity_main_fragment).navigationTag.toString()
val currentFragment = supportFragmentManager.findFragmentById(R.id.main_activity_main_fragment)

if (currentFragment === latestLoadingFragment || currentFragment == null) {
CoroutineScope(Dispatchers.Default).launchSafelyWithoutExceptions {
onStartNavigation(rootFragmentTag)
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -31,12 +45,7 @@ abstract class NavigationMVVMSingleActivity : AppCompatActivity(), KoinComponent
supportFragmentManager.beginTransaction().apply {
replace(R.id.main_activity_main_fragment, loadingFragment)
}.commitNow()
}

val rootFragmentTag = findViewById<View>(R.id.main_activity_main_fragment).navigationTag.toString()

CoroutineScope(Dispatchers.Default).launchSafelyWithoutExceptions {
onStartNavigation(rootFragmentTag)
latestLoadingFragment = loadingFragment
}

onBackPressedDispatcher.addCallback(
Expand All @@ -48,6 +57,12 @@ abstract class NavigationMVVMSingleActivity : AppCompatActivity(), KoinComponent
)
}

override fun onStart() {
super.onStart()

initNavigation()
}

open suspend fun onStartNavigation(rootFragmentTag: String) {
doInUI {
currentAndroidNavigationHost ?.job ?.cancel()
Expand Down

0 comments on commit 9942d4f

Please sign in to comment.