Dan Peluso
03/10/2021, 2:36 AMsubscribeAsState()
, is there a way to unsubscribe from that? Code in threadDan Peluso
03/10/2021, 2:38 AMsubscribeAsState()
function with a disposable. I'm creating my observable with
private var obsVal =
Observable.interval(20, TimeUnit.MILLISECONDS).subscribeOn(<http://Schedulers.io|Schedulers.io>()).share()
and would like to be able to reset it, but I'm unsure how to unsub with compose's functionLeland Richardson [G]
03/10/2021, 2:41 AMsubscribeAsState()
call is no longer in the composition. For instance, in the code:
if (condition) { val x = y.subscribeAsState() }
The subscription would be removed when condition turned false
Leland Richardson [G]
03/10/2021, 2:42 AMkey
useful hereLeland Richardson [G]
03/10/2021, 2:42 AMval value by key(index) { getSOmeObservable(index).subscribaAsState() }
Leland Richardson [G]
03/10/2021, 2:43 AMindex
changes, the code inside of it will be a “new” part of the composition, and the previous one will get removed, and thus, unsubscribedLeland Richardson [G]
03/10/2021, 2:43 AMDan Peluso
03/10/2021, 2:50 AMkey
? I can send over some code tooDan Peluso
03/10/2021, 2:52 AM@Composable
fun renderGraph(plotType: PlotType) {
// state
val state = obsVal.subscribeAsState(initial = 0).value
var plotState: PlotType by remember(calculation = { mutableStateOf(PlotType.PLOT0) })
AndroidView(
factory = {
// create the chart
// set the correct chart type when first building
plotState = plotType
},
modifier = Modifier.fillMaxSize(),
update = {
// how the view knows to reset the graph
if (plotType != plotState) {
// update the chart data from $state
// update the flag
plotState = plotType
//TODO: reset the observable to emit values from 0 again
}
}
}
Dan Peluso
03/10/2021, 2:53 AMDan Peluso
03/10/2021, 2:54 AM@Composable
fun MainView() {
// state
var tabPosition by remember(calculation = { mutableStateOf(0) })
Column {
TabRow(selectedTabIndex = tabPosition, tabs = {
tabs.forEach { plotType ->
Tab(selected = plotType.value == tabPosition, onClick = {
tabPosition = plotType.value
},
text = { Text("Graph: ${plotType.value}") })
}
})
renderGraph(PlotType.fromInt(tabPosition)!!)
}
}