Refactor main menu to Kotlin

So much less code to write and maintain... love it.
This commit is contained in:
Bradlee Speice 2014-10-24 23:53:39 -04:00
parent 8104ffa862
commit 1eb914819d
10 changed files with 146 additions and 318 deletions

View File

@ -14,8 +14,6 @@ import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import rx.Observable;
/** /**
* Manager to keep track of which books have been installed * Manager to keep track of which books have been installed
*/ */
@ -23,6 +21,7 @@ import rx.Observable;
public class InstalledManager implements BooksListener { public class InstalledManager implements BooksListener {
@Inject Books installedBooks; @Inject Books installedBooks;
// TODO: Why is this injected if we initialize in the constructor?
@Inject List<Book> installedBooksList; @Inject List<Book> installedBooksList;
private String TAG = "InstalledManager"; private String TAG = "InstalledManager";
@ -38,12 +37,6 @@ public class InstalledManager implements BooksListener {
return installedBooksList.contains(b); return installedBooksList.contains(b);
} }
public Observable<Book> getInstalledBooks() {
// This method is needed to provide a fresher copy of what's installed
// than Books.getInstalled() does.
return Observable.from(installedBooksList);
}
@Override @Override
public void bookAdded(BooksEvent booksEvent) { public void bookAdded(BooksEvent booksEvent) {
Log.d(TAG, "Book added: " + booksEvent.getBook().toString()); Log.d(TAG, "Book added: " + booksEvent.getBook().toString());

View File

@ -1,185 +0,0 @@
package org.bspeice.minimalbible.activity.navigation;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import org.bspeice.minimalbible.R;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* ExpandableListView Navigation Drawer
* T1 represents Group objects, T2 is child objects
* Not sure if I'll ever actually need to re-use this, but go ahead and make it generic.
* TODO: Document this.
*/
public class ExpListNavAdapter<T1, T2> extends BaseExpandableListAdapter {
// Now we could technically implement this structure using a LinkedHashMap, but
// it's easier both to understand and program if we implement this using two maps.
Map<Integer, T1> indexableBibleBooks;
Map<T1, List<T2>> chaptersForBook;
private int groupHighlighted;
private int childHighlighted;
public ExpListNavAdapter(List<T1> groups, Map<T1, List<T2>> children) {
// Let the map know ahead of time how big it will be
// int bookCount = versification.getBookCount();
indexableBibleBooks = new HashMap<Integer, T1>(groups.size());
chaptersForBook = new HashMap<T1, List<T2>>(groups.size());
// Is it terrible that I don't like using an actual for loop?
for (int index = 0; index < groups.size(); index++) {
T1 gItem = groups.get(index);
indexableBibleBooks.put(index, gItem);
chaptersForBook.put(gItem, children.get(gItem));
}
}
@Override
public int getGroupCount() {
return indexableBibleBooks.size();
}
@Override
public int getChildrenCount(int i) {
return chaptersForBook.get(indexableBibleBooks.get(i)).size();
}
@Override
public T1 getGroup(int i) {
return indexableBibleBooks.get(i);
}
/**
* @param i The group position
* @param i2 The child position
* @return The child chapter value
*/
@Override
public T2 getChild(int i, int i2) {
return chaptersForBook.get(indexableBibleBooks.get(i)).get(i2);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i2) {
return i2;
}
@Override
public boolean hasStableIds() {
return true;
}
/**
* Get the views for this group
*
* @param position
* @param expanded
* @param convertView
* @param parent
* @return
*/
@Override
public View getGroupView(int position, boolean expanded,
View convertView, ViewGroup parent) {
NavItemHolder<T1> bookHolder;
if (convertView == null || convertView.getTag() == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_navigation_drawer,
parent, false);
bookHolder = new NavItemHolder<T1>(convertView);
convertView.setTag(bookHolder);
} else {
bookHolder = (NavItemHolder<T1>) convertView.getTag();
}
bookHolder.bind(getGroup(position), position == groupHighlighted);
return convertView;
}
/**
* Get the view for a child
*
* @param groupPosition
* @param childPosition
* @param isLastChild
* @param convertView
* @param parent
* @return
*/
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
NavItemHolder<T2> chapterHolder;
if (convertView == null || convertView.getTag() == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_navigation_drawer,
parent, false);
chapterHolder = new NavItemHolder<T2>(convertView);
convertView.setTag(chapterHolder);
} else {
chapterHolder = (NavItemHolder<T2>) convertView.getTag();
}
chapterHolder.bind(getChild(groupPosition, childPosition),
childPosition == childHighlighted);
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i2) {
return true;
}
public void setGroupHighlighted(int groupHighlighted) {
this.groupHighlighted = groupHighlighted;
}
public void setChildHighlighted(int childHighlighted) {
this.childHighlighted = childHighlighted;
}
/**
* Class to hold elements for the navbar - doesn't matter if they're group or child.
* T3 is either T1 or T2, doesn't matter.
*/
class NavItemHolder<T3> {
@InjectView(R.id.navlist_content)
TextView content;
View v;
public NavItemHolder(View v) {
this.v = v; // Needed for resolving colors below
ButterKnife.inject(this, v);
}
public void bind(T3 object, boolean highlighted) {
content.setText(object.toString());
if (highlighted) {
content.setTextColor(v.getResources().getColor(R.color.navbar_highlight));
} else {
content.setTextColor(v.getResources().getColor(R.color.navbar_unhighlighted));
}
}
}
}

