-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.android.js
118 lines (100 loc) · 3.04 KB
/
index.android.js
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
/**
* @providesModule SnackbarAndroid
*/
'use strict';
/**
* This exposes the native SnackbarAndroid module in JS.
*/
import { DeviceEventEmitter, NativeModules, processColor } from 'react-native';
var NativeSnackbar = NativeModules.SnackbarAndroid;
// List of registered event listeners
var eventListeners = {}
// Map of event names for consumers to names delivered by native module
var eventListenerMap = {
hide: NativeSnackbar.EVENT_HIDE,
hidden: NativeSnackbar.EVENT_HIDDEN,
show: NativeSnackbar.EVENT_SHOW,
shown: NativeSnackbar.EVENT_SHOWN
}
var SnackbarAndroid = {
SHORT: NativeSnackbar.SHORT,
LONG: NativeSnackbar.LONG,
INDEFINITE: NativeSnackbar.INDEFINITE,
ANIMATION_DURATION: NativeSnackbar.ANIMATION_DURATION,
ANIMATION_FADE_DURATION: NativeSnackbar.ANIMATION_FADE_DURATION,
UNTIL_CLICK: 42,
show: function (
message: string,
options: {
duration: number,
actionColor: string,
actionLabel: string,
actionCallback: Function,
}
): void {
var hideOnClick = false;
if (options == null) {
options = {};
}
var label, callback;
if (options.actionLabel && options.actionCallback) {
if (options.duration == null) {
options.duration = this.INDEFINITE;
}
label = options.actionLabel;
callback = options.actionCallback;
}
if (options.duration == null) {
options.duration = this.SHORT;
}
else if (options.duration == this.UNTIL_CLICK) {
options.duration = this.INDEFINITE;
hideOnClick = true;
}
if (options.actionColor == null) {
options.actionColor = '#EEFF41';
}
var color = processColor(options.actionColor);
this.snackbar = NativeSnackbar.show(
message,
options.duration,
hideOnClick,
color,
label,
callback);
},
dismiss: function(): void {
NativeSnackbar.dismiss();
},
/**
* Add an event listener for the specified event type
*
* @param {String} eventType one of 'hide', 'show', 'hidden' or 'shown'
* @param {Function} callback callback to execute when event is received
*/
addEventListener(eventType: string, callback: Function): void {
if (eventType in eventListenerMap) {
eventListeners[callback] = DeviceEventEmitter.addListener(
eventListenerMap[eventType],
callback
);
}
},
/**
* Removes a previously registered event listener for the specified type.
*
* @param {String} eventType type of event that callback was registered to
* @param {Function} callback listener to remove. Must be the same object
* reference as the function that was originally
* passed to addEventListener()
*/
removeEventListener(eventType: string, callback: Function): void {
if (eventType in eventListenerMap) {
if (callback in eventListeners) {
DeviceEventEmitter.removeSubscription(eventListeners[callback]);
delete eventListeners[callback];
}
}
}
};
module.exports = SnackbarAndroid;