Skip to content

Commit

Permalink
Kennyhu/fence 2091 geofence operatinghours not typed or mapped in nat…
Browse files Browse the repository at this point in the history
…ive sdks (#405)

* add operating hours to geofences

* add tojson

* bump patch version

* add test

* bump to beta

* remove beta tag
  • Loading branch information
KennyHuRadar authored Oct 8, 2024
1 parent ec4845f commit aa7e9cb
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()}"
Expand Down
13 changes: 10 additions & 3 deletions sdk/src/main/java/io/radar/sdk/model/RadarGeofence.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.radar.sdk.model

import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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"
Expand All @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 -> {
Expand Down
67 changes: 67 additions & 0 deletions sdk/src/main/java/io/radar/sdk/model/RadarOperatingHour.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.radar.sdk.model

import org.json.JSONArray
import org.json.JSONObject

class RadarOperatingHour(
val hours: MutableMap<String, Any>
) {

internal companion object {
@JvmStatic
fun fromJson(obj: JSONObject): RadarOperatingHour? {
if (obj == null) {
return null

}

val dictionary = mutableMapOf<String, Any>()

for (key in obj.keys()) {
val value = obj.get(key)
// unwrap day
if (value is JSONArray) {
val list = mutableListOf<Any>()
// 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<Any>()
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
}

}
2 changes: 2 additions & 0 deletions sdk/src/test/java/io/radar/sdk/RadarTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,8 @@ class RadarTest {
assertTrue(geofenceJson.has("geometryRadius"))
assertTrue(geofenceJson.has("geometryCenter"))

assertTrue(geofenceJson.has("operatingHours"))

}

@Test
Expand Down
24 changes: 24 additions & 0 deletions sdk/src/test/resources/search_geofences.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit aa7e9cb

Please sign in to comment.