Skip to content

Instantly share code, notes, and snippets.

@ericntd
Last active January 1, 2022 20:42
Show Gist options
  • Save ericntd/f2875b5267eac367cf981ecd02d7894d to your computer and use it in GitHub Desktop.
Save ericntd/f2875b5267eac367cf981ecd02d7894d to your computer and use it in GitHub Desktop.
ViewModel and Generic ViewModel Factory

The ViewModel

class WeatherViewModel @Inject constructor(
    private val currentInteractor: CurrentWeatherInteractor,
    // ...
): ViewModel() {}

GenericViewModelFactory

class GenericViewModelFactory<T1 : ViewModel> @Inject constructor(
    private val provider: Provider<T1>
) : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return provider.get() as T
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment