Android-Dagger2和MVP使用示例

試著忘記壹切 提交于 2020-01-16 12:09:49

Dagger使用整理:https://blog.csdn.net/ahou2468/article/details/103952052

MVP使用整理:https://blog.csdn.net/ahou2468/article/details/103975635
MVP-模型-视图-表现层(Presenter):Presenter相当于Model和View通信的桥梁,控制着程序的全部逻辑,在Presenter将View和Model对象注入到Presenter;

Dagger:是一个依赖注入框架,Dagger负责实现View和Model注入到Presenter;

目录

1.MVP实现

1.1定义Presenter和View接口

1.2Presenter实现

1.3实现View

2.Dagger实现MVP中依赖的注入

2.1定义Module和Provider实例提供方

2.2定义Component

2.3实现Dagger中Component和与MVP的绑定

a.将会生成Dagger创建Model的工厂方法:(依赖注入提供方)

b.将会生成Dagger创建实现Component的接口的方法:(依赖注入具体消费方)

c.在Activity/Fragment中调用Component接口的实现类,实现绑定操作


1.MVP实现

1.1定义Presenter和View接口

Presenter:实现具体的控制逻辑,定义View调用Presenter通信的方法;

View:定义了Presenter和View的通信方法,Presenter调用实现的View中的方法通知更新UI;

public interface SplashContract {
    interface Presenter {
        void getSplash(String deviceId);
    }
    interface View {
        void updateUI();
    }
}

1.2Presenter实现

实现Presenter接口,通过@Inject标示需要注入View和相关Model;

public class SplashPresenter implements SplashContract.Presenter{
    private SplashContract.View view;
    private ApiService apiService;
    @Inject
    public SplashPresenter(SplashContract.View view, ApiService apiService) {
        this.view = view;
        this.apiService = apiService;
        Logger.d("apppp:"+apiService);
    }
    @Override
    public void getSplash(String deviceId ) {
        String client = "android";
        String version = "1.3.0";
        Long time = TimeUtil.getCurrentSeconds();
        apiService.getSplash(client,version,time,deviceId)
                        ......;
    }
}

1.3实现View

实现View接口,通过@Inject标示要创建Presenter对象;

public class SplashActivity extends BaseActivity implements SplashContract.View, EasyPermissions.PermissionCallbacks {
    @Inject
    SplashPresenter presenter;
}
调用
presenter.getSplash(deviceId);

2.Dagger实现MVP中依赖的注入

2.1定义Module和Provider实例提供方

提供创建Presenter所需注入的依赖(实例)

@Module
public class SplashModule {
    private SplashContract.View view;

    public SplashModule(SplashContract.View view) {
        this.view = view;
    }
    @Provides
    public SplashContract.View provideView(){
        return view;
    }
}

2.2定义Component

定义Component,Component实现接口将实现Presenter对象的创建,负责找到或者创建Presenter需要的依赖对象;

@Component(modules = SplashModule.class,dependencies = NetComponent.class)
public interface SplashComponent {
    void inject(SplashActivity splashActivity);
}

2.3实现Dagger中Component和与MVP的绑定

注意:添加Dagger中的Component和Model要注意build Project重新运行工程;

生成的Model和Component都在对应的Package包下;

a.将会生成Dagger创建Model的工厂方法:(依赖注入提供方)

public final class SplashModule_ProvideViewFactory implements Factory<SplashContract.View> {
  private final SplashModule module;

  public SplashModule_ProvideViewFactory(SplashModule module) {
    this.module = module;
  }

  @Override
  public SplashContract.View get() {
    return Preconditions.checkNotNull(
        module.provideView(), "Cannot return null from a non-@Nullable @Provides method");
  }

  public static SplashModule_ProvideViewFactory create(SplashModule module) {
    return new SplashModule_ProvideViewFactory(module);
  }

  public static SplashContract.View proxyProvideView(SplashModule instance) {
    return Preconditions.checkNotNull(
        instance.provideView(), "Cannot return null from a non-@Nullable @Provides method");
  }
}

b.将会生成Dagger创建实现Component的接口的方法:(依赖注入具体消费方)

public final class DaggerSplashComponent implements SplashComponent {
  private SplashModule splashModule;

  private NetComponent netComponent;

  private DaggerSplashComponent(Builder builder) {
    initialize(builder);
  }

  public static Builder builder() {
    return new Builder();
  }
  //在Component接口实现类中创建Presenter对象;
  private SplashPresenter getSplashPresenter() {
    return new SplashPresenter(
        SplashModule_ProvideViewFactory.proxyProvideView(splashModule),
        Preconditions.checkNotNull(
            netComponent.getApiService(),
            "Cannot return null from a non-@Nullable component method"));
  }

  @SuppressWarnings("unchecked")
  private void initialize(final Builder builder) {
    this.splashModule = builder.splashModule;
    this.netComponent = builder.netComponent;
  }

  @Override
  public void inject(SplashActivity splashActivity) {
    injectSplashActivity(splashActivity);
  }
    
  //将创建的Presenter对象注入到Activity中@Inject注解的presenter字段
  private SplashActivity injectSplashActivity(SplashActivity instance) {
    SplashActivity_MembersInjector.injectPresenter(instance, getSplashPresenter());
    return instance;
  }

  public static final class Builder {
    private SplashModule splashModule;

    private NetComponent netComponent;

    private Builder() {}

    public SplashComponent build() {
      if (splashModule == null) {
        throw new IllegalStateException(SplashModule.class.getCanonicalName() + " must be set");
      }
      if (netComponent == null) {
        throw new IllegalStateException(NetComponent.class.getCanonicalName() + " must be set");
      }
      return new DaggerSplashComponent(this);
    }

    public Builder splashModule(SplashModule splashModule) {
      this.splashModule = Preconditions.checkNotNull(splashModule);
      return this;
    }

    public Builder netComponent(NetComponent netComponent) {
      this.netComponent = Preconditions.checkNotNull(netComponent);
      return this;
    }
  }
}

c.在Activity/Fragment中调用Component接口的实现类,实现绑定操作

DaggerSplashComponent.builder()
                .netComponent(OwspaceApplication.get(this).getNetComponent())   //注入组件依赖的NetComponent
                .splashModule(new SplashModule(this))  //注入组件依赖的提供方Module(Module负责提供对象,Component负责将到提供的对象注入到Presenter同时创建Presenter)
                .build().inject(this);  //注入Activity,在Component中将Presenter对象赋值Activity中的Presenter变量;

注意:Activity将创建Presenter和依赖注入的工作交给将Component接口的实现类,达到降低耦合度的目的;

public class SplashActivity extends BaseActivity implements SplashContract.View, EasyPermissions.PermissionCallbacks {
    @Inject
    SplashPresenter presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DaggerSplashComponent.builder()
                .netComponent(OwspaceApplication.get(this).getNetComponent())
                .splashModule(new SplashModule(this))
                .build().inject(this);
    }
}

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!