Skip to content

Commit

Permalink
add witness handler with types from sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesmac committed Nov 14, 2024
1 parent f5e95ce commit 6026aed
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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__"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<XyoPayload?>> {
@RequiresApi(Build.VERSION_CODES.M)
override suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList<Pair<String, XyoAccount?>>): WitnessResult<List<XyoPayload?>> {
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<List<XyoPayload?>> {
return withContext(Dispatchers.IO) {
var locationPayload: XyoPayload? = null
var bw: XyoPayload? = null
val errors: MutableList<Error> = 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))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package network.xyo.app.xyo.sample.application.witness

import android.content.Context
import network.xyo.client.address.XyoAccount

interface WitnessHandlerInterface<out T> {
suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList<Pair<String, XyoAccount?>>): WitnessResult<T>
}
6 changes: 6 additions & 0 deletions sdk/src/main/java/network/xyo/client/witness/types/Result.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package network.xyo.app.xyo.sample.application.witness

sealed class WitnessResult<out R> {
data class Success<out T>(val data: T) : WitnessResult<T>()
data class Error(val exception: MutableList<kotlin.Error>) : WitnessResult<Nothing>()
}

0 comments on commit 6026aed

Please sign in to comment.