airbnb / mavericks

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

MavericksView invalidate not being called #717

Open Veeresh8 opened 5 months ago

Veeresh8 commented 5 months ago

This is my Activity where the ViewModel is being initiated

 class Dashboard : AppCompatActivity(), MavericksView {

    private val viewModel: DashboardViewModel by viewModel()

    override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      viewModel.fetchUsersList()
    }

    override fun invalidate() = withState(viewModel) { state ->
      println("State: $state")
    }
  }

This is my ViewModel

    data class DashboardState(val users: Async<List<String>> = Uninitialized) : MavericksState

    class DashboardViewModel
    @AssistedInject
    constructor(@Assisted initialState: DashboardState, private val userCase: Usecase) :
        MavericksViewModel<DashboardState>(initialState) {

      fun fetchUsersList() {
        suspend { userCase.getUsersList() }.execute {
            println("Users: $it")
            copy(users = it)
        }
      }

    @AssistedFactory
    interface Factory : AssistedViewModelFactory<DashboardViewModel, DashboardState> {
      override fun create(state: DashboardState): DashboardViewModel
    }

    companion object :
        MavericksViewModelFactory<
            DashboardViewModel, DashboardState> by hiltMavericksViewModelFactory()
  }

The use-case

class Usecase @Inject constructor() {

suspend fun getUsersList(): List<String> {
    delay(2000)
    return listOf("A", "B", "C")
    }
}

However, invalidate is never called in my case, not sure why this is happening. I noticed that when I use the same logic in a fragment using fragmentViewModel it works for me.