From 6026aedda2ba391830ed38c0b7dce8cce06bd4df Mon Sep 17 00:00:00 2001 From: Matt Jones <885417+jonesmac@users.noreply.github.com> Date: Thu, 14 Nov 2024 09:39:55 -0500 Subject: [PATCH] add witness handler with types from sample app --- .../client/XyoAccountPrefsRepositoryTest.kt | 2 +- .../client/witness/location/info/Handler.kt | 55 +++++++++++++++++++ .../client/witness/types/HandlerInterface.kt | 8 +++ .../xyo/client/witness/types/Result.kt | 6 ++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 sdk/src/main/java/network/xyo/client/witness/location/info/Handler.kt create mode 100644 sdk/src/main/java/network/xyo/client/witness/types/HandlerInterface.kt create mode 100644 sdk/src/main/java/network/xyo/client/witness/types/Result.kt diff --git a/sdk/src/androidTest/java/network/xyo/client/XyoAccountPrefsRepositoryTest.kt b/sdk/src/androidTest/java/network/xyo/client/XyoAccountPrefsRepositoryTest.kt index b980365..25bfeda 100644 --- a/sdk/src/androidTest/java/network/xyo/client/XyoAccountPrefsRepositoryTest.kt +++ b/sdk/src/androidTest/java/network/xyo/client/XyoAccountPrefsRepositoryTest.kt @@ -70,7 +70,7 @@ class XyoAccountPrefsRepositoryTest { val instance = XyoAccountPrefsRepository.getInstance(appContext) val originalAddress = instance.getAccount().private.hex - open class UpdatedAccountPreferences : AccountPreferences { + class UpdatedAccountPreferences : AccountPreferences { override val fileName = "network-xyo-sdk-prefs-1" override val storagePath = "__xyo-client-sdk-1__" } diff --git a/sdk/src/main/java/network/xyo/client/witness/location/info/Handler.kt b/sdk/src/main/java/network/xyo/client/witness/location/info/Handler.kt new file mode 100644 index 0000000..4faf973 --- /dev/null +++ b/sdk/src/main/java/network/xyo/client/witness/location/info/Handler.kt @@ -0,0 +1,55 @@ +package network.xyo.client.witness.location.info + +import android.content.Context +import android.os.Build +import android.util.Log +import androidx.annotation.RequiresApi +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.withContext +import network.xyo.app.xyo.sample.application.witness.WitnessHandlerInterface +import network.xyo.app.xyo.sample.application.witness.WitnessResult +import network.xyo.client.XyoPanel +import network.xyo.client.address.XyoAccount +import network.xyo.client.payload.XyoPayload + +open class WitnessLocationHandler : WitnessHandlerInterface> { + @RequiresApi(Build.VERSION_CODES.M) + override suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList>): WitnessResult> { + val panel = XyoPanel(context, nodeUrlsAndAccounts, listOf( + XyoLocationWitness() + )) + return getLocation(panel) + } + + @OptIn(ExperimentalCoroutinesApi::class) + @RequiresApi(Build.VERSION_CODES.M) + private suspend fun getLocation(panel: XyoPanel): WitnessResult> { + return withContext(Dispatchers.IO) { + var locationPayload: XyoPayload? = null + var bw: XyoPayload? = null + val errors: MutableList = mutableListOf() + panel.let { + it.reportAsyncQuery().apiResults?.forEach { action -> + if (action.response !== null) { + val payloads = action.response!!.payloads + if (payloads?.get(0)?.schema.equals("network.xyo.boundwitness")) { + bw = payloads?.get(0) + } + if (payloads?.get(1)?.schema.equals("network.xyo.location.android")) { + locationPayload = payloads?.get(1) + } + } + if (action.errors !== null) { + action.errors.forEach { error -> + Log.e("xyoSampleApp", error.message ?: error.toString()) + errors.add(error) + } + } + } + } + if (errors.size > 0) return@withContext WitnessResult.Error(errors) + return@withContext WitnessResult.Success(listOf(bw, locationPayload)) + } + } +} \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/witness/types/HandlerInterface.kt b/sdk/src/main/java/network/xyo/client/witness/types/HandlerInterface.kt new file mode 100644 index 0000000..1df4f0b --- /dev/null +++ b/sdk/src/main/java/network/xyo/client/witness/types/HandlerInterface.kt @@ -0,0 +1,8 @@ +package network.xyo.app.xyo.sample.application.witness + +import android.content.Context +import network.xyo.client.address.XyoAccount + +interface WitnessHandlerInterface { + suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList>): WitnessResult +} \ No newline at end of file diff --git a/sdk/src/main/java/network/xyo/client/witness/types/Result.kt b/sdk/src/main/java/network/xyo/client/witness/types/Result.kt new file mode 100644 index 0000000..b2ca8da --- /dev/null +++ b/sdk/src/main/java/network/xyo/client/witness/types/Result.kt @@ -0,0 +1,6 @@ +package network.xyo.app.xyo.sample.application.witness + +sealed class WitnessResult { + data class Success(val data: T) : WitnessResult() + data class Error(val exception: MutableList) : WitnessResult() +} \ No newline at end of file