Skip to content

Commit

Permalink
Merge pull request #37 from hamza94max/fix/navigation
Browse files Browse the repository at this point in the history
fix: navigation by not opening already opened screens
  • Loading branch information
hamza94max authored Dec 16, 2023
2 parents ed5a09c + aad5f33 commit 077eac1
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions app/src/main/java/com/islamey/hamza/wazaker/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ import dagger.hilt.android.AndroidEntryPoint
class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
lateinit var navController: NavController
private val navController: NavController by lazy {
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.hostFragment) as NavHostFragment
navHostFragment.navController
}

private var lastOpenedScreenId = R.id.home_action

override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
Expand All @@ -42,30 +48,32 @@ class MainActivity : AppCompatActivity() {
}

private fun setUpBottomViewNavigation() {
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.hostFragment) as NavHostFragment
navController = navHostFragment.navController

binding.bottomNavigationView.setOnItemSelectedListener { menuItem ->
when (menuItem.itemId) {

R.id.home_action -> {
navController.navigate(R.id.homeFragment)
doIfNotCurrent(R.id.home_action){
navController.navigate(R.id.homeFragment)
}
true
}

R.id.qibla_action -> {
openQiblaActivity()
true
// as we open another activity we should not lose current selection
false
}

R.id.forty_action -> {
navController.navigate(R.id.fortyHadithListFragment)
doIfNotCurrent(R.id.forty_action){
navController.navigate(R.id.fortyHadithListFragment)
}
true
}

R.id.settings_action -> {
navController.navigate(R.id.settingsFragment)
doIfNotCurrent(R.id.settings_action){
navController.navigate(R.id.settingsFragment)
}
true
}

Expand All @@ -74,6 +82,13 @@ class MainActivity : AppCompatActivity() {
}
}

private fun doIfNotCurrent(actionId: Int, action : ()-> Unit) {
if (actionId != lastOpenedScreenId){
action.invoke()
lastOpenedScreenId = actionId
}
}

private fun openQiblaActivity() {
val intent = Intent(this, CompassActivity::class.java)
intent.putExtra(Constants.TOOLBAR_BG_COLOR, "#FFFFFF") // Toolbar Background color
Expand Down

0 comments on commit 077eac1

Please sign in to comment.