diff --git a/MinimalBible/src/org/bspeice/minimalbible/MinimalBible.java b/MinimalBible/src/org/bspeice/minimalbible/MinimalBible.java index 9d0705e..2e88fd0 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/MinimalBible.java +++ b/MinimalBible/src/org/bspeice/minimalbible/MinimalBible.java @@ -3,7 +3,11 @@ package org.bspeice.minimalbible; import android.app.Application; import android.content.Context; +import dagger.ObjectGraph; + public class MinimalBible extends Application { + + private ObjectGraph graph; private static MinimalBible instance; @@ -15,4 +19,21 @@ public class MinimalBible extends Application { 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(); + } } diff --git a/MinimalBible/src/org/bspeice/minimalbible/MinimalBibleModules.java b/MinimalBible/src/org/bspeice/minimalbible/MinimalBibleModules.java new file mode 100644 index 0000000..e23674a --- /dev/null +++ b/MinimalBible/src/org/bspeice/minimalbible/MinimalBibleModules.java @@ -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 { +} diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/ActivityModules.java b/MinimalBible/src/org/bspeice/minimalbible/activities/ActivityModules.java new file mode 100644 index 0000000..de3a0d9 --- /dev/null +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/ActivityModules.java @@ -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 { +} diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java new file mode 100644 index 0000000..0411bd2 --- /dev/null +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java @@ -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(); + } +} diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookListFragment.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookListFragment.java index 404a41c..97a8d7f 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookListFragment.java +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/BookListFragment.java @@ -18,6 +18,7 @@ import android.widget.Toast; import com.readystatesoftware.systembartint.SystemBarTintManager; +import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.MinimalBibleConstants; import org.bspeice.minimalbible.R; import org.crosswire.jsword.book.Book; @@ -27,12 +28,15 @@ import org.crosswire.jsword.book.FilterUtil; import java.util.List; +import javax.inject.Inject; + import butterknife.ButterKnife; import butterknife.InjectView; /** * A placeholder fragment containing a simple view. */ + public class BookListFragment extends 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) ListView downloadsAvailable; + @Inject + DownloadManager downloadManager; + private ProgressDialog refreshDialog; /** @@ -58,7 +65,11 @@ public class BookListFragment extends Fragment { return fragment; } - public BookListFragment() { + @Override + public void onCreate(Bundle state) { + super.onCreate(state); + MinimalBible app = MinimalBible.getApplication(getActivity()); + app.inject(this); } @Override @@ -100,10 +111,9 @@ public class BookListFragment extends Fragment { } private void refreshModules() { - DownloadManager dm = DownloadManager.getInstance(); - EventBookList bookList = dm.getDownloadBus().getStickyEvent(EventBookList.class); + EventBookList bookList = downloadManager.getDownloadBus().getStickyEvent(EventBookList.class); if (bookList == null) { - dm.getDownloadBus().registerSticky(this); + downloadManager.getDownloadBus().registerSticky(this); refreshDialog = new ProgressDialog(getActivity()); refreshDialog.setMessage("Refreshing available modules..."); refreshDialog.setCancelable(false); diff --git a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java index c96a20c..2b27183 100644 --- a/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java +++ b/MinimalBible/src/org/bspeice/minimalbible/activities/downloader/DownloadManager.java @@ -4,35 +4,31 @@ import android.util.Log; import org.bspeice.minimalbible.MinimalBible; 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.Installer; import java.util.Map; +import javax.inject.Inject; +import javax.inject.Singleton; + import de.greenrobot.event.EventBus; +@Singleton public class 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, BookCategory.COMMENTARY, BookCategory.DICTIONARY, BookCategory.MAPS }; - public static DownloadManager getInstance() { - if (instance == null) { - instance = new DownloadManager(); - } - return instance; - } - - private DownloadManager() { + public DownloadManager() { + MinimalBible.getApplication().inject(this); setDownloadDir(); - downloadBus = new EventBus(); downloadEvents(); }