Skip to content

Commit

Permalink
Fix deserialization (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-kuznetsov-hypertrack authored Sep 3, 2024
1 parent f0bd800 commit cec4d91
Show file tree
Hide file tree
Showing 30 changed files with 215 additions and 258 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions docs/classes/HyperTrack.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/enums/HyperTrackError.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/index.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/modules.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/Errors.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/Failure.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/Location.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/LocationError.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/LocationWithDeviation.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/NotRunning.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/Order.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/OrderStatus.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/Result.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/Starting.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/types/Success.html

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,6 +62,7 @@ _latest-ios:
@curl -s https://cocoapods.org/pods/HyperTrack | grep -m 1 -o -E "HyperTrack <span>{{SEMVER_REGEX}}" | grep -o -E '{{SEMVER_REGEX}}' | head -n 1

format:
brew upgrade ktlint
ktlint --format sdk/android/src/main/java/

open-docs: docs
Expand Down
3 changes: 1 addition & 2 deletions plugin_android_location_services_google/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -81,7 +81,6 @@
"react": "*",
"react-native": "*"
},
"packageManager": "[email protected]",
"engines": {
"node": ">= 18.0.0"
},
Expand Down
3 changes: 1 addition & 2 deletions plugin_android_location_services_google_19_0_1/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -81,7 +81,6 @@
"react": "*",
"react-native": "*"
},
"packageManager": "[email protected]",
"engines": {
"node": ">= 18.0.0"
},
Expand Down
3 changes: 1 addition & 2 deletions plugin_android_push_service_firebase/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -81,7 +81,6 @@
"react": "*",
"react-native": "*"
},
"packageManager": "[email protected]",
"engines": {
"node": ">= 18.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HyperTrack.Cancellable>? = null

override fun getName(): String {
return NAME
}
override fun getName(): String = NAME

/**
* ReactNative built-in methods
Expand Down Expand Up @@ -181,39 +180,43 @@ class HyperTrackReactNativePlugin(reactContext: ReactApplicationContext?) :
HyperTrackSdkWrapper.setWorkerHandle(args.toHashMap())
}

private fun initListeners(): List<HyperTrack.Cancellable> {
return mutableListOf<HyperTrack.Cancellable>().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<HyperTrack.Cancellable> =
mutableListOf<HyperTrack.Cancellable>().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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NativeModule> {
return listOf(HyperTrackReactNativePlugin(reactContext)).toMutableList()
}
override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> =
listOf(HyperTrackReactNativePlugin(reactContext)).toMutableList()

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return emptyList()
}
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> = emptyList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ internal fun <T> WrapperResult<T>.toPromise(promise: Promise) {
}

@Suppress("UNCHECKED_CAST")
internal fun List<Any>.toWriteableArray(): WritableArray {
return Arguments.createArray().also { writableArray ->
internal fun List<Any>.toWriteableArray(): WritableArray =
Arguments.createArray().also { writableArray ->
forEach {
when (it) {
is String -> {
Expand All @@ -78,7 +78,6 @@ internal fun List<Any>.toWriteableArray(): WritableArray {
}
}
}
}

@Suppress("UNCHECKED_CAST")
internal fun Map<String, Any?>.toWritableMap(): WritableMap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ typealias Serialized = Map<String, Any?>
* It receives serialized params.
*/
internal object HyperTrackSdkWrapper {
fun addGeotag(args: Serialized): WrapperResult<Serialized> {
return deserializeGeotagData(args)
fun addGeotag(args: Serialized): WrapperResult<Serialized> =
deserializeGeotagData(args)
.flatMapSuccess { geotag ->
// TODO: return proper error if JSON is wrong
val geotagMetadata = Json.fromMap(geotag.data)!!
Expand Down Expand Up @@ -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) {
Expand All @@ -84,116 +83,94 @@ internal object HyperTrackSdkWrapper {
)
} else {
HyperTrack.addGeotag(geotagMetadata)
}
.let { serializeLocationResult(it) }
}.let { serializeLocationResult(it) }
}.let {
Success(it)
}
}
}

