Skip to content

Commit 3ac788c

Browse files
committed
dagger code added
1 parent d70f6f9 commit 3ac788c

30 files changed

Lines changed: 540 additions & 0 deletions

dagger/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# dagger
2+
This is repository of http://androidcode.pl blog. It shows uses Dagger in Android. It is a part of Libraries - Dagger post in the blog.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class ComplexDependency {
2+
3+
private NetworkManager networkManager;
4+
//more fields
5+
6+
public ComplexDependency(NetworkManager networkManager) {
7+
this.networkManager = networkManager;
8+
}
9+
10+
public String getInfo() {
11+
return "ComplexDependency is " + networkManager.getUrl() + ":" + networkManager.getPort();
12+
}
13+
14+
//do some actions
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class NetworkManager {
2+
3+
private int port;
4+
private String url;
5+
6+
public NetworkManager(String url, int port) {
7+
this.url = url;
8+
this.port = port;
9+
}
10+
11+
public int getPort() {
12+
return port;
13+
}
14+
15+
public void setPort(int port) {
16+
this.port = port;
17+
}
18+
19+
public String getUrl() {
20+
return url;
21+
}
22+
23+
public void setUrl(String url) {
24+
this.url = url;
25+
}
26+
27+
//do some work
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class SimpleDependency {
2+
3+
private String color;
4+
private String shape;
5+
6+
private SimpleDependency(Builder builder) {
7+
this.color = builder.color;
8+
this.shape = builder.shape;
9+
}
10+
11+
public String getInfo() {
12+
return "SimpleDependency is " + color + " " + shape;
13+
}
14+
15+
public static class Builder {
16+
17+
private String color;
18+
private String shape;
19+
20+
public Builder setColor(String color) {
21+
this.color = color;
22+
return this;
23+
}
24+
25+
public Builder setShape(String shape) {
26+
this.shape = shape;
27+
return this;
28+
}
29+
30+
public SimpleDependency build() {
31+
return new SimpleDependency(this);
32+
}
33+
}
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class UserManager {
2+
3+
private String configuration;
4+
//more fields
5+
6+
public UserManager(String configuration) {
7+
this.configuration = configuration;
8+
}
9+
10+
public String getConfiguration() {
11+
return configuration;
12+
}
13+
14+
public void setConfiguration(String configuration) {
15+
this.configuration = configuration;
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//create main app module to provide Application and Context
2+
@Module
3+
public class AppModule {
4+
5+
private Application application;
6+
7+
public AppModule(Application application) {
8+
this.application = application;
9+
}
10+
11+
@Provides @Singleton
12+
Application providesApplication() {
13+
return application;
14+
}
15+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//some manager module
2+
@Module
3+
public class ManagerModule {
4+
5+
private String url;
6+
7+
public ManagerModule(String name) {
8+
this.url = name;
9+
}
10+
11+
@Provides @Named("red")
12+
SimpleDependency provideSimpleDependencyRed() {
13+
SimpleDependency.Builder builder = new SimpleDependency.Builder();
14+
builder.setColor("red").setShape("oval");
15+
return builder.build();
16+
}
17+
18+
@Provides @Named("blue")
19+
SimpleDependency provideSimpleDependencyBlue() {
20+
SimpleDependency.Builder builder = new SimpleDependency.Builder();
21+
builder.setColor("blue").setShape("triangle");
22+
return builder.build();
23+
}
24+
25+
@Provides @Singleton
26+
ComplexDependency provideComplexDependency(NetworkManager networkDependency) {
27+
return new ComplexDependency(networkDependency);
28+
}
29+
30+
@Provides @Singleton
31+
NetworkManager provideNetworkDependency() {
32+
return new NetworkManager(url, 80);
33+
}
34+
35+
@Provides @Singleton
36+
SharedPreferences providesSharedPreferences(Application application) {
37+
return PreferenceManager.getDefaultSharedPreferences(application);
38+
}
39+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@Module
2+
public class UserModule {
3+
4+
private final UserActivity activity;
5+
6+
public UserModule(UserActivity activity) {
7+
this.activity = activity;
8+
}
9+
10+
@Provides @UserScope
11+
public UserManager providesUserDependency() {
12+
return new UserManager("global");
13+
}
14+
}

dagger/common/qualifier/Type.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@Qualifier
2+
@Retention(RUNTIME)
3+
public @interface Type {
4+
5+
//qualifier annotations to use like @Named, for example:
6+
//@Inject @Type("type")
7+
SimpleDependency dependency
8+
}

dagger/common/scope/UserScope.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@Scope
2+
@Retention(value = RetentionPolicy.RUNTIME)
3+
public @interface UserScope {
4+
5+
//informs about custom lifecycle time
6+
//use this annotations in cases like @Singleton
7+
}

0 commit comments

Comments
 (0)