0

Recently in Android viewModel was useful because of liveData, but currently is it good to verbose your code and proxy all calls from Composable? Or it is fine to call subclass methods directly and left viewModel for merging modules like repositories, dependency injections and some temporal UI states? E.g. is this fine:

@Composable
fun startView(viewModel: MyViewModel) {
    Column {
        viewModel.space.foos.forEach {
            doSomethingWithFoo(it)
        }
    }
}

@Composable
fun doSomethingWithFoo(foo: Foo) {
    IconButton(onClick = { foo.doTheThing(42) }) {
        Icon(imageVector = foo.icon, contentDescription = null)
    }
}

class MyViewModel() : ViewModel() {
    val space = Space()
}

class Space() {
    val foos = listOf(Foo(), Foo())
}

class Foo() {
    val bar = mutableStateOf(0)
    val icon = Icons.Default.Abc

    fun doTheThing(i: Int) {
        bar.value = i
    }
}

or it's better to write a proxy method in viewModel instead of direct call foo.doTheThing(42)?

Psijic
  • 743
  • 7
  • 20

0 Answers0