-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathSocketIoReadableNativeMap.java
More file actions
97 lines (86 loc) · 3.39 KB
/
SocketIoReadableNativeMap.java
File metadata and controls
97 lines (86 loc) · 3.39 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
package com.gcrabtree.rctsocketio;
import android.util.Log;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableNativeMap;
import com.facebook.jni.HybridData;
import java.util.HashMap;
import io.socket.client.IO;
/**
* Created by Greg Crabtree on 5/17/16.
*/
public class SocketIoReadableNativeMap extends ReadableNativeMap {
private static final String TAG = "SIOReadableNativeMap";
protected SocketIoReadableNativeMap(HybridData hybridData) {
super(hybridData);
}
/**
* Note: This will only be necessary until RN version 0.26 goes live
* It will be deprecated from the project, as this is just included in that version of RN.
*
* This converts the SocketIoReadableNativeMap to a Java usable HashMap.
* @return converted HashMap.
*/
public static HashMap<String, Object> toHashMap(ReadableNativeMap map) {
ReadableMapKeySetIterator iterator = map.keySetIterator();
HashMap<String, Object> hashMap = new HashMap<>();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
switch (map.getType(key)) {
case Null:
hashMap.put(key, null);
break;
case Boolean:
hashMap.put(key, map.getBoolean(key));
break;
case Number:
hashMap.put(key, map.getDouble(key));
break;
case String:
hashMap.put(key, map.getString(key));
break;
case Map:
hashMap.put(key, toHashMap(map.getMap(key)));
break;
case Array:
hashMap.put(key, ((SocketIoReadableNativeArray) map.getArray(key)).toArrayList());
break;
default:
throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
}
}
return hashMap;
}
/**
* This converts a SocketIoReadableNativeMap to a SocketIO Option object.
* @param options ReadableNativeMap that is a JS bridged hash of options.
* @return IO.Options object that has been populated. Currently incomplete. PRs welcome.
*/
public static IO.Options mapToOptions(ReadableNativeMap options) {
ReadableMapKeySetIterator iterator = options.keySetIterator();
IO.Options opts = new IO.Options();
while (iterator.hasNextKey()) {
String key = iterator.nextKey().toLowerCase();
switch (key) {
case "force new connection":
case "forcenew":
opts.forceNew = options.getBoolean(key);
break;
case "multiplex":
opts.multiplex = options.getBoolean(key);
break;
case "reconnection":
opts.reconnection = options.getBoolean(key);
break;
case "connect_timeout":
opts.timeout = options.getInt(key);
break;
case "path":
opts.path = options.getString(key);
break;
default:
Log.e(TAG, "Could not convert object with key: " + key + ".");
}
}
return opts;
}
}