-
Notifications
You must be signed in to change notification settings - Fork 18
/
NavigationExtensions.kt
236 lines (214 loc) · 8.35 KB
/
NavigationExtensions.kt
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
225
226
227
228
229
230
231
232
233
234
235
236
package org.p2p.wallet.utils
import androidx.annotation.AnimRes
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentResultListener
import androidx.fragment.app.commit
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.coroutineScope
import androidx.lifecycle.whenStateAtLeast
import kotlin.reflect.KClass
import kotlinx.coroutines.launch
import org.p2p.core.utils.hideKeyboard
import org.p2p.wallet.R
import org.p2p.wallet.common.mvp.BaseFragmentContract
inline fun LifecycleOwner.whenStateAtLeast(state: Lifecycle.State, crossinline block: () -> Unit) {
if (lifecycle.currentState.isAtLeast(state)) {
block()
} else {
lifecycle.coroutineScope.launch {
lifecycle.whenStateAtLeast(state) { block() }
}
}
}
fun Fragment.backStackEntryCount(): Int = requireActivity().supportFragmentManager.backStackEntryCount
fun Fragment.popBackStack(hideKeyboard: Boolean = true) {
if (hideKeyboard) requireActivity().hideKeyboard()
requireActivity().popBackStack()
}
fun FragmentActivity.popBackStack() {
if (supportFragmentManager.backStackEntryCount < 2) {
finish()
} else {
supportFragmentManager.popBackStack()
}
}
fun <T : Fragment> Fragment.popBackStackTo(
target: KClass<T>,
inclusive: Boolean = false,
immediate: Boolean = true
): Boolean = requireActivity().popBackStackTo(target, inclusive, immediate)
fun <T : Fragment> FragmentActivity.popBackStackTo(
target: KClass<T>,
inclusive: Boolean = false,
immediate: Boolean = true
): Boolean {
val flag = if (inclusive) FragmentManager.POP_BACK_STACK_INCLUSIVE else 0
hideKeyboard()
val tag = target.java.name
with(supportFragmentManager) {
return if (backStackEntryCount == 0 || getBackStackEntryAt(0).name == tag && inclusive) {
finish()
true
} else if (immediate) {
popBackStackImmediate(tag, flag)
} else {
popBackStack(tag, flag)
true
}
}
}
fun Fragment.popAndReplaceFragment(
target: Fragment,
popTo: KClass<out Fragment>? = null,
@IdRes containerId: Int = R.id.rootContainer,
addToBackStack: Boolean = true,
inclusive: Boolean = false,
@AnimRes enter: Int = R.anim.nav_enter,
@AnimRes exit: Int = R.anim.nav_exit,
@AnimRes popEnter: Int = R.anim.nav_pop_enter,
@AnimRes popExit: Int = R.anim.nav_pop_exit,
fragmentManager: FragmentManager = requireActivity().supportFragmentManager
) = whenStateAtLeast(Lifecycle.State.STARTED) {
requireActivity().hideKeyboard()
with(fragmentManager) {
// Override exit animation for popping fragment
if (this@popAndReplaceFragment is BaseFragmentContract) {
[email protected](exit)
}
// Make pop entering fragment invisible during transition
popTo?.java?.name
?.let { findFragmentByTag(it) as? BaseFragmentContract }
?.apply { overrideEnterAnimation(R.anim.nav_stay_transparent) }
popBackStack(
popTo?.java?.name,
if (inclusive) FragmentManager.POP_BACK_STACK_INCLUSIVE else 0
)
commit(allowStateLoss = true) {
// Preform immediate replace
setCustomAnimations(enter, 0, popEnter, popExit)
replace(containerId, target, target.javaClass.name)
if (addToBackStack) addToBackStack(target.javaClass.name)
}
}
}
fun FragmentActivity.popAndReplaceFragment(
target: Fragment,
popTo: KClass<out Fragment>? = null,
@IdRes containerId: Int = R.id.rootContainer,
addToBackStack: Boolean = true,
inclusive: Boolean = false,
@AnimRes enter: Int = R.anim.nav_enter,
@AnimRes exit: Int = R.anim.nav_exit,
@AnimRes popEnter: Int = R.anim.nav_pop_enter,
@AnimRes popExit: Int = R.anim.nav_pop_exit,
fragmentManager: FragmentManager = supportFragmentManager
) = whenStateAtLeast(Lifecycle.State.STARTED) {
hideKeyboard()
with(fragmentManager) {
// Override exit animation for popping fragment
if (this@popAndReplaceFragment is BaseFragmentContract) {
[email protected](exit)
}
// Make pop entering fragment invisible during transition
popTo?.java?.name
?.let { findFragmentByTag(it) as? BaseFragmentContract }
?.apply { overrideEnterAnimation(R.anim.nav_stay_transparent) }
popBackStack(
popTo?.java?.name,
if (inclusive) FragmentManager.POP_BACK_STACK_INCLUSIVE else 0
)
commit(allowStateLoss = true) {
// Preform immediate replace
setCustomAnimations(enter, 0, popEnter, popExit)
replace(containerId, target, target.javaClass.name)
if (addToBackStack) addToBackStack(target.javaClass.name)
}
}
}
fun Fragment.addFragment(
target: Fragment,
@IdRes containerId: Int = R.id.rootContainer,
addToBackStack: Boolean = true,
@AnimRes enter: Int = R.anim.nav_enter,
@AnimRes exit: Int = R.anim.nav_exit,
@AnimRes popEnter: Int = R.anim.nav_pop_enter,
@AnimRes popExit: Int = R.anim.nav_pop_exit,
fragmentManager: FragmentManager = requireActivity().supportFragmentManager
) {
whenStateAtLeast(Lifecycle.State.STARTED) {
requireActivity().hideKeyboard()
fragmentManager.commit(allowStateLoss = true) {
setCustomAnimations(enter, exit, popEnter, popExit)
add(containerId, target, target.javaClass.name)
if (addToBackStack) addToBackStack(target.javaClass.name)
}
}
}
fun Fragment.replaceFragment(
target: Fragment,
@IdRes containerId: Int = R.id.rootContainer,
addToBackStack: Boolean = true,
@AnimRes enter: Int = R.anim.nav_enter,
@AnimRes exit: Int = R.anim.nav_exit,
@AnimRes popEnter: Int = R.anim.nav_pop_enter,
@AnimRes popExit: Int = R.anim.nav_pop_exit,
fragmentManager: FragmentManager = requireActivity().supportFragmentManager
) = whenStateAtLeast(Lifecycle.State.STARTED) {
requireActivity().hideKeyboard()
fragmentManager.commit(allowStateLoss = true) {
setCustomAnimations(enter, exit, popEnter, popExit)
replace(containerId, target, target.javaClass.name)
if (addToBackStack) addToBackStack(target.javaClass.name)
}
}
fun Fragment.replaceFragmentForResult(
target: Fragment,
requestKey: String,
onResult: FragmentResultListener,
@IdRes containerId: Int = R.id.rootContainer,
addToBackStack: Boolean = true,
@AnimRes enter: Int = R.anim.nav_enter,
@AnimRes exit: Int = R.anim.nav_exit,
@AnimRes popEnter: Int = R.anim.nav_pop_enter,
@AnimRes popExit: Int = R.anim.nav_pop_exit,
fragmentManager: FragmentManager = requireActivity().supportFragmentManager
) = whenStateAtLeast(Lifecycle.State.STARTED) {
requireActivity().hideKeyboard()
fragmentManager.setFragmentResultListener(requestKey, this, onResult)
fragmentManager.commit(allowStateLoss = true) {
setCustomAnimations(enter, exit, popEnter, popExit)
replace(containerId, target, target.javaClass.name)
if (addToBackStack) addToBackStack(target.javaClass.name)
}
}
fun FragmentActivity.replaceFragment(
target: Fragment,
@IdRes containerId: Int = R.id.rootContainer,
addToBackStack: Boolean = true
) = whenStateAtLeast(Lifecycle.State.STARTED) {
hideKeyboard()
supportFragmentManager.commit(allowStateLoss = true) {
replace(containerId, target, target.javaClass.name)
if (addToBackStack) addToBackStack(target.javaClass.name)
}
}
fun FragmentActivity.addFragment(
target: Fragment,
@IdRes containerId: Int = R.id.rootContainer,
addToBackStack: Boolean = true,
@AnimRes enter: Int = R.anim.nav_enter,
@AnimRes exit: Int = R.anim.nav_exit,
@AnimRes popEnter: Int = R.anim.nav_pop_enter,
@AnimRes popExit: Int = R.anim.nav_pop_exit
) = whenStateAtLeast(Lifecycle.State.STARTED) {
hideKeyboard()
supportFragmentManager.commit(allowStateLoss = true) {
setCustomAnimations(enter, exit, popEnter, popExit)
add(containerId, target, target.javaClass.name)
if (addToBackStack) addToBackStack(target.javaClass.name)
}
}