This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from freefair/develop
retrofit2 and okhttp3
- Loading branch information
Showing
11 changed files
with
233 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
72 changes: 72 additions & 0 deletions
72
modules/okhttp3/src/main/java/io/freefair/android/injection/modules/OkHttp3Module.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
50 changes: 50 additions & 0 deletions
50
modules/retrofit2/src/main/java/io/freefair/android/injection/modules/Retrofit2Module.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...rofit2/src/main/java/io/freefair/android/injection/modules/retrofit2/ServiceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters