Observe data and state changes
Observe SDK live data to consume data and events
Overview
DriverSdk provides live data api to enable easily observing different models and states.
Observe authentication state
Login state can be observed using a LiveData object obtained from DriverSdk:
val driverSdk = DriverSdkProvider.driverSdk
driverSdk.data.login.observeForever { isLoggedIn ->
if (isLoggedIn) {
Log.i(TAG,"user is authenticated with Bringg Cloud Services")
} else {
Log.i(TAG,"user is not authenticated, use driverSdk.login to login")
}
}
Observe online state
Online state can be observed using a LiveData object obtained from DriverSdk:
DriverSdkProvider.driverSdk.data.online.observeForever { isOnline ->
val navController = findNavController()
if (isOnline) {
Log.i(TAG,"user is online, DriverSdk is working in the background")
} else {
Log.i(TAG,"user is not online, DriverSdk is working in the background")
}
}
Observe Tasks and Waypoints
Observe all user tasks
All the tasks assigned to the user can be monitored using a LiveData object obtained from DriverSdk:
DriverSdkProvider.driverSdk.data.taskList.observe(
viewLifecycleOwner,
Observer { taskList ->
Log.i(TAG, "task list updated, task count=${taskList.size}")
})
Observe a single task
Task can be monitored using a LiveData object obtained from DriverSdk:
DriverSdkProvider.driverSdk.data.task(taskId).observe(
viewLifecycleOwner,
Observer { task ->
Log.i(TAG, "task was updated, task status=${task?.status}")
})
Observe a single waypoint
Waypoint can be monitored using a LiveData object obtained from DriverSdk:
DriverSdkProvider.driverSdk.data.waypoint(waypointId).observe(
viewLifecycleOwner,
Observer { waypoint ->
Log.i(TAG, "waypoint was updated, task status=${waypoint?.status}")
})
Observe home list and home states
Homes and their user states can be observed using a LiveData object obtained from DriverSdk:
DriverSdkProvider.driverSdk().data.homeMap.observe(
viewLifecycleOwner,
Observer { homeMap ->
Log.i(TAG, "home states updated:")
homeMap.entries.forEach {
Log.i(TAG, "home=${it.key}, state=${it.value}")
}
})
User home states are: AT_HOME, NEAR_HOME, OUTSIDE_HOME
Observe Extras data on Task, Waypoint, Inventory and Customer
"Extras" is a field that can help merchants pass operational data between services that are connected to the Bringg platform.
On the driver SDK, the extras data can be observed using LiveData object using these interface:
interface DriverExtras {
fun taskExtras(taskId: Long): LiveData<JSONObject?>
fun waypointExtras(waypointId: Long): LiveData<JSONObject?>
fun inventoryExtras(inventoryId: Long): LiveData<JSONObject?>
fun customerExtras(waypointId: Long, customerId: Long): LiveData<JSONObject?>
}
Example:
DriverSdkProvider.driverSdk().data.extras.waypointExtras(waypointId).observe(viewLifecycleOwner) {
waypointView.setExtras(it)
}
Updated over 3 years ago