-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBottomNavController.kt
More file actions
224 lines (187 loc) · 7.67 KB
/
Copy pathBottomNavController.kt
File metadata and controls
224 lines (187 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.anikinkirill.powerfulandroidapp.util
import android.app.Activity
import android.content.Context
import androidx.annotation.IdRes
import androidx.annotation.NavigationRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import com.anikinkirill.powerfulandroidapp.R
import com.google.android.material.bottomnavigation.BottomNavigationView
/**
* Custom bottom navigation controller
*
* Helps to easily manage the back stack of fragments
* The core concept took from here:
* https://stackoverflow.com/questions/50577356/android-jetpack-navigation-bottomnavigationview-with-youtube-or-instagram-like#_=_
*/
class BottomNavController(
private val context: Context,
@IdRes val containerId: Int, // R.id.main_nav_host_fragment
@IdRes private val appStartDestinationId: Int,
private val graphChangeListener: OnNavigationGraphChanged?,
private val navGraphProvider: NavGraphProvider
) {
companion object {
private const val TAG = "AppDebug_BottomNavController"
}
lateinit var activity: Activity
lateinit var fragmentManager: FragmentManager
lateinit var navItemChangeListener: OnNavigationItemChanged
private val navigationBackStack = BackStack.of(appStartDestinationId)
init {
if(context is Activity) {
activity = context
fragmentManager = (activity as FragmentActivity).supportFragmentManager
}
}
fun onNavigationItemSelected(itemId: Int = navigationBackStack.last()) : Boolean {
// Find a fragment or create a new one
val fragment = fragmentManager.findFragmentByTag(itemId.toString()) ?: NavHostFragment.create(navGraphProvider.getNavGraphId(itemId))
// Replace the fragment representing a navigation item
fragmentManager.beginTransaction()
.setCustomAnimations(
R.anim.fade_in,
R.anim.fade_out,
R.anim.fade_in,
R.anim.fade_out
)
.replace(containerId, fragment, itemId.toString())
.addToBackStack(null)
.commit()
// Add to back stack
navigationBackStack.moveLast(itemId)
// Update checked icon
navItemChangeListener.onItemChange(itemId)
// Communicate with Activity
graphChangeListener?.onGraphChange()
return true
}
/**
* For highlighting the selected item
* in the BottomNavigationMenu
* Example: from HOME user clicks CREATE
* So CREATE item is going to be highlighted (blue color)
*
*/
interface OnNavigationItemChanged {
/**
* @param itemId - R.menu.menu_nav_blog, R.menu.menu_nav_create_blog, R.menu.menu_nav_account
*/
fun onItemChange(itemId: Int)
}
fun setOnItemNavigationChanged(listener: (itemId: Int) -> Unit) {
navItemChangeListener = object : OnNavigationItemChanged {
override fun onItemChange(itemId: Int) {
listener.invoke(itemId)
}
}
}
/**
* Get navigation graph id
* @NavigationRes means that returned type of
* getNavGraphId is going to be navigation resource reference
* Example: R.navigation.nav_blog
*/
interface NavGraphProvider {
/**
* @param itemId - R.menu.menu_nav_blog, R.menu.menu_nav_create_blog, R.menu.menu_nav_account
* @return navigation resource reference (e.g R.navigation.nav_blog)
*/
@NavigationRes
fun getNavGraphId(itemId: Int) : Int
}
/**
* Tell the Activity that graph is changed now
* So when user goes from HOME to ACCOUNT
* this interface in going to be triggered
*
* DO NOT MISUNDERSTAND with [OnNavigationItemChanged] interface,
* because [OnNavigationItemChanged] is used INTERNALLY
* only in [BottomNavController] class
*
* But [OnNavigationGraphChanged] is used for telling the
* activity that graph is different
*/
interface OnNavigationGraphChanged {
fun onGraphChange()
}
/**
* Double click on already selected item
* will pop all the stack to the root, refreshing it
* Example: Home -> BlogFragment -> ViewBlogFragment -> UpdateBlogFragment
* double click on HOME will reselect everything and go to HOME root fragment
*/
interface OnNavigationReselectedListener {
fun onReselectNavItem(navController: NavController, fragment: Fragment)
}
private class BackStack : ArrayList<Int>() {
companion object {
fun of(vararg elements: Int) : BackStack {
val backStack = BackStack()
backStack.addAll(elements.toTypedArray())
return backStack
}
}
fun removeLast() = removeAt(size - 1)
fun moveLast(item: Int) {
remove(item)
add(item)
}
}
fun onBackPressed() {
val childFragmentManager = fragmentManager.findFragmentById(containerId)!!.childFragmentManager
when {
childFragmentManager.popBackStackImmediate() -> {
}
// Fragment back stack is empty so try to go back on the navigation stack
navigationBackStack.size > 1 -> {
// Remove last element from navigation back stack
navigationBackStack.removeLast()
// Update the container with a new fragment
onNavigationItemSelected()
}
// If the stack has only one and it's not the navigation home we should
// ensure that the application always leave from startDestination
navigationBackStack.last() != appStartDestinationId -> {
navigationBackStack.removeLast()
navigationBackStack.add(0, appStartDestinationId)
onNavigationItemSelected()
}
// Navigation stack is empty, so finish the activity
else -> activity.finish()
}
}
}
fun BottomNavigationView.setUpNavigation(
bottomNavController: BottomNavController,
onReselectListener: BottomNavController.OnNavigationReselectedListener
) {
// Get this function from BottomNavigationView
// Set a listener that will be notified when a bottom navigation item is selected
setOnNavigationItemSelectedListener {
bottomNavController.onNavigationItemSelected(it.itemId)
}
// Get this function from BottomNavigationView
// Set a listener that will be notified when the currently selected bottom navigation item is reselected
// When item graph (HOME, CREATE, ACCOUNT) is already selected, but user select this item one more time
// Every child/detail fragments should be gone. So user see the very first fragment
setOnNavigationItemReselectedListener {
bottomNavController
.fragmentManager
.findFragmentById(bottomNavController.containerId)!!
.childFragmentManager
.fragments[0]?.let { fragment ->
onReselectListener.onReselectNavItem(
bottomNavController.activity.findNavController(bottomNavController.containerId),
fragment
)
}
}
bottomNavController.setOnItemNavigationChanged { itemId ->
menu.findItem(itemId).isChecked = true
}
}