View File

@ -2,11 +2,9 @@ package org.bspeice.minimalbible.activity.viewer;
import android.util.Log; import android.util.Log;
import org.bspeice.minimalbible.activity.navigation.ExpListNavAdapter;
import org.bspeice.minimalbible.service.book.VerseLookupModules; import org.bspeice.minimalbible.service.book.VerseLookupModules;
import org.bspeice.minimalbible.service.manager.BookManager; import org.bspeice.minimalbible.service.manager.BookManager;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.versification.VersificationUtil;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -21,17 +19,17 @@ import rx.functions.Action1;
import rx.functions.Func1; import rx.functions.Func1;
/** /**
* Created by bspeice on 6/18/14. * Modules used for the BibleViewer activity
*/ */
@Module( @Module(
injects = { injects = {
BibleViewer.class, BibleViewer.class,
BookFragment.class, BookFragment.class,
ExpListNavDrawerFragment.class, ExpListNavDrawerFragment.class
ExpListNavAdapter.class
}, },
includes = VerseLookupModules.class includes = VerseLookupModules.class
) )
@SuppressWarnings("unused")
public class BibleViewerModules { public class BibleViewerModules {
BibleViewer activity; BibleViewer activity;
@ -98,9 +96,4 @@ public class BibleViewerModules {
BookManager bookManager() { BookManager bookManager() {
return new BookManager(); return new BookManager();
} }
@Provides
VersificationUtil provideVersificationUtil() {
return new VersificationUtil();
}
} }

View File

@ -8,22 +8,12 @@ import android.widget.ExpandableListView;
import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.navigation.ExpListNavAdapter;
import org.bspeice.minimalbible.activity.navigation.NavDrawerFragment; import org.bspeice.minimalbible.activity.navigation.NavDrawerFragment;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.versification.BibleBook;
import org.crosswire.jsword.versification.VersificationUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import rx.functions.Action1;
/** /**
* ExpandableListView for managing books of the Bible. * ExpandableListView for managing books of the Bible.
* We extend from @link{BaseNavigationDrawerFragment} so we can inherit some of the lifecycle * We extend from @link{BaseNavigationDrawerFragment} so we can inherit some of the lifecycle
@ -34,7 +24,6 @@ import rx.functions.Action1;
*/ */
public class ExpListNavDrawerFragment extends NavDrawerFragment { public class ExpListNavDrawerFragment extends NavDrawerFragment {
@Inject VersificationUtil vUtil;
@Inject @Named("MainBook") @Inject @Named("MainBook")
Book mainBook; Book mainBook;
@ -48,48 +37,7 @@ public class ExpListNavDrawerFragment extends NavDrawerFragment {
mActualListView = (ExpandableListView) inflater.inflate( mActualListView = (ExpandableListView) inflater.inflate(
R.layout.fragment_expandable_navigation_drawer, container, false); R.layout.fragment_expandable_navigation_drawer, container, false);
/* mActualListView.setAdapter(new BibleMenu(mainBook));
mDrawerListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectItem(position);
}
});
*/
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.
// This isn't a totally functional style map-reduce, but the reduce step is
// unnecessarily verbose. Like this comment.
final Map<String, List<Integer>> chapterMap = new HashMap<String, List<Integer>>();
if (mainBook != null) {
vUtil.getBooks(mainBook).forEach(new Action1<BibleBook>() {
@Override
public void call(BibleBook bibleBook) {
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
}
chapterMap.put(vUtil.getBookName(mainBook, bibleBook), chapterList);
}
});
ExpListNavAdapter<String, Integer> adapter =
new ExpListNavAdapter<String, Integer>(bibleBooks, chapterMap);
mActualListView.setAdapter(adapter);
}
mActualListView.setItemChecked(mCurrentSelectedPosition, true); mActualListView.setItemChecked(mCurrentSelectedPosition, true);
return mActualListView; return mActualListView;

View File

@ -0,0 +1,7 @@
package org.bspeice.minimalbible.exception;
/**
* Error to be thrown when no books are currently installed.
*/
public class NoBooksInstalledException extends Exception {
}

View File

@ -0,0 +1,84 @@
package org.bspeice.minimalbible.activity.viewer
import android.widget.BaseExpandableListAdapter
import android.view.View
import android.view.ViewGroup
import org.crosswire.jsword.book.Book
import org.crosswire.jsword.book.getVersification
import org.crosswire.jsword.versification.getBooks
import org.crosswire.jsword.book.bookName
import android.widget.TextView
import org.bspeice.minimalbible.R
import android.content.Context
import android.view.LayoutInflater
/**
* Created by bspeice on 10/24/14.
*/
class BibleMenu(val b: Book) : BaseExpandableListAdapter() {
// Map BibleBooks to the number of chapters they have
val menuMappings = b.getVersification().getBooks().map {
Pair(it, b.getVersification().getLastChapter(it))
}
var groupHighlighted: Int = 0
var childHighlighted: Int = 0
override fun getGroupCount(): Int = menuMappings.count()
override fun getChildrenCount(group: Int): Int = menuMappings.elementAt(group).component2()
override fun getGroup(group: Int): String =
b.bookName(menuMappings.elementAt(group).component1())
override fun getChild(group: Int, child: Int): Int = child + 1 // Index offset
override fun getGroupId(group: Int): Long = group.toLong()
override fun getChildId(group: Int, child: Int): Long = child.toLong()
override fun hasStableIds(): Boolean = true
override fun isChildSelectable(group: Int, child: Int): Boolean = true
private fun doBinding(convertView: View?, parent: ViewGroup?,
obj: Any, highlight: Boolean): View {
val finalView: View = if (convertView != null) convertView
else {
val inflater = parent!!.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
inflater.inflate(R.layout.list_navigation_drawer, parent, false)
}
val holder: NavItemHolder = if (finalView.getTag() != null) finalView.getTag() as NavItemHolder
else NavItemHolder(finalView, R.id.navlist_content)
holder.bind(obj, highlight)
finalView.setTag(holder)
return finalView
}
override fun getGroupView(position: Int, expanded: Boolean,
convertView: View?, parent: ViewGroup?): View =
doBinding(convertView, parent, getGroup(position), position == groupHighlighted)
override fun getChildView(group: Int, child: Int, isLast: Boolean,
convertView: View?, parent: ViewGroup?): View =
doBinding(convertView, parent, getChild(group, child), child == childHighlighted)
// Resource should be IdRes
class NavItemHolder(val bindTo: View, resource: Int) {
val content: TextView = bindTo.findViewById(resource) as TextView
fun bind(obj: Any, highlighted: Boolean) {
content.setText(obj.toString())
if (highlighted)
content.setTextColor(bindTo.getResources().getColor(R.color.navbar_highlight))
else
content.setTextColor(bindTo.getResources().getColor(R.color.navbar_unhighlighted))
}
}
}

View File

@ -5,10 +5,10 @@ import org.bspeice.minimalbible.service.book.VerseLookupService
import android.webkit.WebViewClient import android.webkit.WebViewClient
import android.webkit.JavascriptInterface import android.webkit.JavascriptInterface
import org.crosswire.jsword.book.Book import org.crosswire.jsword.book.Book
import org.crosswire.jsword.versification.getVersification
import java.util.ArrayList import java.util.ArrayList
import android.util.Log import android.util.Log
import rx.subjects.PublishSubject import rx.subjects.PublishSubject
import org.crosswire.jsword.book.getVersification
/** /**
* Created by bspeice on 9/14/14. * Created by bspeice on 9/14/14.

View File

@ -5,30 +5,30 @@ import org.crosswire.jsword.book.Books
import rx.functions.Action1 import rx.functions.Action1
import org.crosswire.jsword.book.Book import org.crosswire.jsword.book.Book
import rx.functions.Action0 import rx.functions.Action0
import org.bspeice.minimalbible.exception.NoBooksInstalledException
import javax.inject.Singleton
/** /**
* 'Open' class and values for mockito to subclass * 'Open' class and values for mockito to subclass
* http://confluence.jetbrains.com/display/Kotlin/Classes+and+Inheritance * http://confluence.jetbrains.com/display/Kotlin/Classes+and+Inheritance
*/ */
//@Singleton Singleton
open class BookManager() { open class BookManager() {
// Some extra books like to think they're installed, but trigger NPE all over the place... // Some extra books like to think they're installed, but trigger NPE all over the place...
val ignore = array("ERen_no", "ot1nt2"); val ignore = array("ERen_no", "ot1nt2");
open val installedBooks = Observable.from(Books.installed()!!.getBooks()) open val installedBooks = Observable.from(Books.installed()!!.getBooks())
?.filter { !ignore.contains(it!!.getInitials()) } ?.filter { !ignore.contains(it!!.getInitials()) }
?.cache() ?.cache() ?: throw NoBooksInstalledException()
var refreshComplete = false; var refreshComplete = false;
{ {
// TODO: Cleaner way of expressing this? // TODO: Cleaner way of expressing this?
installedBooks?.subscribe(Action1<Book> { result -> }, installedBooks.subscribe(Action1<Book> { result -> },
Action1<Throwable> { error -> }, Action1<Throwable> { error -> },
Action0 { refreshComplete = true }) Action0 { refreshComplete = true })
} }
fun getBooks(): Observable<Book> {
return installedBooks as Observable
}
} }

View File

@ -0,0 +1,28 @@
package org.crosswire.jsword.book
import org.crosswire.jsword.versification.Versification
import org.crosswire.jsword.versification.system.Versifications
import android.util.Log
import org.bspeice.minimalbible.service.manager.InvalidBookException
import org.crosswire.jsword.versification.getBookNames
import org.crosswire.jsword.versification.BibleBook
/**
* Utility methods for dealing with Books
*/
fun Book.getVersification(): Versification {
val v = Versifications.instance()!!.getVersification(
this.getBookMetaData()!!.getProperty(BookMetaData.KEY_VERSIFICATION).toString()
)
if (v == null) {
Log.e(javaClass<Book>().getSimpleName(), "Invalid book: " + this.getInitials())
throw InvalidBookException(this.getInitials())
} else
return v
}
fun Book.bookNames(): List<String> = this.getVersification().getBookNames()
fun Book.bookName(bBook: BibleBook): String =
this.getVersification().getBookName(bBook).getLongName()

View File

@ -1,45 +1,16 @@
package org.crosswire.jsword.versification package org.crosswire.jsword.versification
import org.crosswire.jsword.book.Book
import java.util.ArrayList import java.util.ArrayList
import org.crosswire.jsword.versification.system.Versifications
import org.crosswire.jsword.book.BookMetaData
import rx.Observable
import android.util.Log
import org.bspeice.minimalbible.service.manager.InvalidBookException
/** /**
* Created by bspeice on 9/10/14. * VersificationUtil class allows Java to easily reach in to Kotlin
*/ */
object INTRO_BOOKS {
class VersificationUtil() { val INTROS = array(
class object { BibleBook.INTRO_BIBLE,
val INTROS = array( BibleBook.INTRO_OT,
BibleBook.INTRO_BIBLE, BibleBook.INTRO_NT
BibleBook.INTRO_OT, )
BibleBook.INTRO_NT
)
}
fun getBookNames(b: Book): Observable<String> {
return Observable.from(b.getVersification().getBookNames())
}
fun getBooks(b: Book): Observable<BibleBook> {
return Observable.from(b.getVersification().getBooks())
}
fun getChapterCount(b: Book, bibleBook: BibleBook): Int {
return b.getVersification().getChapterCount(bibleBook)
}
fun getBookName(b: Book, bibleBook: BibleBook): String {
return b.getVersification().getLongName(bibleBook)
}
fun getVersification(b: Book): Versification {
return b.getVersification()
}
} }
// TODO: Refactor (is there a better way to accomplish this?) and move // TODO: Refactor (is there a better way to accomplish this?) and move
@ -52,26 +23,15 @@ fun <T> Iterator<T>.iterable(): Iterable<T> {
return list return list
} }
fun Versification.getBooks(): List<BibleBook> { fun Versification.getAllBooks(): List<BibleBook> =
return this.getBookIterator()!!.iterable() this.getBookIterator()!!.iterable().toList()
.filter { !VersificationUtil.INTROS.contains(it) }
}
fun Versification.getBookNames(): List<String> { fun Versification.getBooks(): List<BibleBook> =
return this.getBooks().map { this.getLongName(it) } this.getAllBooks().filter { !INTRO_BOOKS.INTROS.contains(it) }
}
fun Versification.getBookNames(): List<String> =
this.getBooks().map { this.getLongName(it) }
fun Versification.getChapterCount(b: BibleBook): Int { fun Versification.getChapterCount(b: BibleBook): Int {
return this.getLastChapter(b) return this.getLastChapter(b)
} }
fun Book.getVersification(): Versification {
val v = Versifications.instance()!!.getVersification(
this.getBookMetaData()!!.getProperty(BookMetaData.KEY_VERSIFICATION).toString()
)
if (v == null) {
Log.e(javaClass<Book>().getSimpleName(), "Invalid book: " + this.getInitials())
throw InvalidBookException(this.getInitials())
} else
return v
}