mirror of
https://github.com/MinimalBible/MinimalBible
synced 2025-07-13 03:35:20 -04:00
Refactor main menu to Kotlin
So much less code to write and maintain... love it.
This commit is contained in:
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -5,10 +5,10 @@ import org.bspeice.minimalbible.service.book.VerseLookupService
|
||||
import android.webkit.WebViewClient
|
||||
import android.webkit.JavascriptInterface
|
||||
import org.crosswire.jsword.book.Book
|
||||
import org.crosswire.jsword.versification.getVersification
|
||||
import java.util.ArrayList
|
||||
import android.util.Log
|
||||
import rx.subjects.PublishSubject
|
||||
import org.crosswire.jsword.book.getVersification
|
||||
|
||||
/**
|
||||
* Created by bspeice on 9/14/14.
|
||||
|
@ -5,30 +5,30 @@ import org.crosswire.jsword.book.Books
|
||||
import rx.functions.Action1
|
||||
import org.crosswire.jsword.book.Book
|
||||
import rx.functions.Action0
|
||||
import org.bspeice.minimalbible.exception.NoBooksInstalledException
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* 'Open' class and values for mockito to subclass
|
||||
* http://confluence.jetbrains.com/display/Kotlin/Classes+and+Inheritance
|
||||
*/
|
||||
|
||||
//@Singleton
|
||||
Singleton
|
||||
open class BookManager() {
|
||||
// Some extra books like to think they're installed, but trigger NPE all over the place...
|
||||
val ignore = array("ERen_no", "ot1nt2");
|
||||
|
||||
|
||||
open val installedBooks = Observable.from(Books.installed()!!.getBooks())
|
||||
?.filter { !ignore.contains(it!!.getInitials()) }
|
||||
?.cache()
|
||||
?.cache() ?: throw NoBooksInstalledException()
|
||||
|
||||
var refreshComplete = false;
|
||||
|
||||
{
|
||||
// TODO: Cleaner way of expressing this?
|
||||
installedBooks?.subscribe(Action1<Book> { result -> },
|
||||
installedBooks.subscribe(Action1<Book> { result -> },
|
||||
Action1<Throwable> { error -> },
|
||||
Action0 { refreshComplete = true })
|
||||
}
|
||||
|
||||
fun getBooks(): Observable<Book> {
|
||||
return installedBooks as Observable
|
||||
}
|
||||
}
|
28
app/src/main/kotlin/org/crosswire/jsword/book/BookUtil.kt
Normal file
28
app/src/main/kotlin/org/crosswire/jsword/book/BookUtil.kt
Normal 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()
|
@ -1,45 +1,16 @@
|
||||
package org.crosswire.jsword.versification
|
||||
|
||||
import org.crosswire.jsword.book.Book
|
||||
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
|
||||
*/
|
||||
|
||||
class VersificationUtil() {
|
||||
class object {
|
||||
val INTROS = array(
|
||||
BibleBook.INTRO_BIBLE,
|
||||
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()
|
||||
}
|
||||
object INTRO_BOOKS {
|
||||
val INTROS = array(
|
||||
BibleBook.INTRO_BIBLE,
|
||||
BibleBook.INTRO_OT,
|
||||
BibleBook.INTRO_NT
|
||||
)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
fun Versification.getBooks(): List<BibleBook> {
|
||||
return this.getBookIterator()!!.iterable()
|
||||
.filter { !VersificationUtil.INTROS.contains(it) }
|
||||
}
|
||||
fun Versification.getAllBooks(): List<BibleBook> =
|
||||
this.getBookIterator()!!.iterable().toList()
|
||||
|
||||
fun Versification.getBookNames(): List<String> {
|
||||
return this.getBooks().map { this.getLongName(it) }
|
||||
}
|
||||
fun Versification.getBooks(): List<BibleBook> =
|
||||
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 {
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user