Skip to content

Loong-T/DaggerViewModel

Repository files navigation

DaggerViewModel

Usage

This library depends on dagger.android and Jetpack's ViewModel. You should know how to use dagger.android and ViewModel first.

allprojects {
  repositories {
    maven { url 'https://jitpack.io' }
  }
}

dependencies {
    implementation 'in.nerd-is:daggerviewmodel:0.2.1'
}
@Module
abstract class ViewModelFactoryModule {
  @Binds
  abstract fun bindViewModelFactory(factory: DaggerViewModelFactory): ViewModelProvider.Factory
}
@Singleton
@Component(modules = [
  AndroidInjectionModule::class,
  ViewModelFactoryModule::class
])
interface AppComponent {
  fun inject(app: ThisApp)
}

In Activity or Fragment:

@Inject
lateinit var vmFactory: ViewModelProvider.Factory

private val viewModel by lazy(LazyThreadSafetyMode.NONE) {
  ViewModelProviders.of(this, vmFactory)[YourViewModel::class.java]
}

About

A common ViewModel Factory for dagger2.android

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages