如何使MVRX视图模型与dagger2一起使用?

时间:2019-04-28 15:12:28

标签: android kotlin dagger-2 android-viewmodel dagger

修改

添加@ViewModelKey并确保所有的视图模型都具有@Inject批注

使用Dagger2 Di库和ViewModelFactory注入ViewModels会导致缺少绑定构建错误。

我得到的错误如下:

 AppComponent.java:12: 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. public abstract interface AppComponent extends dagger.android.AndroidInjector<com.honing.daggerexploration.DaggerExplorationApplication> {
                ^
      java.util.Map<java.lang.Class<? extends androidx.lifecycle.ViewModel>,javax.inject.Provider<androidx.lifecycle.ViewModel>> is injected at
          com.honing.daggerexploration.di.DaggerViewModelFactory(creators)
      com.honing.daggerexploration.di.DaggerViewModelFactory is injected at
          com.honing.daggerexploration.features.MainActivity.viewModelFactory
      com.honing.daggerexploration.features.MainActivity is injected at
          dagger.android.AndroidInjector.inject(T) [com.honing.daggerexploration.di.AppComponent → com.honing.daggerexploration.di.modules.ActivityModule_BindActivityMain.MainActivitySubcomponent]

我搜索了其他stackoverflow问题,但是没有一个问题为我解决了这个问题。

我使用的Dagger版本是最新的2.22.1

我敢打赌这个错误与MVRX无关,因为我能够在不涉及mvrx视图模型类的情况下在小型库中重现它,但是我的意图是最终将dagger2与mvrx框架一起使用,并能够对其注入依赖项。 / p>

与此相关的一些代码:

DaggerExplorationApplication

class DaggerExplorationApplication : DaggerApplication() {
    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
        return DaggerAppComponent.builder().create(this)
    }

}

DaggerViewModelFactory:

    /**
     * ViewModelFactory which uses Dagger to create the instances.
     */
    class DaggerViewModelFactory @Inject constructor(
        private val creators: @JvmSuppressWildcards Map<Class<out ViewModel>, Provider<ViewModel>>
    ) : ViewModelProvider.Factory {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            var creator: Provider<out ViewModel>? = creators[modelClass]
            if (creator == null) {
                for ((key, value) in creators) {
                    if (modelClass.isAssignableFrom(key)) {
                        creator = value
                        break
                    }
                }
            }
            if (creator == null) {
                throw IllegalArgumentException("Unknown model class: $modelClass")
            }
            try {
                @Suppress("UNCHECKED_CAST")
                return creator.get() as T
            } catch (e: Exception) {
                throw RuntimeException(e)
            }
        }
    }

ViewModelFactoryModule

@Module
abstract class ViewModelFactoryModule {

    @Binds
    abstract fun bindViewModelFactory(viewModelFactory: DaggerViewModelFactory): ViewModelProvider.Factory
}

ActivityModule

@Module
abstract class ActivityModule {


    @ContributesAndroidInjector(modules = [ViewModelFactoryModule::class])
    abstract fun bindActivityMain(): MainActivity
}

关于我用匕首实现mvrx的工作,根据这一点,我需要按平方使用AssistedInject库,我观看了视频并相当了解其背后的原因。但是由于上述错误,我无法进行项目构建。 chrisbanes对此话题的一个有趣话题是在this link

带有dagger2的MVRX ViewModels已由chrisbanes使用this project(Tivi)成功实现,我尝试遵循它们所做的操作,但我也失败了。帖子顶部描述的问题阻止了我。准备提供任何缺少的代码,如果需要,可以提供更多信息以解决此问题。

2 个答案:

答案 0 :(得分:1)

这就是我使用MVVM架构使我的Android应用程序与Dagger 2一起工作的方式。这是Java,但我希望它可以始终将您带向正确的方向。

AppComponent

@ApplicationScope
@Component(modules = {
        AndroidInjectionModule.class,
        AppModule.class,
        ActivityBuilder.class
})
public interface AppComponent {

    void inject(MyApp app);

    @Component.Builder
    interface Builder {

        @BindsInstance
        Builder application(Application application);

        AppComponent build();
    }
}

ViewModelFactory

@ApplicationScope
public class ViewModelFactory implements ViewModelProvider.Factory {
    private final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators;

