Skip to content

Commit 5a1cbed

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

File tree

8 files changed

+75
-6
lines changed

8 files changed

+75
-6
lines changed

com.vogella.android.github.issuetracker/app/build.gradle

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,17 @@ android {
3535
dataBinding {
3636
enabled = true
3737
}
38+
39+
configurations.all {
40+
resolutionStrategy.force 'com.google.code.findbugs:jsr305:2.0.1'
41+
}
3842
}
3943

44+
repositories{
45+
maven{
46+
url 'https://oss.sonatype.org/content/repositories/snapshots/'
47+
}
48+
}
4049
dependencies {
4150
compile fileTree(dir: 'libs', include: ['*.jar'])
4251
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
@@ -48,6 +57,13 @@ dependencies {
4857
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
4958
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
5059
compile 'com.squareup.okhttp3:logging-interceptor:3.7.0'
51-
compile "com.android.support:recyclerview-v7:25.1.1"
52-
testCompile 'junit:junit:4.12'
60+
compile "com.android.support:recyclerview-v7:25.3.1"
61+
62+
compile 'com.google.dagger:dagger:HEAD-SNAPSHOT'
63+
compile 'com.google.dagger:dagger-android:HEAD-SNAPSHOT'
64+
compile 'com.google.dagger:dagger-android-support:HEAD-SNAPSHOT'
65+
annotationProcessor 'com.google.dagger:dagger-android-processor:HEAD-SNAPSHOT'
66+
annotationProcessor 'com.google.dagger:dagger-compiler:HEAD-SNAPSHOT'
67+
68+
androidTestCompile 'junit:junit:4.12'
5369
}

com.vogella.android.github.issuetracker/app/src/main/AndroidManifest.xml

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

55
<uses-permission android:name="android.permission.INTERNET" />
66
<application
7+
android:name=".MyApplication"
78
android:allowBackup="true"
89
android:icon="@mipmap/ic_launcher"
910
android:label="@string/app_name"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.vogella.android.github.issuetracker;
2+
3+
import dagger.Module;
4+
import dagger.android.ContributesAndroidInjector;
5+
6+
@Module
7+
public abstract class ActivityModule {
8+
@ContributesAndroidInjector
9+
abstract MainActivity contributeMainActivity();
10+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.List;
44

5+
import javax.inject.Inject;
6+
57
import io.reactivex.android.schedulers.AndroidSchedulers;
68
import io.reactivex.observers.DisposableSingleObserver;
79
import io.reactivex.schedulers.Schedulers;
@@ -16,6 +18,7 @@ public class CommunicationController {
1618

1719
private GithubApi githubApi;
1820

21+
@Inject
1922
public CommunicationController() {
2023
initGithubApi();
2124
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.vogella.android.github.issuetracker;
2+
3+
import dagger.Component;
4+
import dagger.android.AndroidInjectionModule;
5+
import dagger.android.AndroidInjector;
6+
7+
@Component(modules = {AndroidInjectionModule.class,ActivityModule.class })
8+
public interface IApplicationComponent extends AndroidInjector<MyApplication> {
9+
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import java.util.ArrayList;
1717
import java.util.List;
1818

19+
import javax.inject.Inject;
20+
21+
import dagger.android.AndroidInjection;
1922
import io.reactivex.observers.DisposableSingleObserver;
2023

2124
public class MainActivity extends AppCompatActivity implements CredentialsDialog.ICredentialsDialogListener {
@@ -26,11 +29,13 @@ public class MainActivity extends AppCompatActivity implements CredentialsDialog
2629
ArrayList<Issue> issues;
2730
private String password = "";
2831
private String username = "";
29-
private CommunicationController communicationController;
32+
@Inject
33+
CommunicationController communicationController;
3034
DisposableSingleObserver<List<Issue>> issuesObserver;
3135

3236
@Override
3337
protected void onCreate(Bundle savedInstanceState) {
38+
AndroidInjection.inject(this);
3439
super.onCreate(savedInstanceState);
3540
setContentView(R.layout.activity_main);
3641
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
@@ -57,8 +62,6 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
5762
list.setLayoutManager(new LinearLayoutManager(this));
5863
issues = new ArrayList<>();
5964
list.setAdapter(new IssueAdapter(issues));
60-
61-
communicationController = new CommunicationController();
6265
}
6366

6467
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.vogella.android.github.issuetracker;
2+
3+
4+
import android.app.Activity;
5+
import android.app.Application;
6+
7+
import javax.inject.Inject;
8+
9+
import dagger.android.DispatchingAndroidInjector;
10+
import dagger.android.HasActivityInjector;
11+
12+
public class MyApplication extends Application implements HasActivityInjector {
13+
14+
@Inject
15+
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
16+
17+
@Override
18+
public void onCreate() {
19+
super.onCreate();
20+
DaggerIApplicationComponent.create().inject(this);
21+
}
22+
23+
@Override
24+
public DispatchingAndroidInjector<Activity> activityInjector() {
25+
return dispatchingAndroidInjector;
26+
}
27+
}

com.vogella.android.github.issuetracker/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.4.0-alpha6'
8+
classpath 'com.android.tools.build:gradle:2.3.1'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

0 commit comments

Comments
 (0)