Remove the InstalledManager

Largely duplicated work, and makes testing easier!
This commit is contained in:
Bradlee Speice
2014-11-11 23:46:51 -05:00
parent 35b515add7
commit b65b5680f9
7 changed files with 106 additions and 283 deletions

View File

@ -11,9 +11,8 @@ import com.todddavies.components.progressbar.ProgressWheel;
import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadManager;
import org.bspeice.minimalbible.activity.downloader.manager.BookManager;
import org.bspeice.minimalbible.activity.downloader.manager.DLProgressEvent;
import org.bspeice.minimalbible.activity.downloader.manager.InstalledManager;
import org.crosswire.jsword.book.Book;
import javax.inject.Inject;
@ -45,9 +44,7 @@ public class BookItemHolder {
@InjectView(R.id.download_prg_download)
ProgressWheel downloadProgress;
@Inject
BookDownloadManager bookDownloadManager;
@Inject
InstalledManager installedManager;
BookManager bookManager;
@Inject @Named("DownloadActivityContext")
Context ctx;
private Subscription subscription;
@ -62,14 +59,14 @@ public class BookItemHolder {
public void bindHolder() {
acronym.setText(b.getInitials());
itemName.setText(b.getName());
DLProgressEvent dlProgressEvent = bookDownloadManager.getDownloadProgress(b);
DLProgressEvent dlProgressEvent = bookManager.getDownloadProgress(b);
if (dlProgressEvent != null) {
displayProgress((int) dlProgressEvent.toCircular());
} else if (installedManager.isInstalled(b)) {
} else if (bookManager.isInstalled(b)) {
displayInstalled();
}
//TODO: Refactor
subscription = bookDownloadManager.getDownloadEvents()
subscription = bookManager.getDownloadEvents()
.observeOn(AndroidSchedulers.mainThread())
.filter(new Func1<DLProgressEvent, Boolean>() {
@Override
@ -91,9 +88,9 @@ public class BookItemHolder {
@OnClick(R.id.download_ibtn_download)
public void onDownloadItem(View v) {
if (installedManager.isInstalled(b)) {
if (bookManager.isInstalled(b)) {
// Remove the book
boolean didRemove = installedManager.removeBook(b);
boolean didRemove = bookManager.removeBook(b);
if (didRemove) {
isDownloaded.setImageResource(R.drawable.ic_action_download);
} else {
@ -101,7 +98,7 @@ public class BookItemHolder {
, Toast.LENGTH_SHORT).show();
}
} else {
bookDownloadManager.installBook(this.b);
bookManager.installBook(this.b);
}
}

View File

@ -5,8 +5,7 @@ import android.net.ConnectivityManager;
import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.MinimalBibleModules;
import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadManager;
import org.bspeice.minimalbible.activity.downloader.manager.InstalledManager;
import org.bspeice.minimalbible.activity.downloader.manager.BookManager;
import org.bspeice.minimalbible.activity.downloader.manager.LocaleManager;
import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager;
import org.crosswire.common.util.Language;
@ -34,11 +33,10 @@ import de.devland.esperandro.Esperandro;
injects = {
BookListFragment.class,
BookItemHolder.class,
BookDownloadManager.class,
BookManager.class,
RefreshManager.class,
DownloadNavDrawerFragment.class,
DownloadActivity.class,
InstalledManager.class
DownloadActivity.class
},
addsTo = MinimalBibleModules.class,
library = true
@ -77,8 +75,8 @@ public class DownloadActivityModules {
}
@Provides @Singleton
BookDownloadManager provideBookDownloadManager(Books installedBooks, RefreshManager rm) {
return new BookDownloadManager(installedBooks, rm);
BookManager provideBookDownloadManager(Books installedBooks, RefreshManager rm) {
return new BookManager(installedBooks, rm);
}
@Provides @Singleton

View File

@ -1,77 +0,0 @@
package org.bspeice.minimalbible.activity.downloader.manager;
import android.util.Log;
import org.bspeice.minimalbible.Injector;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookException;
import org.crosswire.jsword.book.Books;
import org.crosswire.jsword.book.BooksEvent;
import org.crosswire.jsword.book.BooksListener;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
/**
* Manager to keep track of which books have been installed
*/
@Singleton
public class InstalledManager implements BooksListener {
@Inject Books installedBooks;
// TODO: Why is this injected if we initialize in the constructor?
@Inject List<Book> installedBooksList;
private String TAG = "InstalledManager";
@Inject
InstalledManager(Injector injector) {
injector.inject(this);
installedBooksList.addAll(installedBooks.getBooks());
installedBooks.addBooksListener(this);
}
public boolean isInstalled(Book b) {
return installedBooksList.contains(b);
}
@Override
public void bookAdded(BooksEvent booksEvent) {
Log.d(TAG, "Book added: " + booksEvent.getBook().toString());
Book b = booksEvent.getBook();
if (!installedBooksList.contains(b)) {
installedBooksList.add(b);
}
}
@Override
public void bookRemoved(BooksEvent booksEvent) {
Log.d(TAG, "Book removed: " + booksEvent.getBook().toString());
Book b = booksEvent.getBook();
if (installedBooksList.contains(b)) {
installedBooksList.remove(b);
}
}
/**
* Remove a book from being installed.
* Currently only supports books that have been installed outside the current application run.
* Not quite sure why this is, but And-Bible exhibits the same behavior.
* @param b The book to remove
* @return Whether the book was removed.
*/
public boolean removeBook(Book b) {
try {
// This worked in the past, but isn't now...
// installedBooks.remove(b);
Book realBook = installedBooks.getBook(b.getInitials());
b.getDriver().delete(realBook);
return true;
} catch (BookException e) {
Log.e("InstalledManager", "Unable to remove book (already uninstalled?): " + e.getLocalizedMessage());
return false;
}
}
}

View File

@ -13,20 +13,29 @@ import org.crosswire.jsword.book.BooksListener;
import rx.Observable;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;
import org.crosswire.jsword.book.BookException
import org.crosswire.jsword.book.remove
/**
* Single point of authority for what is being downloaded and its progress
* Please note that you should never be modifying installedBooks,
* only operate on installedBooksList
*/
//TODO: Install indexes for Bibles
class BookDownloadManager(val installedBooks: Books, val rM: RefreshManager) :
class BookManager(private val installedBooks: Books, val rM: RefreshManager) :
WorkListener, BooksListener {
/**
* Cached copy of downloads in progress so views displaying this info can get it quickly.
*/
// TODO: Combine to one map
var bookMappings: MutableMap<String, Book> = hashMapOf()
var inProgressDownloads: MutableMap<Book, DLProgressEvent> = hashMapOf()
val bookMappings: MutableMap<String, Book> = hashMapOf()
val inProgressDownloads: MutableMap<Book, DLProgressEvent> = hashMapOf()
/**
* A list of books that is locally maintained - installedBooks isn't always up-to-date
*/
val installedBooksList: MutableList<Book> = installedBooks.getBooks() ?: linkedListOf()
val downloadEvents: PublishSubject<DLProgressEvent> = PublishSubject.create();
{
@ -41,7 +50,7 @@ class BookDownloadManager(val installedBooks: Books, val rM: RefreshManager) :
* @param b The book to predict the download job name of
* @return The name of the job that will/is download/ing this book
*/
fun getJobId(b: Book) = "INSTALL_BOOK-" + b.getInitials()
fun getJobId(b: Book) = "INSTALL_BOOK-${b.getInitials()}"
fun installBook(b: Book) {
downloadBook(b)
@ -57,11 +66,37 @@ class BookDownloadManager(val installedBooks: Books, val rM: RefreshManager) :
// First, look up where the Book came from
Observable.just(rM installerFromBook b)
.subscribeOn(Schedulers.io())
.subscribe { it.toBlocking().first().install(b) }
.subscribe { it.toBlocking().first() install b }
downloadEvents onNext DLProgressEvent(DLProgressEvent.PROGRESS_BEGINNING, b)
}
/**
* Remove a book from being installed.
* Currently only supports books that have been installed outside the current application run.
* Not quite sure why this is, but And-Bible exhibits the same behavior.
* @param b The book to remove
* @return Whether the book was removed.
*/
fun removeBook(b: Book): Boolean {
try {
b.remove()
return installedBooksList remove b
} catch (e: BookException) {
Log.e("InstalledManager",
"Unable to remove book (already uninstalled?): ${e.getDetailedMessage()}");
return false;
}
}
/**
* Check the status of a book download in progress.
* @param b The book to get the current progress of
* @return The most recent DownloadProgressEvent for the book, or null if not downloading
*/
fun getDownloadProgress(b: Book) = inProgressDownloads get b
fun isInstalled(b: Book) = installedBooksList contains b
// TODO: I have a strange feeling I can simplify this further...
override fun workProgressed(ev: WorkEvent) {
val job = ev.getJob()
@ -79,13 +114,6 @@ class BookDownloadManager(val installedBooks: Books, val rM: RefreshManager) :
}
}
/**
* Check the status of a book download in progress.
* @param b The book to get the current progress of
* @return The most recent DownloadProgressEvent for the book, or null if not downloading
*/
fun getDownloadProgress(b: Book) = inProgressDownloads.get(b)
override fun workStateChanged(ev: WorkEvent) {
Log.d("BookDownloadManager", ev.toString())
}
@ -94,15 +122,18 @@ class BookDownloadManager(val installedBooks: Books, val rM: RefreshManager) :
// It's possible the install finished before we received a progress event for it,
// we handle that case here.
val b = booksEvent.getBook()
Log.d("BookDownloadManager", "Book added: " + b.getName())
Log.d("BookDownloadManager", "Book added: ${b.getName()}")
inProgressDownloads remove b
// Not sure why, but the inProgressDownloads might not have our book,
// so we always trigger the PROGRESS_COMPLETE event.
downloadEvents onNext DLProgressEvent(DLProgressEvent.PROGRESS_COMPLETE, b)
// And update the locally available list
installedBooksList add b
}
override fun bookRemoved(booksEvent: BooksEvent) {
installedBooksList remove booksEvent.getBook()
}
}

View File

@ -25,4 +25,6 @@ fun Book.getVersification(): Versification {
fun Book.bookNames(): List<String> = this.getVersification().getBookNames()
fun Book.bookName(bBook: BibleBook): String =
this.getVersification().getBookName(bBook).getLongName()
this.getVersification().getBookName(bBook).getLongName()
fun Book.remove() = this.getDriver().delete(this)