-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathBottomTabs.java
More file actions
137 lines (114 loc) · 4.18 KB
/
BottomTabs.java
File metadata and controls
137 lines (114 loc) · 4.18 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
package com.reactnativenavigation.views.bottomtabs;
import static com.reactnativenavigation.utils.CollectionUtils.forEach;
import static com.reactnativenavigation.utils.ViewUtils.findChildByClass;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.IntRange;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
import com.reactnativenavigation.R;
import com.reactnativenavigation.options.LayoutDirection;
import java.util.ArrayList;
import java.util.List;
@SuppressLint("ViewConstructor")
public class BottomTabs extends AHBottomNavigation {
private boolean itemsCreationEnabled = true;
private boolean shouldCreateItems = true;
private List<Runnable> onItemCreationEnabled = new ArrayList<>();
public BottomTabs(Context context) {
super(context);
setId(R.id.bottomTabs);
setDefaultBackgroundColor(Color.TRANSPARENT);
}
public void disableItemsCreation() {
itemsCreationEnabled = false;
}
public void enableItemsCreation() {
itemsCreationEnabled = true;
if (shouldCreateItems) {
shouldCreateItems = false;
createItems();
forEach(onItemCreationEnabled, Runnable::run);
onItemCreationEnabled.clear();
}
}
@Override
public void onSizeChanged(int w, int h, int oldw, int oldh) {
if (hasItemsAndIsMeasured(w, h, oldw, oldh)) createItems();
}
@Override
protected void createItems() {
if (itemsCreationEnabled) {
superCreateItems();
} else {
shouldCreateItems = true;
}
}
// TODO Find a better way to do this
public void superCreateItems() {
super.createItems();
}
@Override
public void setCurrentItem(@IntRange(from = 0) int position) {
setCurrentItem(position, true);
}
@Override
public void setCurrentItem(@IntRange(from = 0) int position, boolean useCallback) {
if (itemsCreationEnabled) {
super.setCurrentItem(position, useCallback);
} else {
onItemCreationEnabled.add(() -> super.setCurrentItem(position, useCallback));
}
}
@Override
public void setTitleState(TitleState titleState) {
if (getTitleState() != titleState) super.setTitleState(titleState);
}
@Override
public void setBackgroundColor(int color) {
super.setBackgroundColor(color);
if (getDefaultBackgroundColor() != color) setDefaultBackgroundColor(color);
}
@Override
public void restoreBottomNavigation(boolean withAnimation) {
super.restoreBottomNavigation(withAnimation);
if (!withAnimation) setVisibility(View.VISIBLE);
}
@Override
public void hideBottomNavigation(boolean withAnimation) {
super.hideBottomNavigation(withAnimation);
if (!withAnimation) setVisibility(View.GONE);
}
public void setText(int index, String text) {
AHBottomNavigationItem item = getItem(index);
if (!item.getTitle(getContext()).equals(text)) {
item.setTitle(text);
refresh();
}
}
public void setIcon(int index, Drawable icon) {
AHBottomNavigationItem item = getItem(index);
if (!item.getDrawable(getContext()).equals(icon)) {
item.setIcon(icon);
refresh();
}
}
public void setSelectedIcon(int index, Drawable icon) {
AHBottomNavigationItem item = getItem(index);
if (!item.getDrawable(getContext()).equals(icon)) {
item.setSelectedIcon(icon);
refresh();
}
}
public void setLayoutDirection(LayoutDirection direction) {
LinearLayout tabsContainer = findChildByClass(this, LinearLayout.class);
if (tabsContainer != null) tabsContainer.setLayoutDirection(direction.get());
}
private boolean hasItemsAndIsMeasured(int w, int h, int oldw, int oldh) {
return w != 0 && h != 0 && (w != oldw || h != oldh) && getItemsCount() > 0;
}
}