Handle no books installed on start properly.

This commit is contained in:
Bradlee Speice
2014-09-03 22:49:18 -04:00
parent 54d0157785
commit 80238f3cf2
6 changed files with 150 additions and 68 deletions

View File

@ -25,6 +25,7 @@ import javax.inject.Inject;
import dagger.ObjectGraph;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
public class BibleViewer extends BaseActivity implements
NavDrawerFragment.NavigationDrawerCallbacks,
@ -66,35 +67,42 @@ public class BibleViewer extends BaseActivity implements
bvObjectGraph.inject(o);
}
@Override
/**
* Set up the application
* TODO: Get the main book, rather than the first installed book.
*
* @param savedInstanceState Android's savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.inject(this);
// If no books are installed, we need to download one first.
int count = bookManager.getInstalledBooks()
.count()
.toBlocking()
.last();
if (count <= 0) {
Intent i = new Intent(this, DownloadActivity.class);
startActivityForResult(i, 0);
finish();
} else {
bookManager.getInstalledBooks()
.first()
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Book>() {
@Override
public void call(Book book) {
Log.d("BibleViewer", "Subscribed to display book: " + book.getName());
displayMainBook(book);
}
});
}
setContentView(R.layout.activity_bible_viewer);
// If no books are installed, we need to download one first. However,
// RxJava will error if there's nothing installed.
bookManager.getInstalledBooks()
.first()
.onErrorReturn(new Func1<Throwable, Book>() {
@Override
public Book call(Throwable throwable) {
// If there are no books installed...
Log.e(getLocalClassName(), "No books are currently installed, starting DownloadManager");
Intent i = new Intent(BibleViewer.this, DownloadActivity.class);
startActivityForResult(i, 0);
finish();
return null;
}
})
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Book>() {
@Override
public void call(Book book) {
Log.e("BibleViewer", "Subscribed to display book: " + book.getName());
displayMainBook(book);
}
});
setContentView(R.layout.activity_bible_viewer);
mNavigationDrawerFragment = (ExpListNavDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);

View File

@ -2,13 +2,12 @@ package org.bspeice.minimalbible.activity.viewer;
import android.util.Log;
import com.google.gson.Gson;
import org.bspeice.minimalbible.activity.navigation.ExpListNavAdapter;
import org.bspeice.minimalbible.activity.viewer.bookutil.VersificationUtil;
import org.bspeice.minimalbible.service.book.VerseLookupModules;
import org.crosswire.jsword.book.Book;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicReference;
import javax.inject.Named;
@ -69,13 +68,25 @@ public class BibleViewerModules {
});
if (mBook.get() == null) {
Book fallback;
fallback = bookManager.getInstalledBooks()
.toBlocking().first();
prefs.defaultBookName(fallback.getName());
return fallback;
try {
Book fallback;
fallback = bookManager.getInstalledBooks()
.onErrorReturn(new Func1<Throwable, Book>() {
@Override
public Book call(Throwable throwable) {
// If there's no book installed, we can't select the main one...
return null;
}
})
.toBlocking().first();
prefs.defaultBookName(fallback.getName());
return fallback;
} catch (NoSuchElementException e) {
// If no books are installed, there's really nothing we can do...
Log.d("BibleViewerModules", "No books are installed, so can't select a main book.");
return null;
}
} else {
return mBook.get();
}

View File

@ -58,46 +58,52 @@ public class ExpListNavDrawerFragment extends NavDrawerFragment {
});
*/
List<String> bibleBooks = vUtil.getBookNames(mainBook)
.toList().toBlocking().first();
List<String> bibleBooks;
if (mainBook != null) {
bibleBooks = vUtil.getBookNames(mainBook)
.toList().toBlocking().first();
} else {
bibleBooks = new ArrayList<String>();
}
// I really don't like how we build the chapters, but I'm not adding Guava just for Range.
// RXJava does get ridiculous with the angle brackets, you have me there. But Intellij
// folds nicely.
Map<String, List<Integer>> chapterMap =
vUtil.getBooks(mainBook).map(new Func1<BibleBook, Map<String, List<Integer>>>() {
@Override
public Map<String, List<Integer>> call(BibleBook bibleBook) {
// These lines are important
int bookCount = vUtil.getChapterCount(mainBook, bibleBook);
List<Integer> chapterList = new ArrayList<Integer>(bookCount);
for (int i = 0; i < bookCount; i++) {
chapterList.add(i + 1); // Index to chapter number
}
// </important>
Map<String, List<Integer>> bookListMap =
new HashMap<String, List<Integer>>(1);
bookListMap.put(vUtil.getBookName(mainBook, bibleBook), chapterList);
return bookListMap;
Map<String, List<Integer>> chapterMap = new HashMap<String, List<Integer>>();
if (mainBook != null) {
vUtil.getBooks(mainBook).map(new Func1<BibleBook, Map<String, List<Integer>>>() {
@Override
public Map<String, List<Integer>> call(BibleBook bibleBook) {
// These lines are important
int bookCount = vUtil.getChapterCount(mainBook, bibleBook);
List<Integer> chapterList = new ArrayList<Integer>(bookCount);
for (int i = 0; i < bookCount; i++) {
chapterList.add(i + 1); // Index to chapter number
}
})
.reduce(new Func2<Map<String, List<Integer>>,
Map<String, List<Integer>>,
Map<String, List<Integer>>>() {
@Override
public Map<String, List<Integer>>
call(Map<String, List<Integer>> acc,
Map<String, List<Integer>> value) {
// These lines are important
acc.putAll(value);
return acc;
// </important>
}
})
.toBlocking()
.first();
// </important>
Map<String, List<Integer>> bookListMap =
new HashMap<String, List<Integer>>(1);
bookListMap.put(vUtil.getBookName(mainBook, bibleBook), chapterList);
return bookListMap;
}
})
.reduce(new Func2<Map<String, List<Integer>>,
Map<String, List<Integer>>,
Map<String, List<Integer>>>() {
@Override
public Map<String, List<Integer>>
call(Map<String, List<Integer>> acc,
Map<String, List<Integer>> value) {
// These lines are important
acc.putAll(value);
return acc;
// </important>
}
})
.toBlocking()
.first();
}
ExpListNavAdapter<String, Integer> adapter =
new ExpListNavAdapter<String, Integer>(bibleBooks, chapterMap);