'LazyColumn and List auto update
I have a singleton class which handle nearby connections API live connections for me in all the app, I'd like to show it devices in a list and as still of now its not working for me, if there's any problem in the object or better way to represent it I'd like to know too minimal example:
object:
private val STRATEGY = Strategy.P2P_CLUSTER
data class ConnectedDevice(val endpointId: String, val connectionInfo: DiscoveredEndpointInfo)
object NearbyConnectionsApi {
private lateinit var mConnectionsClient: ConnectionsClient
fun setContext(currentContext: Context) {
mConnectionsClient = Nearby.getConnectionsClient(currentContext)
}
var mDevicesList: MutableList<ConnectedDevice> = ArrayList() //the place i want to have live in the screen
private val mEndpointDiscoveryCallback: EndpointDiscoveryCallback =
object : EndpointDiscoveryCallback() {
override fun onEndpointFound(
endpointId: String,
discoveredEndpointInfo: DiscoveredEndpointInfo
) {
Log.d("nearby", "find $endpointId, ${discoveredEndpointInfo.endpointName}")
mDevicesList.add(ConnectedDevice(endpointId, discoveredEndpointInfo))
}
override fun onEndpointLost(endpointId: String) {
mDevicesList.remove(mDevicesList.find { it.endpointId == endpointId })
// A previously discovered endpoint has gone away.
}
}
}
screen:
@Composable
fun ScanDevicesScreen(
navController: NavController
) {
LazyColumn{
items(NearbyConnectionsApi.mDevicesList){ item ->
Row{
Text(item.connectionInfo.serviceId)
Text(item.connectionInfo.endpointName)
}
}
}
}
i don't know why but the list isn't automatically updating (need to go out from the string and after I back there duplicates, and remove items when disconnect not working as well, please advice, thanks in advance
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
