Updated on 2026-08-14
This commit is contained in:
parent
b24b3b457b
commit
4eef7d1eb6
3 changed files with 75 additions and 17 deletions
|
|
@ -0,0 +1,16 @@
|
|||
package com.tangem.tangemtest.extensions
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
fun <T> List<T>.print(delimiter: String = ", ", wrap: Boolean = true): String {
|
||||
val builder = StringBuilder()
|
||||
forEach { builder.append(it).append(delimiter) }
|
||||
val length = builder.length
|
||||
if (length > delimiter.length) {
|
||||
builder.delete(length - delimiter.length, length)
|
||||
}
|
||||
val result = builder.toString()
|
||||
|
||||
return if (wrap) "[$result]" else result
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
package com.tangem.tangemtest.ucase.domain.responses
|
||||
|
||||
import com.google.gson.*
|
||||
import com.tangem.commands.ProductMask
|
||||
import com.tangem.commands.Settings
|
||||
import com.tangem.commands.SettingsMask
|
||||
import com.tangem.commands.SigningMethod
|
||||
import com.tangem.common.extensions.toHexString
|
||||
import java.lang.reflect.Type
|
||||
|
||||
/**
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
class GsonInitializer {
|
||||
val gson: Gson by lazy { init() }
|
||||
|
||||
private fun init(): Gson {
|
||||
val builder = GsonBuilder().apply {
|
||||
registerTypeAdapter(ByteArray::class.java, ByteTypeAdapter())
|
||||
registerTypeAdapter(SigningMethod::class.java, SigningMethodTypeAdapter())
|
||||
registerTypeAdapter(SettingsMask::class.java, SettingsMaskTypeAdapter())
|
||||
registerTypeAdapter(ProductMask::class.java, ProductMaskTypeAdapter())
|
||||
}
|
||||
builder.setPrettyPrinting()
|
||||
return builder.create()
|
||||
}
|
||||
}
|
||||
|
||||
class ByteTypeAdapter : JsonSerializer<ByteArray> {
|
||||
override fun serialize(src: ByteArray, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
|
||||
return JsonPrimitive(src.toHexString())
|
||||
}
|
||||
}
|
||||
|
||||
class SettingsMaskTypeAdapter : JsonSerializer<SettingsMask> {
|
||||
override fun serialize(src: SettingsMask, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
|
||||
val arrayElement = JsonArray()
|
||||
Settings.values()
|
||||
.filter { src.contains(it) }
|
||||
.forEach { arrayElement.add(it.name) }
|
||||
return arrayElement
|
||||
}
|
||||
}
|
||||
|
||||
class ProductMaskTypeAdapter : JsonSerializer<ProductMask> {
|
||||
override fun serialize(src: ProductMask, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
|
||||
return JsonPrimitive(src.rawValue.toString())
|
||||
}
|
||||
}
|
||||
|
||||
class SigningMethodTypeAdapter : JsonSerializer<SigningMethod> {
|
||||
override fun serialize(src: SigningMethod, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
|
||||
return JsonPrimitive(src.rawValue.toString())
|
||||
}
|
||||
}
|
||||
|
|
@ -5,17 +5,14 @@ import androidx.lifecycle.MutableLiveData
|
|||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.JsonPrimitive
|
||||
import com.google.gson.JsonSerializer
|
||||
import com.tangem.CardManager
|
||||
import com.tangem.commands.Card
|
||||
import com.tangem.common.extensions.toHexString
|
||||
import com.tangem.tangemtest._arch.SingleLiveEvent
|
||||
import com.tangem.tangemtest._arch.structure.Id
|
||||
import com.tangem.tangemtest._arch.structure.abstraction.Item
|
||||
import com.tangem.tangemtest.commons.performAction
|
||||
import com.tangem.tangemtest.ucase.domain.paramsManager.ParamsManager
|
||||
import com.tangem.tangemtest.ucase.domain.responses.GsonInitializer
|
||||
import com.tangem.tasks.ScanEvent
|
||||
import com.tangem.tasks.TaskError
|
||||
import com.tangem.tasks.TaskEvent
|
||||
|
|
@ -76,18 +73,8 @@ class ParamsViewModel(val paramsManager: ParamsManager) : ViewModel() {
|
|||
|
||||
internal class Notifier(private val vm: ParamsViewModel) {
|
||||
|
||||
private val gsonConverter: Gson by lazy { createGson() }
|
||||
|
||||
private fun createGson(): Gson {
|
||||
val builder = GsonBuilder()
|
||||
builder.registerTypeAdapter(ByteArray::class.java, JsonSerializer<ByteArray> { src, typeOfSrc, context ->
|
||||
JsonPrimitive(src.toHexString())
|
||||
})
|
||||
builder.setPrettyPrinting()
|
||||
return builder.create()
|
||||
}
|
||||
|
||||
private var notShowedError: TaskError? = null
|
||||
private val gson: Gson = GsonInitializer().gson
|
||||
|
||||
fun handleActionResult(response: TaskEvent<*>, list: List<Item>) {
|
||||
notifyParameterChanges(list)
|
||||
|
|
@ -112,12 +99,12 @@ internal class Notifier(private val vm: ParamsViewModel) {
|
|||
when (event) {
|
||||
is ScanEvent.OnReadEvent -> {
|
||||
vm.ldCard.postValue(event.card)
|
||||
vm.ldResponse.postValue(gsonConverter.toJson(event))
|
||||
vm.ldResponse.postValue(gson.toJson(event))
|
||||
}
|
||||
is ScanEvent.OnVerifyEvent -> {
|
||||
vm.ldIsVerified.postValue(true)
|
||||
}
|
||||
else -> vm.ldResponse.postValue(gsonConverter.toJson(event))
|
||||
else -> vm.ldResponse.postValue(gson.toJson(event))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue