From 1a8b3f2eee9326d55d0da5686312e523fa6c6605 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Wed, 4 Jun 2014 21:42:41 -0400 Subject: [PATCH] Add code to remove books --- .../activities/downloader/BookItemHolder.java | 10 ++++++- .../downloader/manager/InstalledManager.java | 30 +++++++++++++------ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java index f0409e2..24de2d1 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java @@ -14,6 +14,8 @@ import org.bspeice.minimalbible.activities.downloader.manager.DLProgressEvent; import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager; import org.bspeice.minimalbible.activities.downloader.manager.InstalledManager; import org.crosswire.jsword.book.Book; +import org.crosswire.jsword.book.BookException; +import org.crosswire.jsword.book.Books; import javax.inject.Inject; @@ -64,7 +66,13 @@ public class BookItemHolder { @OnClick(R.id.download_ibtn_download) public void onDownloadItem(View v) { - bookDownloadManager.installBook(this.b); + if (installedManager.isInstalled(b)) { + // Remove the book + installedManager.removeBook(b); + isDownloaded.setImageResource(R.drawable.ic_action_download); + } else { + bookDownloadManager.installBook(this.b); + } } public void onEventMainThread(DLProgressEvent event) { diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/InstalledManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/InstalledManager.java index 91ec6fc..02df1c1 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/InstalledManager.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/InstalledManager.java @@ -1,6 +1,9 @@ package org.bspeice.minimalbible.activities.downloader.manager; +import android.util.Log; + 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; @@ -18,7 +21,8 @@ public class InstalledManager implements BooksListener { @Inject DownloadManager downloadManager; - private List installedBooks; + private Books installedBooks; + private List installedBooksList; /** * Register our manager to receive events on Book install @@ -26,31 +30,39 @@ public class InstalledManager implements BooksListener { * so we don't put it in the constructor. */ public void initialize() { - Books books = Books.installed(); - installedBooks = books.getBooks(); - books.addBooksListener(this); + installedBooks = Books.installed(); + installedBooksList = installedBooks.getBooks(); + installedBooks.addBooksListener(this); } public boolean isInstalled(Book b) { if (installedBooks == null) { initialize(); } - return installedBooks.contains(b); + return installedBooksList.contains(b); } @Override public void bookAdded(BooksEvent booksEvent) { Book b = booksEvent.getBook(); - if (!installedBooks.contains(b)) { - installedBooks.add(b); + if (!installedBooksList.contains(b)) { + installedBooksList.add(b); } } @Override public void bookRemoved(BooksEvent booksEvent) { Book b = booksEvent.getBook(); - if (installedBooks.contains(b)) { - installedBooks.remove(b); + if (installedBooksList.contains(b)) { + installedBooksList.remove(b); + } + } + + public void removeBook(Book b) { + try { + installedBooks.removeBook(b); + } catch (BookException e) { + Log.e("InstalledManager", "Unable to remove book (already uninstalled?): " + e.getLocalizedMessage()); } } }