From 0014ec8badd740d0153c194cc30b4a67845d6fae Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Fri, 11 Jul 2014 19:56:02 -0400 Subject: [PATCH] Display books of the Bible in the navbar --- .../activity/viewer/BibleViewer.java | 3 +- .../activity/viewer/BibleViewerModules.java | 78 ++++++++++++++++++- .../viewer/BibleViewerPreferences.java | 13 ++++ .../activity/viewer/BookFragment.java | 48 ++++-------- .../viewer/ViewerNavDrawerFragment.java | 24 ++++-- .../viewer/bookutil/VersificationUtil.java | 54 +++++++++++++ .../minimalbible/util/IteratorUtil.java | 17 ++++ 7 files changed, 196 insertions(+), 41 deletions(-) create mode 100644 app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerPreferences.java create mode 100644 app/src/main/java/org/bspeice/minimalbible/activity/viewer/bookutil/VersificationUtil.java create mode 100644 app/src/main/java/org/bspeice/minimalbible/util/IteratorUtil.java diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java index d87e4f1..924b5bc 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java @@ -17,7 +17,6 @@ import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activity.BaseActivity; import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment; import org.bspeice.minimalbible.activity.downloader.DownloadActivity; -import org.bspeice.minimalbible.activity.downloader.DownloadActivityModules; import org.crosswire.jsword.book.Book; import javax.inject.Inject; @@ -39,7 +38,7 @@ public class BibleViewer extends BaseActivity implements private void buildObjGraph() { if (bvObjectGraph == null) { bvObjectGraph = MinimalBible.get(this) - .plus(new BibleViewerModules()); + .plus(new BibleViewerModules(this)); } bvObjectGraph.inject(this); } diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java index a53d410..1ecb7fe 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java @@ -1,6 +1,21 @@ package org.bspeice.minimalbible.activity.viewer; +import android.util.Log; + +import org.bspeice.minimalbible.Injector; +import org.bspeice.minimalbible.activity.viewer.bookutil.VersificationUtil; +import org.crosswire.jsword.book.Book; + +import java.util.concurrent.atomic.AtomicReference; + +import javax.inject.Named; +import javax.inject.Singleton; + import dagger.Module; +import dagger.Provides; +import de.devland.esperandro.Esperandro; +import rx.functions.Action1; +import rx.functions.Func1; /** * Created by bspeice on 6/18/14. @@ -8,8 +23,65 @@ import dagger.Module; @Module( injects = { BibleViewer.class, - BookFragment.class - } + BookFragment.class, + ViewerNavDrawerFragment.class + }, + library = true ) public class BibleViewerModules { -} + BibleViewer activity; + + public BibleViewerModules(BibleViewer activity) { + this.activity = activity; + } + + @Provides @Singleton + Injector provideInjector() { + return activity; + } + + @Provides @Singleton + BibleViewerPreferences providePrefs() { + return Esperandro.getPreferences(BibleViewerPreferences.class, activity); + } + + @Provides @Named("MainBook") + Book provideMainBook(BookManager bookManager, final BibleViewerPreferences prefs) { + final AtomicReference mBook = new AtomicReference(null); + bookManager.getInstalledBooks() + .first(new Func1() { + @Override + public Boolean call(Book book) { + return book.getName().equals(prefs.defaultBookName()); + } + }) + .subscribe(new Action1() { + @Override + public void call(Book book) { + mBook.set(book); + } + },new Action1() { + @Override + public void call(Throwable throwable) { + Log.d("BibleViewerModules", throwable.getLocalizedMessage()); + } + }); + + if (mBook.get() == null) { + Book fallback; + fallback = bookManager.getInstalledBooks() + .toBlocking().first(); + + prefs.defaultBookName(fallback.getName()); + return fallback; + + } else { + return mBook.get(); + } + } + + @Provides + VersificationUtil provideVersificationUtil() { + return new VersificationUtil(); + } +} \ No newline at end of file diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerPreferences.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerPreferences.java new file mode 100644 index 0000000..6b081a6 --- /dev/null +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerPreferences.java @@ -0,0 +1,13 @@ +package org.bspeice.minimalbible.activity.viewer; + +import de.devland.esperandro.annotations.SharedPreferences; + +/** + * Created by bspeice on 7/11/14. + */ +@SharedPreferences(name = "BibleViewerPreferences") +public interface BibleViewerPreferences { + + String defaultBookName(); + void defaultBookName(String defaultBookName); +} diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java index 5b5f274..571b7a5 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java @@ -10,41 +10,41 @@ import android.webkit.WebView; import android.webkit.WebViewClient; import org.bspeice.minimalbible.Injector; -import org.bspeice.minimalbible.MinimalBible; import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activity.BaseFragment; import org.crosswire.jsword.book.Book; +import org.crosswire.jsword.book.BookMetaData; +import org.crosswire.jsword.versification.Versification; import java.util.List; import javax.inject.Inject; +import javax.inject.Named; import butterknife.ButterKnife; import butterknife.InjectView; -import rx.android.schedulers.AndroidSchedulers; -import rx.functions.Action1; -import rx.functions.Func1; + +import static org.crosswire.jsword.versification.system.Versifications.instance; /** * A placeholder fragment containing a simple view. */ public class BookFragment extends BaseFragment { + Injector i; - @Inject BookManager bookManager; + @Inject @Named("MainBook") Book mBook; @InjectView(R.id.book_content) WebView mainContent; private static final String ARG_BOOK_NAME = "book_name"; - private Book mBook; - /** * Returns a new instance of this fragment for the given section number. */ public static BookFragment newInstance(String bookName, Injector injector) { BookFragment fragment = new BookFragment(); - injector.inject(fragment); + fragment.i = injector; Bundle args = new Bundle(); args.putString(ARG_BOOK_NAME, bookName); fragment.setArguments(args); @@ -57,7 +57,7 @@ public class BookFragment extends BaseFragment { @Override public void onCreate(Bundle state) { super.onCreate(state); - ((Injector)getActivity()).inject(this); + } @Override @@ -65,37 +65,23 @@ public class BookFragment extends BaseFragment { Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_viewer_main, container, false); + i.inject(this); ButterKnife.inject(this, rootView); mainContent.getSettings().setJavaScriptEnabled(true); // TODO: Load initial text from SharedPreferences - // And due to Observable async, we can kick off fetching the actual book asynchronously! - bookManager.getInstalledBooks() - .first(new Func1() { - @Override - public Boolean call(Book book) { - String mBookName = getArguments().getString(ARG_BOOK_NAME); - return book.getName().equals(mBookName); - } - }) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new Action1() { - @Override - public void call(Book book) { - BookFragment.this.mBook = book; - displayBook(book); - } - }, new Action1() { - @Override - public void call(Throwable throwable) { - Log.d("BookFragment", "No books installed?"); - } - }); + displayBook(mBook); + + Log.d("BookFragment", getVersification(mBook).toString()); return rootView; } + private Versification getVersification(Book b) { + return instance().getVersification((String) b.getBookMetaData().getProperty(BookMetaData.KEY_VERSIFICATION)); + } + // TODO: Remove? @Override public void onAttach(Activity activity) { diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/ViewerNavDrawerFragment.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/ViewerNavDrawerFragment.java index 47d3c32..3a71e2e 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/ViewerNavDrawerFragment.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/ViewerNavDrawerFragment.java @@ -8,14 +8,28 @@ import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; +import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment; +import org.bspeice.minimalbible.activity.viewer.bookutil.VersificationUtil; +import org.crosswire.jsword.book.Book; + +import java.util.List; + +import javax.inject.Inject; +import javax.inject.Named; public class ViewerNavDrawerFragment extends BaseNavigationDrawerFragment { - @Override + @Inject VersificationUtil vUtil; + @Inject @Named("MainBook") + Book mainBook; + + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + ((Injector)getActivity()).inject(this); + mDrawerListView = (ListView) inflater.inflate( R.layout.fragment_navigation_drawer, container, false); mDrawerListView @@ -26,12 +40,12 @@ public class ViewerNavDrawerFragment extends BaseNavigationDrawerFragment { selectItem(position); } }); + List bookNames = vUtil.getNiceBookNames(mainBook) + .toList().toBlocking().first(); + mDrawerListView.setAdapter(new ArrayAdapter(getActionBar() .getThemedContext(), android.R.layout.simple_list_item_1, - android.R.id.text1, new String[] { - getString(R.string.title_section1), - getString(R.string.title_section2), - getString(R.string.title_section3)})); + android.R.id.text1, bookNames)); mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); return mDrawerListView; } diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/bookutil/VersificationUtil.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/bookutil/VersificationUtil.java new file mode 100644 index 0000000..17f346d --- /dev/null +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/bookutil/VersificationUtil.java @@ -0,0 +1,54 @@ +package org.bspeice.minimalbible.activity.viewer.bookutil; + +import org.bspeice.minimalbible.util.IteratorUtil; +import org.crosswire.jsword.book.Book; +import org.crosswire.jsword.book.BookMetaData; +import org.crosswire.jsword.versification.BibleBook; +import org.crosswire.jsword.versification.Versification; +import org.crosswire.jsword.versification.system.Versifications; + +import java.util.ArrayList; +import java.util.List; + +import rx.Observable; +import rx.functions.Func1; + +/** + * Created by bspeice on 7/11/14. + */ +public class VersificationUtil { + + private static final List INTROS = new ArrayList() {{ + add(BibleBook.INTRO_BIBLE); + add(BibleBook.INTRO_OT); + add(BibleBook.INTRO_NT); + }}; + + public Versification getVersification(Book b) { + return Versifications.instance().getVersification( + (String) b.getBookMetaData().getProperty(BookMetaData.KEY_VERSIFICATION) + ); + } + + public Observable getBookNames(Book b) { + Versification v = getVersification(b); + return Observable.from(IteratorUtil.copyIterator(v.getBookIterator())) + .filter(new Func1() { + @Override + public Boolean call(BibleBook bibleBook) { + return !INTROS.contains(bibleBook); + } + }); + } + + public Observable getNiceBookNames(final Book b) { + + return getBookNames(b) + .map(new Func1() { + @Override + public String call(BibleBook bibleBook) { + return getVersification(b).getLongName(bibleBook); + } + }); + } +} diff --git a/app/src/main/java/org/bspeice/minimalbible/util/IteratorUtil.java b/app/src/main/java/org/bspeice/minimalbible/util/IteratorUtil.java new file mode 100644 index 0000000..f8603f5 --- /dev/null +++ b/app/src/main/java/org/bspeice/minimalbible/util/IteratorUtil.java @@ -0,0 +1,17 @@ +package org.bspeice.minimalbible.util; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * Created by bspeice on 7/11/14. + */ +public class IteratorUtil { + public static List copyIterator(Iterator iter) { + List copy = new ArrayList(); + while (iter.hasNext()) + copy.add(iter.next()); + return copy; + } +}