fun getDeviceId(): WrapperResult<Serialized> {
return Success(serializeDeviceId(HyperTrack.deviceID))
}
fun getDeviceId(): WrapperResult<Serialized> = Success(serializeDeviceId(HyperTrack.deviceID))

fun getDynamicPublishableKey(): WrapperResult<Serialized> {
return Success(serializeDynamicPublishableKey(HyperTrack.dynamicPublishableKey))
}
fun getDynamicPublishableKey(): WrapperResult<Serialized> = Success(serializeDynamicPublishableKey(HyperTrack.dynamicPublishableKey))

fun getErrors(): WrapperResult<List<Serialized>> {
return Success(serializeErrors(HyperTrack.errors))
}
fun getErrors(): WrapperResult<List<Serialized>> = Success(serializeErrors(HyperTrack.errors))

fun getIsAvailable(): WrapperResult<Serialized> {
return Success(
fun getIsAvailable(): WrapperResult<Serialized> =
Success(
serializeIsAvailable(HyperTrack.isAvailable),
)
}

fun getIsTracking(): WrapperResult<Serialized> {
return Success(
fun getIsTracking(): WrapperResult<Serialized> =
Success(
serializeIsTracking(HyperTrack.isTracking),
)
}

fun getLocation(): WrapperResult<Serialized> {
return HyperTrack
fun getLocation(): WrapperResult<Serialized> =
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<Serialized> {
return Success(
fun getMetadata(): WrapperResult<Serialized> =
Success(
serializeMetadata(HyperTrack.metadata.toMap()),
)
}

fun getName(): WrapperResult<Serialized> {
return Success(
fun getName(): WrapperResult<Serialized> =
Success(
serializeName(HyperTrack.name),
)
}

fun getOrders(): WrapperResult<Serialized> {
return Success(
fun getOrders(): WrapperResult<Serialized> =
Success(
serializeOrders(HyperTrack.orders.values),
)
}

fun getWorkerHandle(): WrapperResult<Serialized> {
return Success(
fun getWorkerHandle(): WrapperResult<Serialized> =
Success(
serializeWorkerHandle(HyperTrack.workerHandle),
)
}

fun setDynamicPublishableKey(args: Serialized): WrapperResult<Unit> {
return deserializeDynamicPublishableKey(args)
fun setDynamicPublishableKey(args: Serialized): WrapperResult<Unit> =
deserializeDynamicPublishableKey(args)
.mapSuccess { publishableKey ->
HyperTrack.dynamicPublishableKey = publishableKey
}
}

fun setIsAvailable(args: Serialized): WrapperResult<Unit> {
return deserializeIsAvailable(args)
fun setIsAvailable(args: Serialized): WrapperResult<Unit> =
deserializeIsAvailable(args)
.mapSuccess { isAvailable ->
HyperTrack.isAvailable = isAvailable
}
}

fun setIsTracking(args: Serialized): WrapperResult<Unit> {
return deserializeIsTracking(args)
fun setIsTracking(args: Serialized): WrapperResult<Unit> =
deserializeIsTracking(args)
.mapSuccess { isTracking ->
HyperTrack.isTracking = isTracking
}
}

fun setMetadata(args: Serialized): WrapperResult<Unit> {
return deserializeMetadata(args)
fun setMetadata(args: Serialized): WrapperResult<Unit> =
deserializeMetadata(args)
.flatMapSuccess { metadata ->
WrapperResult.tryAsResult {
// TODO: return proper error if JSON is wrong
HyperTrack.metadata = Json.fromMap(metadata)!!
}
}
}

fun setName(args: Serialized): WrapperResult<Unit> {
return deserializeName(args)
fun setName(args: Serialized): WrapperResult<Unit> =
deserializeName(args)
.mapSuccess { name ->
HyperTrack.name = name
}
}

fun setWorkerHandle(args: Serialized): WrapperResult<Unit> {
return deserializeWorkerHandle(args)
fun setWorkerHandle(args: Serialized): WrapperResult<Unit> =
deserializeWorkerHandle(args)
.mapSuccess { workerHandle ->
HyperTrack.workerHandle = workerHandle
}
}
}
Loading

0 comments on commit cec4d91

Please sign in to comment.