mirror of
				https://github.com/MinimalBible/MinimalBible
				synced 2025-11-03 18:10:27 -05:00 
			
		
		
		
	Get a first manager to Kotlin
Extension functions are fun...
This commit is contained in:
		@ -18,6 +18,7 @@ import org.bspeice.minimalbible.R;
 | 
				
			|||||||
import org.bspeice.minimalbible.activity.BaseActivity;
 | 
					import org.bspeice.minimalbible.activity.BaseActivity;
 | 
				
			||||||
import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
 | 
					import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
 | 
				
			||||||
import org.bspeice.minimalbible.activity.navigation.NavDrawerFragment;
 | 
					import org.bspeice.minimalbible.activity.navigation.NavDrawerFragment;
 | 
				
			||||||
 | 
					import org.bspeice.minimalbible.service.manager.BookManager;
 | 
				
			||||||
import org.crosswire.jsword.book.Book;
 | 
					import org.crosswire.jsword.book.Book;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import javax.inject.Inject;
 | 
					import javax.inject.Inject;
 | 
				
			||||||
 | 
				
			|||||||
@ -4,6 +4,7 @@ import android.util.Log;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import org.bspeice.minimalbible.activity.navigation.ExpListNavAdapter;
 | 
					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.crosswire.jsword.book.Book;
 | 
					import org.crosswire.jsword.book.Book;
 | 
				
			||||||
import org.crosswire.jsword.versification.VersificationUtil;
 | 
					import org.crosswire.jsword.versification.VersificationUtil;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,66 +0,0 @@
 | 
				
			|||||||
package org.bspeice.minimalbible.activity.viewer;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import org.crosswire.jsword.book.Book;
 | 
					 | 
				
			||||||
import org.crosswire.jsword.book.Books;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import java.util.ArrayList;
 | 
					 | 
				
			||||||
import java.util.List;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import javax.inject.Inject;
 | 
					 | 
				
			||||||
import javax.inject.Singleton;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import rx.Observable;
 | 
					 | 
				
			||||||
import rx.functions.Action0;
 | 
					 | 
				
			||||||
import rx.functions.Action1;
 | 
					 | 
				
			||||||
import rx.functions.Func1;
 | 
					 | 
				
			||||||
import rx.schedulers.Schedulers;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * Created by bspeice on 6/18/14.
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
@Singleton
 | 
					 | 
				
			||||||
public class BookManager {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private Observable<Book> installedBooks;
 | 
					 | 
				
			||||||
    private Boolean refreshComplete;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Some of these books seem to think they're installed...
 | 
					 | 
				
			||||||
    private List<String> excludeBooks = new ArrayList<String>() {{
 | 
					 | 
				
			||||||
        add("ERen_no");
 | 
					 | 
				
			||||||
        add("ot1nt2");
 | 
					 | 
				
			||||||
    }};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Inject
 | 
					 | 
				
