From aa7e9cbf7e1d9727ca32c12634e5fe486e44edd0 Mon Sep 17 00:00:00 2001 From: KennyHuRadar <139801512+KennyHuRadar@users.noreply.github.com> Date: Tue, 8 Oct 2024 11:20:10 -0400 Subject: [PATCH] Kennyhu/fence 2091 geofence operatinghours not typed or mapped in native sdks (#405) * add operating hours to geofences * add tojson * bump patch version * add test * bump to beta * remove beta tag --- sdk/build.gradle | 2 +- .../java/io/radar/sdk/model/RadarGeofence.kt | 13 +++- .../io/radar/sdk/model/RadarOperatingHour.kt | 67 +++++++++++++++++++ sdk/src/test/java/io/radar/sdk/RadarTest.kt | 2 + sdk/src/test/resources/search_geofences.json | 24 +++++++ 5 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 sdk/src/main/java/io/radar/sdk/model/RadarOperatingHour.kt diff --git a/sdk/build.gradle b/sdk/build.gradle index dc3c99aed..dd144f87e 100644 --- a/sdk/build.gradle +++ b/sdk/build.gradle @@ -10,7 +10,7 @@ apply plugin: "org.jetbrains.dokka" apply plugin: 'io.radar.mvnpublish' ext { - radarVersion = '3.18.1' + radarVersion = '3.18.2' } String buildNumber = ".${System.currentTimeMillis()}" diff --git a/sdk/src/main/java/io/radar/sdk/model/RadarGeofence.kt b/sdk/src/main/java/io/radar/sdk/model/RadarGeofence.kt index ee55f519b..4d6bcaefb 100644 --- a/sdk/src/main/java/io/radar/sdk/model/RadarGeofence.kt +++ b/sdk/src/main/java/io/radar/sdk/model/RadarGeofence.kt @@ -1,7 +1,6 @@ package io.radar.sdk.model import org.json.JSONArray -import org.json.JSONException import org.json.JSONObject /** @@ -35,7 +34,10 @@ class RadarGeofence( */ val metadata: JSONObject?, - + /** + * The optional set of custom key-value pairs for the geofence. + */ + val operatingHours: RadarOperatingHour?, /** * The geometry of the geofence. */ @@ -48,6 +50,7 @@ class RadarGeofence( private const val FIELD_DESCRIPTION = "description" private const val FIELD_TAG = "tag" private const val FIELD_EXTERNAL_ID = "externalId" + private const val FIELD_OPERATING_HOURS = "operatingHours" private const val FIELD_METADATA = "metadata" private const val FIELD_TYPE = "type" private const val FIELD_GEOMETRY = "geometry" @@ -73,6 +76,9 @@ class RadarGeofence( val tag: String? = obj.optString(FIELD_TAG) ?: null val externalId: String? = obj.optString(FIELD_EXTERNAL_ID) ?: null val metadata: JSONObject? = obj.optJSONObject(FIELD_METADATA) ?: null + val operatingHours: RadarOperatingHour? = obj.optJSONObject(FIELD_OPERATING_HOURS)?.let { operatingHours -> + RadarOperatingHour.fromJson(operatingHours) + } val center = obj.optJSONObject(FIELD_GEOMETRY_CENTER)?.optJSONArray(FIELD_COORDINATES)?.let { coordinate -> RadarCoordinate( coordinate.optDouble(1), @@ -117,7 +123,7 @@ class RadarGeofence( else -> null } ?: RadarCircleGeometry(RadarCoordinate(0.0, 0.0), 0.0) - return RadarGeofence(id, description, tag, externalId, metadata, geometry) + return RadarGeofence(id, description, tag, externalId, metadata, operatingHours, geometry) } @JvmStatic @@ -168,6 +174,7 @@ class RadarGeofence( obj.putOpt(FIELD_EXTERNAL_ID, this.externalId) obj.putOpt(FIELD_DESCRIPTION, this.description) obj.putOpt(FIELD_METADATA, this.metadata) + obj.putOpt(FIELD_OPERATING_HOURS, this.operatingHours?.toJson() ?: null) this.geometry?.let { geometry -> when (geometry) { is RadarCircleGeometry -> { diff --git a/sdk/src/main/java/io/radar/sdk/model/RadarOperatingHour.kt b/sdk/src/main/java/io/radar/sdk/model/RadarOperatingHour.kt new file mode 100644 index 000000000..157eac7e1 --- /dev/null +++ b/sdk/src/main/java/io/radar/sdk/model/RadarOperatingHour.kt @@ -0,0 +1,67 @@ +package io.radar.sdk.model + +import org.json.JSONArray +import org.json.JSONObject + +class RadarOperatingHour( + val hours: MutableMap +) { + + internal companion object { + @JvmStatic + fun fromJson(obj: JSONObject): RadarOperatingHour? { + if (obj == null) { + return null + + } + + val dictionary = mutableMapOf() + + for (key in obj.keys()) { + val value = obj.get(key) + // unwrap day + if (value is JSONArray) { + val list = mutableListOf() + // unwrap pairs within the day + for (i in 0 until value.length()) { + val item = value.get(i) + + if (item is JSONArray && item.length() == 2) { + val innerList = mutableListOf() + innerList.add(item.get(0)) + innerList.add(item.get(1)) + list.add(innerList) + } + + } + dictionary[key] = list + } + } + return RadarOperatingHour(dictionary) + } + } + + fun toJson(): JSONObject { + val jsonObject = JSONObject() + + for ((key, value) in hours) { + if (value is List<*>) { + val jsonArray = JSONArray() + + for (innerList in value) { + if (innerList is List<*>) { + val jsonInnerArray = JSONArray() + for (item in innerList) { + jsonInnerArray.put(item) + } + jsonArray.put(jsonInnerArray) + } + } + jsonObject.put(key, jsonArray) + } + } + + return jsonObject + } + +} \ No newline at end of file diff --git a/sdk/src/test/java/io/radar/sdk/RadarTest.kt b/sdk/src/test/java/io/radar/sdk/RadarTest.kt index e6b057d9b..e4245056a 100644 --- a/sdk/src/test/java/io/radar/sdk/RadarTest.kt +++ b/sdk/src/test/java/io/radar/sdk/RadarTest.kt @@ -1104,6 +1104,8 @@ class RadarTest { assertTrue(geofenceJson.has("geometryRadius")) assertTrue(geofenceJson.has("geometryCenter")) + assertTrue(geofenceJson.has("operatingHours")) + } @Test diff --git a/sdk/src/test/resources/search_geofences.json b/sdk/src/test/resources/search_geofences.json index 41e39f921..174e04237 100644 --- a/sdk/src/test/resources/search_geofences.json +++ b/sdk/src/test/resources/search_geofences.json @@ -14,6 +14,30 @@ "metadata": { "foo": "bar" }, + "operatingHours":{ + "Mon":[ + ["03:43","12:12"], + ["13:45","20:54"] + ], + "Tue":[ + ["08:00","20:30"] + ], + "Wed":[ + ["08:00","20:30"] + ], + "Thu":[ + ["08:00","20:30"] + ], + "Fri":[ + ["08:00","20:30"] + ], + "Sat":[ + ["08:00","18:00"] + ], + "Sun":[ + ["08:00","18:00"] + ] + }, "tag": "station", "externalId": "admiralty", "geometry": {