Last active
January 20, 2020 13:46
-
-
Save baudouxbenjamin/00a1c7e68c0d1f4c7185f7dcba10ce05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : DaggerAppComppatActivity() { | |
@Inject | |
lateinit var viewModel: MainViewModel | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
class MainModule { | |
@Provides | |
fun provideModel(activity: MainActivity, factory: MainViewModelFactory): MainViewModel { | |
return ViewModelProviders.of(activity, factory).get(MainViewModel::class.java) | |
} | |
@Suppress("UNCHECKED_CAST") | |
class MainViewModelFactory @Inject constructor(private val database: Database): ViewModelProvider.Factory { | |
override fun <T : ViewModel> create(modelClass: Class<T>): T { | |
return MainViewModel(database) as T | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainViewModel constructor(private val database: Database) : ViewModel() { | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment