-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectionsServiceModule.java
166 lines (146 loc) · 5.69 KB
/
DirectionsServiceModule.java
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
package com.mapsindoorsrn.core;
import androidx.annotation.NonNull;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.ReadableArray;
import com.google.gson.Gson;
import com.mapsindoors.core.MPDirectionsService;
import com.mapsindoors.core.MPPoint;
import com.mapsindoors.core.errors.MapsIndoorsException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
public class DirectionsServiceModule extends ReactContextBaseJavaModule {
private HashMap<String, MPDirectionsService> serviceMap = new HashMap<>();
private static final String NO_DS = "This DirectionsService is not available";
private final Gson gson = new Gson();
public DirectionsServiceModule(@NonNull ReactApplicationContext reactContext) {
super(reactContext);
}
@ReactMethod
public void create(final Promise promise) {
MPDirectionsService service = new MPDirectionsService();
String uuid = UUID.randomUUID().toString();
serviceMap.put(uuid, service);
promise.resolve(uuid);
}
@ReactMethod
public void addAvoidWayType(String wayType, final String id, final Promise promise) {
MPDirectionsService service = serviceMap.get(id);
if (service != null) {
service.addAvoidWayType(wayType);
promise.resolve(null);
} else {
promise.reject(NO_DS,new MapsIndoorsException((NO_DS)));
}
}
@ReactMethod
public void clearWayType(final String id, final Promise promise) {
MPDirectionsService service = serviceMap.get(id);
if (service != null) {
service.clearAvoidWayType();
promise.resolve(null);
} else {
promise.reject(NO_DS,new MapsIndoorsException((NO_DS)));
}
}
@ReactMethod
public void addExcludeWayType(String wayType, final String id, final Promise promise) {
MPDirectionsService service = serviceMap.get(id);
if (service != null) {
service.addExcludeWayType(wayType);
promise.resolve(null);
} else {
promise.reject(NO_DS,new MapsIndoorsException((NO_DS)));
}
}
@ReactMethod
public void clearExcludeWayType(final String id, final Promise promise) {
MPDirectionsService service = serviceMap.get(id);
if (service != null) {
service.clearExcludeWayType();
promise.resolve(null);
} else {
promise.reject(NO_DS,new MapsIndoorsException((NO_DS)));
}
}
@ReactMethod
public void setIsDeparture(boolean isDeparture, final String id, final Promise promise) {
MPDirectionsService service = serviceMap.get(id);
if (service != null) {
service.setIsDeparture(isDeparture);
promise.resolve(null);
} else {
promise.reject(NO_DS,new MapsIndoorsException((NO_DS)));
}
}
@ReactMethod
public void getRoute(String originString, String destinationString, ReadableArray stops, boolean optimized, final String id, final Promise promise) {
MPDirectionsService service = serviceMap.get(id);
if (service != null) {
service.setRouteResultListener((route, error) -> {
WritableMap map = Arguments.createMap();
if (route != null) {
map.putString("route", gson.toJson(route));
}
if (error != null) {
map.putString("error", gson.toJson(error));
}
promise.resolve(map);
});
MPPoint origin = gson.fromJson(originString, MPPoint.class);
MPPoint destination = gson.fromJson(destinationString, MPPoint.class);
List<MPPoint> points = new ArrayList<>();
if (stops != null) {
for (int i = 0; i < stops.size(); i++) {
points.add(gson.fromJson(stops.getString(i), MPPoint.class));
}
}
if (origin != null && destination != null) {
if (!points.isEmpty()) {
service.query(origin, destination, points, optimized);
} else {
service.query(origin, destination);
}
} else {
promise.reject(new MapsIndoorsException("Origin:" + originString +
", or Destination:" + destinationString + ", are not parsable"));
}
} else {
promise.reject(NO_DS,new MapsIndoorsException((NO_DS)));
}
}
@ReactMethod
public void setTravelMode(String travelMode, final String id, final Promise promise) {
MPDirectionsService service = serviceMap.get(id);
if (service != null) {
service.setTravelMode(travelMode);
promise.resolve(null);
} else {
promise.reject(NO_DS,new MapsIndoorsException((NO_DS)));
}
}
@ReactMethod
public void setTime(int time, final String id, final Promise promise) {
MPDirectionsService service = serviceMap.get(id);
if (service != null) {
Date date = new Date();
date.setTime(time);
service.setTime(date);
promise.resolve(null);
} else {
promise.reject(NO_DS,new MapsIndoorsException((NO_DS)));
}
}
@NonNull
@Override
public String getName() {
return "DirectionsService";
}
}