使用Userscope的Dagger AndroidInjection

时间:2018-07-18 09:20:04

标签: android dagger-2 dagger

我遇到了匕首2的问题。

我希望至少有两个具有两个不同范围的组件:

  
      
  • 单人
  •   
  • 用户范围
  •   
用户成功登录后,应创建

UserScope

我已经创建了一个演示者工厂,它将使用绑定和 @Inject 构造函数来提供完全注入的演示者:

    @Singleton
public class PresenterFactory {

    private final Map<Class<? extends Presenter>, Provider<Presenter>> mCreators;

    @Inject
    public PresenterFactory(Map<Class<? extends Presenter>, Provider<Presenter>> creators) {
        mCreators = creators;
    }

    /**
     * Creates a fully injected presenter. If no presenter can be instantiated, an exception is thrown.
     *
     * @param presenterClass Presenter class.
     * @param <T>            Presenter instance.
     * @return Instance of presenter.
     */
    @SuppressWarnings("unchecked")
    public <T extends Presenter> T create(Class<T> presenterClass) {
        Provider<Presenter> presenterProvider = mCreators.get(presenterClass);
        if (presenterProvider == null) {
            for (Map.Entry<Class<? extends Presenter>, Provider<Presenter>> entry : mCreators.entrySet()) {
                if (presenterClass.isAssignableFrom(entry.getKey())) {
                    presenterProvider = entry.getValue();
                }
            }
        }

        if (presenterProvider == null) {
            throw new IllegalArgumentException("Class " + presenterClass + " seems not provided");
        }

        try {
            return (T) presenterProvider.get();
        } catch (Exception e) {
            MVPLogger.e("PresenterFactory", e.getMessage(), e);
            throw e;
        }
    }
}

实际上,当我想在单例范围内注入活动和演示者时,一切正常,但我无法使用用户范围。 我的代码是:

应用程序:

public class MVPApplication extends Application implements HasActivityInjector {

    private static AppComponent mAppComponent;

    private static UserComponent mUserComponent;

    @Inject
    DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;

    @Override
    public void onCreate() {
        super.onCreate();

        initializeInjector();
    }

    /**
     * Initializes the injector.
     */
    private void initializeInjector() {
        mAppComponent = DaggerAppComponent
            .builder()
            .application(this)
            .build();

        mAppComponent.inject(this);
    }

    public static void makeUserComponent(){
        mUserComponent = mAppComponent.createUserComponent().build();
    }

    public static UserComponent getUserComponent(){
        return mUserComponent;
    }

    @Override
    public AndroidInjector<Activity> activityInjector() {
        return dispatchingAndroidInjector;
    }
}

AppComponent:

@Singleton
@Component(modules = {
        AndroidInjectionModule.class,
        ActivityBindingModule.class,
        CoreModule.class,
        AppModule.class,
        PresenterModule.class
})
public interface AppComponent {

    void inject(MVPApplication application);

    UserComponent.Builder createUserComponent();

    @Component.Builder
    interface Builder {

        /** Add the application object to the graph. */
        @BindsInstance
        Builder application(MVPApplication application);

        /** Build the component. */
        AppComponent build();
    }
}

ActivityBindingModule:

@Module
public abstract class ActivityBindingModule {

    @ContributesAndroidInjector
    abstract MainActivity contributeMainActivity();

    @ContributesAndroidInjector
    abstract UserActivity contributeUserActivity();
}

CoreModule(来自另一个项目模块):

@Module
public class CoreModule {

    /**
     * Provides the presenter cache.
     *
     * @return Presenter cache.
     */
    @Singleton
    @Provides
    public PresenterCache getPresenterCache(){
        return new PresenterCache();
    }
}

AppModule:

@Module
public class AppModule {

    @Singleton
    @Provides
    public TaskManager getTaskManager() {
        return new TaskManager();
    }
}

PresenterModule:

@Module
public abstract class PresenterModule {

    @Binds
    @IntoMap
    @PresenterKey(MainPresenter.class)
    abstract Presenter bindMainPresenter(MainPresenter presenter);
}

UserComponent:

@UserScope
@Subcomponent(modules = {
        UserModule.class,
        UserPresenterModule.class,

})
public interface UserComponent {

    @Subcomponent.Builder
    interface Builder {
        UserComponent build();
    }
}

UserModule:

@Module
public class UserModule {

    @Provides
    @UserScope
    @Named("TestString")
    public String getString(){
        return "Injected";
    }
}

UserPresenterModule:

@Module
public abstract class UserPresenterModule {

    @Binds
    @IntoMap
    @PresenterKey(UserPresenter.class)
    abstract Presenter bindUserPresenter(UserPresenter presenter);
}

我正在使用AndroidInjection.inject(this);

注入我的活动

我的 UserPresenter 被注入

@Inject
public UserPresenter(TaskManager taskManager, @Named("testString") String test) 
{
    mTaskManager = taskManager;
    mString = test;
}

我知道我想念一些东西,但我不知道在什么地方。我想避免做

public UserPresenter(){
     MVPApplication.getUserComponent().inject(this);
}

为了使我的演示者易于使用注入的构造函数进行测试。

我得到的错误是:似乎未提供类class UserPresenter

谢谢您的帮助。

0 个答案:

没有答案