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.
This is my Activity where the ViewModel is being initiated
This is my ViewModel
The use-case
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.