Skip to content

Commit

Permalink
Using dependency injection with dagger
Browse files Browse the repository at this point in the history
  • Loading branch information
abusenius committed Feb 9, 2014
1 parent 808a996 commit cbebb7c
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="lib" path="libs/dagger-1.2.0.jar"/>
<classpathentry kind="lib" path="libs/javax.inject-1.jar"/>
<classpathentry kind="src" path="apt_generated">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
6 changes: 6 additions & 0 deletions .factorypath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<factorypath>
<factorypathentry kind="WKSPJAR" id="/android-stopwatch/libs/dagger-1.2.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="WKSPJAR" id="/android-stopwatch/libs/javax.inject-1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="WKSPJAR" id="/android-stopwatch/libs/dagger-compiler-1.2.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="WKSPJAR" id="/android-stopwatch/libs/javawriter-2.4.0.jar" enabled="true" runInBatchMode="false"/>
</factorypath>
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ proguard/
*.ipr
*.iws
.idea/
/apt_generated/
/lint.xml
4 changes: 4 additions & 0 deletions .settings/org.eclipse.jdt.apt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=true
org.eclipse.jdt.apt.genSrcDir=apt_generated
org.eclipse.jdt.apt.reconcileEnabled=true
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/AppTheme" android:name="TimerApp">
<activity
android:name="org.alsoftlab.stopwatch.TimerActivity"
android:label="@string/app_name" >
Expand Down
Binary file added libs/dagger-1.2.0.jar
Binary file not shown.
Binary file added libs/dagger-compiler-1.2.0.jar
Binary file not shown.
Binary file added libs/javawriter-1.0.5.jar
Binary file not shown.
Binary file added libs/javawriter-2.4.0.jar
Binary file not shown.
Binary file added libs/javax.inject-1.jar
Binary file not shown.
20 changes: 20 additions & 0 deletions src/org/alsoftlab/stopwatch/Injector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.alsoftlab.stopwatch;

import dagger.ObjectGraph;

/**
* Initialized dependency graph.
*/
public class Injector
{
public static ObjectGraph graph;
public static void init(Object... modules)
{
graph = ObjectGraph.create(modules);
}

public static void inject(Object target)
{
graph.inject(target);
}
}
16 changes: 13 additions & 3 deletions src/org/alsoftlab/stopwatch/TimerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayList;

import javax.inject.Inject;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
Expand All @@ -26,20 +28,28 @@ public class TimerActivity extends Activity {
*/
private final static int REFRESH_PERIOD_MS = 1000 / TICK_COUNT;

private TimerLogic mTimerLogic = new TimerLogic();
@Inject
TimerLogic mTimerLogic;

private TextView mChronometer;
private TextView mText;
private Handler mViewHandler = new Handler();
private Runnable mUpdateView;
private ArrayList<ImageView> mImages = new ArrayList<ImageView>();

public TimerActivity()
{
super();

Injector.inject(this);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Log.d(getClass().getSimpleName(), "onCreate");

setContentView(R.layout.timer_activity);
mChronometer = (TextView) findViewById(R.id.textView2);
mText = (TextView) findViewById(R.id.textView1);
Expand Down
24 changes: 24 additions & 0 deletions src/org/alsoftlab/stopwatch/TimerApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
*
*/
package org.alsoftlab.stopwatch;

import android.app.Application;
import android.util.Log;

/**
* @author al
*
*/
public class TimerApp extends Application {

@Override
public void onCreate() {
super.onCreate();

Log.d("TimerApp", "onCreate");

Injector.init(new TimerLogicModule());
}

}
4 changes: 2 additions & 2 deletions src/org/alsoftlab/stopwatch/TimerLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public TimerLogic() {
mStarted = false;
mStartTime = 0;
}

/**
* @return true iff the timer is running
*/
public boolean isRunning() {
return mStarted;
}

/**
* Start the timer if stopped and vice vers
*
Expand Down
11 changes: 11 additions & 0 deletions src/org/alsoftlab/stopwatch/TimerLogicModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.alsoftlab.stopwatch;

import dagger.*;

@Module(injects = TimerActivity.class)
public class TimerLogicModule {
@Provides
TimerLogic provideTimerLogic() {
return new TimerLogic();
}
}

0 comments on commit cbebb7c

Please sign in to comment.