Skip to content

MavericksView invalidate not being called #717

@Veeresh8

Description

@Veeresh8

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions