Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/show and hide bottom and top panels after search #5

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Bundle
import androidx.annotation.IdRes
import androidx.core.os.bundleOf
import androidx.navigation.NavController
import androidx.navigation.NavController.OnDestinationChangedListener
import com.rees46.demo_android.navigation.Destination
import com.rees46.demo_android.navigation.Navigator
import com.rees46.demo_android.navigation.ProductDetails
Expand Down Expand Up @@ -41,9 +42,16 @@ class AppNavigator(private val navController: NavController) : Navigator {
navController.popBackStack()
}

override fun getCurrentDestination() : Int? =
override fun getCurrentDestinationId() : Int? =
navController.currentDestination?.id

override fun getPreviousDestinationId() : Int? =
navController.previousBackStackEntry?.destination?.id

override fun addOnDestinationChangedListener(listener: OnDestinationChangedListener) {
navController.addOnDestinationChangedListener(listener)
}

private fun navigate(@IdRes resId: Int, args: Bundle?) {
navController.navigate(
resId = resId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.getRoot())

setupNavigator()
setupTopAppBar()
setupBottomNavigationView()
setupPopBackStack()
Expand All @@ -58,16 +59,33 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
}

private fun switchBottomTab(id: Int) {
if(navigator.getCurrentDestination() == id) return
if(navigator.getCurrentDestinationId() == id) return

navigator.navigate(id)
}

private fun setupNavigator() {
navigator.addOnDestinationChangedListener { _, destination, _ ->
if(navigator.getPreviousDestinationId() == R.id.searchFragment) {
showBars(true)
return@addOnDestinationChangedListener
}

if(destination.id == R.id.searchFragment) {
showBars(false)
}
}
}

private fun setupPopBackStack() {
onBackPressedDispatcher.addCallback(
owner = this,
onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if(navigator.getCurrentDestinationId() == R.id.searchFragment) {
showBars(true)
}

navigator.popBackStack()

changeSelectedBottomItem()
Expand All @@ -78,7 +96,7 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {

private fun changeSelectedBottomItem() {
with(binding.bottomNavigation) {
when (navigator.getCurrentDestination()) {
when (navigator.getCurrentDestinationId()) {
R.id.homeFragment -> selectedItemId = R.id.home
R.id.categoryFragment -> selectedItemId = R.id.category
R.id.cartFragment -> selectedItemId = R.id.cart
Expand All @@ -101,8 +119,6 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
true
}
R.id.menu_top_app_search -> {
supportActionBar?.hide()
binding.bottomNavigation.isVisible = false
navigator.navigate(R.id.searchFragment)
true
}
Expand All @@ -111,4 +127,15 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
}
})
}

private fun showBars(isShow: Boolean) {
if(isShow) {
supportActionBar?.show()
}
else {
supportActionBar?.hide()
}

binding.bottomNavigation.isVisible = isShow
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.rees46.demo_android.navigation

import androidx.navigation.NavController

interface Navigator {
fun navigate(destination: Destination)

fun navigate(id: Int)

fun popBackStack()
fun getCurrentDestination() : Int?
fun getCurrentDestinationId() : Int?
fun getPreviousDestinationId() : Int?

fun addOnDestinationChangedListener(listener: NavController.OnDestinationChangedListener)
}