airbnb / mavericks

Mavericks: Android on Autopilot
https://airbnb.io/mavericks/
Apache License 2.0
5.83k stars 500 forks source link

onAsync and onEach are protected #617

Closed kypeli closed 2 years ago

kypeli commented 2 years ago

In the Mavericks documentation here it shows how we can listen for property state changes using onAsync from the viewModel.

However, in latest Mavericks version, onAsync and onEach are protected so they cannot be called from the Fragment which is the consumer of the view model. I don't understand the documentation in this case.

Sample code:


class WeatherFragment : Fragment(R.layout.fragment_weather), MavericksView {
    private val viewModel: WeatherViewModel by fragmentViewModel()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.onAsync(...) // <- "Cannot access onAsync. It is protected in WeatherViewModel.

    }
}

class WeatherViewModel(initial: Weather) : MavericksViewModel<Weather>(initial) {
...
}
gpeal commented 2 years ago

That's because the ones that you call from MavericksView are here. They should just work though and if you are having issues, it's likely specific to your project. https://github.com/airbnb/mavericks/blob/main/mvrx/src/main/kotlin/com/airbnb/mvrx/MavericksView.kt#L300

kypeli commented 2 years ago

Thanks for the comment! Android Studio suggested the protected version by default and could not pick up the extension function on the view model. Overriding the arguments picked up the correct function.

Leaving this as a comment if others have difficulties with this just based on reading the docs.