From 80b484e2adf44c805810d0eeca96051dfbdcaa68 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 23 May 2014 12:31:17 -0500 Subject: [PATCH] Use dagger for astrid activities --- astrid/build.gradle | 1 - astrid/src/main/AndroidManifest.xml | 1 + .../astrid/activity/AstridActivity.java | 11 ++++----- .../astrid/activity/ShareLinkActivity.java | 14 ++++------- .../astrid/activity/TaskListActivity.java | 6 ++--- .../service/AstridDependencyInjector.java | 24 +++++++++++++++++-- astrid/src/main/java/org/tasks/Injector.java | 16 ------------- astrid/src/main/java/org/tasks/Tasks.java | 24 +++++++++++++++++++ .../org/tasks/injection/ActivityModule.java | 16 +++++++++++++ .../injection/InjectingActionBarActivity.java | 13 ++++++++++ .../java/org/tasks/injection/Injector.java | 7 ++++++ 11 files changed, 95 insertions(+), 38 deletions(-) delete mode 100644 astrid/src/main/java/org/tasks/Injector.java create mode 100644 astrid/src/main/java/org/tasks/Tasks.java create mode 100644 astrid/src/main/java/org/tasks/injection/ActivityModule.java create mode 100644 astrid/src/main/java/org/tasks/injection/InjectingActionBarActivity.java create mode 100644 astrid/src/main/java/org/tasks/injection/Injector.java diff --git a/astrid/build.gradle b/astrid/build.gradle index 3ee422a11..3bf689822 100644 --- a/astrid/build.gradle +++ b/astrid/build.gradle @@ -71,7 +71,6 @@ android.applicationVariants.all { variant -> dependencies { provided group: 'com.squareup.dagger', name: 'dagger-compiler', version: '1.2.1' - compile group: 'com.android.support', name: 'support-v4', version: '19.1.+' compile group: 'com.android.support', name: 'appcompat-v7', version: '19.1.+' compile 'net.simonvt.menudrawer:menudrawer:3.0.4@aar' diff --git a/astrid/src/main/AndroidManifest.xml b/astrid/src/main/AndroidManifest.xml index a62e03453..68f8e65de 100644 --- a/astrid/src/main/AndroidManifest.xml +++ b/astrid/src/main/AndroidManifest.xml @@ -85,6 +85,7 @@ android:label="@string/app_name" android:theme="@style/Tasks" android:allowBackup="true" + android:name=".Tasks" android:hardwareAccelerated="false" android:manageSpaceActivity="com.todoroo.astrid.core.OldTaskPreferences"> diff --git a/astrid/src/main/java/com/todoroo/astrid/activity/AstridActivity.java b/astrid/src/main/java/com/todoroo/astrid/activity/AstridActivity.java index 565aa47b3..62ddd8643 100644 --- a/astrid/src/main/java/com/todoroo/astrid/activity/AstridActivity.java +++ b/astrid/src/main/java/com/todoroo/astrid/activity/AstridActivity.java @@ -13,13 +13,11 @@ import android.speech.SpeechRecognizer; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; -import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.WindowManager.BadTokenException; import android.widget.EditText; import android.widget.Toast; -import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.DialogUtilities; @@ -34,7 +32,6 @@ import com.todoroo.astrid.core.PluginServices; import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.data.Task; -import com.todoroo.astrid.service.AstridDependencyInjector; import com.todoroo.astrid.service.StartupService; import com.todoroo.astrid.subtasks.SubtasksHelper; import com.todoroo.astrid.ui.DateChangedAlerts; @@ -43,6 +40,9 @@ import com.todoroo.astrid.voice.RecognizerApi.RecognizerApiListener; import com.todoroo.astrid.voice.VoiceRecognizer; import org.tasks.R; +import org.tasks.injection.InjectingActionBarActivity; + +import javax.inject.Inject; /** * This wrapper activity contains all the glue-code to handle the callbacks between the different @@ -55,7 +55,7 @@ import org.tasks.R; * @author Arne * */ -public class AstridActivity extends ActionBarActivity +public class AstridActivity extends InjectingActionBarActivity implements FilterListFragment.OnFilterItemClickedListener, TaskListFragment.OnTaskListItemClickedListener, RecognizerApiListener { @@ -69,7 +69,7 @@ public class AstridActivity extends ActionBarActivity private final RepeatConfirmationReceiver repeatConfirmationReceiver = new RepeatConfirmationReceiver(); - @Autowired private TaskDao taskDao; + @Inject TaskDao taskDao; public FilterListFragment getFilterListFragment() { return (FilterListFragment) getSupportFragmentManager() @@ -94,7 +94,6 @@ public class AstridActivity extends ActionBarActivity @Override protected void onCreate(Bundle savedInstanceState) { - AstridDependencyInjector.inject(this); super.onCreate(savedInstanceState); ContextManager.setContext(this); diff --git a/astrid/src/main/java/com/todoroo/astrid/activity/ShareLinkActivity.java b/astrid/src/main/java/com/todoroo/astrid/activity/ShareLinkActivity.java index 800fca612..1645b31ac 100644 --- a/astrid/src/main/java/com/todoroo/astrid/activity/ShareLinkActivity.java +++ b/astrid/src/main/java/com/todoroo/astrid/activity/ShareLinkActivity.java @@ -6,13 +6,13 @@ package com.todoroo.astrid.activity; import android.content.Intent; import android.os.Bundle; -import com.todoroo.andlib.service.Autowired; -import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.service.TaskService; import com.todoroo.astrid.tags.TagService; import com.todoroo.astrid.ui.QuickAddBar; +import javax.inject.Inject; + /** * @author joshuagross * @@ -20,20 +20,14 @@ import com.todoroo.astrid.ui.QuickAddBar; */ public final class ShareLinkActivity extends TaskListActivity { - @Autowired private TaskService taskService; - - @Autowired private TagService tagService; + @Inject TaskService taskService; + @Inject TagService tagService; private String subject; private boolean handled; private static final String TOKEN_LINK_HANDLED = "linkHandled"; //$NON-NLS-1$ - public ShareLinkActivity () { - super(); - DependencyInjectionService.getInstance().inject(this); - } - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/astrid/src/main/java/com/todoroo/astrid/activity/TaskListActivity.java b/astrid/src/main/java/com/todoroo/astrid/activity/TaskListActivity.java index 77742d0be..2988b33f8 100644 --- a/astrid/src/main/java/com/todoroo/astrid/activity/TaskListActivity.java +++ b/astrid/src/main/java/com/todoroo/astrid/activity/TaskListActivity.java @@ -23,8 +23,6 @@ import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.FrameLayout; -import com.todoroo.andlib.service.Autowired; -import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.sql.QueryTemplate; import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.astrid.actfm.CommentsFragment; @@ -54,9 +52,11 @@ import net.simonvt.menudrawer.MenuDrawer; import org.tasks.R; +import javax.inject.Inject; + public class TaskListActivity extends AstridActivity implements OnPageChangeListener { - @Autowired TagDataDao tagDataDao; + @Inject TagDataDao tagDataDao; MenuDrawer menuDrawer; diff --git a/astrid/src/main/java/com/todoroo/astrid/service/AstridDependencyInjector.java b/astrid/src/main/java/com/todoroo/astrid/service/AstridDependencyInjector.java index 51dee064a..374ba0409 100644 --- a/astrid/src/main/java/com/todoroo/astrid/service/AstridDependencyInjector.java +++ b/astrid/src/main/java/com/todoroo/astrid/service/AstridDependencyInjector.java @@ -24,12 +24,15 @@ import com.todoroo.astrid.gtasks.sync.GtasksSyncService; import com.todoroo.astrid.tags.TagService; import org.tasks.Broadcaster; -import org.tasks.Injector; +import org.tasks.TasksModule; import org.tasks.filters.FilterCounter; +import org.tasks.injection.Injector; import org.tasks.scheduling.RefreshScheduler; import javax.inject.Inject; +import dagger.ObjectGraph; + /** * Astrid application dependency injector loads classes in Astrid with the * appropriate instantiated objects necessary for their operation. For @@ -46,6 +49,8 @@ public class AstridDependencyInjector extends AbstractDependencyInjector { */ private static AstridDependencyInjector instance = null; + private Injector injector; + @Inject Database database; @Inject MetadataDao metadataDao; @Inject TagDataDao tagDataDao; @@ -76,7 +81,17 @@ public class AstridDependencyInjector extends AbstractDependencyInjector { */ @Override protected void addInjectables() { - new Injector().inject(this); + injector = new Injector() { + ObjectGraph objectGraph = ObjectGraph.create(new TasksModule()); + + @Override + public void inject(Object caller, Object... modules) { + objectGraph + .plus(modules) + .inject(caller); + } + }; + injector.inject(this); // com.todoroo.astrid.dao injectables.put("database", database); @@ -144,4 +159,9 @@ public class AstridDependencyInjector extends AbstractDependencyInjector { instance = null; initialize(); } + + public static Injector getInjector() { + initialize(); + return instance.injector; + } } diff --git a/astrid/src/main/java/org/tasks/Injector.java b/astrid/src/main/java/org/tasks/Injector.java deleted file mode 100644 index 8a06067e5..000000000 --- a/astrid/src/main/java/org/tasks/Injector.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.tasks; - -import dagger.ObjectGraph; - -public class Injector { - - ObjectGraph objectGraph; - - public Injector() { - objectGraph = ObjectGraph.create(new TasksModule()); - } - - public void inject(Object caller) { - objectGraph.inject(caller); - } -} diff --git a/astrid/src/main/java/org/tasks/Tasks.java b/astrid/src/main/java/org/tasks/Tasks.java new file mode 100644 index 000000000..df6067578 --- /dev/null +++ b/astrid/src/main/java/org/tasks/Tasks.java @@ -0,0 +1,24 @@ +package org.tasks; + +import android.app.Application; + +import com.todoroo.astrid.service.AstridDependencyInjector; + +import org.tasks.injection.Injector; + +public class Tasks extends Application implements Injector { + + Injector injector; + + @Override + public void onCreate() { + super.onCreate(); + + injector = AstridDependencyInjector.getInjector(); + } + + @Override + public void inject(Object caller, Object... modules) { + injector.inject(caller, modules); + } +} diff --git a/astrid/src/main/java/org/tasks/injection/ActivityModule.java b/astrid/src/main/java/org/tasks/injection/ActivityModule.java new file mode 100644 index 000000000..502be3543 --- /dev/null +++ b/astrid/src/main/java/org/tasks/injection/ActivityModule.java @@ -0,0 +1,16 @@ +package org.tasks.injection; + +import com.todoroo.astrid.activity.ShareLinkActivity; +import com.todoroo.astrid.activity.TaskEditActivity; +import com.todoroo.astrid.activity.TaskListActivity; + +import dagger.Module; + +@Module(library = true, + injects = { + TaskListActivity.class, + TaskEditActivity.class, + ShareLinkActivity.class + }) +public class ActivityModule { +} diff --git a/astrid/src/main/java/org/tasks/injection/InjectingActionBarActivity.java b/astrid/src/main/java/org/tasks/injection/InjectingActionBarActivity.java new file mode 100644 index 000000000..51eb88c0d --- /dev/null +++ b/astrid/src/main/java/org/tasks/injection/InjectingActionBarActivity.java @@ -0,0 +1,13 @@ +package org.tasks.injection; + +import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; + +public class InjectingActionBarActivity extends ActionBarActivity { + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Injector) getApplication()).inject(this, new ActivityModule()); + + super.onCreate(savedInstanceState); + } +} diff --git a/astrid/src/main/java/org/tasks/injection/Injector.java b/astrid/src/main/java/org/tasks/injection/Injector.java new file mode 100644 index 000000000..00c04f6b9 --- /dev/null +++ b/astrid/src/main/java/org/tasks/injection/Injector.java @@ -0,0 +1,7 @@ +package org.tasks.injection; + +public interface Injector { + + public void inject(Object caller, Object... modules); + +}