Skip to content
rustie edited this page Mar 17, 2019 · 2 revisions

Dagger Doc 1.0

Berkeley Mobile integrates with multiple external cloud providers such as Heroku and Firebase, and other campus tools. Keeping track of each of these through the application flow gets tough as the app gets new features. We deploy Google Dagger 2 as a dependency injector.

Currently we're only using Dagger to inject singletons for each of our backends (e.g. Firebase suite and Heroku).

Components: main interface of injection mappings

Modules: defines singletons injected in hierarchical format

Current dependency structure:

  • RepositoryComponent
    • AppModule: provides application instance and context
    • RepositoryModule: supermodule for various backends supporting data repository abstraction
      • FirebaseModule: handle Firebase suite: Firestore, analytics,
      • RetrofitModule: handles Retrofit interface with BM backend service

RetrofitModule defining HTTP client singleton.

@Provides @Singleton
    public OkHttpClient provideOkHttpClient() {
        // caching config
        File cacheDir = new File(GlobalApplication.getAppContext().getCacheDir(), "okHttpCache");
        Cache cache = new Cache(cacheDir, CACHE_SIZE * 1024 * 1024);

        // okhttp config for cache
        return new OkHttpClient.Builder()
                .cache(cache)
                .addNetworkInterceptor(new ResponseCachingInterceptor())
                .addInterceptor(new OfflineResponseCacheInterceptor())
                .build();
    }
Clone this wiki locally