Skip to content

Commit 205a69d

Browse files
authored
Merge pull request #5 from rees46/fix/show-and-hide-bottom-and-top-panels-after-search
Fix/show and hide bottom and top panels after search
2 parents 3ec1872 + 7355443 commit 205a69d

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

app/src/main/java/rees46/demo_android/app/navigation/AppNavigator.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.os.Bundle
44
import androidx.annotation.IdRes
55
import androidx.core.os.bundleOf
66
import androidx.navigation.NavController
7+
import androidx.navigation.NavController.OnDestinationChangedListener
78
import com.rees46.demo_android.navigation.Destination
89
import com.rees46.demo_android.navigation.Navigator
910
import com.rees46.demo_android.navigation.ProductDetails
@@ -41,9 +42,16 @@ class AppNavigator(private val navController: NavController) : Navigator {
4142
navController.popBackStack()
4243
}
4344

44-
override fun getCurrentDestination() : Int? =
45+
override fun getCurrentDestinationId() : Int? =
4546
navController.currentDestination?.id
4647

48+
override fun getPreviousDestinationId() : Int? =
49+
navController.previousBackStackEntry?.destination?.id
50+
51+
override fun addOnDestinationChangedListener(listener: OnDestinationChangedListener) {
52+
navController.addOnDestinationChangedListener(listener)
53+
}
54+
4755
private fun navigate(@IdRes resId: Int, args: Bundle?) {
4856
navController.navigate(
4957
resId = resId,

app/src/main/java/rees46/demo_android/app/presentation/MainActivity.kt

+31-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
3434
binding = ActivityMainBinding.inflate(layoutInflater)
3535
setContentView(binding.getRoot())
3636

37+
setupNavigator()
3738
setupTopAppBar()
3839
setupBottomNavigationView()
3940
setupPopBackStack()
@@ -58,16 +59,33 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
5859
}
5960

6061
private fun switchBottomTab(id: Int) {
61-
if(navigator.getCurrentDestination() == id) return
62+
if(navigator.getCurrentDestinationId() == id) return
6263

6364
navigator.navigate(id)
6465
}
6566

67+
private fun setupNavigator() {
68+
navigator.addOnDestinationChangedListener { _, destination, _ ->
69+
if(navigator.getPreviousDestinationId() == R.id.searchFragment) {
70+
showBars(true)
71+
return@addOnDestinationChangedListener
72+
}
73+
74+
if(destination.id == R.id.searchFragment) {
75+
showBars(false)
76+
}
77+
}
78+
}
79+
6680
private fun setupPopBackStack() {
6781
onBackPressedDispatcher.addCallback(
6882
owner = this,
6983
onBackPressedCallback = object : OnBackPressedCallback(true) {
7084
override fun handleOnBackPressed() {
85+
if(navigator.getCurrentDestinationId() == R.id.searchFragment) {
86+
showBars(true)
87+
}
88+
7189
navigator.popBackStack()
7290

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

7997
private fun changeSelectedBottomItem() {
8098
with(binding.bottomNavigation) {
81-
when (navigator.getCurrentDestination()) {
99+
when (navigator.getCurrentDestinationId()) {
82100
R.id.homeFragment -> selectedItemId = R.id.home
83101
R.id.categoryFragment -> selectedItemId = R.id.category
84102
R.id.cartFragment -> selectedItemId = R.id.cart
@@ -101,8 +119,6 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
101119
true
102120
}
103121
R.id.menu_top_app_search -> {
104-
supportActionBar?.hide()
105-
binding.bottomNavigation.isVisible = false
106122
navigator.navigate(R.id.searchFragment)
107123
true
108124
}
@@ -111,4 +127,15 @@ class MainActivity : AppCompatActivity(), LifecycleOwner {
111127
}
112128
})
113129
}
130+
131+
private fun showBars(isShow: Boolean) {
132+
if(isShow) {
133+
supportActionBar?.show()
134+
}
135+
else {
136+
supportActionBar?.hide()
137+
}
138+
139+
binding.bottomNavigation.isVisible = isShow
140+
}
114141
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.rees46.demo_android.navigation
22

3+
import androidx.navigation.NavController
4+
35
interface Navigator {
46
fun navigate(destination: Destination)
57

68
fun navigate(id: Int)
79

810
fun popBackStack()
9-
fun getCurrentDestination() : Int?
11+
fun getCurrentDestinationId() : Int?
12+
fun getPreviousDestinationId() : Int?
13+
14+
fun addOnDestinationChangedListener(listener: NavController.OnDestinationChangedListener)
1015
}

0 commit comments

Comments
 (0)