From cec4d91d53eb7890438fe425d80da4039ea5dcd3 Mon Sep 17 00:00:00 2001 From: pavel-kuznetsov-hypertrack <78730314+pavel-kuznetsov-hypertrack@users.noreply.github.com> Date: Tue, 3 Sep 2024 11:09:15 +0300 Subject: [PATCH] Fix deserialization (#112) --- CHANGELOG.md | 7 + docs/classes/HyperTrack.html | 4 +- docs/enums/HyperTrackError.html | 4 +- docs/index.html | 4 +- docs/modules.html | 4 +- docs/types/Errors.html | 2 +- docs/types/Failure.html | 2 +- docs/types/Location.html | 2 +- docs/types/LocationError.html | 2 +- docs/types/LocationWithDeviation.html | 2 +- docs/types/NotRunning.html | 2 +- docs/types/Order.html | 2 +- docs/types/OrderStatus.html | 2 +- docs/types/Result.html | 2 +- docs/types/Starting.html | 2 +- docs/types/Success.html | 2 +- justfile | 3 +- .../package.json | 3 +- .../package.json | 3 +- .../package.json | 3 +- .../HyperTrackReactNativePlugin.kt | 77 ++++---- .../HyperTrackSdkPackage.kt | 9 +- .../ReactNativeSerialization.kt | 5 +- .../common/HyperTrackSdkWrapper.kt | 107 +++++------ .../common/Serialization.kt | 172 +++++++----------- .../common/WrapperResult.kt | 28 +-- sdk/package.json | 2 +- sdk/src/HyperTrack/HyperTrack.ts | 9 +- .../data_types/internal/IsInsideGeofence.ts | 4 + .../data_types/internal/OrderInternal.ts | 3 +- 30 files changed, 215 insertions(+), 258 deletions(-) create mode 100644 sdk/src/HyperTrack/data_types/internal/IsInsideGeofence.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c305111..b6467c13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [13.5.1] - 2024-09-02 + +### Fixed + +- Serialization issue with `Order.isInsideGeofence` + ## [13.5.0] - 2024-08-21 ### Added @@ -908,3 +914,4 @@ Initial release. [13.3.5]: https://github.com/hypertrack/sdk-react-native/releases/tag/13.3.5 [13.4.0]: https://github.com/hypertrack/sdk-react-native/releases/tag/13.4.0 [13.5.0]: https://github.com/hypertrack/sdk-react-native/releases/tag/13.5.0 +[13.5.1]: https://github.com/hypertrack/sdk-react-native/releases/tag/13.5.1 diff --git a/docs/classes/HyperTrack.html b/docs/classes/HyperTrack.html index 9bf71000..6984f48a 100644 --- a/docs/classes/HyperTrack.html +++ b/docs/classes/HyperTrack.html @@ -1,4 +1,4 @@ -HyperTrack | HyperTrack React Native SDK API - v13.5.0

Constructors

constructor +HyperTrack | HyperTrack React Native SDK API - v13.5.1

Constructors

Properties

Methods

addGeotag getDeviceId @@ -102,4 +102,4 @@

Parameters

  • listener: ((orders) => void)
      • (orders): void
      • Parameters

        • orders: Map<string, Order>

        Returns void

Returns EmitterSubscription

EmitterSubscription

Example

const subscription = HyperTrack.subscribeToOrders(orders => {
...
})

// later, to stop listening
subscription.remove()
-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/enums/HyperTrackError.html b/docs/enums/HyperTrackError.html index d130362c..9a7109e0 100644 --- a/docs/enums/HyperTrackError.html +++ b/docs/enums/HyperTrackError.html @@ -1,4 +1,4 @@ -HyperTrackError | HyperTrack React Native SDK API - v13.5.0

Enumeration Members

blockedFromRunning +HyperTrackError | HyperTrack React Native SDK API - v13.5.1

Enumeration Members

permissionsLocationRestricted: "permissions.location.restricted"

[iOS only] The app is not authorized to use location services.

permissionsNotificationsDenied: "permissions.notifications.denied"

[Android only] The user denied notification permissions needed to display a persistent notification needed for foreground location tracking.

-
\ No newline at end of file +
\ No newline at end of file diff --git a/docs/index.html b/docs/index.html index 5d1ea5cc..b33d5ac3 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,2 +1,2 @@ -HyperTrack React Native SDK API - v13.5.0

HyperTrack React Native SDK API - v13.5.0

hypertrack-sdk-react-native

The main package for HyperTrack SDK React Native.

-
\ No newline at end of file +HyperTrack React Native SDK API - v13.5.1

HyperTrack React Native SDK API - v13.5.1

hypertrack-sdk-react-native

The main package for HyperTrack SDK React Native.

+
\ No newline at end of file diff --git a/docs/modules.html b/docs/modules.html index c980b858..622a1411 100644 --- a/docs/modules.html +++ b/docs/modules.html @@ -1,4 +1,4 @@ -HyperTrack React Native SDK API - v13.5.0

HyperTrack React Native SDK API - v13.5.0

References

default +HyperTrack React Native SDK API - v13.5.1

HyperTrack React Native SDK API - v13.5.1

References

Enumerations

Classes

Type Aliases

Location @@ -12,4 +12,4 @@ Result NotRunning Starting -

References

Renames and re-exports HyperTrack
\ No newline at end of file +

References

Renames and re-exports HyperTrack
\ No newline at end of file diff --git a/docs/types/Errors.html b/docs/types/Errors.html index 88568b59..d14047e9 100644 --- a/docs/types/Errors.html +++ b/docs/types/Errors.html @@ -1 +1 @@ -Errors | HyperTrack React Native SDK API - v13.5.0
Errors: {
    type: "errors";
    value: HyperTrackError[];
}

Type declaration

\ No newline at end of file +Errors | HyperTrack React Native SDK API - v13.5.1
Errors: {
    type: "errors";
    value: HyperTrackError[];
}

Type declaration

\ No newline at end of file diff --git a/docs/types/Failure.html b/docs/types/Failure.html index 5eacb233..a4c32037 100644 --- a/docs/types/Failure.html +++ b/docs/types/Failure.html @@ -1 +1 @@ -Failure | HyperTrack React Native SDK API - v13.5.0
Failure<F>: {
    type: "failure";
    value: F;
}

Type Parameters

  • F

Type declaration

  • type: "failure"
  • value: F
\ No newline at end of file +Failure | HyperTrack React Native SDK API - v13.5.1
Failure<F>: {
    type: "failure";
    value: F;
}

Type Parameters

  • F

Type declaration

  • type: "failure"
  • value: F
\ No newline at end of file diff --git a/docs/types/Location.html b/docs/types/Location.html index 2a4e1db8..b466f305 100644 --- a/docs/types/Location.html +++ b/docs/types/Location.html @@ -1 +1 @@ -Location | HyperTrack React Native SDK API - v13.5.0
Location: {
    latitude: number;
    longitude: number;
}

Type declaration

  • latitude: number
  • longitude: number
\ No newline at end of file +Location | HyperTrack React Native SDK API - v13.5.1
Location: {
    latitude: number;
    longitude: number;
}

Type declaration

  • latitude: number
  • longitude: number
\ No newline at end of file diff --git a/docs/types/LocationError.html b/docs/types/LocationError.html index c95ba09a..165434e8 100644 --- a/docs/types/LocationError.html +++ b/docs/types/LocationError.html @@ -1 +1 @@ -LocationError | HyperTrack React Native SDK API - v13.5.0
LocationError: NotRunning | Starting | Errors
\ No newline at end of file +LocationError | HyperTrack React Native SDK API - v13.5.1
LocationError: NotRunning | Starting | Errors
\ No newline at end of file diff --git a/docs/types/LocationWithDeviation.html b/docs/types/LocationWithDeviation.html index fd507d49..cfb5dc08 100644 --- a/docs/types/LocationWithDeviation.html +++ b/docs/types/LocationWithDeviation.html @@ -1 +1 @@ -LocationWithDeviation | HyperTrack React Native SDK API - v13.5.0
LocationWithDeviation: {
    location: Location;
    deviation: number;
}

Type declaration

\ No newline at end of file +LocationWithDeviation | HyperTrack React Native SDK API - v13.5.1
LocationWithDeviation: {
    location: Location;
    deviation: number;
}

Type declaration

\ No newline at end of file diff --git a/docs/types/NotRunning.html b/docs/types/NotRunning.html index 6a8164d0..be11f6eb 100644 --- a/docs/types/NotRunning.html +++ b/docs/types/NotRunning.html @@ -1 +1 @@ -NotRunning | HyperTrack React Native SDK API - v13.5.0
NotRunning: {
    type: "notRunning";
}

Type declaration

  • type: "notRunning"
\ No newline at end of file +NotRunning | HyperTrack React Native SDK API - v13.5.1
NotRunning: {
    type: "notRunning";
}

Type declaration

  • type: "notRunning"
\ No newline at end of file diff --git a/docs/types/Order.html b/docs/types/Order.html index 047e6898..003f8f75 100644 --- a/docs/types/Order.html +++ b/docs/types/Order.html @@ -1 +1 @@ -Order | HyperTrack React Native SDK API - v13.5.0
Order: {
    orderHandle: string;
    isInsideGeofence: Result<boolean, LocationError>;
}

Type declaration

\ No newline at end of file +Order | HyperTrack React Native SDK API - v13.5.1
Order: {
    orderHandle: string;
    isInsideGeofence: Result<boolean, LocationError>;
}

Type declaration

\ No newline at end of file diff --git a/docs/types/OrderStatus.html b/docs/types/OrderStatus.html index 73629739..af35b4e3 100644 --- a/docs/types/OrderStatus.html +++ b/docs/types/OrderStatus.html @@ -1 +1 @@ -OrderStatus | HyperTrack React Native SDK API - v13.5.0
OrderStatus: {
    type: "orderStatusClockIn";
} | {
    type: "orderStatusClockOut";
} | {
    type: "orderStatusCustom";
    value: string;
}

Type declaration

  • type: "orderStatusClockIn"

Type declaration

  • type: "orderStatusClockOut"

Type declaration

  • type: "orderStatusCustom"
  • value: string
\ No newline at end of file +OrderStatus | HyperTrack React Native SDK API - v13.5.1
OrderStatus: {
    type: "orderStatusClockIn";
} | {
    type: "orderStatusClockOut";
} | {
    type: "orderStatusCustom";
    value: string;
}

Type declaration

  • type: "orderStatusClockIn"

Type declaration

  • type: "orderStatusClockOut"

Type declaration

  • type: "orderStatusCustom"
  • value: string
\ No newline at end of file diff --git a/docs/types/Result.html b/docs/types/Result.html index 810e6fe3..dda3b734 100644 --- a/docs/types/Result.html +++ b/docs/types/Result.html @@ -1 +1 @@ -Result | HyperTrack React Native SDK API - v13.5.0
Result<S, F>: Success<S> | Failure<F>

Type Parameters

  • S

  • F

\ No newline at end of file +Result | HyperTrack React Native SDK API - v13.5.1
Result<S, F>: Success<S> | Failure<F>

Type Parameters

  • S

  • F

\ No newline at end of file diff --git a/docs/types/Starting.html b/docs/types/Starting.html index b6b9ec38..797c1414 100644 --- a/docs/types/Starting.html +++ b/docs/types/Starting.html @@ -1 +1 @@ -Starting | HyperTrack React Native SDK API - v13.5.0
Starting: {
    type: "starting";
}

Type declaration

  • type: "starting"
\ No newline at end of file +Starting | HyperTrack React Native SDK API - v13.5.1
Starting: {
    type: "starting";
}

Type declaration

  • type: "starting"
\ No newline at end of file diff --git a/docs/types/Success.html b/docs/types/Success.html index 4ec2508f..bc79bb82 100644 --- a/docs/types/Success.html +++ b/docs/types/Success.html @@ -1 +1 @@ -Success | HyperTrack React Native SDK API - v13.5.0
Success<S>: {
    type: "success";
    value: S;
}

Type Parameters

  • S

Type declaration

  • type: "success"
  • value: S
\ No newline at end of file +Success | HyperTrack React Native SDK API - v13.5.1
Success<S>: {
    type: "success";
    value: S;
}

Type Parameters

  • S

Type declaration

  • type: "success"
  • value: S
\ No newline at end of file diff --git a/justfile b/justfile index 1a5fd0d6..2f36e37a 100644 --- a/justfile +++ b/justfile @@ -29,7 +29,7 @@ SEMVER_REGEX := "(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d _ask-confirm: @bash -c 'read confirmation; if [[ $confirmation != "y" && $confirmation != "Y" ]]; then echo "Okay 😮‍💨 😅"; exit 1; fi' -build: get-dependencies docs +build: get-dependencies docs format yarn --cwd sdk prepare yarn --cwd plugin_android_location_services_google prepare yarn --cwd plugin_android_location_services_google_19_0_1 prepare @@ -62,6 +62,7 @@ _latest-ios: @curl -s https://cocoapods.org/pods/HyperTrack | grep -m 1 -o -E "HyperTrack {{SEMVER_REGEX}}" | grep -o -E '{{SEMVER_REGEX}}' | head -n 1 format: + brew upgrade ktlint ktlint --format sdk/android/src/main/java/ open-docs: docs diff --git a/plugin_android_location_services_google/package.json b/plugin_android_location_services_google/package.json index cfc222b1..063f8e9e 100644 --- a/plugin_android_location_services_google/package.json +++ b/plugin_android_location_services_google/package.json @@ -1,6 +1,6 @@ { "name": "hypertrack-sdk-react-native-plugin-android-location-services-google", - "version": "13.5.0", + "version": "13.5.1", "description": "A wrapper around `Location Services Google` plugin for HyperTrack SDK Android that is used with `hypertrack-sdk-react-native`", "main": "lib/commonjs/index", "module": "lib/module/index", @@ -81,7 +81,6 @@ "react": "*", "react-native": "*" }, - "packageManager": "yarn@3.6.1", "engines": { "node": ">= 18.0.0" }, diff --git a/plugin_android_location_services_google_19_0_1/package.json b/plugin_android_location_services_google_19_0_1/package.json index d76cb699..38a09a89 100644 --- a/plugin_android_location_services_google_19_0_1/package.json +++ b/plugin_android_location_services_google_19_0_1/package.json @@ -1,6 +1,6 @@ { "name": "hypertrack-sdk-react-native-plugin-android-location-services-google-19-0-1", - "version": "13.5.0", + "version": "13.5.1", "description": "A wrapper around `Location Services Google 19.0.1` plugin for HyperTrack SDK Android that is used with `hypertrack-sdk-react-native`", "main": "lib/commonjs/index", "module": "lib/module/index", @@ -81,7 +81,6 @@ "react": "*", "react-native": "*" }, - "packageManager": "yarn@3.6.1", "engines": { "node": ">= 18.0.0" }, diff --git a/plugin_android_push_service_firebase/package.json b/plugin_android_push_service_firebase/package.json index 6db52ce4..9876ebfd 100644 --- a/plugin_android_push_service_firebase/package.json +++ b/plugin_android_push_service_firebase/package.json @@ -1,6 +1,6 @@ { "name": "hypertrack-sdk-react-native-plugin-android-push-service-firebase", - "version": "13.5.0", + "version": "13.5.1", "description": "A wrapper around `Push Service Firebase` plugin for HyperTrack SDK Android that is used with `hypertrack-sdk-react-native`", "main": "lib/commonjs/index", "module": "lib/module/index", @@ -81,7 +81,6 @@ "react": "*", "react-native": "*" }, - "packageManager": "yarn@3.6.1", "engines": { "node": ">= 18.0.0" }, diff --git a/sdk/android/src/main/java/com/reactnativehypertracksdk/HyperTrackReactNativePlugin.kt b/sdk/android/src/main/java/com/reactnativehypertracksdk/HyperTrackReactNativePlugin.kt index 1896109e..058b7f7e 100644 --- a/sdk/android/src/main/java/com/reactnativehypertracksdk/HyperTrackReactNativePlugin.kt +++ b/sdk/android/src/main/java/com/reactnativehypertracksdk/HyperTrackReactNativePlugin.kt @@ -20,14 +20,13 @@ import com.reactnativehypertracksdk.common.Serialization.serializeOrders import com.reactnativehypertracksdk.common.Success @ReactModule(name = HyperTrackReactNativePlugin.NAME) -class HyperTrackReactNativePlugin(reactContext: ReactApplicationContext?) : - ReactContextBaseJavaModule(reactContext) { +class HyperTrackReactNativePlugin( + reactContext: ReactApplicationContext?, +) : ReactContextBaseJavaModule(reactContext) { private var locateSubscription: HyperTrack.Cancellable? = null private var subscriptions: List? = null - override fun getName(): String { - return NAME - } + override fun getName(): String = NAME /** * ReactNative built-in methods @@ -181,39 +180,43 @@ class HyperTrackReactNativePlugin(reactContext: ReactApplicationContext?) : HyperTrackSdkWrapper.setWorkerHandle(args.toHashMap()) } - private fun initListeners(): List { - return mutableListOf().also { result -> - HyperTrack.subscribeToErrors { - emitEvent(EVENT_ERRORS, serializeErrors(it).toWriteableArray()) - }.also { - result.add(it) - } - - HyperTrack.subscribeToIsAvailable { - emitEvent(EVENT_IS_AVAILABLE, serializeIsAvailable(it).toWritableMap()) - }.also { - result.add(it) - } - - HyperTrack.subscribeToIsTracking { - emitEvent(EVENT_IS_TRACKING, serializeIsTracking(it).toWritableMap()) - }.also { - result.add(it) - } - - HyperTrack.subscribeToLocation { - emitEvent(EVENT_LOCATION, serializeLocationResult(it).toWritableMap()) - }.also { - result.add(it) - } - - HyperTrack.subscribeToOrders { - emitEvent(EVENT_ORDERS, serializeOrders(it.values).toWritableMap()) - }.also { - result.add(it) - } + private fun initListeners(): List = + mutableListOf().also { result -> + HyperTrack + .subscribeToErrors { + emitEvent(EVENT_ERRORS, serializeErrors(it).toWriteableArray()) + }.also { + result.add(it) + } + + HyperTrack + .subscribeToIsAvailable { + emitEvent(EVENT_IS_AVAILABLE, serializeIsAvailable(it).toWritableMap()) + }.also { + result.add(it) + } + + HyperTrack + .subscribeToIsTracking { + emitEvent(EVENT_IS_TRACKING, serializeIsTracking(it).toWritableMap()) + }.also { + result.add(it) + } + + HyperTrack + .subscribeToLocation { + emitEvent(EVENT_LOCATION, serializeLocationResult(it).toWritableMap()) + }.also { + result.add(it) + } + + HyperTrack + .subscribeToOrders { + emitEvent(EVENT_ORDERS, serializeOrders(it.values).toWritableMap()) + }.also { + result.add(it) + } } - } private fun emitEvent( event: String, diff --git a/sdk/android/src/main/java/com/reactnativehypertracksdk/HyperTrackSdkPackage.kt b/sdk/android/src/main/java/com/reactnativehypertracksdk/HyperTrackSdkPackage.kt index 4d7059d0..e29e679b 100644 --- a/sdk/android/src/main/java/com/reactnativehypertracksdk/HyperTrackSdkPackage.kt +++ b/sdk/android/src/main/java/com/reactnativehypertracksdk/HyperTrackSdkPackage.kt @@ -5,11 +5,8 @@ import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.uimanager.ViewManager class HyperTrackSdkPackage : ReactPackage { - override fun createNativeModules(reactContext: ReactApplicationContext): MutableList { - return listOf(HyperTrackReactNativePlugin(reactContext)).toMutableList() - } + override fun createNativeModules(reactContext: ReactApplicationContext): MutableList = + listOf(HyperTrackReactNativePlugin(reactContext)).toMutableList() - override fun createViewManagers(reactContext: ReactApplicationContext): List> { - return emptyList() - } + override fun createViewManagers(reactContext: ReactApplicationContext): List> = emptyList() } diff --git a/sdk/android/src/main/java/com/reactnativehypertracksdk/ReactNativeSerialization.kt b/sdk/android/src/main/java/com/reactnativehypertracksdk/ReactNativeSerialization.kt index 3aa874ea..b8bcf056 100644 --- a/sdk/android/src/main/java/com/reactnativehypertracksdk/ReactNativeSerialization.kt +++ b/sdk/android/src/main/java/com/reactnativehypertracksdk/ReactNativeSerialization.kt @@ -60,8 +60,8 @@ internal fun WrapperResult.toPromise(promise: Promise) { } @Suppress("UNCHECKED_CAST") -internal fun List.toWriteableArray(): WritableArray { - return Arguments.createArray().also { writableArray -> +internal fun List.toWriteableArray(): WritableArray = + Arguments.createArray().also { writableArray -> forEach { when (it) { is String -> { @@ -78,7 +78,6 @@ internal fun List.toWriteableArray(): WritableArray { } } } -} @Suppress("UNCHECKED_CAST") internal fun Map.toWritableMap(): WritableMap { diff --git a/sdk/android/src/main/java/com/reactnativehypertracksdk/common/HyperTrackSdkWrapper.kt b/sdk/android/src/main/java/com/reactnativehypertracksdk/common/HyperTrackSdkWrapper.kt index e4240488..500920de 100644 --- a/sdk/android/src/main/java/com/reactnativehypertracksdk/common/HyperTrackSdkWrapper.kt +++ b/sdk/android/src/main/java/com/reactnativehypertracksdk/common/HyperTrackSdkWrapper.kt @@ -31,8 +31,8 @@ typealias Serialized = Map * It receives serialized params. */ internal object HyperTrackSdkWrapper { - fun addGeotag(args: Serialized): WrapperResult { - return deserializeGeotagData(args) + fun addGeotag(args: Serialized): WrapperResult = + deserializeGeotagData(args) .flatMapSuccess { geotag -> // TODO: return proper error if JSON is wrong val geotagMetadata = Json.fromMap(geotag.data)!! @@ -60,18 +60,17 @@ internal object HyperTrackSdkWrapper { ) } else { HyperTrack.addGeotag(geotagMetadata, expectedLocation) - } - .let { - when (it) { - is Result.Failure -> { - serializeLocationErrorFailure(it.failure) - } - - is Result.Success -> { - serializeLocationWithDeviationSuccess(it.success) - } + }.let { + when (it) { + is Result.Failure -> { + serializeLocationErrorFailure(it.failure) + } + + is Result.Success -> { + serializeLocationWithDeviationSuccess(it.success) } } + } } else { if (orderHandle != null || orderStatus != null) { if (orderHandle == null || orderStatus == null) { @@ -84,116 +83,94 @@ internal object HyperTrackSdkWrapper { ) } else { HyperTrack.addGeotag(geotagMetadata) - } - .let { serializeLocationResult(it) } + }.let { serializeLocationResult(it) } }.let { Success(it) } } - } - fun getDeviceId(): WrapperResult { - return Success(serializeDeviceId(HyperTrack.deviceID)) - } + fun getDeviceId(): WrapperResult = Success(serializeDeviceId(HyperTrack.deviceID)) - fun getDynamicPublishableKey(): WrapperResult { - return Success(serializeDynamicPublishableKey(HyperTrack.dynamicPublishableKey)) - } + fun getDynamicPublishableKey(): WrapperResult = Success(serializeDynamicPublishableKey(HyperTrack.dynamicPublishableKey)) - fun getErrors(): WrapperResult> { - return Success(serializeErrors(HyperTrack.errors)) - } + fun getErrors(): WrapperResult> = Success(serializeErrors(HyperTrack.errors)) - fun getIsAvailable(): WrapperResult { - return Success( + fun getIsAvailable(): WrapperResult = + Success( serializeIsAvailable(HyperTrack.isAvailable), ) - } - fun getIsTracking(): WrapperResult { - return Success( + fun getIsTracking(): WrapperResult = + Success( serializeIsTracking(HyperTrack.isTracking), ) - } - fun getLocation(): WrapperResult { - return HyperTrack + fun getLocation(): WrapperResult = + HyperTrack .location .let { when (it) { is Result.Failure -> serializeLocationErrorFailure(it.failure) is Result.Success -> serializeLocationSuccess(it.success) } - } - .let { Success(it) } - } + }.let { Success(it) } - fun getMetadata(): WrapperResult { - return Success( + fun getMetadata(): WrapperResult = + Success( serializeMetadata(HyperTrack.metadata.toMap()), ) - } - fun getName(): WrapperResult { - return Success( + fun getName(): WrapperResult = + Success( serializeName(HyperTrack.name), ) - } - fun getOrders(): WrapperResult { - return Success( + fun getOrders(): WrapperResult = + Success( serializeOrders(HyperTrack.orders.values), ) - } - fun getWorkerHandle(): WrapperResult { - return Success( + fun getWorkerHandle(): WrapperResult = + Success( serializeWorkerHandle(HyperTrack.workerHandle), ) - } - fun setDynamicPublishableKey(args: Serialized): WrapperResult { - return deserializeDynamicPublishableKey(args) + fun setDynamicPublishableKey(args: Serialized): WrapperResult = + deserializeDynamicPublishableKey(args) .mapSuccess { publishableKey -> HyperTrack.dynamicPublishableKey = publishableKey } - } - fun setIsAvailable(args: Serialized): WrapperResult { - return deserializeIsAvailable(args) + fun setIsAvailable(args: Serialized): WrapperResult = + deserializeIsAvailable(args) .mapSuccess { isAvailable -> HyperTrack.isAvailable = isAvailable } - } - fun setIsTracking(args: Serialized): WrapperResult { - return deserializeIsTracking(args) + fun setIsTracking(args: Serialized): WrapperResult = + deserializeIsTracking(args) .mapSuccess { isTracking -> HyperTrack.isTracking = isTracking } - } - fun setMetadata(args: Serialized): WrapperResult { - return deserializeMetadata(args) + fun setMetadata(args: Serialized): WrapperResult = + deserializeMetadata(args) .flatMapSuccess { metadata -> WrapperResult.tryAsResult { // TODO: return proper error if JSON is wrong HyperTrack.metadata = Json.fromMap(metadata)!! } } - } - fun setName(args: Serialized): WrapperResult { - return deserializeName(args) + fun setName(args: Serialized): WrapperResult = + deserializeName(args) .mapSuccess { name -> HyperTrack.name = name } - } - fun setWorkerHandle(args: Serialized): WrapperResult { - return deserializeWorkerHandle(args) + fun setWorkerHandle(args: Serialized): WrapperResult = + deserializeWorkerHandle(args) .mapSuccess { workerHandle -> HyperTrack.workerHandle = workerHandle } - } } diff --git a/sdk/android/src/main/java/com/reactnativehypertracksdk/common/Serialization.kt b/sdk/android/src/main/java/com/reactnativehypertracksdk/common/Serialization.kt index 47f0b867..f3664c34 100644 --- a/sdk/android/src/main/java/com/reactnativehypertracksdk/common/Serialization.kt +++ b/sdk/android/src/main/java/com/reactnativehypertracksdk/common/Serialization.kt @@ -9,17 +9,16 @@ import com.hypertrack.sdk.android.Result * to Map or List where T is any JSON-compatible type */ internal object Serialization { - fun deserializeDynamicPublishableKey(args: Serialized): WrapperResult { - return parse(args) { + fun deserializeDynamicPublishableKey(args: Serialized): WrapperResult = + parse(args) { it.assertValue(key = KEY_TYPE, value = TYPE_DYNAMIC_PUBLISHABLE_KEY) it .get(KEY_VALUE) .getOrThrow() } - } - fun deserializeGeotagData(map: Serialized): WrapperResult { - return parse(map) { + fun deserializeGeotagData(map: Serialized): WrapperResult = + parse(map) { val data = it .get(KEY_GEOTAG_DATA) @@ -49,69 +48,61 @@ internal object Serialization { orderStatus = orderStatus, ) } - } - fun deserializeIsAvailable(isAvailable: Serialized): WrapperResult { - return parse(isAvailable) { + fun deserializeIsAvailable(isAvailable: Serialized): WrapperResult = + parse(isAvailable) { it.assertValue(key = KEY_TYPE, value = TYPE_IS_AVAILABLE) it .get(KEY_VALUE) .getOrThrow() } - } - fun deserializeIsTracking(isTracking: Serialized): WrapperResult { - return parse(isTracking) { + fun deserializeIsTracking(isTracking: Serialized): WrapperResult = + parse(isTracking) { it.assertValue(key = KEY_TYPE, value = TYPE_IS_TRACKING) it .get(KEY_VALUE) .getOrThrow() } - } - fun deserializeMetadata(metadata: Serialized): WrapperResult { - return parse(metadata) { + fun deserializeMetadata(metadata: Serialized): WrapperResult = + parse(metadata) { it.assertValue(key = KEY_TYPE, value = TYPE_METADATA) it .get(KEY_VALUE) .getOrThrow() } - } - fun deserializeName(name: Serialized): WrapperResult { - return parse(name) { + fun deserializeName(name: Serialized): WrapperResult = + parse(name) { it.assertValue(key = KEY_TYPE, value = TYPE_NAME) it .get(KEY_VALUE) .getOrThrow() } - } - fun deserializeWorkerHandle(map: Serialized): WrapperResult { - return parse(map) { + fun deserializeWorkerHandle(map: Serialized): WrapperResult = + parse(map) { it.assertValue(key = KEY_TYPE, value = TYPE_WORKER_HANDLE) it .get(KEY_VALUE) .getOrThrow() } - } - fun serializeDeviceId(deviceId: String): Serialized { - return mapOf( + fun serializeDeviceId(deviceId: String): Serialized = + mapOf( KEY_TYPE to TYPE_DEVICE_ID, KEY_VALUE to deviceId, ) - } - fun serializeDynamicPublishableKey(publishableKey: String): Serialized { - return mapOf( + fun serializeDynamicPublishableKey(publishableKey: String): Serialized = + mapOf( KEY_TYPE to TYPE_DYNAMIC_PUBLISHABLE_KEY, KEY_VALUE to publishableKey, ) - } - fun serializeError(error: HyperTrack.Error): Map { - return mapOf( + fun serializeError(error: HyperTrack.Error): Map = + mapOf( KEY_TYPE to TYPE_ERROR, KEY_VALUE to when (error) { @@ -128,30 +119,26 @@ internal object Serialization { HyperTrack.Error.Permissions.Notifications.Denied -> "permissions.notifications.denied" }, ) - } - fun serializeErrors(errors: Set): List> { - return errors.map { + fun serializeErrors(errors: Set): List> = + errors.map { serializeError(it) } - } - fun serializeIsAvailable(isAvailable: Boolean): Serialized { - return mapOf( + fun serializeIsAvailable(isAvailable: Boolean): Serialized = + mapOf( KEY_TYPE to TYPE_IS_AVAILABLE, KEY_VALUE to isAvailable, ) - } - fun serializeIsTracking(isTracking: Boolean): Serialized { - return mapOf( + fun serializeIsTracking(isTracking: Boolean): Serialized = + mapOf( KEY_TYPE to TYPE_IS_TRACKING, KEY_VALUE to isTracking, ) - } - fun serializeLocateResult(locationResult: Result>): Serialized { - return when (locationResult) { + fun serializeLocateResult(locationResult: Result>): Serialized = + when (locationResult) { is Result.Failure -> { serializeFailure(serializeErrors(locationResult.failure)) } @@ -160,10 +147,9 @@ internal object Serialization { serializeLocationSuccess(locationResult.success) } } - } - fun serializeLocationResult(locationResult: Result): Serialized { - return when (locationResult) { + fun serializeLocationResult(locationResult: Result): Serialized = + when (locationResult) { is Result.Failure -> { serializeLocationErrorFailure(locationResult.failure) } @@ -172,40 +158,33 @@ internal object Serialization { serializeLocationSuccess(locationResult.success) } } - } - fun serializeLocationErrorFailure(locationError: HyperTrack.LocationError): Serialized { - return serializeFailure(serializeLocationError(locationError)) - } + fun serializeLocationErrorFailure(locationError: HyperTrack.LocationError): Serialized = + serializeFailure(serializeLocationError(locationError)) - fun serializeLocationSuccess(location: HyperTrack.Location): Serialized { - return serializeSuccess(serializeLocation(location)) - } + fun serializeLocationSuccess(location: HyperTrack.Location): Serialized = serializeSuccess(serializeLocation(location)) - fun serializeLocationWithDeviationSuccess(locationWithDeviation: HyperTrack.LocationWithDeviation): Serialized { - return serializeSuccess( + fun serializeLocationWithDeviationSuccess(locationWithDeviation: HyperTrack.LocationWithDeviation): Serialized = + serializeSuccess( serializeLocationWithDeviation( locationWithDeviation, ), ) - } - fun serializeMetadata(metadata: Serialized): Serialized { - return mapOf( + fun serializeMetadata(metadata: Serialized): Serialized = + mapOf( KEY_TYPE to TYPE_METADATA, KEY_VALUE to metadata, ) - } - fun serializeName(name: String): Serialized { - return mapOf( + fun serializeName(name: String): Serialized = + mapOf( KEY_TYPE to TYPE_NAME, KEY_VALUE to name, ) - } - fun serializeOrders(orders: Collection): Serialized { - return mapOf( + fun serializeOrders(orders: Collection): Serialized = + mapOf( KEY_TYPE to TYPE_ORDERS, KEY_VALUE to orders.mapIndexed { index, order -> @@ -216,17 +195,15 @@ internal object Serialization { ) }, ) - } - fun serializeWorkerHandle(workerHandle: String): Serialized { - return mapOf( + fun serializeWorkerHandle(workerHandle: String): Serialized = + mapOf( KEY_TYPE to TYPE_WORKER_HANDLE, KEY_VALUE to workerHandle, ) - } - private fun deserializeLocation(map: Serialized): WrapperResult { - return parse(map) { + private fun deserializeLocation(map: Serialized): WrapperResult = + parse(map) { it.assertValue(key = KEY_TYPE, value = TYPE_LOCATION) val value = it @@ -247,19 +224,17 @@ internal object Serialization { } }.getOrThrow() } - } - private fun deserializeOrderHandle(map: Serialized): WrapperResult { - return parse(map) { + private fun deserializeOrderHandle(map: Serialized): WrapperResult = + parse(map) { it.assertValue(key = KEY_TYPE, value = TYPE_ORDER_HANDLE) it .get(KEY_VALUE) .getOrThrow() } - } - private fun deserializeOrderStatus(map: Serialized): WrapperResult { - return parse(map) { + private fun deserializeOrderStatus(map: Serialized): WrapperResult = + parse(map) { when (it.get(KEY_TYPE).getOrThrow()) { TYPE_GEOTAG_ORDER_STATUS_CLOCK_IN -> HyperTrack.OrderStatus.ClockIn TYPE_GEOTAG_ORDER_STATUS_CLOCK_OUT -> HyperTrack.OrderStatus.ClockOut @@ -271,10 +246,9 @@ internal object Serialization { else -> throw Error("Unknown order status: $map") } } - } - private fun serializeIsInsideGeofence(isInsideGeofence: Result): Serialized { - return when (isInsideGeofence) { + private fun serializeIsInsideGeofence(isInsideGeofence: Result): Serialized = + when (isInsideGeofence) { is Result.Failure -> { serializeFailure(serializeLocationError(isInsideGeofence.failure)) } @@ -288,10 +262,9 @@ internal object Serialization { ) } } - } - private fun serializeLocation(location: HyperTrack.Location): Serialized { - return mapOf( + private fun serializeLocation(location: HyperTrack.Location): Serialized = + mapOf( KEY_TYPE to TYPE_LOCATION, KEY_VALUE to mapOf( @@ -299,10 +272,9 @@ internal object Serialization { KEY_LONGITUDE to location.longitude, ), ) - } - private fun serializeLocationWithDeviation(locationWithDeviation: HyperTrack.LocationWithDeviation): Serialized { - return mapOf( + private fun serializeLocationWithDeviation(locationWithDeviation: HyperTrack.LocationWithDeviation): Serialized = + mapOf( KEY_TYPE to TYPE_LOCATION_WITH_DEVIATION, KEY_VALUE to mapOf( @@ -310,31 +282,27 @@ internal object Serialization { KEY_DEVIATION to locationWithDeviation.deviation, ), ) - } - private fun serializeFailure(failure: List): Serialized { - return mapOf( + private fun serializeFailure(failure: List): Serialized = + mapOf( KEY_TYPE to TYPE_RESULT_FAILURE, KEY_VALUE to failure, ) - } - private fun serializeFailure(failure: Serialized): Serialized { - return mapOf( + private fun serializeFailure(failure: Serialized): Serialized = + mapOf( KEY_TYPE to TYPE_RESULT_FAILURE, KEY_VALUE to failure, ) - } - private fun serializeSuccess(success: Serialized): Serialized { - return mapOf( + private fun serializeSuccess(success: Serialized): Serialized = + mapOf( KEY_TYPE to TYPE_RESULT_SUCCESS, KEY_VALUE to success, ) - } - private fun serializeLocationError(locationError: HyperTrack.LocationError): Serialized { - return when (locationError) { + private fun serializeLocationError(locationError: HyperTrack.LocationError): Serialized = + when (locationError) { HyperTrack.LocationError.NotRunning -> { mapOf(KEY_TYPE to TYPE_LOCATION_ERROR_NOT_RUNNING) } @@ -352,7 +320,6 @@ internal object Serialization { ) } } - } fun parse( source: Serialized, @@ -382,8 +349,8 @@ internal object Serialization { private val _exceptions = mutableListOf() val exceptions: List = _exceptions - inline fun get(key: String): WrapperResult { - return try { + inline fun get(key: String): WrapperResult = + try { Success(source[key]!! as T) } catch (e: Exception) { Failure( @@ -393,10 +360,9 @@ internal object Serialization { }, ) } - } - inline fun getOptional(key: String): WrapperResult { - return try { + inline fun getOptional(key: String): WrapperResult = + try { Success(source[key] as T?) } catch (e: Exception) { Failure( @@ -406,7 +372,6 @@ internal object Serialization { }, ) } - } inline fun assertValue( key: String, @@ -422,7 +387,8 @@ internal object Serialization { val source: Any, val exceptions: List, ) : Throwable( - exceptions.joinToString("\n") + exceptions + .joinToString("\n") .let { "Invalid input:\n\n${source}\n\n$it" }, diff --git a/sdk/android/src/main/java/com/reactnativehypertracksdk/common/WrapperResult.kt b/sdk/android/src/main/java/com/reactnativehypertracksdk/common/WrapperResult.kt index 02d3a0ff..1f30ae37 100644 --- a/sdk/android/src/main/java/com/reactnativehypertracksdk/common/WrapperResult.kt +++ b/sdk/android/src/main/java/com/reactnativehypertracksdk/common/WrapperResult.kt @@ -1,8 +1,8 @@ package com.reactnativehypertracksdk.common internal sealed class WrapperResult { - fun flatMapSuccess(onSuccess: (SuccessType) -> WrapperResult): WrapperResult { - return when (this) { + fun flatMapSuccess(onSuccess: (SuccessType) -> WrapperResult): WrapperResult = + when (this) { is Success -> { onSuccess.invoke(this.success) } @@ -11,10 +11,9 @@ internal sealed class WrapperResult { Failure(this.failure) } } - } - fun mapSuccess(onSuccess: (SuccessType) -> MappedSuccess): WrapperResult { - return when (this) { + fun mapSuccess(onSuccess: (SuccessType) -> MappedSuccess): WrapperResult = + when (this) { is Success -> { Success(onSuccess.invoke(this.success)) } @@ -23,29 +22,30 @@ internal sealed class WrapperResult { Failure(this.failure) } } - } - fun getOrThrow(): SuccessType { - return when (this) { + fun getOrThrow(): SuccessType = + when (this) { is Success -> this.success is Failure -> throw Exception( "Result unwrapping failed: ${this.failure}", this.failure, ) } - } companion object { - fun tryAsResult(block: () -> SuccessType): WrapperResult { - return try { + fun tryAsResult(block: () -> SuccessType): WrapperResult = + try { Success(block.invoke()) } catch (e: Exception) { Failure(e) } - } } } -internal data class Success(val success: SuccessType) : WrapperResult() +internal data class Success( + val success: SuccessType, +) : WrapperResult() -internal data class Failure(val failure: Throwable) : WrapperResult() +internal data class Failure( + val failure: Throwable, +) : WrapperResult() diff --git a/sdk/package.json b/sdk/package.json index e3b73979..0e981d46 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -1,6 +1,6 @@ { "name": "hypertrack-sdk-react-native", - "version": "13.5.0", + "version": "13.5.1", "description": "React Native HyperTrack SDK is a wrapper around native iOS and Android SDKs that allows to integrate HyperTrack into React Native apps.", "main": "lib/commonjs/index", "module": "lib/module/index", diff --git a/sdk/src/HyperTrack/HyperTrack.ts b/sdk/src/HyperTrack/HyperTrack.ts index 27567553..5baaa9aa 100644 --- a/sdk/src/HyperTrack/HyperTrack.ts +++ b/sdk/src/HyperTrack/HyperTrack.ts @@ -26,6 +26,7 @@ import type { WorkerHandle } from './data_types/internal/WorkerHandle'; import type { Order } from './data_types/Order'; import type { OrdersInternal } from './data_types/internal/OrdersInternal'; import type { OrderInternal } from './data_types/internal/OrderInternal'; +import type { IsInsideGeofence } from './data_types/internal/IsInsideGeofence'; const EVENT_ERRORS = 'errors'; const EVENT_IS_AVAILABLE = 'isAvailable'; @@ -594,13 +595,17 @@ export default class HyperTrack { /** @ignore */ private static deserializeIsInsideGeofence( - isInsideGeofence: Result + isInsideGeofence: Result, ): Result { switch (isInsideGeofence.type) { case 'success': + let successValue = isInsideGeofence.value; + if (successValue.type !== 'isInsideGeofence') { + throw new Error(`Invalid isInsideGeofence: ${JSON.stringify(successValue)}`); + } return { type: 'success', - value: isInsideGeofence.value, + value: successValue.value, }; case 'failure': return { diff --git a/sdk/src/HyperTrack/data_types/internal/IsInsideGeofence.ts b/sdk/src/HyperTrack/data_types/internal/IsInsideGeofence.ts new file mode 100644 index 00000000..90a2b347 --- /dev/null +++ b/sdk/src/HyperTrack/data_types/internal/IsInsideGeofence.ts @@ -0,0 +1,4 @@ +export type IsInsideGeofence = { + type: 'isInsideGeofence'; + value: boolean; +}; diff --git a/sdk/src/HyperTrack/data_types/internal/OrderInternal.ts b/sdk/src/HyperTrack/data_types/internal/OrderInternal.ts index 00aac24a..963876c8 100644 --- a/sdk/src/HyperTrack/data_types/internal/OrderInternal.ts +++ b/sdk/src/HyperTrack/data_types/internal/OrderInternal.ts @@ -1,8 +1,9 @@ import type { Result } from '../Result'; +import type { IsInsideGeofence } from './IsInsideGeofence'; import type { LocationErrorInternal } from './LocationErrorInternal'; export type OrderInternal = { orderHandle: string; - isInsideGeofence: Result; + isInsideGeofence: Result; index: number; };