Dagger support now working

Could've saved an hour or two if someone had told me every class needs to inject itself...
This commit is contained in:
Bradlee Speice 2014-05-07 02:06:38 -04:00
parent 503ae75d79
commit 4da25f0e23
6 changed files with 108 additions and 17 deletions

View File

@ -3,7 +3,11 @@ package org.bspeice.minimalbible;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import dagger.ObjectGraph;
public class MinimalBible extends Application { public class MinimalBible extends Application {
private ObjectGraph graph;
private static MinimalBible instance; private static MinimalBible instance;
@ -15,4 +19,21 @@ public class MinimalBible extends Application {
return instance; return instance;
} }
@Override
public void onCreate() {
graph = ObjectGraph.create(new MinimalBibleModules());
graph.inject(this);
}
public void inject(Object o) {
graph.inject(o);
}
public static MinimalBible getApplication(Context ctx) {
return (MinimalBible)ctx.getApplicationContext();
}
public static MinimalBible getApplication() {
return (MinimalBible)getAppContext();
}
} }

View File

@ -0,0 +1,19 @@
package org.bspeice.minimalbible;
import org.bspeice.minimalbible.activities.ActivityModules;
import dagger.Module;
/**
* Modules for the global application
*/
@Module(
injects = {
MinimalBible.class
},
includes = {
ActivityModules.class
}
)
public class MinimalBibleModules {
}

View File

@ -0,0 +1,16 @@
package org.bspeice.minimalbible.activities;
import org.bspeice.minimalbible.activities.downloader.ActivityDownloaderModule;
import dagger.Module;
/**
* Modules for all activities
*/
@Module(
includes = {
ActivityDownloaderModule.class
}
)
public class ActivityModules {
}

View File

@ -0,0 +1,29 @@
package org.bspeice.minimalbible.activities.downloader;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import de.greenrobot.event.EventBus;
/**
* Module mappings for the classes under the Download Activity
*/
@Module(
injects = {
BookListFragment.class,
DownloadManager.class
}
)
public class ActivityDownloaderModule {
@Provides @Singleton
DownloadManager provideDownloadManager() {
return new DownloadManager();
}
@Provides
EventBus provideBus() {
return new EventBus();
}
}

View File

@ -18,6 +18,7 @@ import android.widget.Toast;
import com.readystatesoftware.systembartint.SystemBarTintManager; import com.readystatesoftware.systembartint.SystemBarTintManager;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.MinimalBibleConstants; import org.bspeice.minimalbible.MinimalBibleConstants;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
@ -27,12 +28,15 @@ import org.crosswire.jsword.book.FilterUtil;
import java.util.List; import java.util.List;
import javax.inject.Inject;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.InjectView; import butterknife.InjectView;
/** /**
* A placeholder fragment containing a simple view. * A placeholder fragment containing a simple view.
*/ */
public class BookListFragment extends Fragment { public class BookListFragment extends Fragment {
/** /**
* The fragment argument representing the section number for this fragment. * The fragment argument representing the section number for this fragment.
@ -44,6 +48,9 @@ public class BookListFragment extends Fragment {
@InjectView(R.id.lst_download_available) @InjectView(R.id.lst_download_available)
ListView downloadsAvailable; ListView downloadsAvailable;
@Inject
DownloadManager downloadManager;
private ProgressDialog refreshDialog; private ProgressDialog refreshDialog;
/** /**
@ -58,7 +65,11 @@ public class BookListFragment extends Fragment {
return fragment; return fragment;
} }
public BookListFragment() { @Override
public void onCreate(Bundle state) {
super.onCreate(state);
MinimalBible app = MinimalBible.getApplication(getActivity());
app.inject(this);
} }
@Override @Override
@ -100,10 +111,9 @@ public class BookListFragment extends Fragment {
} }
private void refreshModules() { private void refreshModules() {
DownloadManager dm = DownloadManager.getInstance(); EventBookList bookList = downloadManager.getDownloadBus().getStickyEvent(EventBookList.class);
EventBookList bookList = dm.getDownloadBus().getStickyEvent(EventBookList.class);
if (bookList == null) { if (bookList == null) {
dm.getDownloadBus().registerSticky(this); downloadManager.getDownloadBus().registerSticky(this);
refreshDialog = new ProgressDialog(getActivity()); refreshDialog = new ProgressDialog(getActivity());
refreshDialog.setMessage("Refreshing available modules..."); refreshDialog.setMessage("Refreshing available modules...");
refreshDialog.setCancelable(false); refreshDialog.setCancelable(false);

View File

@ -4,35 +4,31 @@ import android.util.Log;
import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBible;
import org.crosswire.jsword.book.BookCategory; import org.crosswire.jsword.book.BookCategory;
import org.crosswire.jsword.book.BookFilter;
import org.crosswire.jsword.book.BookFilters;
import org.crosswire.jsword.book.install.InstallManager; import org.crosswire.jsword.book.install.InstallManager;
import org.crosswire.jsword.book.install.Installer; import org.crosswire.jsword.book.install.Installer;
import java.util.Map; import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import de.greenrobot.event.EventBus; import de.greenrobot.event.EventBus;
@Singleton
public class DownloadManager { public class DownloadManager {
private final String TAG = "DownloadManager"; private final String TAG = "DownloadManager";
private static DownloadManager instance;
private EventBus downloadBus; @Inject
protected EventBus downloadBus;
public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE, public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE,
BookCategory.COMMENTARY, BookCategory.DICTIONARY, BookCategory.COMMENTARY, BookCategory.DICTIONARY,
BookCategory.MAPS }; BookCategory.MAPS };
public static DownloadManager getInstance() { public DownloadManager() {
if (instance == null) { MinimalBible.getApplication().inject(this);
instance = new DownloadManager();
}
return instance;
}
private DownloadManager() {
setDownloadDir(); setDownloadDir();
downloadBus = new EventBus();
downloadEvents(); downloadEvents();
} }