1
1
package com.hypertrack.sdk.cordova.plugin
2
2
3
- import android.util.Log
4
3
import com.hypertrack.sdk.*
5
4
import com.hypertrack.sdk.cordova.plugin.common.*
6
- import com.hypertrack.sdk.cordova.plugin.common.HyperTrackSdkWrapper.initializeSdk
5
+
7
6
import com.hypertrack.sdk.cordova.plugin.common.Result
8
7
import com.hypertrack.sdk.cordova.plugin.common.Serialization.serializeIsAvailable
9
8
import com.hypertrack.sdk.cordova.plugin.common.Serialization.serializeIsTracking
9
+ import java.util.*
10
10
import org.apache.cordova.CallbackContext
11
11
import org.apache.cordova.CordovaPlugin
12
12
import org.apache.cordova.PluginResult
13
13
import org.json.JSONArray
14
14
import org.json.JSONObject
15
- import java.util.*
16
15
17
- class HyperTrackCordovaPlugin : CordovaPlugin () {
16
+ class HyperTrackCordovaPlugin : CordovaPlugin () {
18
17
19
18
private var isTrackingEventStream: CallbackContext ? = null
20
19
private var isAvailableEventStream: CallbackContext ? = null
@@ -33,7 +32,7 @@ class HyperTrackCordovaPlugin: CordovaPlugin() {
33
32
argsJson : JSONArray ,
34
33
callbackContext : CallbackContext
35
34
): Result <* > {
36
- return when (val method = SdkMethod .values().firstOrNull { it.name == methodName }) {
35
+ return when (val method = SdkMethod .values().firstOrNull { it.name == methodName }) {
37
36
SdkMethod .initialize -> {
38
37
withArgs<Map <String , Any ?>, Unit > (argsJson) { args ->
39
38
HyperTrackSdkWrapper .initializeSdk(args).mapSuccess {
@@ -83,23 +82,23 @@ class HyperTrackCordovaPlugin: CordovaPlugin() {
83
82
HyperTrackSdkWrapper .sync()
84
83
}
85
84
else -> {
86
- when (
85
+ when (
87
86
SubscriptionCall .values().firstOrNull {
88
87
it.name == methodName
89
88
}
90
89
) {
91
90
SubscriptionCall .subscribeToTracking -> {
92
91
isTrackingEventStream?.let { disposeCallback(it) }
93
92
isTrackingEventStream = callbackContext
94
- HyperTrackSdkWrapper .isTracking().mapSuccess {
93
+ HyperTrackSdkWrapper .isTracking().mapSuccess {
95
94
sendEvent(callbackContext, it)
96
95
NoCallback
97
96
}
98
97
}
99
98
SubscriptionCall .subscribeToAvailability -> {
100
99
isAvailableEventStream?.let { disposeCallback(it) }
101
100
isAvailableEventStream = callbackContext
102
- HyperTrackSdkWrapper .isAvailable().mapSuccess {
101
+ HyperTrackSdkWrapper .isAvailable().mapSuccess {
103
102
sendEvent(callbackContext, it)
104
103
NoCallback
105
104
}
@@ -129,7 +128,7 @@ class HyperTrackCordovaPlugin: CordovaPlugin() {
129
128
}
130
129
}
131
130
}
132
-
131
+
133
132
private fun sendEvent (callbackContext : CallbackContext , data : Any ) {
134
133
try {
135
134
when (data) {
@@ -164,8 +163,11 @@ class HyperTrackCordovaPlugin: CordovaPlugin() {
164
163
callbackContext.sendPluginResult(result)
165
164
}
166
165
167
- private inline fun <reified T , N > withArgs (args : JSONArray , crossinline sdkMethodCall : (T ) -> Result <N >): Result <N > {
168
- return when (T ::class ) {
166
+ private inline fun <reified T , N > withArgs (
167
+ args : JSONArray ,
168
+ crossinline sdkMethodCall : (T ) -> Result <N >
169
+ ): Result <N > {
170
+ return when (T ::class ) {
169
171
Map ::class -> {
170
172
try {
171
173
Success (args.getJSONObject(0 ))
@@ -232,9 +234,9 @@ class HyperTrackCordovaPlugin: CordovaPlugin() {
232
234
}
233
235
234
236
private fun <S > Result<S>.sendAsCallbackResult (callbackContext : CallbackContext ): Boolean {
235
- return when (this ) {
237
+ return when (this ) {
236
238
is Success -> {
237
- when (val success = this .success) {
239
+ when (val success = this .success) {
238
240
is Map <* , * > -> {
239
241
callbackContext.success(JSONObject (success))
240
242
true
@@ -251,7 +253,9 @@ private fun <S> Result<S>.sendAsCallbackResult(callbackContext: CallbackContext)
251
253
true
252
254
}
253
255
else -> {
254
- callbackContext.failure(IllegalArgumentException (" Invalid response ${this .success} " ))
256
+ callbackContext.failure(
257
+ IllegalArgumentException (" Invalid response ${this .success} " )
258
+ )
255
259
false
256
260
}
257
261
}
@@ -265,7 +269,7 @@ private fun <S> Result<S>.sendAsCallbackResult(callbackContext: CallbackContext)
265
269
266
270
private fun JSONObject.toMap (): Map <String , Any ?> {
267
271
return keys().asSequence().associateWith { key ->
268
- when (val value = this .get(key)) {
272
+ when (val value = this .get(key)) {
269
273
is Boolean ,
270
274
is String ,
271
275
is Double ,
0 commit comments