-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhypertrack.dart
322 lines (285 loc) · 11.6 KB
/
hypertrack.dart
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import 'package:flutter/services.dart';
import 'package:hypertrack_plugin/data_types/json.dart';
import 'package:hypertrack_plugin/data_types/location_error.dart';
import 'package:hypertrack_plugin/data_types/location_with_deviation.dart';
import 'package:hypertrack_plugin/data_types/result.dart';
import 'package:hypertrack_plugin/src/sdk_method.dart';
import 'package:hypertrack_plugin/src/serialization.dart';
import 'data_types/hypertrack_error.dart';
import 'data_types/location.dart';
import 'data_types/order.dart';
import 'data_types/order_status.dart';
/// This plugin allows you to use HyperTrack SDK for Flutter apps
class HyperTrack {
/// Adds a new geotag. Check [Shift tracking](https://hypertrack.com/docs/shift-tracking) and [Clock In/Out tagging](https://hypertrack.com/docs/clock-inout-tracking) docs to learn how to use Order handle and Order status params.
/// Accepts:
/// - orderHandle - Order handle.
/// - orderStatus - Order status.
/// - [data] - Geotag data JSON.
/// Returns current location if success or [LocationError] if failure.
static Future<Result<Location, LocationError>> addGeotag(
String orderHandle, OrderStatus orderStatus, JSONObject data) {
return _invokeSdkMethod<Map<Object?, Object?>>(SdkMethod.addGeotag,
serializeGeotagData(orderHandle, orderStatus, data, null))
.then((value) {
return deserializeLocationResult(value);
});
}
/// Adds a new geotag with expected location. Check [Shift tracking](https://hypertrack.com/docs/shift-tracking) doc to learn how to use Order handle and Order status params.
/// Accepts:
/// - orderHandle - Order handle.
/// - orderStatus - Order status.
/// - [data] - Geotag data JSON.
/// - [expectedLocation] - Expected location.
/// Returns current location with deviation from expected location if success
/// or [LocationError] if failure.
static Future<Result<LocationWithDeviation, LocationError>>
addGeotagWithExpectedLocation(String orderHandle, OrderStatus orderStatus,
JSONObject data, Location expectedLocation) {
return _invokeSdkMethod<Map<Object?, Object?>>(
SdkMethod.addGeotag,
serializeGeotagData(
orderHandle, orderStatus, data, expectedLocation))
.then((value) {
return deserializeLocationWithDeviationResult(value);
});
}
/// Deprecated. Use addGeotag with orderHandle and orderStatus instead.
static Future<Result<Location, LocationError>> addGeotagDeprecated(
JSONObject data) {
return _invokeSdkMethod<Map<Object?, Object?>>(
SdkMethod.addGeotag, serializeGeotagData(null, null, data, null))
.then((value) {
return deserializeLocationResult(value);
});
}
/// Deprecated. Use addGeotagWithExpectedLocation with orderHandle and orderStatus instead.
static Future<Result<LocationWithDeviation, LocationError>>
addGeotagWithExpectedLocationDeprecated(
JSONObject data, Location expectedLocation) {
return _invokeSdkMethod<Map<Object?, Object?>>(SdkMethod.addGeotag,
serializeGeotagData(null, null, data, expectedLocation))
.then((value) {
return deserializeLocationWithDeviationResult(value);
});
}
/// If disallowed, the HyperTrack platform will display and outage if mocked location is detected
static Future<bool> get allowMockLocation async {
return _invokeSdkMethod<Map<Object?, Object?>>(SdkMethod.getAllowMockLocation)
.then((value) {
return deserializeAllowMockLocation(value);
});
}
/// Returns a string that is used to uniquely identify the device.
static Future<String> get deviceId async {
return _invokeSdkMethod<Map<Object?, Object?>>(SdkMethod.getDeviceID)
.then((value) {
return deserializeDeviceId(value);
});
}
/// Returns a list of errors that blocks the SDK from tracking.
static Future<Set<HyperTrackError>> get errors async {
return _invokeSdkMethod<List<Object?>>(SdkMethod.getErrors).then((value) {
return deserializeErrors(value.cast<Map<Object?, Object?>>());
});
}
/// Reflects availability of the device for the Nearby search.
static Future<bool> get isAvailable async {
return _invokeSdkMethod<Map<Object?, Object?>>(SdkMethod.getIsAvailable)
.then((value) {
return deserializeIsAvailable(value);
});
}
/// Reflects the tracking intent for the device.
static Future<bool> get isTracking async {
return _invokeSdkMethod<Map<Object?, Object?>>(SdkMethod.getIsTracking)
.then((value) {
return deserializeIsTracking(value);
});
}
/// Reflects the current location of the user or an outage reason.
static Future<Result<Location, LocationError>> get location async {
return _invokeSdkMethod<Map<Object?, Object?>>(SdkMethod.getLocation)
.then((value) {
return deserializeLocationResult(value);
});
}
/// Gets the metadata that is set for the device.
static Future<JSONObject> get metadata async {
return _invokeSdkMethod<Map<Object?, Object?>>(SdkMethod.getMetadata)
.then((value) {
return deserializeMetadata(value);
});
}
/// Gets the name that is set for the device.
static Future<String> get name async {
return _invokeSdkMethod<Map<Object?, Object?>>(SdkMethod.getName)
.then((value) {
return deserializeName(value);
});
}
/// Gets the active orders for the worker. The orders are sorted with the
/// ordering in which the worker should complete them.
static Future<Map<String, Order>> get orders async {
return _invokeSdkMethod<Map<Object?, Object?>>(SdkMethod.getOrders)
.then((value) {
return deserializeOrders(
value,
(String orderHandle) {
return _invokeSdkMethod<Map<Object?, Object?>>(
SdkMethod.getOrderIsInsideGeofence,
serializeOrderHandle(orderHandle));
}
);
});
}
/// A primary identifier that uniquely identifies the worker outside of
/// HyperTrack.
/// Example: email, phone number, database id
/// It is usually obtained and set when the worker logs into the app.
/// Set it to an empty string "" when the worker logs out of the app to
/// un-bind the device from the worker and avoid unintentional tracking.
static Future<String> get workerHandle async {
return _invokeSdkMethod<Map<Object?, Object?>>(SdkMethod.getWorkerHandle)
.then((value) {
return deserializeWorkerHandle(value);
});
}
/// Requests one-time location update and returns the [Location] once it is
/// available, or [LocationError].
static Stream<Result<Location, Set<HyperTrackError>>> locate() {
return _locateChannel.receiveBroadcastStream().map((event) {
return deserializeLocateResult(event);
});
}
/// Allows mocking location data.
///
/// Check the [Test with mock locations](https://hypertrack.com/docs/mock-location)
/// guide for more information.
///
/// To avoid issues related to race conditions in your code use this API **only if**
/// modifying the compiled `HyperTrackAllowMockLocation` AndroidManifest.xml/Info.plist
/// value is insufficient for your needs.
/// Example: if for some reason you aren't able to recompile with `HyperTrackAllowMockLocation`
/// set to `YES`/`true` for your prod app QA mock location tests and need to set
/// up the value in runtime.
static void setAllowMockLocation(bool allow) {
_invokeSdkVoidMethod(
SdkMethod.setAllowMockLocation, serializeAllowMockLocation(allow));
}
/// Sets the availability of the device for the Nearby search.
static void setIsAvailable(bool available) {
_invokeSdkVoidMethod(
SdkMethod.setIsAvailable, serializeIsAvailable(available));
}
/// Sets the tracking intent for the device.
static void setIsTracking(bool tracking) {
_invokeSdkVoidMethod(
SdkMethod.setIsTracking, serializeIsTracking(tracking));
}
/// Sets the metadata for the device.
static void setMetadata(JSONObject data) {
_invokeSdkVoidMethod(SdkMethod.setMetadata, serializeMetadata(data));
}
/// Sets the name for the device.
static void setName(String name) {
_invokeSdkVoidMethod(SdkMethod.setName, serializeName(name));
}
/// A primary identifier that uniquely identifies the worker outside of HyperTrack.
/// Example: email, phone number, database id
/// It is usually obtained and set when the worker logs into the app.
/// Set it to an empty string "" when the worker logs out of the app to un-bind the device from the worker and
/// avoid unintentional tracking.
static void setWorkerHandle(String workerHandle) {
_invokeSdkVoidMethod(
SdkMethod.setWorkerHandle, serializeWorkerHandle(workerHandle));
}
/// Subscribe to tracking errors.
static Stream<Set<HyperTrackError>> get errorsSubscription {
return _errorsChannel.receiveBroadcastStream().map((event) {
return deserializeErrors(event.cast<Map<Object?, Object?>>());
});
}
/// Subscribe to availability changes.
static Stream<bool> get isAvailableSubscription {
return _isAvailableChannel.receiveBroadcastStream().map((event) {
return deserializeIsAvailable(event);
});
}
/// Subscribe to tracking intent changes.
static Stream<bool> get isTrackingSubscription {
return _isTrackingChannel.receiveBroadcastStream().map((event) {
return deserializeIsTracking(event);
});
}
/// Subscribe to location changes.
static Stream<Result<Location, LocationError>> get locationSubscription {
return _locationChannel.receiveBroadcastStream().map((event) {
return deserializeLocationResult(event);
});
}
/// Subscribe to changes in the orders assigned to the worker
static Stream<Map<String, Order>> get ordersSubscription {
return _ordersChannel.receiveBroadcastStream().map((event) {
return deserializeOrders(
event,
(String orderHandle) {
return _invokeSdkMethod<Map<Object?, Object?>>(
SdkMethod.getOrderIsInsideGeofence,
serializeOrderHandle(orderHandle));
}
);
});
}
/// @nodoc
@override
String toString() {
return super.toString();
}
/// @nodoc
@override
dynamic noSuchMethod(Invocation invocation) {
return super.noSuchMethod(invocation);
}
/// @nodoc
@override
bool operator ==(Object other) {
return super == other;
}
/// @nodoc
@override
int get hashCode {
return super.hashCode;
}
/// @nodoc
@override
Type get runtimeType {
return super.runtimeType;
}
}
const _channelPrefix = 'sdk.hypertrack.com';
// channel for invoking SDK methods
const _methodChannel = MethodChannel('$_channelPrefix/methods');
// channels for receiving events from the SDK
const EventChannel _errorsChannel = EventChannel('$_channelPrefix/errors');
const EventChannel _isAvailableChannel =
EventChannel('$_channelPrefix/isAvailable');
const EventChannel _isTrackingChannel =
EventChannel('$_channelPrefix/isTracking');
const EventChannel _locationChannel = EventChannel('$_channelPrefix/location');
const EventChannel _locateChannel = EventChannel('$_channelPrefix/locate');
const EventChannel _ordersChannel = EventChannel('$_channelPrefix/orders');
Future<T> _invokeSdkMethod<T>(SdkMethod method,
[Map<Object?, Object?>? arguments]) {
return _methodChannel.invokeMethod(method.name, arguments).then((value) {
if (value == null) {
throw Exception("Unexpected null response for ${method.toString()}");
} else {
return value;
}
});
}
// we omit nullability check as void is encoded as null on the native side
Future<void> _invokeSdkVoidMethod(SdkMethod method, [dynamic arguments]) {
return _methodChannel.invokeMethod(method.name, arguments);
}