Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dagger 2

Dagger 2

Slides da palestra sobre Dagger 2

José Caique Oliveira

February 16, 2017
Tweet

More Decks by José Caique Oliveira

Other Decks in Programming

Transcript

  1. Provides Identifica que um método é um provedor de dependência

    @Module public class LoginModule { @Provides AuthenticationLayer providesAuthenticationLayer(Context context) { return new AuthenticationLayer(context); } }
  2. Outro “provedor” @Module public class ApplicationModule { private Application application;

    public ApplicationModule(Application application) { this.application = application; } @Provides Application providesApplication() { return application; } @Provides Context providesContext(Application application) { return application.getBaseContext(); } }
  3. Component Define quais módulos podem ser utilizados e quem pode

    utilizar tais módulos @Component(modules = { ApplicationModule.class}) public interface MainComponent { void inject(LoginActivity activity); }
  4. SubComponent Repartição do grafo de dependências @ActivityScope @Subcomponent(modules = {LoginModule.class})

    public interface ActivityComponent { void inject(LoginActivity activity); } @Component(modules = {ApplicationModule.class}) public interface MainComponent { ActivityComponent activityComponent(); FragmentComponent fragmentComponent(); }
  5. Scopo Altera o ciclo de vida de um objeto @Scope

    @Retention(RetentionPolicy.RUNTIME) public @interface ActivityScope { }
  6. Reusable Indica que um dado objeto pode ser reutilizado @Module

    public class ManagerModule { @Reusable @Provides DataController providesDataController(Context context) { return new DataController(context); } @Provides @Reusable AuthenticationLayer providesAuthenticationLayer(Context context, AuthenticationClient authenticationClient) { return new AuthenticationLayer(context,authenticationClient); } }
  7. Singleton Indica que se precisa da mesma instância do objeto

    @Module public class ManagerModule { @Reusable @Singleton DataController providesDataController(Context context) { return new DataController(context); } @Provides @Singleton AuthenticationLayer providesAuthenticationLayer(Context context, AuthenticationClient authenticationClient) { return new AuthenticationLayer(context,authenticationClient); } }
  8. Inject Indica que é necessário injetar uma depêndencia public class

    LoginActivity extends BaseActivity { @Inject public LoginPresenter loginPresenter; @Override public void onStart() { super.onStart(); ((Application) getApplication()).getMainComponent().activityComponent().inject(this); loginPresenter.bindView(this, this); } }
  9. Inject Indica que é necessário injetar uma depêndencia public class

    Application extends CoreApplication { private MainComponent mainComponent; @Override public void onCreate() { super.onCreate(); initDagger(); } private void initDagger() { mainComponent = DaggerMainComponent.builder().applicationModule(new ApplicationModule(this)).build(); } public MainComponent getMainComponent() { return mainComponent; } }
  10. Refatorando - LoginPresenterImpl class LoginPresenterImpl implements LoginPresenter, OnLoginFinishedListener { private

    Context context; private LoginView loginView; private LoginInteractor loginInteractor; LoginPresenterImpl(Context context, LoginView loginView) { this.context = context; this.loginView = loginView; this.loginInteractor = new LoginInteractorImpl(context); } ... }
  11. Refatorando - LoginPresenterImpl class LoginPresenterImpl implements LoginPresenter, OnLoginFinishedListener { private

    Context context; private LoginView loginView; private LoginInteractor loginInteractor; @Inject LoginPresenterImpl(LoginInteractor loginInteractor) { this.loginInteractor = loginInteractor; } @Override public void bindView(Context context, LoginView view) { this.context = context; this.loginView = view; } }
  12. Refatorando - LoginInteractorImpl class LoginInteractorImpl implements LoginInteractor { private Context

    context; LoginInteractorImpl(Context context) { this.context = context; } @Override public void authenticate(String email, String password, OnLoginFinishedListener listener) { //... new AuthenticationLayer(context).postAuthenticate(new AuthenticateRequest(email, password)); } @Override public void busAuthenticate(AuthenticateResponse authenticateResponse, OnLoginFinishedListener listener) { if (authenticateResponse.isSuccess()) { saveInformations(authenticateResponse); new UserManager().deleteAll(); new UserLayer(context).getUsers(); }else{ //... } } private void saveInformations(AuthenticateResponse authenticateResponse) { DataController dataController = new DataController(context); dataController.writeData(Constants.SharedPreferences.CONTROLLER_EMAIL, authenticateResponse.getEmail()); } }
  13. Refatorando - LoginInteractorImpl class LoginInteractorImpl implements LoginInteractor { private UserLayer

    userLayer; private Context context; private AuthenticationLayer authenticationLayer; private DataController dataController; private UserManager userManager; @Inject LoginInteractorImpl(Context context, AuthenticationLayer authenticationLayer, DataController dataController, UserManager userManager, UserLayer userLayer) { this.authenticationLayer = authenticationLayer; this.context = context; this.dataController = dataController; this.userManager = userManager; this.userLayer = userLayer; } … }
  14. LoginModule @Module public class LoginModule { @Provides AuthenticationLayer providesAuthenticationLayer(Context context)

    { return new AuthenticationLayer(context); } @Provides @Reusable LoginInteractor providesLoginInteractor(Context context, AuthenticationLayer layer, DataController dataController, UserManager userManager, UserLayer userLayer) { return new LoginInteractorImpl(context, layer, dataController, userManager, userLayer); } @Provides @Reusable LoginPresenter providesLoginPresenter(LoginInteractor loginInteractor) { return new LoginPresenterImpl(loginInteractor); }
  15. LoginModule @Module public class LoginModule { @Provides @Reusable DataController providesDataController(Context

    context) { return new DataController(context); } @Provides @Reusable UserLayer providesUserLayer(Context context) { return new UserLayer(context); } @Provides @Reusable UserManager providesUserManager() { return new UserManager(); } }