SHARE
TWEET

Untitled

a guest Jun 27th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. error: [Dagger/MissingBinding] java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.
  2. public abstract interface AppComponent {
  3.             ^
  4.   java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
  5.       reporter.awaaz.live.di.module.CustomViewModelFactory(creators)
  6.   reporter.awaaz.live.di.module. CustomViewModelFactory is injected at
  7.       reporter.awaaz.live.ui.fragment.dashboard.ItemCreateFragment.viewModelFactory
  8.   reporter.awaaz.live.ui.fragment.dashboard.ItemCreateFragment is injected at
  9.       dagger.android.AndroidInjector.inject(T) [reporter.awaaz.live.di.component.AppComponent → reporter.awaaz.live.di.module.FragmentModule_ItemCreateFragment$app_debug.ItemCreateFragmentSubcomponent]
  10.      
  11. @Singleton
  12. @Component(modules = [AndroidInjectionModule::class,
  13. ActivityModule::class, FragmentModule::class, NetworkModule::class,
  14. AppModule::class ])
  15. interface AppComponent {
  16.  
  17. @Component.Builder
  18. interface Builder {
  19.  
  20.     @BindsInstance
  21.     fun application(application: Application): Builder
  22.  
  23.     fun build(): AppComponent
  24. }
  25.  
  26. fun inject(awaazApplication: AwaazApplication)
  27. }
  28.      
  29. @Module(includes = [ViewModelModule::class])
  30. internal class AppModule {
  31.  
  32. @Singleton
  33. @Provides
  34. fun provideRoomDatabase(app: Application): AppDatabase {
  35.     return Room.databaseBuilder(app, AppDatabase::class.java, "awaaz-partner.db").build()
  36. }
  37.  
  38. @Singleton
  39. @Provides
  40. fun provideItemDao(db: AppDatabase): itemDao {
  41.     return db.itemDao()
  42. }
  43. }
  44.      
  45. @Module
  46. abstract class ActivityModule {
  47.  
  48. @ContributesAndroidInjector
  49. internal abstract fun registerActivity(): RegisterActivity
  50.  
  51. @ContributesAndroidInjector
  52. internal abstract fun dashboardActivity(): DashboardActivity
  53. }
  54.      
  55. CustomViewModelFactory @Inject constructor(private val creators:
  56. Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>):
  57. ViewModelProvider.Factory {
  58. override fun <T : ViewModel> create(modelClass: Class<T>): T {
  59.     var creator: Provider<out ViewModel>? = creators.get(modelClass)
  60.     if (creator == null) {
  61.         for (entry in creators.entries) {
  62.             if (modelClass.isAssignableFrom(entry.key)) {
  63.                 creator = entry.value
  64.                 break
  65.             }
  66.         }
  67.     }
  68.     if (creator == null) {
  69.         throw IllegalArgumentException("unknown model class $modelClass")
  70.     }
  71.  
  72.     try {
  73.         return creator.get() as T
  74.     } catch (e: Exception) {
  75.         throw RuntimeException(e)
  76.     }
  77. }
  78. }
  79.      
  80. @Module
  81. abstract class FragmentModule {
  82.  
  83. @ContributesAndroidInjector
  84. internal abstract fun itemCreateFragment(): ItemCreateFragment
  85. }
  86.      
  87. @Module
  88. class NetworkModule {
  89.  
  90. @Provides
  91. @Singleton
  92. fun provideApolloClient(context: Context): ApolloClient {
  93.     return repository.getApolloClient(context)
  94. }
  95. }
  96.      
  97. @MustBeDocumented
  98. @Target(
  99.         AnnotationTarget.FUNCTION,
  100.         AnnotationTarget.PROPERTY_GETTER,
  101.         AnnotationTarget.PROPERTY_SETTER
  102. )
  103. @Retention(AnnotationRetention.RUNTIME)
  104. @MapKey
  105. internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
  106.      
  107. @Module
  108. internal abstract class ViewModelModule {
  109.  
  110.     @Binds
  111.     @IntoMap
  112.     @ViewModelKey(ItemViewModel::class)
  113.     abstract fun bindItemViewModel(itemViewModel: itemViewModel): ViewModel
  114.   }
  115.      
  116. class ItemViewModel @Inject constructor(apolloClient: ApolloClient) : ViewModel() {
  117.   //Some methods
  118.   }
  119.      
  120. class ItemCreateFragment : Fragment() {
  121.  
  122. @Inject
  123. lateinit var viewModelFactory: CustomViewModelFactory
  124. private lateinit var itemsViewModel: ItemViewModel
  125. override fun onAttach(context: Context) {
  126.     AndroidSupportInjection.inject(this)
  127.     super.onAttach(context)
  128.     this.mContext = context
  129. }
  130. override fun onActivityCreated(savedInstanceState: Bundle?) {
  131.     super.onActivityCreated(savedInstanceState)
  132.     itemsViewModel = ViewModelProviders.of(this, viewModelFactory)[ItemViewModel::class.java]
  133.     itemsViewModel.getStatus().observe(this, Observer { Util.showToast(mContext, "Statues $it") })
  134. }
  135.      
  136. class CustomApplication : Application(), HasActivityInjector, HasSupportFragmentInjector {
  137.  
  138.     override fun activityInjector(): AndroidInjector<Activity> = activityInjector
  139.  
  140.     override fun supportFragmentInjector(): AndroidInjector<Fragment> = fragmentInjector
  141.  
  142.     @Inject
  143.     lateinit var activityInjector: DispatchingAndroidInjector<Activity>
  144.  
  145.     @Inject
  146.     lateinit var fragmentInjector: DispatchingAndroidInjector<Fragment>
  147.  
  148.     override fun onCreate() {
  149.         super.onCreate()
  150.  
  151.         DaggerAppComponent.builder().application(this).build().inject(this)
  152.  
  153.     }
  154.    }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top