Skip to content

Commit

Permalink
Rename RadarOperatingHours File (#406)
Browse files Browse the repository at this point in the history
* add operating hours to geofences

* add tojson

* bump patch version

* add test

* bump to beta

* remove beta tag

* rename file
  • Loading branch information
KennyHuRadar authored Oct 8, 2024
1 parent aa7e9cb commit bf54cc0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
6 changes: 3 additions & 3 deletions sdk/src/main/java/io/radar/sdk/model/RadarGeofence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class RadarGeofence(
/**
* The optional set of custom key-value pairs for the geofence.
*/
val operatingHours: RadarOperatingHour?,
val operatingHours: RadarOperatingHours?,
/**
* The geometry of the geofence.
*/
Expand Down Expand Up @@ -76,8 +76,8 @@ 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 operatingHours: RadarOperatingHours? = obj.optJSONObject(FIELD_OPERATING_HOURS)?.let { operatingHours ->
RadarOperatingHours.fromJson(operatingHours)
}
val center = obj.optJSONObject(FIELD_GEOMETRY_CENTER)?.optJSONArray(FIELD_COORDINATES)?.let { coordinate ->
RadarCoordinate(
Expand Down
67 changes: 67 additions & 0 deletions sdk/src/main/java/io/radar/sdk/model/RadarOperatingHours.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 RadarOperatingHours(
val hours: MutableMap<String, Any>
) {

internal companion object {
@JvmStatic
fun fromJson(obj: JSONObject): RadarOperatingHours? {
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 RadarOperatingHours(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
}

}

0 comments on commit bf54cc0

Please sign in to comment.