You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
activityViewModel() is designed to get an activity scoped view model from a Fragment. You want just by viewModel()
However, I recommend using Fragments. Putting UI directly inside of an Activity is no longer recommended in general for Android.
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.The text was updated successfully, but these errors were encountered: