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

Порядочные связи вместе с Dagger2

Порядочные связи вместе с Dagger2

Andrii Horishnii - android developer at Ekreative
Часом буває так, що треба додати малесеньку фічу до майже готового проекту. Щоб цей процес не спричиняв головного болю через сильну зв’язність компонентів проекту - слід зазадалегідь потурбуватися про залежності.
Як зробити життя простішим собі та іншим? Дізнаємось вже цієї суботи та розглянемо як зконфігурувати фреймворк Dagger2 для вашого android додатку

GDG Cherkasy

February 27, 2017
Tweet

More Decks by GDG Cherkasy

Other Decks in Programming

Transcript

  1. Dependency inversion principle public interface NotificationHelper { void showNotification(String message);

    } public class NotificationHelperImpl implements NotificationHelper { private Context mContext; public NotificationHelperImpl(Context context) { mContext = context; } @Override public void showNotification(String message) { Toast.makeText(mContext, message, Toast.LENGTH_LONG).show(); } }
  2. Dependency inversion principle public class MainActivity extends AppCompatActivity { private

    NotificationHelper mNotificationHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mNotificationHelper = new NotificationHelperImpl(this); mNotificationHelper.showNotification("It's work! :)"); } }
  3. JSR-330 (javax.inject) @Inject Identifies injectable constructors, methods, and fields. @Named

    String-based qualifier. @Qualifier Identifies qualifier annotations. @Scope Identifies scope annotations. @Singleton Identifies a type that the injector only instantiates once.
  4. @Inject public class MainActivityPresenterImpl implements MainActivityPresenter { private NotificationHelper mNotificationHelper;

    private SaturnMoonsModel mSaturnMoonsModel; @Inject public MainActivityPresenterImpl(NotificationHelper notificationHelper, SaturnMoonsModel saturnMoonsModel) { mNotificationHelper = notificationHelper; mSaturnMoonsModel = saturnMoonsModel; } }
  5. @Inject public class MainActivityPresenterImpl implements MainActivityPresenter { @Inject NotificationHelper mNotificationHelper;

    @Inject SaturnMoonsModel mSaturnMoonsModel; @Inject public MainActivityPresenterImpl() { } }
  6. @Inject public class MainActivityPresenterImpl implements MainActivityPresenter { ... @Inject public

    MainActivityPresenterImpl() { } @Inject public void setNotificationHelper(NotificationHelper notificationHelper) { mNotificationHelper = notificationHelper; } @Inject public void setSaturnMoonsModel(SaturnMoonsModel saturnMoonsModel) { mSaturnMoonsModel = saturnMoonsModel; } }
  7. Dagger2 basics Module Modules provide dependencies through public methods that

    return the objects they provide. Provides Method to satisfy a dependency. The method’s return type defines which dependency it satisfies. Component Components are the objects that requesters contact for dependencies.
  8. @Module @Module public class AppModule { private Application mApplication; public

    AppModule(Application application) { mApplication = application; } ... }
  9. @Provides @Module public class AppModule { private Application mApplication; public

    AppModule(Application application) { mApplication = application; } @Provides Context provideContext() { return mApplication; } }
  10. @Provides @Module public class AppModule { @Provides @Singleton SharedPreferences provideSharedPreferences(Context

    context) { return context.getSharedPreferences("data", Context.MODE_PRIVATE); } @Provides @Named("AnotherPreferences") SharedPreferences provideAnotherSharedPreferences(Context context) { return context.getSharedPreferences("another", Context.MODE_PRIVATE); } }
  11. @Provides @Module public class PresenterModule { @Provides MainActivityPresenter providePresenter(MainActivityPresenterImpl presenter)

    { return presenter; } } public class MainActivityPresenterImpl implements MainActivityPresenter { @Inject public MainActivityPresenterImpl(NotificationHelper notificationHelper, SaturnMoonsModel saturnMoonsModel) { ... } }
  12. @Component @Singleton @Component(modules = {AppModule.class, HelperModule.class}) public interface AppComponent {

    void inject(MyActivity activity); void inject(MyFragment fragment); void inject(MyService service); }
  13. @Component public class AppClass extends Application { private AppComponent mAppComponent;

    @Override public void onCreate() { super.onCreate(); mAppComponent = DaggerAppComponent.builder() .appModule(new AppModule(this)) .build(); } public static AppComponent getAppComponent(Context context) { return ((AppClass) context.getApplicationContext()).mAppComponent; } }
  14. @Inject into Android classes public class MainActivity extends AppCompatActivity {

    @Inject NotificationHelper mNotificationHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AppClass.getAppComponent(this).inject(this); mNotificationHelper.showNotification("It's work! :)"); } }
  15. @Component @Singleton @Component(modules = {AppModule.class, HelperModule.class}) public interface AppComponent {

    ... NotificationHelper getNotificationHelper(); } mNotificationHelper = getAppComponent().getNotificationHelper();
  16. @Subcomponent @ActivityScope @Subcomponent(modules = {PresenterModule.class}) public interface ActivityComponent { void

    inject(MainActivity activity); } @Singleton @Component(modules = {AppModule.class, HelperModule.class}) public interface AppComponent { ActivityComponent plusActivityComponent(); ... }
  17. @Subcomponent public class MainActivity extends AppCompatActivity implements MainView { @Inject

    MainActivityPresenter mPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActivityComponent activityComponent = AppClass.getAppComponent(this).plusActivityComponent(); activityComponent.inject(this); } }