Skip to content

Commit e47898d

Browse files
committed
Add HTTP cache support.
1 parent 4464075 commit e47898d

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

src/main/java/org/amahi/anywhere/AmahiApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void onCreate() {
2121
}
2222

2323
private void setUpInjections() {
24-
injector = ObjectGraph.create(new AmahiModule());
24+
injector = ObjectGraph.create(new AmahiModule(this));
2525
}
2626

2727
public void inject(Object injectionsConsumer) {

src/main/java/org/amahi/anywhere/AmahiModule.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package org.amahi.anywhere;
22

3+
import android.app.Application;
4+
35
import org.amahi.anywhere.activity.ServersActivity;
46
import org.amahi.anywhere.server.ApiModule;
57

8+
import javax.inject.Singleton;
9+
610
import dagger.Module;
11+
import dagger.Provides;
712

813
@Module(
914
includes = {
@@ -15,4 +20,15 @@
1520
)
1621
class AmahiModule
1722
{
23+
private final Application application;
24+
25+
public AmahiModule(Application application) {
26+
this.application = application;
27+
}
28+
29+
@Provides
30+
@Singleton
31+
Application provideApplication() {
32+
return application;
33+
}
1834
}

src/main/java/org/amahi/anywhere/server/ApiModule.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package org.amahi.anywhere.server;
22

3+
import android.app.Application;
4+
5+
import com.squareup.okhttp.HttpResponseCache;
36
import com.squareup.okhttp.OkHttpClient;
7+
import com.squareup.okhttp.OkResponseCache;
48

59
import org.amahi.anywhere.server.header.ApiHeaders;
610

11+
import java.io.File;
12+
import java.io.IOException;
13+
714
import javax.inject.Singleton;
815

916
import dagger.Module;
@@ -12,20 +19,38 @@
1219
import retrofit.client.OkClient;
1320

1421
@Module(
22+
complete = false,
1523
library = true
1624
)
1725
public class ApiModule
1826
{
1927
@Provides
2028
@Singleton
21-
OkHttpClient provideHttpClient() {
22-
return new OkHttpClient();
29+
Client provideClient(OkHttpClient httpClient) {
30+
return new OkClient(httpClient);
2331
}
2432

2533
@Provides
2634
@Singleton
27-
Client provideClient(OkHttpClient httpClient) {
28-
return new OkClient(httpClient);
35+
OkHttpClient provideHttpClient(OkResponseCache httpCache) {
36+
OkHttpClient httpClient = new OkHttpClient();
37+
38+
httpClient.setOkResponseCache(httpCache);
39+
40+
return httpClient;
41+
}
42+
43+
@Provides
44+
@Singleton
45+
OkResponseCache provideHttpCache(Application application) {
46+
try {
47+
File cacheDirectory = new File(application.getCacheDir(), "http-cache");
48+
int cacheSize = 5 * 1024 * 1024;
49+
50+
return new HttpResponseCache(cacheDirectory, cacheSize);
51+
} catch (IOException e) {
52+
return null;
53+
}
2954
}
3055

3156
@Provides

0 commit comments

Comments
 (0)