Skip to content

Commit

Permalink
Fix #49, prevent multiple same requests after configuration changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Jul 9, 2024
1 parent 2a3d7c1 commit 3512526
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions app/src/main/java/org/blackcandy/android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import kotlinx.coroutines.runBlocking
import org.blackcandy.android.compose.player.MiniPlayer
import org.blackcandy.android.compose.player.PlayerScreen
import org.blackcandy.android.databinding.ActivityMainBinding
import org.blackcandy.android.fragments.navs.LibraryNavHostFragment
import org.blackcandy.android.viewmodels.MainViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

Expand Down Expand Up @@ -262,7 +263,11 @@ class MainActivity : AppCompatActivity(), TurboActivity, OnItemSelectedListener

private fun restoreSavedState(savedInstanceState: Bundle?) {
if (savedInstanceState != null) {
showSelectedNavItem(savedInstanceState.getInt(SELECTED_NAV_ITEM_ID_KEY, R.id.nav_menu_home))
val selectedNavItemId = savedInstanceState.getInt(SELECTED_NAV_ITEM_ID_KEY, R.id.nav_menu_home)
showSelectedNavItem(selectedNavItemId)

binding.bottomNav?.menu?.findItem(selectedNavItemId)?.isChecked = true
binding.railNav?.menu?.findItem(selectedNavItemId)?.isChecked = true
}
}

Expand All @@ -282,15 +287,21 @@ class MainActivity : AppCompatActivity(), TurboActivity, OnItemSelectedListener
}

R.id.nav_menu_library -> {
val libraryNavFragment =
supportFragmentManager.findFragmentById(viewModel.libraryNav.id)
val libraryNavFragment = binding.libraryContainer.getFragment<LibraryNavHostFragment?>()

// Lazily add the library nav host fragment.
if (libraryNavFragment == null) {
supportFragmentManager.commitNow {
add(R.id.library_container, viewModel.libraryNav)
}
}

// Because there is no direct way to determine if a nav host fragment is registered or not,
// but the navHostFragment function in delegate will throw an IllegalArgumentException if the nav host fragment is not registered.
// So, we can use this to determine if the nav host fragment is registered or not.
try {
delegate.navHostFragment(R.id.library_container)
} catch (e: IllegalArgumentException) {
delegate.registerNavHostFragment(R.id.library_container)
}

Expand Down

0 comments on commit 3512526

Please sign in to comment.