-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename RadarOperatingHours File (#406)
* add operating hours to geofences * add tojson * bump patch version * add test * bump to beta * remove beta tag * rename file
- Loading branch information
1 parent
aa7e9cb
commit bf54cc0
Showing
2 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
sdk/src/main/java/io/radar/sdk/model/RadarOperatingHours.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |