Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from freefair/develop
Browse files Browse the repository at this point in the history
retrofit2 and okhttp3
  • Loading branch information
larsgrefer committed Feb 1, 2016
2 parents 51a862c + 620a1ff commit 8a82c46
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ The modules enable the injection of certain classes:
- `io.freefair.android.util.logging.Logger`
- `okhttp`
- `com.squareup.okhttp.OkHttpClient`
- `okhttp3`
- `okhttp3.OkHttpClient`
- `retrofit`
- `retrofit.RestAdapter`
- all your services
- `retrofit2`
- `retrofit2.Retrofit`
- all your services
- `realm`
- `io.realm.Realm`
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import java.io.File;

import io.freefair.android.injection.injector.InjectionContainer;
import io.freefair.android.injection.InjectionModule;
import io.freefair.android.injection.injector.InjectionContainer;
import io.freefair.android.util.function.Consumer;
import io.freefair.android.util.function.Supplier;
import io.freefair.android.util.function.Suppliers;
Expand All @@ -19,7 +19,13 @@ public class OkHttpModule implements InjectionModule {

private Consumer<OkHttpClient> configurator;

public OkHttpModule(Consumer<OkHttpClient> configurator){
/**
* Create a new {@link OkHttpModule} with the given configurator
*
* @param configurator A {@link Consumer} which will get the created {@link OkHttpClient}
* in order to do further configuration
*/
public OkHttpModule(Consumer<OkHttpClient> configurator) {
this.configurator = configurator;
}

Expand All @@ -36,7 +42,10 @@ public OkHttpClient get() {
}));
}

