mirror of
https://github.com/MinimalBible/MinimalBible
synced 2025-07-05 07:44:43 -04:00
Remove the InstalledManager
Largely duplicated work, and makes testing easier!
This commit is contained in:
@ -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()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user