Add code to remove books

This commit is contained in:
Bradlee Speice 2014-06-04 21:42:41 -04:00
parent 4514c0f33f
commit 1a8b3f2eee
2 changed files with 30 additions and 10 deletions

View File

@ -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) {

View File

@ -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<Book> installedBooks;
private Books installedBooks;
private List<Book> 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());
}
}
}