匕首与导体控制器

时间:2018-01-04 11:10:01

标签: android dagger-2 dagger conductor

我正在尝试使用Dagger 2中的' new 'Android支持。
我想在Dagger中设置以下架构:

  

应用程序=>活动=>控制器(Conductor

(控制器基本上是由系统实例化的View。您可以将其视为Fragment但没有Dagger Injection支持)

对于每个级别,我都定义了一个依赖项:ApplicationDepActivityDepControllerDep

  1. 我的Controller应该能够注入所有这些依赖项。
  2. 我的Activity应该可以注入ApplicationDepActivityDep
  3. 我的Application应该只能注入ApplicationDep
  4. Controller之外,一切正常 我无法注入ActivityDep

    public class MyController extends Controller {
      @Inject
      ApplicationDependency applicationDependency;
      //@Inject
      //ActivityDependency activityDependency; // can not be provided
      @Inject
      ControllerDependency controllerDependency;
    
      @NonNull @Override protected View onCreateView(@NonNull LayoutInflater layoutInflater, @NonNull ViewGroup viewGroup) {
        ConductorInjection.inject(this);
        return layoutInflater.inflate(R.layout.controller_main, viewGroup, false);
      }
    }
    

    目前我的ControllerBindingModule绑定了我的ApplicationComponent,但这应该绑定在Activity上,以便Controller中也可以使用@Singleton @Component(modules = { ApplicationModule.class, ActivityBindingModule.class, AndroidSupportInjectionModule.class, ConductorInjectionModule.class, ControllerBindingModule.class }) interface AppComponent extends AndroidInjector<App> { @Component.Builder abstract class Builder extends AndroidInjector.Builder<App> {} } 。<登记/> 我怎么能这样做?

    import re
    from PIL import Image
    from pytesseract import image_to_string
    
    hand = image_to_string(Image.open("new1.png"))
    m = re.search(r'5545', hand)
    if m:
       print ("the line: ", m.group(0))
    else:
       print "nope"
    

    完整代码可在Github上找到 感谢。

1 个答案:

答案 0 :(得分:2)

听起来控制器是活动组件的子组件 我看了看你的GitHub,所以我改变了你的一些代码来回答。

首先,注射Activity Controller不是Appcomponent的子组件,因此只需要ActivityBindingModule

<强> AppComponent.java

@Singleton
@Component(modules = {
        AppModule.class
        ActivityBindingModule.class,
        AndroidSupportInjectionModule.class,
})
interface AppComponent {
    @Component.Builder
    abstract class Builder extends AndroidInjector.Builder<App> {}
}

出于同样的原因,我们只需要在HasActivityInjector中实施App

<强> App.java

public class App extends Application implements HasActivityInjector {
    @Inject protected DispatchingAndroidInjector<Activity> dispatchingActivityInjector;

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

    @Override
    public void onCreate() {
        super.onCreate();
        DaggerAppComponent
            .builder()
            .create(this)
            .inject(this);
    }
}

因为我需要将Controller声明为ActivityComponent的子组件, 我用的是第2步&amp; Dagger documentation about injecting activity objects

中的第3步

更改ActivityBindingModule

<强> ActivityBindingModule.java

@Module(subcomponents = {MainActivityComponent.class})
public abstract class ActivityBindingModule {

    @Binds
    @IntoMap
    @ActivityKey(MainActivity.class)
    abstract AndroidInjector.Factory<? extends Activity>
    bindYourActivityInjectorFactory(MainActivityComponent.Builder builder);
}

创建ActivityComponent

<强> MainActivityComponent.java

@Subcomponent(modules = {MainActivityModule.class})
@ActivityScope
public interface MainActivityComponent extends AndroidInjector<MainActivity>{
    @Subcomponent.Builder
    abstract class Builder extends AndroidInjector.Builder<MainActivity> {}
}

接下来,是时候将控制器设置为activitycomponent的子组件了 将ControllerBindingModule.class添加到MainActivityComponent

@Subcomponent(modules = {MainActivityModule.class, ControllerBindingModule.class})
@ActivityScope
public interface MainActivityComponent extends AndroidInjector<MainActivity>{
    @Subcomponent.Builder
    abstract class Builder extends AndroidInjector.Builder<MainActivity> {}
}

HasControllerInjector中实施MainActivity

<强> MainActivity.java

public class MainActivity extends AppCompatActivity implements HasControllerInjector {

    @Inject protected DispatchingAndroidInjector<Controller> dispatchingControllerInjector;

    @Override
    public DispatchingAndroidInjector<Controller> controllerInjector() {
        return dispatchingControllerInjector;
}

无需更改其他文件,您想要完成的任务。