Skip to content

Instantly share code, notes, and snippets.

@alhazmy13
Last active November 18, 2016 00:38
Show Gist options
  • Save alhazmy13/98aedc43f4a36ac52179f5f800cc9edc to your computer and use it in GitHub Desktop.
Save alhazmy13/98aedc43f4a36ac52179f5f800cc9edc to your computer and use it in GitHub Desktop.
Dagger example with App & Net Module
public class App extends Application {
private static final String TAG = "App";
private NetComponent mNetComponent;
@Override
public void onCreate() {
super.onCreate();
mNetComponent = DaggerNetComponent.builder()
.appModule(new AppModule(this)) // This also corresponds to the name of your module: %component_name%Module
.netModule(new NetModule(UrlHelper.getBaseUrl()))
.build();
}
public NetComponent getNetComponent() {
return mNetComponent;
}
}
@Module
class AppModule {
private Application mApplication;
AppModule(Application application) {
mApplication = application;
}
@Provides
@Singleton
Application providesApplication() {
return mApplication;
}
@Provides
@Singleton
Realm providesRealm(Application application) {
RealmConfiguration realmConfig = new RealmConfiguration.Builder(application)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(realmConfig);
return Realm.getDefaultInstance();
}
}
@Singleton
@Component(modules = {AppModule.class, NetModule.class})
public interface NetComponent {
void inject(LoginActivity activity);
}
@Module
class NetModule {
private String mBaseUrl;
// Constructor needs one parameter to instantiate.
NetModule(String baseUrl) {
this.mBaseUrl = baseUrl;
}
@Provides
@Singleton
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
@Provides
@Singleton
Session providesSession(Application application) {
return new Session(application);
}
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gsonBuilder.create();
}
@Provides
@Singleton
Retrofit providesRetrofit(OkHttpClient okHttpClient) {
// TODO: Use your own attributes to track content views in your app
return new Retrofit.Builder()
.baseUrl(mBaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
Interceptor interceptor = new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request()
.newBuilder()
.addHeader("User-Agent", "Android APP").build();
return chain.proceed(newRequest);
}
};
return new OkHttpClient.Builder()
.addInterceptor(logging)
.addInterceptor(interceptor)
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment