Hi folks, this is not really a Kotlin question but...
# android
c
Hi folks, this is not really a Kotlin question but I hope you don’t mind. I am implementing a ViewModel for the first time as per https://developer.android.com/topic/libraries/architecture/viewmodel#implement Looking at the code example here (from above link):
Copy code
class MyViewModel : ViewModel() {
    private val users: MutableLiveData<List<User>> by lazy {
        MutableLiveData().also {
            loadUsers()
        }
    }

    fun getUsers(): LiveData<List<User>> {
        return users
    }

    private fun loadUsers() {
        // Do an asynchronous operation to fetch users.
    }
}
It is not clear how
loadUsers()
could be implemented, as
MyViewModel
accepts no dependencies. In particular, if I want to instantiate MyViewModel like so:
Copy code
val model: MyViewModel by viewModels { SavedStateViewModelFactory(this.application, this) }
it’s not clear to me how I could pass dependencies (like “MySomethingRepository”) to the view model
a
You can extend AbstractSavedStateViewModelFactory and create instances of your ViewModel there, passing the dependencies in the constructor