SHARE
TWEET

Untitled

a guest Jan 2nd, 2017 266 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Module
  2. public final class AndroidModule {
  3.  
  4.     private final Application mApplication;
  5.  
  6.     public AndroidModule(Application application) {
  7.         mApplication = checkNotNull(application);
  8.     }
  9.  
  10.     @Provides
  11.     @Singleton
  12.     Application providesApplication() {
  13.         return mApplication;
  14.     }
  15.  
  16. }
  17.  
  18.  
  19. @Singleton
  20. @Component(modules = {
  21.     AndroidModule.class
  22. })
  23. public interface ApplicationComponent {
  24.  
  25.     Application getApplication();
  26.  
  27.     @ApplicationContext
  28.     Context getApplicationContext();
  29.  
  30. }
  31.  
  32. @Module(includes = {
  33.     AndroidModule.class
  34. })
  35. public final class NetworkModule {
  36.  
  37.     private static final int DEFAULT_CACHE_SIZE = 10 * 1024 * 1024; // 10 Mib
  38.  
  39.     public NetworkModule() {
  40.         // Default empty constructor
  41.     }
  42.  
  43.     @Provides
  44.     @Singleton
  45.     Cache provideOkHttpCache(Application application) {
  46.         return new Cache(application.getCacheDir(), DEFAULT_CACHE_SIZE);
  47.     }
  48.  
  49.     @Provides
  50.     @Singleton
  51.     OkHttpClient provideOkHttpClient(Cache cache) {
  52.         return new OkHttpClient.Builder().cache(cache).build();
  53.     }
  54.  
  55. }
  56.  
  57.  
  58. @Singleton
  59. @Component(modules = {
  60.     NetworkModule.class
  61. })
  62. public interface FragmentComponent {
  63.  
  64.     void inject(MyFragment myFragment);
  65.  
  66. }
  67.  
  68. @Module
  69. public final class OsModule {
  70.  
  71.     public OsModule() {
  72.         // Default empty constructor
  73.     }
  74.  
  75.     @Provides
  76.     @Singleton
  77.     Handler provideMainHandler() {
  78.         return new Handler(Looper.getMainLooper());
  79.     }
  80.  
  81. }
  82.  
  83. @Singleton
  84. @Component(modules = {
  85.     NetworkModule.class,
  86.     OsModule.class
  87. })
  88. public interface PojoComponent {
  89.  
  90.     void inject(MyPojo myPojo);
  91.  
  92. }
  93.  
  94. public class MyApplication extends Application {
  95.  
  96.     private static ApplicationComponent sApplicationComponent;
  97.     private static FragmentComponent sFragmentComponent;
  98.     private static PojoComponent sPojoComponent;
  99.  
  100.     public static ApplicationComponent getApplicationComponent() {
  101.         return sApplicationComponent;
  102.     }
  103.  
  104.     public static FragmentComponent getFragmentComponent() {
  105.         return sFragmentComponent;
  106.     }
  107.  
  108.     public static PojoComponent getPojoComponent() {
  109.         return sPojoComponent;
  110.     }
  111.  
  112.     @Override
  113.     public void onCreate() {
  114.         initializeInjectionComponents();
  115.     }
  116.  
  117.     private void initializeInjectionComponents() {
  118.         AndroidModule androidModule = new AndroidModule(this);
  119.         NetworkModule networkModule = new NetworkModule();
  120.  
  121.         sApplicationComponent = DaggerApplicationComponent.builder()
  122.             .androidModule(androidModule)
  123.             .build();
  124.  
  125.         sFragmentComponent = DaggerFragmentComponent.builder()
  126.             .androidModule(androidModule)
  127.             .networkModule(networkModule)
  128.             .build();
  129.  
  130.         sPojoComponent = DaggerPojoComponent.builder()
  131.             .androidModule(androidModule)
  132.             .networkModule(networkModule)
  133.             .osModule(new OsModule())
  134.             .build();
  135.     }
  136.  
  137. }
  138.  
  139. public class MyFragment extends Fragment {
  140.  
  141.     @Inject
  142.     OkHttpClient mOkHttpClient;
  143.  
  144.     @Override
  145.     public void onCreate(@Nullable Bundle savedInstanceState) {
  146.         super.onCreate(savedInstanceState);
  147.  
  148.         MyApplication.getFragmentComponent().inject(this);
  149.     }
  150.  
  151. }
  152.  
  153. public class MyPojo {
  154.  
  155.     @Inject
  156.     OkHttpClient mOkHttpClient;
  157.  
  158.     @Inject
  159.     Handler mMainHandler;
  160.  
  161.     public MyPojo() {
  162.         MyApplication.getPojoComponent().inject(this);
  163.     }
  164.  
  165. }
  166.  
  167. abstract class BaseUtils {
  168.  
  169.     static Application getApplication() {
  170.         return BaseApplication.getApplicationComponent().getApplication();
  171.     }
  172.  
  173.     static Context getApplicationContext() {
  174.         return BaseApplication.getApplicationComponent().getApplicationContext();
  175.     }
  176.  
  177. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top