Skip to content

Commit

Permalink
use deferred location witness observer
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesmac committed Nov 11, 2024
1 parent 6f954d4 commit 4a862c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
19 changes: 12 additions & 7 deletions sdk/src/androidTest/java/network/xyo/client/LocationWitnessTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.rule.GrantPermissionRule
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import network.xyo.client.witness.location.info.LocationActivity
import network.xyo.client.witness.location.info.XyoLocationPayload
import network.xyo.client.witness.location.info.XyoLocationWitness
Expand All @@ -26,13 +29,15 @@ class LocationWitnessTest {
// Get the application context
val context = ApplicationProvider.getApplicationContext<Context>()

val witness = XyoLocationWitness()
val payload = witness.observe(context)
CoroutineScope(Dispatchers.Main).launch {
val witness = XyoLocationWitness()
val payload = witness.observe(context)

assertInstanceOf<XyoLocationPayload>(payload)
assert(payload.schema == "network.xyo.location.android")
assert((payload.currentLocation) !== null)
assert(payload.currentLocation?.coords?.latitude !== null)
assert(payload.currentLocation?.coords?.longitude !== null)
assertInstanceOf<XyoLocationPayload>(payload)
assert(payload.schema == "network.xyo.location.android")
assert((payload.currentLocation) !== null)
assert(payload.currentLocation?.coords?.latitude !== null)
assert(payload.currentLocation?.coords?.longitude !== null)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class XyoLocationPayload (
companion object {

@SuppressLint("MissingPermission")
fun detect(context: Context): XyoLocationPayload? {
suspend fun detect(context: Context): XyoLocationPayload? {
return XyoLocationPayload(
XyoLocationCurrent.detect(context)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ package network.xyo.client.witness.location.info
import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import network.xyo.client.DeferredObserver
import network.xyo.client.XyoWitness
import network.xyo.client.address.XyoAccount

class DeferredLocationObserver : DeferredObserver<XyoLocationPayload>() {
override suspend fun deferredDetect(
context: Context,
previousHash: String?
): XyoLocationPayload? {
return XyoLocationPayload.detect(context)
}
}

@RequiresApi(Build.VERSION_CODES.M)
class XyoLocationWitness(address: XyoAccount = XyoAccount()) : XyoWitness<XyoLocationPayload>(
address,
fun (context: Context, _: String?): XyoLocationPayload? {
return XyoLocationPayload.detect(context)
})
DeferredLocationObserver(),
"",
address
)

0 comments on commit 4a862c8

Please sign in to comment.