-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add witness handler with types from sample app
- Loading branch information
Showing
4 changed files
with
70 additions
and
1 deletion.
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
55 changes: 55 additions & 0 deletions
55
sdk/src/main/java/network/xyo/client/witness/location/info/Handler.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,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)) | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
sdk/src/main/java/network/xyo/client/witness/types/HandlerInterface.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,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> | ||
} |
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,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>() | ||
} |