-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathRNTooltipsModule.java
More file actions
142 lines (119 loc) · 4.99 KB
/
RNTooltipsModule.java
File metadata and controls
142 lines (119 loc) · 4.99 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
package px.tooltips;
import android.app.Activity;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.UIBlock;
import com.facebook.react.uimanager.NativeViewHierarchyManager;
import com.github.florent37.viewtooltip.ViewTooltip;
public class RNTooltipsModule extends ReactContextBaseJavaModule {
private final ReactApplicationContext reactContext;
private Callback _onHide = null;
private ViewTooltip tooltip;
public RNTooltipsModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@Override
public String getName() {
return "RNTooltips";
}
@ReactMethod
public void Show(final int targetId, final int parentId, final ReadableMap props, final Callback onHide) {
this.reactContext.getNativeModule(UIManagerModule.class).prependUIBlock(new UIBlock() {
@Override
public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
final ViewGroup target = (ViewGroup) nativeViewHierarchyManager.resolveView(targetId);
reactContext.runOnUiQueueThread(new Runnable() {
@Override
public void run() {
if (target == null) {
// it is possible that target end up being NULL
// when findNodeHandle returns the wrong tag, findViewById won't be able to retrieve the view
// there was an issue opened related to this problem 2 years ago, but it has never been fixed
// https://github.com/facebook/react-native/issues/10385
return;
}
_onHide = onHide;
String text = props.getString("text");
int position = props.getInt("position");
int align = props.getInt("align");
boolean autoHide = props.getBoolean("autoHide");
int duration = props.getInt("duration");
boolean clickToHide = props.getBoolean("clickToHide");
int corner = props.getInt("corner");
String tintColor = props.getString("tintColor");
String textColor = props.getString("textColor");
int textSize = props.getInt("textSize");
int gravity = props.getInt("gravity");
boolean arrow = props.getBoolean("arrow");
boolean shadow = props.getBoolean("shadow");
// parent reference is not required
// ViewTooltip.on can retrieve the parent Context by itself
tooltip = ViewTooltip.on(target);
tooltip = tooltip.text(text);
if (!arrow) {
tooltip.arrowHeight(0);
tooltip.arrowWidth(0);
}
if (position == 1) {
tooltip = tooltip.position(ViewTooltip.Position.LEFT);
} else if (position == 2) {
tooltip = tooltip.position(ViewTooltip.Position.RIGHT);
} else if (position == 3) {
tooltip = tooltip.position(ViewTooltip.Position.TOP);
} else if (position == 4) {
tooltip = tooltip.position(ViewTooltip.Position.BOTTOM);
}
if (align == 1) {
tooltip = tooltip.align(ViewTooltip.ALIGN.START);
} else if (align == 2) {
tooltip = tooltip.align(ViewTooltip.ALIGN.CENTER);
} else if (align == 3) {
tooltip = tooltip.align(ViewTooltip.ALIGN.END);
}
tooltip = tooltip.autoHide(autoHide, duration);
tooltip = tooltip.clickToHide(clickToHide);
tooltip = tooltip.corner(corner);
tooltip = tooltip.color(Color.parseColor(tintColor));
tooltip = tooltip.textColor(Color.parseColor(textColor));
tooltip = tooltip.textSize(TypedValue.COMPLEX_UNIT_SP, textSize);
tooltip = tooltip.setTextGravity(gravity);
tooltip = tooltip.withShadow(shadow);
tooltip.onHide(new ViewTooltip.ListenerHide() {
@Override
public void onHide(View view) {
if (_onHide != null) {
_onHide.invoke();
_onHide = null;
}
return;
}
});
tooltip.show();
}
});
}
});
}
@ReactMethod
public void Dismiss(final int view) {
reactContext.runOnUiQueueThread(new Runnable() {
@Override
public void run() {
if (tooltip == null) {
return;
}
tooltip.close();
tooltip = null;
}
});
}
}