    @Inject
    public ViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
        this.creators = creators;
    }

    @SuppressWarnings("unchecked")
    @Override
    @NonNull
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        Provider<? extends ViewModel> creator = creators.get(modelClass);
        if (creator == null) {
            for (Map.Entry<Class<? extends ViewModel>, Provider<ViewModel>> entry : creators.entrySet()) {
                if (modelClass.isAssignableFrom(entry.getKey())) {
                    creator = entry.getValue();
                    break;
                }
            }
        }
        if (creator == null) {
            throw new IllegalArgumentException("unknown model class " + modelClass);
        }
        try {
            return (T) creator.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

ViewModelModule

@Module
public abstract class ViewModelModule {
    @Binds
    @IntoMap
    @ViewModelKey(MainActivityViewModel.class)
    abstract ViewModel bindMainActivityViewModel(MainActivityViewModel mainActivityViewModel);

    // Same thing for each view model

    ...

}

ActivityBuilder

@Module
public abstract class ActivityBuilder {

    @ContributesAndroidInjector(modules = {
            MainActivityModule.class
            // Add the Provider of each child fragment's viewmodel.
    })
    public abstract MainActivity bindMainActivity();

    // Same for each new activity
}

ViewModelKey

@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@MapKey
public @interface ViewModelKey {
    Class<? extends ViewModel> value();
}

ApplicationScope

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ApplicationScope {
}

AppModule

@Module(includes = {
        ViewModelModule.class,
})
public class AppModule {

    // Provides all the things needed for the whole application, such as Daos, Retrofit interface, contexts, etc.

}

MyApp

public class MyApp extends Application implements HasActivityInjector {

    @Inject
    DispatchingAndroidInjector<Activity> activityDispatchingAndroidInjector;


    @Override
    public void onCreate() {

        DaggerAppComponent
                .builder()
                .application(this)
                .build()
                .inject(this);

        super.onCreate();
    }

    @Override
    public DispatchingAndroidInjector<Activity> activityInjector() {
        return activityDispatchingAndroidInjector;
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {
    @Inject
    ViewModelProvider.Factory mViewModelFactory;

    private MainActivityViewModel mViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewModel = ViewModelProviders.of(this, mViewModelFactory).get(MainActivityViewModel.class);

        ...
     }
}

MainActivityModule

@Module
public class MainActivityModule {

    //Provides all the things needed just for your MainActivity

}

MainActivityViewModel

public class MainActivityViewModel extends ViewModel {

    // Member variables

    @Inject
    public MainActivityViewModel(... things to inject into the viewmodel, such as daos, repositories, contexts, etc. ... ) {
         ...
    }

}

因此,它看起来像很多配置,但是一旦完成了初始设置,在此基础上构建起来将更加容易。

总结:

  • 创建注释@ApplicationScope(或使用默认的@Singleton),更重要的是创建@ViewModelKey注释。
  • 使您的应用程序类实现HasActivityInjector并使用DaggerAppComponent构建器进行注入。
  • 完全像我之前提到的那样实施AppComponentViewModelFactory
  • 定义您的AppModule,以提供您的应用所需的所有内容。但是请记住包括ViewModelModule,因为该人负责提供ViewModel。
  • 每次您要使用自己的ViewModel添加新活动时,都必须执行以下操作:
    1. 创建ActivityActivityModuleActivityViewModel
    2. ViewModelModule中添加一个条目以绑定视图模型。
    3. ActivityBuilder中添加一个条目以提供活动。
    4. 享受。

答案 1 :(得分:1)

您缺少地图多重绑定的配置。

Tivi有@ViewModelKey

/*
 * Copyright 2017 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package app.tivi.inject

import androidx.lifecycle.ViewModel
import dagger.MapKey
import kotlin.reflect.KClass

@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ViewModelKey(val value: KClass<out ViewModel>)

它具有一个模块,该模块将ViewModelKey绑定到ViewModel的特定子类型,使其以ViewModel(并带有键标记)的形式显示:

/*
 * Copyright 2017 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
@Binds
@IntoMap
@ViewModelKey(PopularShowsViewModel::class)
abstract fun bindPopularShowsViewModel(viewModel: PopularShowsViewModel): ViewModel

@Binds
@IntoMap
@ViewModelKey(TrendingShowsViewModel::class)
abstract fun bindTrendingShowsViewModel(viewModel: TrendingShowsViewModel): ViewModel

@Binds
@IntoMap
@ViewModelKey(ShowDetailsNavigatorViewModel::class)
abstract fun bindDetailsNavigatorViewModel(viewModel: ShowDetailsNavigatorViewModel): ViewModel

因此,您需要使用模块将这些多绑定配置设置为组件。

同样重要的是,它们的ViewModel类必须具有@Inject注释的构造函数,才能正常工作。