public static OkHttpModule withEmptyConfig(){
/**
* @return An {@link OkHttpModule} with an empty (default) configuration
*/
public static OkHttpModule withEmptyConfig() {
return new OkHttpModule(new Consumer<OkHttpClient>() {
@Override
public void accept(OkHttpClient value) {
Expand All @@ -45,7 +54,14 @@ public void accept(OkHttpClient value) {
});
}

public static OkHttpModule withCache(final Context context){
/**
* Create an {@link OkHttpModule} with an 10MB cache
*
* @param context Context of the current Application.
* Used for {@link Context#getCacheDir()}
* @return An {@link OkHttpModule} with an 10MB cache
*/
public static OkHttpModule withCache(final Context context) {
return new OkHttpModule(new Consumer<OkHttpClient>() {
@Override
public void accept(OkHttpClient value) {
Expand Down
21 changes: 21 additions & 0 deletions modules/okhttp3/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apply plugin: 'com.android.library'

android {
defaultConfig {
minSdkVersion 10
//consumerProguardFiles 'proguard-rules.pro'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
lintOptions {
warning 'InvalidPackage' //okio
}
}

dependencies {
compile project(':injection')
compile 'com.squareup.okhttp3:okhttp:3.0.1'
testCompile 'junit:junit:4.12'
}
6 changes: 6 additions & 0 deletions modules/okhttp3/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="io.freefair.android.injection.modules.okhttp3">

<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.freefair.android.injection.modules;

import android.content.Context;
import android.support.annotation.Nullable;

import java.io.File;

import io.freefair.android.injection.InjectionModule;
import io.freefair.android.injection.injector.InjectionContainer;
import io.freefair.android.util.function.Consumer;
import io.freefair.android.util.function.Supplier;
import io.freefair.android.util.function.Suppliers;
import okhttp3.Cache;
import okhttp3.OkHttpClient;

@SuppressWarnings("unused")
public class OkHttp3Module implements InjectionModule {

private Consumer<OkHttpClient.Builder> configurator;

/**
* Create a new {@link OkHttp3Module} with the given configurator
*
* @param configurator A {@link Consumer} which will get the {@link OkHttpClient.Builder}
* in order to perform further configuration
*/
public OkHttp3Module(Consumer<OkHttpClient.Builder> configurator) {
this.configurator = configurator;
}

@Override
public void configure(InjectionContainer injectionContainer) {
injectionContainer.registerSupplier(OkHttpClient.class, Suppliers.cache(new Supplier<OkHttpClient>() {
@Nullable
@Override
public OkHttpClient get() {
OkHttpClient.Builder client = new OkHttpClient.Builder();
configurator.accept(client);
return client.build();
}
}));
}

/**
* @return An {@link OkHttp3Module} with an empty (default) configuration
*/
public static OkHttp3Module withEmptyConfig() {
return new OkHttp3Module(new Consumer<OkHttpClient.Builder>() {
@Override
public void accept(OkHttpClient.Builder okHttpClientBuilder) {
}
});
}

/**
* Create an {@link OkHttp3Module} with an 10MB cache
*
* @param context Context of the current Application.
* Used for {@link Context#getCacheDir()}
* @return An {@link OkHttp3Module} with an 10MB cache
*/
public static OkHttp3Module withCache(final Context context) {
return new OkHttp3Module(new Consumer<OkHttpClient.Builder>() {
@Override
public void accept(OkHttpClient.Builder okHttpClientBuilder) {
File cacheDir = new File(context.getCacheDir(), "okhttp3");
long size = 1024 * 1024 * 10;
okHttpClientBuilder.cache(new Cache(cacheDir, size));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import retrofit.RestAdapter;

/**
* An {@link InjectionModule} which enables the injection of {@link RestAdapter the retrofit instance}
* An {@link InjectionModule} which enables the injection of {@link RestAdapter the RestAdapter instance}
* and Services.
*/
@SuppressWarnings("unused")
Expand All @@ -22,7 +22,7 @@ public class RetrofitModule implements InjectionModule {
private Predicate<Class<?>> servicePredicate;

/**
* Create a new RetrofitModule
* Create a new {@link RetrofitModule}
*
* @param configurator Use this, to configure your {@link RestAdapter} instance (baseUrl etc.)
* @param servicePredicate This one will be used in order to identify services.
Expand Down
21 changes: 21 additions & 0 deletions modules/retrofit2/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apply plugin: 'com.android.library'

android {
defaultConfig {
minSdkVersion 10
//consumerProguardFiles 'proguard-rules.pro'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
lintOptions {
warning 'InvalidPackage'
}
}

dependencies {
compile project(':injection')
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
testCompile 'junit:junit:4.12'
}
6 changes: 6 additions & 0 deletions modules/retrofit2/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="io.freefair.android.injection.modules.retrofit2">

<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.freefair.android.injection.modules;

import android.support.annotation.Nullable;

import io.freefair.android.injection.InjectionModule;
import io.freefair.android.injection.injector.InjectionContainer;
import io.freefair.android.injection.modules.retrofit2.ServiceProvider;
import io.freefair.android.util.function.Consumer;
import io.freefair.android.util.function.Predicate;
import io.freefair.android.util.function.Supplier;
import io.freefair.android.util.function.Suppliers;
import retrofit2.Retrofit;

/**
* An {@link InjectionModule} which enables the injection of {@link Retrofit the retrofit instance}
* and Services.
*/
@SuppressWarnings("unused")
public class Retrofit2Module implements InjectionModule {

private Consumer<Retrofit.Builder> configurator;
private Predicate<Class<?>> servicePredicate;

/**
* Create a new {@link Retrofit2Module}
*
* @param configurator Use this, to configure your {@link Retrofit} instance (baseUrl etc.)
* @param servicePredicate This one will be used in order to identify services.
* Return true here, if the given interface is a service.
*/
public Retrofit2Module(Consumer<Retrofit.Builder> configurator, Predicate<Class<?>> servicePredicate) {
this.configurator = configurator;
this.servicePredicate = servicePredicate;
}

@Override
public void configure(final InjectionContainer injectionContainer) {
injectionContainer.registerSupplier(Retrofit.class, Suppliers.cache(new Supplier<Retrofit>() {
@Nullable
@Override
public Retrofit get() {
Retrofit.Builder builder = new Retrofit.Builder();
configurator.accept(builder);
return builder.build();
}
}));

injectionContainer.registerProvider(new ServiceProvider(servicePredicate));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.freefair.android.injection.modules.retrofit2;

import io.freefair.android.injection.InjectionProvider;
import io.freefair.android.injection.injector.Injector;
import io.freefair.android.util.function.Predicate;
import retrofit2.Retrofit;

@SuppressWarnings("unused")
public class ServiceProvider implements InjectionProvider {

private Predicate<Class<?>> servicePredicate;

public ServiceProvider(Predicate<Class<?>> servicePredicate) {
this.servicePredicate = servicePredicate;
}

@Override
public boolean canProvide(Class<?> clazz) {
return clazz.isInterface() && servicePredicate.test(clazz);
}

@Override
@SuppressWarnings("unchecked")
public <T> T provide(Class<? super T> clazz, Object instance, Injector injector) {
Retrofit restAdapter = injector.resolveValue(Retrofit.class, instance);
return (T) restAdapter.create(clazz);
}
}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ include ':injection'
include ':modules:logging'
include ':modules:realm'
include ':modules:okhttp'
include ':modules:okhttp3'
include ':modules:retrofit'
include ':modules:retrofit2'

0 comments on commit 8a82c46

Please sign in to comment.