Skip to content

Commit ea1750b

Browse files
author
David Weiser
committed
adds dagger 2
1 parent 5a1cbed commit ea1750b

File tree

7 files changed

+38
-27
lines changed

7 files changed

+38
-27
lines changed

com.vogella.android.github.issuetracker/app/src/main/java/com/vogella/android/github/issuetracker/ActivityModule.java renamed to com.vogella.android.github.issuetracker/app/src/main/java/com/vogella/android/github/issuetracker/ApplicationModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import dagger.android.ContributesAndroidInjector;
55

66
@Module
7-
public abstract class ActivityModule {
8-
@ContributesAndroidInjector
7+
public abstract class ApplicationModule {
8+
@ContributesAndroidInjector(modules = GithubApiModule.class)
99
abstract MainActivity contributeMainActivity();
1010
}

com.vogella.android.github.issuetracker/app/src/main/java/com/vogella/android/github/issuetracker/CommunicationController.java

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,14 @@
88
import io.reactivex.observers.DisposableSingleObserver;
99
import io.reactivex.schedulers.Schedulers;
1010
import okhttp3.Credentials;
11-
import okhttp3.OkHttpClient;
12-
import okhttp3.logging.HttpLoggingInterceptor;
13-
import retrofit2.Retrofit;
14-
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
15-
import retrofit2.converter.gson.GsonConverterFactory;
1611

1712
public class CommunicationController {
1813

19-
private GithubApi githubApi;
20-
2114
@Inject
22-
public CommunicationController() {
23-
initGithubApi();
24-
}
25-
26-
private void initGithubApi() {
27-
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
28-
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
29-
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();
30-
Retrofit retrofit = new Retrofit.Builder()
31-
.client(okHttpClient)
32-
.baseUrl(GithubApi.BASE_URL)
33-
.addConverterFactory(GsonConverterFactory.create())
34-
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
35-
.build();
15+
GithubApi githubApi;
3616

37-
githubApi = retrofit.create(GithubApi.class);
38-
}
17+
@Inject
18+
public CommunicationController() {}
3919

4020
public void loadIssues(String username, String password, DisposableSingleObserver<List<Issue>> observer, String owner, String repository) {
4121
githubApi.getIssues(Credentials.basic(username, password), owner, repository)

com.vogella.android.github.issuetracker/app/src/main/java/com/vogella/android/github/issuetracker/GithubApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44

5+
import dagger.Provides;
56
import io.reactivex.Single;
67
import retrofit2.http.GET;
78
import retrofit2.http.Header;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.vogella.android.github.issuetracker;
2+
3+
4+
import dagger.Module;
5+
import dagger.Provides;
6+
import okhttp3.OkHttpClient;
7+
import okhttp3.logging.HttpLoggingInterceptor;
8+
import retrofit2.Retrofit;
9+
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
10+
import retrofit2.converter.gson.GsonConverterFactory;
11+
12+
@Module
13+
public class GithubApiModule {
14+
15+
@Provides
16+
static GithubApi provideGithubApi() {
17+
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
18+
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
19+
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();
20+
Retrofit retrofit = new Retrofit.Builder()
21+
.client(okHttpClient)
22+
.baseUrl(GithubApi.BASE_URL)
23+
.addConverterFactory(GsonConverterFactory.create())
24+
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
25+
.build();
26+
27+
return retrofit.create(GithubApi.class);
28+
}
29+
}

com.vogella.android.github.issuetracker/app/src/main/java/com/vogella/android/github/issuetracker/IApplicationComponent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
import dagger.android.AndroidInjectionModule;
55
import dagger.android.AndroidInjector;
66

7-
@Component(modules = {AndroidInjectionModule.class,ActivityModule.class })
7+
@Component(modules = {AndroidInjectionModule.class, ApplicationModule.class})
88
public interface IApplicationComponent extends AndroidInjector<MyApplication> {
99
}

com.vogella.android.github.issuetracker/app/src/main/java/com/vogella/android/github/issuetracker/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,4 @@ protected void onStop() {
141141
issuesObserver.dispose();
142142
}
143143
}
144-
}
144+
}

com.vogella.android.github.issuetracker/app/src/test/java/com/vogella/android/github/issuetracker/ExampleUnitTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44

5+
import static junit.framework.Assert.assertEquals;
56
import static org.junit.Assert.*;
67

78
/**

0 commit comments

Comments
 (0)