			||||||
    BookManager() {
 | 
					 | 
				
			||||||
        // TODO:  Any way this can be sped up goes straight to the initialization time.
 | 
					 | 
				
			||||||
        installedBooks = Observable.from(Books.installed().getBooks())
 | 
					 | 
				
			||||||
                .filter(new Func1<Book, Boolean>() {
 | 
					 | 
				
			||||||
                    @Override
 | 
					 | 
				
			||||||
                    public Boolean call(Book book) {
 | 
					 | 
				
			||||||
                        return !excludeBooks.contains(book.getInitials());
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                })
 | 
					 | 
				
			||||||
            .cache();
 | 
					 | 
				
			||||||
        installedBooks.subscribeOn(Schedulers.io())
 | 
					 | 
				
			||||||
                .subscribe(new Action1<Book>() {
 | 
					 | 
				
			||||||
            @Override
 | 
					 | 
				
			||||||
            public void call(Book book) {}
 | 
					 | 
				
			||||||
        }, new Action1<Throwable>() {
 | 
					 | 
				
			||||||
            @Override
 | 
					 | 
				
			||||||
            public void call(Throwable throwable) {}
 | 
					 | 
				
			||||||
        }, new Action0() {
 | 
					 | 
				
			||||||
            @Override
 | 
					 | 
				
			||||||
            public void call() {
 | 
					 | 
				
			||||||
                BookManager.this.refreshComplete = true;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public Observable<Book> getInstalledBooks() {
 | 
					 | 
				
			||||||
        return installedBooks;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public Boolean isRefreshComplete() {
 | 
					 | 
				
			||||||
        return refreshComplete;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					package org.bspeice.minimalbible.service.manager
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import rx.Observable
 | 
				
			||||||
 | 
					import org.crosswire.jsword.book.Books
 | 
				
			||||||
 | 
					import rx.functions.Action1
 | 
				
			||||||
 | 
					import org.crosswire.jsword.book.Book
 | 
				
			||||||
 | 
					import rx.functions.Action0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Created by bspeice on 9/10/14.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//@Singleton
 | 
				
			||||||
 | 
					class BookManager() {
 | 
				
			||||||
 | 
					    // Some extra books like to think they're installed, but trigger NPE all over the place...
 | 
				
			||||||
 | 
					    val ignore = array("ERen_no", "ot1nt2");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    val installedBooks = Observable.from(Books.installed()!!.getBooks())
 | 
				
			||||||
 | 
					            ?.filter { !ignore.contains(it!!.getInitials()) }
 | 
				
			||||||
 | 
					            ?.cache()
 | 
				
			||||||
 | 
					    var refreshComplete = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        // TODO: Cleaner way of expressing this?
 | 
				
			||||||
 | 
					        installedBooks?.subscribe(Action1<Book> { result -> },
 | 
				
			||||||
 | 
					                Action1<Throwable> { error -> },
 | 
				
			||||||
 | 
					                Action0 { refreshComplete = true })
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fun getBooks(): Observable<Book> {
 | 
				
			||||||
 | 
					        return installedBooks as Observable
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					package org.bspeice.minimalbible.service.manager
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Created by bspeice on 9/11/14.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class InvalidBookException(message: String?) : Exception(message) {}
 | 
				
			||||||
@ -5,6 +5,8 @@ import java.util.ArrayList
 | 
				
			|||||||
import org.crosswire.jsword.versification.system.Versifications
 | 
					import org.crosswire.jsword.versification.system.Versifications
 | 
				
			||||||
import org.crosswire.jsword.book.BookMetaData
 | 
					import org.crosswire.jsword.book.BookMetaData
 | 
				
			||||||
import rx.Observable
 | 
					import rx.Observable
 | 
				
			||||||
 | 
					import android.util.Log
 | 
				
			||||||
 | 
					import org.bspeice.minimalbible.service.manager.InvalidBookException
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Created by bspeice on 9/10/14.
 | 
					 * Created by bspeice on 9/10/14.
 | 
				
			||||||
@ -20,11 +22,11 @@ class VersificationUtil() {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun getBookNames(b: Book): Observable<String> {
 | 
					    fun getBookNames(b: Book): Observable<String> {
 | 
				
			||||||
        return Observable.from(b.getVersification().getBookNames(b)) as Observable
 | 
					        return Observable.from(b.getVersification().getBookNames()) as Observable
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun getBooks(b: Book): Observable<BibleBook> {
 | 
					    fun getBooks(b: Book): Observable<BibleBook> {
 | 
				
			||||||
        return Observable.from(b.getVersification().getBooks(b)) as Observable
 | 
					        return Observable.from(b.getVersification().getBooks()) as Observable
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun getChapterCount(b: Book, bibleBook: BibleBook): Int {
 | 
					    fun getChapterCount(b: Book, bibleBook: BibleBook): Int {
 | 
				
			||||||
@ -40,7 +42,7 @@ class VersificationUtil() {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// There's probably a better way to do this
 | 
					// TODO: Refactor (is there a better way to accomplish this?) and move
 | 
				
			||||||
fun <T> Iterator<T>.iterable(): Iterable<T> {
 | 
					fun <T> Iterator<T>.iterable(): Iterable<T> {
 | 
				
			||||||
    val list: MutableList<T> = ArrayList()
 | 
					    val list: MutableList<T> = ArrayList()
 | 
				
			||||||
    while (this.hasNext()) {
 | 
					    while (this.hasNext()) {
 | 
				
			||||||
@ -50,13 +52,13 @@ fun <T> Iterator<T>.iterable(): Iterable<T> {
 | 
				
			|||||||
    return list
 | 
					    return list
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fun Versification.getBooks(b: Book): List<BibleBook> {
 | 
					fun Versification.getBooks(): List<BibleBook> {
 | 
				
			||||||
    return this.getBookIterator()!!.iterable()
 | 
					    return this.getBookIterator()!!.iterable()
 | 
				
			||||||
            .filter { VersificationUtil.INTROS.contains(it) }
 | 
					            .filter { VersificationUtil.INTROS.contains(it) }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fun Versification.getBookNames(b: Book): List<String> {
 | 
					fun Versification.getBookNames(): List<String> {
 | 
				
			||||||
    return this.getBooks(b).map { this.getLongName(it) as String }
 | 
					    return this.getBooks().map { this.getLongName(it) as String }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fun Versification.getChapterCount(b: BibleBook): Int {
 | 
					fun Versification.getChapterCount(b: BibleBook): Int {
 | 
				
			||||||
@ -64,7 +66,12 @@ fun Versification.getChapterCount(b: BibleBook): Int {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fun Book.getVersification(): Versification {
 | 
					fun Book.getVersification(): Versification {
 | 
				
			||||||
    return Versifications.instance()!!.getVersification(
 | 
					    val v = Versifications.instance()!!.getVersification(
 | 
				
			||||||
            this.getBookMetaData()!!.getProperty(BookMetaData.KEY_VERSIFICATION) as String
 | 
					            this.getBookMetaData()!!.getProperty(BookMetaData.KEY_VERSIFICATION).toString()
 | 
				
			||||||
    ) as Versification
 | 
					    )
 | 
				
			||||||
 | 
					    if (v == null) {
 | 
				
			||||||
 | 
					        Log.e(getClass()!!.getSimpleName(), "Invalid book: " + this.getInitials())
 | 
				
			||||||
 | 
					        throw InvalidBookException(this.getInitials())
 | 
				
			||||||
 | 
					    } else
 | 
				
			||||||
 | 
					        return v
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -2,7 +2,7 @@ package org.bspeice.minimalbible;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
 | 
					import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
 | 
				
			||||||
import org.bspeice.minimalbible.activity.viewer.BookManager;
 | 
					import org.bspeice.minimalbible.service.manager.BookManager;
 | 
				
			||||||
import org.crosswire.jsword.book.BookCategory;
 | 
					import org.crosswire.jsword.book.BookCategory;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
@ -30,7 +30,8 @@ public class TestModules {
 | 
				
			|||||||
        return testActivityTitle;
 | 
					        return testActivityTitle;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Provides @Singleton
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
    @Named("ValidCategories")
 | 
					    @Named("ValidCategories")
 | 
				
			||||||
    List<BookCategory> provideValidCategories() {
 | 
					    List<BookCategory> provideValidCategories() {
 | 
				
			||||||
        return new ArrayList<BookCategory>() {{
 | 
					        return new ArrayList<BookCategory>() {{
 | 
				
			||||||
@ -46,6 +47,7 @@ public class TestModules {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Provides
 | 
					    @Provides
 | 
				
			||||||
 | 
					    @Singleton
 | 
				
			||||||
    BookManager provideBookManager() {
 | 
					    BookManager provideBookManager() {
 | 
				
			||||||
        return bookManager;
 | 
					        return bookManager;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user