ViewModelFactory boilerplate code

PHOTO EMBED

Fri Feb 26 2021 04:53:44 GMT+0000 (Coordinated Universal Time)

Saved by @swalia #kotlin #android

class SleepTrackerViewModelFactory(
       private val dataSource: SleepDatabaseDao,
       private val application: Application) : ViewModelProvider.Factory {
   @Suppress("unchecked_cast")
   override fun <T : ViewModel?> create(modelClass: Class<T>): T {
       if (modelClass.isAssignableFrom(SleepTrackerViewModel::class.java)) {
           return SleepTrackerViewModel(dataSource, application) as T
       }
       throw IllegalArgumentException("Unknown ViewModel class")
   }
}
content_copyCOPY

https://developer.android.com/codelabs/kotlin-android-training-coroutines-and-room