TP-32499 | variable replacement in json (#86)
This commit is contained in:
committed by
GitHub Enterprise
parent
7cf8a14e05
commit
80d0a14deb
@@ -0,0 +1,53 @@
|
||||
package com.navi.uitron.utils
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.navi.uitron.model.data.UiTronActionData
|
||||
|
||||
class JsonVariableSubstitutor {
|
||||
|
||||
fun substituteForActionData(jsonInput: String, variableMap: Map<String, Any>, gson: Gson): UiTronActionData {
|
||||
var outputJson = jsonInput
|
||||
variableMap.forEach { (variable, value) ->
|
||||
val variableKey = "@\\{\\{${variable}\\}\\}"
|
||||
val replacement = when (value) {
|
||||
is String -> value
|
||||
is Number, is Boolean -> value.toString()
|
||||
is List<*> -> value.joinToString(",", "[", "]") { listValue ->
|
||||
when (listValue) {
|
||||
is String -> escapeQuotes(listValue)
|
||||
else -> listValue.toString()
|
||||
}
|
||||
}
|
||||
is Map<*, *> -> convertMapToJson(value)
|
||||
else -> ""
|
||||
}
|
||||
outputJson = outputJson.replace(variableKey.toRegex(), replacement)
|
||||
}
|
||||
|
||||
return gson.fromJson(outputJson, UiTronActionData::class.java)
|
||||
}
|
||||
|
||||
private fun escapeQuotes(value: String): String {
|
||||
return "\"$value\""
|
||||
}
|
||||
|
||||
private fun convertMapToJson(map: Map<*, *>): String {
|
||||
val entries = map.entries.joinToString(",", "{", "}") { (key, value) ->
|
||||
val jsonValue = when (value) {
|
||||
is String -> escapeQuotes(value)
|
||||
is Number, is Boolean -> value.toString()
|
||||
is List<*> -> value.joinToString(",", "[", "]") { listValue ->
|
||||
when (listValue) {
|
||||
is String -> escapeQuotes(listValue)
|
||||
else -> listValue.toString()
|
||||
}
|
||||
}
|
||||
is Map<*, *> -> convertMapToJson(value)
|
||||
else -> ""
|
||||
}
|
||||
"\"$key\": $jsonValue"
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,14 +7,17 @@
|
||||
|
||||
package com.navi.uitron.viewmodel
|
||||
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.google.gson.Gson
|
||||
import com.navi.uitron.model.data.SliderDependentValue
|
||||
import com.navi.uitron.model.data.TextData
|
||||
import com.navi.uitron.model.data.UiTronAction
|
||||
import com.navi.uitron.model.data.UiTronActionData
|
||||
import com.navi.uitron.utils.ActionHandler
|
||||
import com.navi.uitron.utils.JsonVariableSubstitutor
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import getDataId
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
@@ -23,10 +26,11 @@ import kotlinx.coroutines.launch
|
||||
|
||||
@Suppress("unused")
|
||||
@HiltViewModel
|
||||
open class UiTronViewModel constructor(
|
||||
open class UiTronViewModel(
|
||||
val handle: SavedStateHandle = SavedStateHandle(),
|
||||
val stateHolder: ComposeStateHolder = ComposeStateHolder(),
|
||||
val countDownTimerHelper: CountDownTimerHelper = CountDownTimerHelper()
|
||||
val countDownTimerHelper: CountDownTimerHelper = CountDownTimerHelper(),
|
||||
private val substitutor: JsonVariableSubstitutor = JsonVariableSubstitutor()
|
||||
) : ViewModel() {
|
||||
|
||||
private val actionHandler = ActionHandler(handle)
|
||||
@@ -134,6 +138,11 @@ open class UiTronViewModel constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun handleActionsFromJson(actionsString: String, variableMap: Map<String, Any>, gson: Gson) {
|
||||
val actionData = substitutor.substituteForActionData(actionsString, variableMap, gson)
|
||||
handleActions(actionData)
|
||||
}
|
||||
|
||||
fun clearHandle() {
|
||||
handle.keys().forEach {
|
||||
handle.remove<Any>(it)
|
||||
|
||||
Reference in New Issue
Block a user