airbnb / mavericks

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

How can I change the view after the state is initialized? #261

Closed 6iovan closed 5 years ago

6iovan commented 5 years ago

Hello! :smile:

I want to change the view multiple times, but "XXX was mutated. State classes should be immutable."

thank you.

elihart commented 5 years ago

You must call setState from the ViewModel. The data class should be copied and a new version made.

see the wiki https://github.com/airbnb/MvRx/wiki#core-concepts https://github.com/airbnb/MvRx/wiki#updating-state

6iovan commented 5 years ago

@elihart I did call setState or execute from the ViewModel, but it’s not work when I use epoxy + paging support.

6iovan commented 5 years ago

Thank you for your answer!~

implementation 'com.airbnb.android:mvrx:0.7.2'
implementation 'com.airbnb.android:epoxy:2.19.0'
implementation 'com.airbnb.android:epoxy-paging:2.19.0'
data class LandMainState(
    val historyItemData: Async<List<LandMainAlbum>> = Uninitialized,
    ...
    val albumAllItemData: Async<PagedList<Pair<Album, Album>>> = Uninitialized
) : MvRxState
class LandMainViewModel(state: LandMainState) : MvRxViewModel<LandMainState>(state) {
    fun loadHistoryCards() {
        LandMainLocalDataSource.getHistoryCars()
            .subscribeOn(Schedulers.io())
            .execute { copy(historyItemData = it) }
    }
}
viewModel.asyncSubscribe(this, LandMainViewModel::albumAllItemData) {
    rxController.submitList(it)
}

I call submitList in the Activity#onCreate(), then i want to call loadHistoryCards in the Activity#onResume(). Crash happened..

6iovan commented 5 years ago

I got it, PagedList cannot be used in State.