Books are now deleted correctly

Still need to handle all the network related errors...
This commit is contained in:
Bradlee Speice 2014-11-12 01:01:34 -05:00
parent 8137c4795a
commit 18c9df404e
3 changed files with 16 additions and 11 deletions

View File

@ -135,6 +135,7 @@ public class BookManagerTest extends MBTestCase implements Injector {
BookDriver driver = mock(BookDriver.class); BookDriver driver = mock(BookDriver.class);
Book mockBook = mock(Book.class); Book mockBook = mock(Book.class);
Book secondMockBook = mock(Book.class);
when(mockBook.getDriver()).thenReturn(driver); when(mockBook.getDriver()).thenReturn(driver);
BooksEvent event = mock(BooksEvent.class); BooksEvent event = mock(BooksEvent.class);
@ -142,9 +143,9 @@ public class BookManagerTest extends MBTestCase implements Injector {
bookManager.getInstalledBooksList().add(mockBook); bookManager.getInstalledBooksList().add(mockBook);
assertTrue(bookManager.getInstalledBooksList().contains(mockBook)); assertTrue(bookManager.getInstalledBooksList().contains(mockBook));
bookManager.removeBook(mockBook, driver); bookManager.removeBook(mockBook, secondMockBook);
assertFalse(bookManager.getInstalledBooksList().contains(mockBook)); assertFalse(bookManager.getInstalledBooksList().contains(mockBook));
verify(driver, times(1)).delete(mockBook); verify(driver, times(1)).delete(secondMockBook);
} }
/** /**

View File

@ -90,7 +90,7 @@ public class BookItemHolder {
public void onDownloadItem(View v) { public void onDownloadItem(View v) {
if (bookManager.isInstalled(b)) { if (bookManager.isInstalled(b)) {
// Remove the book // Remove the book
boolean didRemove = bookManager.removeBook(b, bookManager.getRealDriver(b)); boolean didRemove = bookManager.removeBook(b, bookManager.getRealBook(b));
if (didRemove) { if (didRemove) {
isDownloaded.setImageResource(R.drawable.ic_action_download); isDownloaded.setImageResource(R.drawable.ic_action_download);
} else { } else {

View File

@ -14,7 +14,6 @@ import rx.Observable;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject; import rx.subjects.PublishSubject;
import org.crosswire.jsword.book.BookException import org.crosswire.jsword.book.BookException
import org.crosswire.jsword.book.BookDriver
/** /**
* Single point of authority for what is being downloaded and its progress * Single point of authority for what is being downloaded and its progress
@ -73,28 +72,33 @@ class BookManager(private val installedBooks: Books, val rM: RefreshManager) :
/** /**
* For whatever reason, not just any old "book" reference will do. We need to actually * For whatever reason, not just any old "book" reference will do. We need to actually
* get a reference corresponding to a physically installed book to get the proper driver. * get a reference corresponding to a physically installed book for the driver to remove.
* Plus, it makes the removeBook method easier to test. * Plus, it makes the removeBook method easier to test.
* @param b The book to find the actual driver for * @param b The book to find the actual driver for
* @return The driver corresponding to the physical book * @return The driver corresponding to the physical book
*/ */
fun getRealDriver(b: Book): BookDriver = (installedBooks getBook b.getInitials()).getDriver() fun getRealBook(b: Book): Book = installedBooks getBook b.getInitials()
/** /**
* Remove a book from being installed. * Remove a book from being installed.
* Currently only supports books that have been installed outside the current application run. * 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. * Not quite sure why this is, but And-Bible exhibits the same behavior.
* Also, I'll document it for the future: It seems like a book is only remove if you return
* true from this method. Which is incredibly strange, because this method should have no
* effect on the actual process of deleting a book. Even so, things work when
* I return true here.
* @param b The book to remove * @param b The book to remove
* @return Whether the book was removed. * @return Whether the book was removed.
*/ */
fun removeBook(b: Book, driver: BookDriver): Boolean { fun removeBook(b: Book, realBook: Book = getRealBook(b)): Boolean {
try { try {
driver delete b b.getDriver() delete realBook
return installedBooksList remove b installedBooksList remove b
return true
} catch (e: BookException) { } catch (e: BookException) {
Log.e("InstalledManager", Log.e("InstalledManager",
"Unable to remove book (already uninstalled?): ${e.getDetailedMessage()}"); "Unable to remove book (already uninstalled?): ${e.getDetailedMessage()}")
return false; return false
} }
} }
/** /**