mirror of
https://github.com/MinimalBible/MinimalBible-Legacy
synced 2024-11-14 12:08:51 -05:00
Add code to remove books
This commit is contained in:
parent
4514c0f33f
commit
1a8b3f2eee
@ -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) {
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user