-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathBottomTabsPresenter.kt
More file actions
379 lines (332 loc) · 16.1 KB
/
BottomTabsPresenter.kt
File metadata and controls
379 lines (332 loc) · 16.1 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package com.reactnativenavigation.viewcontrollers.bottomtabs
import android.animation.Animator
import android.graphics.Color
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import androidx.annotation.IntRange
import androidx.core.view.updateMargins
import com.aurelhubert.ahbottomnavigation.AHBottomNavigation.TitleState
import com.reactnativenavigation.RNNFeatureToggles
import com.reactnativenavigation.RNNToggles.TAB_BAR_TRANSLUCENCE
import com.reactnativenavigation.options.Options
import com.reactnativenavigation.options.params.BottomTabsLayoutStyle
import com.reactnativenavigation.options.params.Fraction
import com.reactnativenavigation.utils.UiUtils
import com.reactnativenavigation.viewcontrollers.viewcontroller.ViewController
import com.reactnativenavigation.views.bottomtabs.BottomTabs
import com.reactnativenavigation.views.bottomtabs.BottomTabsContainer
import com.reactnativenavigation.views.bottomtabs.BottomTabsLayout
import kotlin.math.max
import kotlin.math.roundToInt
class BottomTabsPresenter(
private val tabs: List<ViewController<*>>,
private var defaultOptions: Options,
val animator: BottomTabsAnimator
) {
private val bottomTabFinder: BottomTabFinder = BottomTabFinder(tabs)
private lateinit var bottomTabsContainer: BottomTabsContainer
private lateinit var bottomTabsLayout: BottomTabsLayout
private lateinit var bottomTabs: BottomTabs
private lateinit var tabSelector: TabSelector
private val defaultTitleState: TitleState
get() {
for (i in 0 until bottomTabs.itemsCount) {
if (bottomTabs.getItem(i)?.hasIcon() == true) return TitleState.SHOW_WHEN_ACTIVE
}
return TitleState.ALWAYS_SHOW
}
fun setDefaultOptions(defaultOptions: Options) {
this.defaultOptions = defaultOptions
}
fun bindView(bottomTabsContainer: BottomTabsContainer, bottomTabsLayout: BottomTabsLayout, tabSelector: TabSelector) {
this.bottomTabsContainer = bottomTabsContainer
this.bottomTabsLayout = bottomTabsLayout
this.bottomTabs = bottomTabsContainer.bottomTabs
this.tabSelector = tabSelector
animator.bindView(this.bottomTabs)
}
fun mergeOptions(options: Options, view: ViewController<*>) {
mergeBottomTabsOptions(options, view)
}
fun applyOptions(options: Options) {
applyBottomTabsOptions(options.copy().withDefaultOptions(defaultOptions))
}
fun applyChildOptions(options: Options, child: ViewController<*>) {
val tabIndex = bottomTabFinder.findByControllerId(child.id)
if (tabIndex >= 0) {
applyBottomTabsOptions(options.copy().withDefaultOptions(defaultOptions))
applyDrawBehind(tabIndex)
}
}
fun mergeChildOptions(options: Options, child: ViewController<*>) {
mergeBottomTabsOptions(options, child)
val tabIndex = bottomTabFinder.findByControllerId(child.id)
if (tabIndex >= 0) mergeDrawBehind(tabIndex)
}
private fun mergeBottomTabsOptions(options: Options, view: ViewController<*>) {
val bottomTabsOptions = options.bottomTabsOptions
if (bottomTabsOptions.layoutStyle == BottomTabsLayoutStyle.COMPACT) {
bottomTabs.layoutParams.width = WRAP_CONTENT
bottomTabs.refresh()
}
if (bottomTabsOptions.bottomMargin.hasValue()) {
val margin = extractBottomMarginPx(bottomTabsOptions.bottomMargin)
bottomTabsLayout.setBottomMargin(margin)
}
if (bottomTabsOptions.cornerRadius.hasValue()) {
val radius = extractCornerRadius(bottomTabsOptions.cornerRadius)
bottomTabsContainer.setRoundedCorners(radius)
}
// Keep this before the translucent check below
if (bottomTabsOptions.backgroundColor.hasValue()) {
bottomTabsContainer.setBackgroundColor(bottomTabsOptions.backgroundColor.get())
}
if (RNNFeatureToggles.isEnabled(TAB_BAR_TRANSLUCENCE)) {
if (bottomTabsOptions.translucent.isTrue) {
if (bottomTabsOptions.blurRadius.hasValue()) {
bottomTabsContainer.setBlurRadius(bottomTabsOptions.blurRadius.get().toFloat())
}
if (bottomTabsOptions.backgroundColor.hasValue()) {
bottomTabsContainer.setBlurColor(bottomTabsOptions.backgroundColor.get())
}
bottomTabsContainer.enableBackgroundBlur()
} else if (bottomTabsOptions.translucent.isFalse) {
bottomTabsContainer.disableBackgroundBlur()
}
}
if (bottomTabsOptions.elevation.hasValue()) {
bottomTabsContainer.setElevation(bottomTabsOptions.elevation)
}
if (options.layout.direction.hasValue()) bottomTabs.setLayoutDirection(options.layout.direction)
if (bottomTabsOptions.preferLargeIcons.hasValue()) bottomTabs.setPreferLargeIcons(bottomTabsOptions.preferLargeIcons.get())
if (bottomTabsOptions.titleDisplayMode.hasValue()) {
bottomTabs.setTitleState(bottomTabsOptions.titleDisplayMode.toState())
}
if (bottomTabsOptions.animateTabSelection.hasValue()) {
bottomTabs.setAnimateTabSelection(bottomTabsOptions.animateTabSelection.get())
}
if (bottomTabsOptions.currentTabIndex.hasValue()) {
val tabIndex = bottomTabsOptions.currentTabIndex.get()
if (tabIndex >= 0) tabSelector.selectTab(tabIndex)
}
if (bottomTabsOptions.testId.hasValue()) {
bottomTabs.tag = bottomTabsOptions.testId.get()
}
if (bottomTabsOptions.currentTabId.hasValue()) {
val tabIndex = bottomTabFinder.findByControllerId(bottomTabsOptions.currentTabId.get())
if (tabIndex >= 0) tabSelector.selectTab(tabIndex)
}
if (bottomTabsOptions.hideOnScroll.hasValue()) {
bottomTabs.setBehaviorTranslationEnabled(bottomTabsOptions.hideOnScroll.get())
}
if (bottomTabsOptions.borderColor.hasValue()) {
bottomTabsContainer.setTopOutLineColor(bottomTabsOptions.borderColor.get())
bottomTabsContainer.showTopLine()
}
if (bottomTabsOptions.borderWidth.hasValue()) {
bottomTabsContainer.setTopOutlineWidth(bottomTabsOptions.borderWidth.get().roundToInt())
bottomTabsContainer.showTopLine()
}
if (bottomTabsOptions.shadowOptions.hasValue()) {
if (bottomTabsOptions.shadowOptions.color.hasValue())
bottomTabsContainer.shadowColor = bottomTabsOptions.shadowOptions.color.get()
if (bottomTabsOptions.shadowOptions.radius.hasValue())
bottomTabsContainer.shadowRadius = bottomTabsOptions.shadowOptions.radius.get().toFloat()
if (bottomTabsOptions.shadowOptions.opacity.hasValue())
bottomTabsContainer.setShadowOpacity(bottomTabsOptions.shadowOptions.opacity.get().toFloat())
bottomTabsContainer.showShadow()
}
if (view.isViewShown) {
if (bottomTabsOptions.visible.isTrue) {
if (bottomTabsOptions.animate.isTrueOrUndefined) {
animator.show()
} else {
bottomTabs.restoreBottomNavigation(false)
}
bottomTabsContainer.revealTopOutline()
}
if (bottomTabsOptions.visible.isFalse) {
if (bottomTabsOptions.animate.isTrueOrUndefined) {
animator.hide()
} else {
bottomTabs.hideBottomNavigation(false)
}
bottomTabsContainer.hideTopOutLine()
}
}
}
private fun applyDrawBehind(@IntRange(from = 0) tabIndex: Int) {
tabs[tabIndex].applyBottomInset()
}
private fun mergeDrawBehind(tabIndex: Int) {
tabs[tabIndex].applyBottomInset()
}
private fun applyBottomTabsOptions(options: Options) {
val bottomTabsOptions = options.bottomTabsOptions
if (bottomTabsOptions.layoutStyle == BottomTabsLayoutStyle.COMPACT) {
bottomTabs.layoutParams.width = WRAP_CONTENT
}
if (bottomTabsOptions.bottomMargin.hasValue()) {
val margin = extractBottomMarginPx(bottomTabsOptions.bottomMargin)
bottomTabsLayout.setBottomMargin(margin)
}
if (bottomTabsOptions.cornerRadius.hasValue()) {
val radius = extractCornerRadius(bottomTabsOptions.cornerRadius)
bottomTabsContainer.setRoundedCorners(radius)
} else {
bottomTabsContainer.clearRoundedCorners()
}
if (RNNFeatureToggles.isEnabled(TAB_BAR_TRANSLUCENCE) && bottomTabsOptions.translucent.isTrue) {
if (bottomTabsOptions.blurRadius.hasValue()) {
bottomTabsContainer.setBlurRadius(bottomTabsOptions.blurRadius.get().toFloat())
}
if (bottomTabsOptions.backgroundColor.hasValue()) {
bottomTabsContainer.setBlurColor(bottomTabsOptions.backgroundColor.get())
}
bottomTabsContainer.enableBackgroundBlur()
} else {
bottomTabsContainer.disableBackgroundBlur()
bottomTabsContainer.setBackgroundColor(bottomTabsOptions.backgroundColor.get(Color.WHITE)!!)
}
bottomTabs.setLayoutDirection(options.layout.direction)
bottomTabs.setPreferLargeIcons(options.bottomTabsOptions.preferLargeIcons[false])
bottomTabs.setTitleState(bottomTabsOptions.titleDisplayMode[defaultTitleState])
bottomTabs.setAnimateTabSelection(bottomTabsOptions.animateTabSelection.get(true))
if (bottomTabsOptions.currentTabIndex.hasValue()) {
val tabIndex = bottomTabsOptions.currentTabIndex.get()
if (tabIndex >= 0) {
bottomTabsOptions.currentTabIndex.consume()
tabSelector.selectTab(tabIndex)
}
}
if (bottomTabsOptions.testId.hasValue()) bottomTabs.tag = bottomTabsOptions.testId.get()
if (bottomTabsOptions.currentTabId.hasValue()) {
val tabIndex = bottomTabFinder.findByControllerId(bottomTabsOptions.currentTabId.get())
if (tabIndex >= 0) {
bottomTabsOptions.currentTabId.consume()
tabSelector.selectTab(tabIndex)
}
}
if (bottomTabsOptions.visible.isTrueOrUndefined) {
if (bottomTabsOptions.animate.isTrueOrUndefined) {
animator.show()
} else {
bottomTabs.restoreBottomNavigation(false)
}
}
if (bottomTabsOptions.visible.isFalse) {
if (bottomTabsOptions.animate.isTrueOrUndefined) {
animator.hide()
} else {
bottomTabs.hideBottomNavigation(false)
}
}
if (bottomTabsOptions.elevation.hasValue()) {
bottomTabsContainer.setElevation(bottomTabsOptions.elevation)
}
when {
bottomTabsOptions.borderColor.hasValue() -> {
bottomTabsContainer.setTopOutLineColor(bottomTabsOptions.borderColor.get())
bottomTabsContainer.showTopLine()
}
bottomTabsOptions.borderWidth.hasValue() -> {
bottomTabsContainer.setTopOutlineWidth(bottomTabsOptions.borderWidth.get().roundToInt())
bottomTabsContainer.showTopLine()
}
else -> {
bottomTabsContainer.clearTopOutline()
}
}
if (bottomTabsOptions.shadowOptions.hasValue()) {
if (bottomTabsOptions.shadowOptions.color.hasValue())
bottomTabsContainer.shadowColor = bottomTabsOptions.shadowOptions.color.get()
if (bottomTabsOptions.shadowOptions.radius.hasValue())
bottomTabsContainer.shadowRadius = bottomTabsOptions.shadowOptions.radius.get().toFloat()
if (bottomTabsOptions.shadowOptions.opacity.hasValue())
bottomTabsContainer.setShadowOpacity(bottomTabsOptions.shadowOptions.opacity.get().toFloat())
bottomTabsContainer.showShadow()
} else {
bottomTabsContainer.clearShadow()
}
bottomTabs.setBehaviorTranslationEnabled(bottomTabsOptions.hideOnScroll[false])
}
fun applyBottomInset(bottomInset: Int) {
(bottomTabsContainer.layoutParams as ViewGroup.MarginLayoutParams).updateMargins(bottom = bottomInset)
bottomTabsContainer.requestLayout()
}
fun getBottomInset(resolvedOptions: Options): Int {
return if (resolvedOptions.withDefaultOptions(defaultOptions).bottomTabsOptions.isHiddenOrDrawBehind) 0 else bottomTabs.height
}
fun getPushAnimation(appearingOptions: Options): Animator? {
if (appearingOptions.bottomTabsOptions.animate.isFalse) return null
return animator.getPushAnimation(
appearingOptions.animations.push.bottomTabs,
appearingOptions.bottomTabsOptions.visible
)
}
fun getPopAnimation(appearingOptions: Options, disappearingOptions: Options): Animator? {
if (appearingOptions.bottomTabsOptions.animate.isFalse) return null
return animator.getPopAnimation(
disappearingOptions.animations.pop.bottomTabs,
appearingOptions.bottomTabsOptions.visible
)
}
fun getSetStackRootAnimation(appearingOptions: Options): Animator? {
if (appearingOptions.bottomTabsOptions.animate.isFalse) return null
return animator.getSetStackRootAnimation(
appearingOptions.animations.setStackRoot.bottomTabs,
appearingOptions.bottomTabsOptions.visible
)
}
fun findTabIndexByTabId(id: String?): Int {
val index = bottomTabFinder.findByControllerId(id)
return max(index, 0)
}
fun onConfigurationChanged(options: Options) {
val bottomTabsOptions = options.withDefaultOptions(defaultOptions).bottomTabsOptions
if (bottomTabsOptions.layoutStyle == BottomTabsLayoutStyle.COMPACT) {
bottomTabs.layoutParams.width = WRAP_CONTENT
}
if (bottomTabsOptions.bottomMargin.hasValue()) {
val margin = extractBottomMarginPx(bottomTabsOptions.bottomMargin)
bottomTabsLayout.setBottomMargin(margin)
}
if (bottomTabsOptions.cornerRadius.hasValue()) {
val radius = extractCornerRadius(bottomTabsOptions.cornerRadius)
bottomTabsContainer.setRoundedCorners(radius)
} else {
bottomTabsContainer.clearRoundedCorners()
}
if (RNNFeatureToggles.isEnabled(TAB_BAR_TRANSLUCENCE) && bottomTabsOptions.translucent.isTrue) {
if (bottomTabsOptions.blurRadius.hasValue()) {
bottomTabsContainer.setBlurRadius(bottomTabsOptions.blurRadius.get().toFloat())
}
if (bottomTabsOptions.backgroundColor.hasValue()) {
bottomTabsContainer.setBlurColor(bottomTabsOptions.backgroundColor.get())
}
bottomTabsContainer.enableBackgroundBlur()
} else {
bottomTabsContainer.disableBackgroundBlur()
// TODO Change to bottomTabsContainer.setBackgroundColor()?
bottomTabs.setBackgroundColor(bottomTabsOptions.backgroundColor.get(Color.WHITE)!!)
}
if (bottomTabsOptions.shadowOptions.hasValue()) {
if (bottomTabsOptions.shadowOptions.color.hasValue())
bottomTabsContainer.shadowColor = bottomTabsOptions.shadowOptions.color.get()
}
if (bottomTabsOptions.borderColor.hasValue()) {
bottomTabsContainer.setTopOutLineColor(bottomTabsOptions.borderColor.get())
bottomTabsContainer.showTopLine()
}
}
private fun extractCornerRadius(cornerRadius: Fraction): Float {
val radiusDp = cornerRadius.get()
val radius = UiUtils.dpToPx(bottomTabsContainer.context, radiusDp.toFloat())
return radius
}
private fun extractBottomMarginPx(bottomMargin: Fraction): Int {
val marginDp = bottomMargin.get()
val margin = UiUtils.dpToPx(bottomTabsContainer.context, marginDp.toFloat()).roundToInt()
return margin
}
}