Display books of the Bible in the navbar

This commit is contained in:
Bradlee Speice 2014-07-11 19:56:02 -04:00
parent 365cf0dccb
commit 0014ec8bad
7 changed files with 196 additions and 41 deletions

View File

@ -17,7 +17,6 @@ import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseActivity; import org.bspeice.minimalbible.activity.BaseActivity;
import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment; import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment;
import org.bspeice.minimalbible.activity.downloader.DownloadActivity; import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
import org.bspeice.minimalbible.activity.downloader.DownloadActivityModules;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import javax.inject.Inject; import javax.inject.Inject;
@ -39,7 +38,7 @@ public class BibleViewer extends BaseActivity implements
private void buildObjGraph() { private void buildObjGraph() {
if (bvObjectGraph == null) { if (bvObjectGraph == null) {
bvObjectGraph = MinimalBible.get(this) bvObjectGraph = MinimalBible.get(this)
.plus(new BibleViewerModules()); .plus(new BibleViewerModules(this));
} }
bvObjectGraph.inject(this); bvObjectGraph.inject(this);
} }

View File

@ -1,6 +1,21 @@
package org.bspeice.minimalbible.activity.viewer; 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.Module;
import dagger.Provides;
import de.devland.esperandro.Esperandro;
import rx.functions.Action1;
import rx.functions.Func1;
/** /**
* Created by bspeice on 6/18/14. * Created by bspeice on 6/18/14.
@ -8,8 +23,65 @@ import dagger.Module;
@Module( @Module(
injects = { injects = {
BibleViewer.class, BibleViewer.class,
BookFragment.class BookFragment.class,
} ViewerNavDrawerFragment.class
},
library = true
) )
public class BibleViewerModules { 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<Book> mBook = new AtomicReference<Book>(null);
bookManager.getInstalledBooks()
.first(new Func1<Book, Boolean>() {
@Override
public Boolean call(Book book) {
return book.getName().equals(prefs.defaultBookName());
}
})
.subscribe(new Action1<Book>() {
@Override
public void call(Book book) {
mBook.set(book);
}
},new Action1<Throwable>() {
@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();
}
} }

View File

@ -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);
}

View File

@ -10,41 +10,41 @@ import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseFragment; import org.bspeice.minimalbible.activity.BaseFragment;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookMetaData;
import org.crosswire.jsword.versification.Versification;
import java.util.List; import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.InjectView; import butterknife.InjectView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1; import static org.crosswire.jsword.versification.system.Versifications.instance;
import rx.functions.Func1;
/** /**
* A placeholder fragment containing a simple view. * A placeholder fragment containing a simple view.
*/ */
public class BookFragment extends BaseFragment { public class BookFragment extends BaseFragment {
Injector i;
@Inject BookManager bookManager; @Inject @Named("MainBook") Book mBook;
@InjectView(R.id.book_content) @InjectView(R.id.book_content)
WebView mainContent; WebView mainContent;
private static final String ARG_BOOK_NAME = "book_name"; private static final String ARG_BOOK_NAME = "book_name";
private Book mBook;
/** /**
* Returns a new instance of this fragment for the given section number. * Returns a new instance of this fragment for the given section number.
*/ */
public static BookFragment newInstance(String bookName, Injector injector) { public static BookFragment newInstance(String bookName, Injector injector) {
BookFragment fragment = new BookFragment(); BookFragment fragment = new BookFragment();
injector.inject(fragment); fragment.i = injector;
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString(ARG_BOOK_NAME, bookName); args.putString(ARG_BOOK_NAME, bookName);
fragment.setArguments(args); fragment.setArguments(args);
@ -57,7 +57,7 @@ public class BookFragment extends BaseFragment {
@Override @Override
public void onCreate(Bundle state) { public void onCreate(Bundle state) {
super.onCreate(state); super.onCreate(state);
((Injector)getActivity()).inject(this);
} }
@Override @Override
@ -65,37 +65,23 @@ public class BookFragment extends BaseFragment {
Bundle savedInstanceState) { Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_viewer_main, container, View rootView = inflater.inflate(R.layout.fragment_viewer_main, container,
false); false);
i.inject(this);
ButterKnife.inject(this, rootView); ButterKnife.inject(this, rootView);
mainContent.getSettings().setJavaScriptEnabled(true); mainContent.getSettings().setJavaScriptEnabled(true);
// TODO: Load initial text from SharedPreferences // TODO: Load initial text from SharedPreferences
// And due to Observable async, we can kick off fetching the actual book asynchronously! displayBook(mBook);
bookManager.getInstalledBooks()
.first(new Func1<Book, Boolean>() { Log.d("BookFragment", getVersification(mBook).toString());
@Override
public Boolean call(Book book) {
String mBookName = getArguments().getString(ARG_BOOK_NAME);
return book.getName().equals(mBookName);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Book>() {
@Override
public void call(Book book) {
BookFragment.this.mBook = book;
displayBook(book);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Log.d("BookFragment", "No books installed?");
}
});
return rootView; return rootView;
} }
private Versification getVersification(Book b) {
return instance().getVersification((String) b.getBookMetaData().getProperty(BookMetaData.KEY_VERSIFICATION));
}
// TODO: Remove? // TODO: Remove?
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {

View File

@ -8,14 +8,28 @@ import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment; 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 { public class ViewerNavDrawerFragment extends BaseNavigationDrawerFragment {
@Override @Inject VersificationUtil vUtil;
@Inject @Named("MainBook")
Book mainBook;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
((Injector)getActivity()).inject(this);
mDrawerListView = (ListView) inflater.inflate( mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false); R.layout.fragment_navigation_drawer, container, false);
mDrawerListView mDrawerListView
@ -26,12 +40,12 @@ public class ViewerNavDrawerFragment extends BaseNavigationDrawerFragment {
selectItem(position); selectItem(position);
} }
}); });
List<String> bookNames = vUtil.getNiceBookNames(mainBook)
.toList().toBlocking().first();
mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar() mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar()
.getThemedContext(), android.R.layout.simple_list_item_1, .getThemedContext(), android.R.layout.simple_list_item_1,
android.R.id.text1, new String[] { android.R.id.text1, bookNames));
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3)}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView; return mDrawerListView;
} }

View File

@ -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<BibleBook> INTROS = new ArrayList<BibleBook>() {{
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<BibleBook> getBookNames(Book b) {
Versification v = getVersification(b);
return Observable.from(IteratorUtil.copyIterator(v.getBookIterator()))
.filter(new Func1<BibleBook, Boolean>() {
@Override
public Boolean call(BibleBook bibleBook) {
return !INTROS.contains(bibleBook);
}
});
}
public Observable<String> getNiceBookNames(final Book b) {
return getBookNames(b)
.map(new Func1<BibleBook, String>() {
@Override
public String call(BibleBook bibleBook) {
return getVersification(b).getLongName(bibleBook);
}
});
}
}

View File

@ -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 <T> List<T> copyIterator(Iterator<T> iter) {
List<T> copy = new ArrayList<T>();
while (iter.hasNext())
copy.add(iter.next());
return copy;
}
}