Skip to content

Commit

Permalink
Dagger multiple dependency changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sreeharivhth committed Aug 23, 2018
1 parent 9ca5f68 commit 444ed10
Show file tree
Hide file tree
Showing 20 changed files with 280 additions and 209 deletions.
48 changes: 15 additions & 33 deletions app/src/main/java/com/dev/movieapp/app/MovieApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,32 @@
import android.app.Application;
import android.content.res.Configuration;

import com.dev.movieapp.components.ContextComponent;
import com.dev.movieapp.components.DaggerContextComponent;
import com.dev.movieapp.components.DaggerNetworkComponent;
import com.dev.movieapp.components.NetworkComponent;
import com.dev.movieapp.modules.ContextModule;
import com.dev.movieapp.modules.NetworkModule;
import com.dev.movieapp.dipinject.components.ApplicationComponent;
import com.dev.movieapp.dipinject.components.DaggerApplicationComponent;
import com.dev.movieapp.dipinject.modules.ApplicationModule;
import com.dev.movieapp.dipinject.modules.NetworkModule;

import java.io.File;

public class MovieApp extends Application {

private static MovieApp movieAppInstance;
private NetworkComponent mNetworkComponent;
private ContextComponent mContextComponent;
private ApplicationComponent mApplicationComponent;
private File mCacheFile;

@Override
public void onCreate() {
super.onCreate();
movieAppInstance=this;

File cacheFile = new File(getCacheDir(), "popularmovieresponses");
mNetworkComponent = createNetworkComponent(cacheFile);
mContextComponent = createContextComponent();
}

public static MovieApp getInstance(){
return movieAppInstance;
}

private NetworkComponent createNetworkComponent(File cacheFile) {
return DaggerNetworkComponent.builder().networkModule(new NetworkModule(cacheFile)).build();
mCacheFile = new File(getCacheDir(), "popular_movie_response");
}

public NetworkComponent getNetworkComponent() {
return mNetworkComponent;
public ApplicationComponent getApplicationComponent() {
if (mApplicationComponent == null) {
mApplicationComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.networkModule(new NetworkModule(mCacheFile, this))
.build();
}
return mApplicationComponent;
}

@Override
Expand All @@ -54,13 +45,4 @@ public void onLowMemory() {
public void onTerminate() {
super.onTerminate();
}

private ContextComponent createContextComponent() {

return DaggerContextComponent.builder().contextModule(new ContextModule(this)).build();
}

public ContextComponent getContextComponent(){
return mContextComponent;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.dev.movieapp.dipinject.components;


import com.dev.movieapp.dipinject.customscopes.ApplicationScope;
import com.dev.movieapp.dipinject.modules.ApplicationModule;
import com.dev.movieapp.dipinject.modules.NetworkModule;

import dagger.Component;

@ApplicationScope
@Component
(
modules = {ApplicationModule.class, NetworkModule.class}
)
public interface ApplicationComponent {


InjectionSubComponent newInjectionComponent();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.dev.movieapp.dipinject.components;

import com.dev.movieapp.ui.activities.landing.ResultListActivity;
import com.dev.movieapp.ui.activities.splash.SplashActivity;
import com.dev.movieapp.ui.fragments.detailfrag.ResultDetailFragment;
import com.dev.movieapp.ui.fragments.detailtabfrag.ResultDetailTabFragment;

import dagger.Subcomponent;

@Subcomponent
public interface InjectionSubComponent {

void inject(ResultListActivity activity);

void inject(ResultDetailFragment fragment);

void inject(SplashActivity activity);

void inject(ResultDetailTabFragment fragment);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.dev.movieapp.dipinject.customscopes;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.inject.Scope;

/**
* Custom scope for global application singleton dependencies
*/

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ApplicationScope {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.dev.movieapp.dipinject.modules;

import android.app.Application;

import com.dev.movieapp.dipinject.customscopes.ApplicationScope;
import com.dev.movieapp.utils.AppLogger;
import com.dev.movieapp.utils.AppUtils;

import dagger.Module;
import dagger.Provides;

@Module
public class ApplicationModule {

private final Application mApplication;

public ApplicationModule(Application application) {
mApplication = application;
}

@Provides
@ApplicationScope
Application application() {
return mApplication;
}

@Provides
@ApplicationScope
AppLogger logger() {
return new AppLogger();
}

@Provides
@ApplicationScope
AppUtils getUtils() {
return new AppUtils(mApplication);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.dev.movieapp.modules;
package com.dev.movieapp.dipinject.modules;

import android.content.Context;
import android.util.Log;

import com.dev.movieapp.BuildConfig;
import com.dev.movieapp.dipinject.customscopes.ApplicationScope;
import com.dev.movieapp.networking.NetworkProcessor;
import com.dev.movieapp.networking.NetworkService;
import com.dev.movieapp.utils.AppUtils;
Expand All @@ -14,16 +16,13 @@
import java.util.Collections;
import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.Cache;
import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.TlsVersion;
import retrofit2.Retrofit;
Expand All @@ -37,26 +36,29 @@
public class NetworkModule {

private File mCacheFile;
private Context mContext;

/**
* Constructor for NetworkModule
*
* @param cacheFile
*/
public NetworkModule(File cacheFile) {
public NetworkModule(File cacheFile, Context context) {
this.mCacheFile = cacheFile;
mContext = context;
}

@Provides
@Singleton
Retrofit provideCall(){
@ApplicationScope
Retrofit provideCall() {

//Sets up default cache with 10 MB
Cache cache = null;
int cacheSize = 10 * 1024 * 1024;
try {
cache = new Cache(mCacheFile, cacheSize);
} catch (Exception e) {
Log.e("TAG","Cache create error "+e.toString());
Log.e("TAG", "Cache create error " + e.toString());
}

ConnectionSpec spec = new
Expand All @@ -72,42 +74,42 @@ Retrofit provideCall(){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(cache_interceptor)
.cache(cache)
.connectTimeout(AppUtils.TIME_OUT , TimeUnit.SECONDS)
.readTimeout(AppUtils.TIME_OUT , TimeUnit.SECONDS)
.connectTimeout(AppUtils.TIME_OUT, TimeUnit.SECONDS)
.readTimeout(AppUtils.TIME_OUT, TimeUnit.SECONDS)
.connectionSpecs(Collections.singletonList(spec))
.build();

// RxJava2 service call support
// Sets OkHttpClient
// Sets Gson converter
// RxJava2 service call support
// Sets OkHttpClient
// Sets Gson converter
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.addConverterFactory(providesGsonConverterFactory())
.baseUrl(BuildConfig.BASE_URL).build();
return retrofit;
return retrofit;

}

@Provides
@Singleton
@ApplicationScope
@SuppressWarnings("unused")
public NetworkService providesNetworkService(Retrofit retrofit) {
return retrofit.create(NetworkService.class);
}

@Provides
@Singleton
@ApplicationScope
@SuppressWarnings("unused")
public NetworkProcessor providesService(
NetworkService networkService) {
public NetworkProcessor providesService(NetworkService networkService) {
return new NetworkProcessor(networkService);
}

private static final Interceptor cache_interceptor = new Interceptor() {
@Override public Response intercept(Chain chain) throws IOException {
private final Interceptor cache_interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
if (new AppUtils().isNetworkConnected()) {
if (new AppUtils(mContext).isNetworkConnected()) {
int maxAge = 60; // read from cache for 1 minute
return originalResponse.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
Expand All @@ -118,7 +120,8 @@ public NetworkProcessor providesService(
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
}};
}
};

private GsonConverterFactory providesGsonConverterFactory() {
GsonBuilder gsonBuilder = new GsonBuilder();
Expand Down
27 changes: 0 additions & 27 deletions app/src/main/java/com/dev/movieapp/modules/ContextModule.java

This file was deleted.

9 changes: 0 additions & 9 deletions app/src/main/java/com/dev/movieapp/modules/GlideModule.java

This file was deleted.

Loading

0 comments on commit 444ed10

Please sign in to comment.