mirror of
https://github.com/MinimalBible/MinimalBible
synced 2025-07-12 11:14:46 -04:00
Remove the InstalledManager
Largely duplicated work, and makes testing easier!
This commit is contained in:
@ -11,9 +11,8 @@ import com.todddavies.components.progressbar.ProgressWheel;
|
||||
|
||||
import org.bspeice.minimalbible.Injector;
|
||||
import org.bspeice.minimalbible.R;
|
||||
import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadManager;
|
||||
import org.bspeice.minimalbible.activity.downloader.manager.BookManager;
|
||||
import org.bspeice.minimalbible.activity.downloader.manager.DLProgressEvent;
|
||||
import org.bspeice.minimalbible.activity.downloader.manager.InstalledManager;
|
||||
import org.crosswire.jsword.book.Book;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@ -45,9 +44,7 @@ public class BookItemHolder {
|
||||
@InjectView(R.id.download_prg_download)
|
||||
ProgressWheel downloadProgress;
|
||||
@Inject
|
||||
BookDownloadManager bookDownloadManager;
|
||||
@Inject
|
||||
InstalledManager installedManager;
|
||||
BookManager bookManager;
|
||||
@Inject @Named("DownloadActivityContext")
|
||||
Context ctx;
|
||||
private Subscription subscription;
|
||||
@ -62,14 +59,14 @@ public class BookItemHolder {
|
||||
public void bindHolder() {
|
||||
acronym.setText(b.getInitials());
|
||||
itemName.setText(b.getName());
|
||||
DLProgressEvent dlProgressEvent = bookDownloadManager.getDownloadProgress(b);
|
||||
DLProgressEvent dlProgressEvent = bookManager.getDownloadProgress(b);
|
||||
if (dlProgressEvent != null) {
|
||||
displayProgress((int) dlProgressEvent.toCircular());
|
||||
} else if (installedManager.isInstalled(b)) {
|
||||
} else if (bookManager.isInstalled(b)) {
|
||||
displayInstalled();
|
||||
}
|
||||
//TODO: Refactor
|
||||
subscription = bookDownloadManager.getDownloadEvents()
|
||||
subscription = bookManager.getDownloadEvents()
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.filter(new Func1<DLProgressEvent, Boolean>() {
|
||||
@Override
|
||||
@ -91,9 +88,9 @@ public class BookItemHolder {
|
||||
|
||||
@OnClick(R.id.download_ibtn_download)
|
||||
public void onDownloadItem(View v) {
|
||||
if (installedManager.isInstalled(b)) {
|
||||
if (bookManager.isInstalled(b)) {
|
||||
// Remove the book
|
||||
boolean didRemove = installedManager.removeBook(b);
|
||||
boolean didRemove = bookManager.removeBook(b);
|
||||
if (didRemove) {
|
||||
isDownloaded.setImageResource(R.drawable.ic_action_download);
|
||||
} else {
|
||||
@ -101,7 +98,7 @@ public class BookItemHolder {
|
||||
, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else {
|
||||
bookDownloadManager.installBook(this.b);
|
||||
bookManager.installBook(this.b);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,8 +5,7 @@ import android.net.ConnectivityManager;
|
||||
|
||||
import org.bspeice.minimalbible.Injector;
|
||||
import org.bspeice.minimalbible.MinimalBibleModules;
|
||||
import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadManager;
|
||||
import org.bspeice.minimalbible.activity.downloader.manager.InstalledManager;
|
||||
import org.bspeice.minimalbible.activity.downloader.manager.BookManager;
|
||||
import org.bspeice.minimalbible.activity.downloader.manager.LocaleManager;
|
||||
import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager;
|
||||
import org.crosswire.common.util.Language;
|
||||
@ -34,11 +33,10 @@ import de.devland.esperandro.Esperandro;
|
||||
injects = {
|
||||
BookListFragment.class,
|
||||
BookItemHolder.class,
|
||||
BookDownloadManager.class,
|
||||
BookManager.class,
|
||||
RefreshManager.class,
|
||||
DownloadNavDrawerFragment.class,
|
||||
DownloadActivity.class,
|
||||
InstalledManager.class
|
||||
DownloadActivity.class
|
||||
},
|
||||
addsTo = MinimalBibleModules.class,
|
||||
library = true
|
||||
@ -77,8 +75,8 @@ public class DownloadActivityModules {
|
||||
}
|
||||
|
||||
@Provides @Singleton
|
||||
BookDownloadManager provideBookDownloadManager(Books installedBooks, RefreshManager rm) {
|
||||
return new BookDownloadManager(installedBooks, rm);
|
||||
BookManager provideBookDownloadManager(Books installedBooks, RefreshManager rm) {
|
||||
return new BookManager(installedBooks, rm);
|
||||
}
|
||||
|
||||
@Provides @Singleton
|
||||
|
@ -1,77 +0,0 @@
|
||||
package org.bspeice.minimalbible.activity.downloader.manager;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.bspeice.minimalbible.Injector;
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Manager to keep track of which books have been installed
|
||||
*/
|
||||
@Singleton
|
||||
public class InstalledManager implements BooksListener {
|
||||
|
||||
@Inject Books installedBooks;
|
||||
// TODO: Why is this injected if we initialize in the constructor?
|
||||
@Inject List<Book> installedBooksList;
|
||||
|
||||
private String TAG = "InstalledManager";
|
||||
|
||||
@Inject
|
||||
InstalledManager(Injector injector) {
|
||||
injector.inject(this);
|
||||
installedBooksList.addAll(installedBooks.getBooks());
|
||||
installedBooks.addBooksListener(this);
|
||||
}
|
||||
|
||||
public boolean isInstalled(Book b) {
|
||||
return installedBooksList.contains(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bookAdded(BooksEvent booksEvent) {
|
||||
Log.d(TAG, "Book added: " + booksEvent.getBook().toString());
|
||||
Book b = booksEvent.getBook();
|
||||
if (!installedBooksList.contains(b)) {
|
||||
installedBooksList.add(b);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bookRemoved(BooksEvent booksEvent) {
|
||||
Log.d(TAG, "Book removed: " + booksEvent.getBook().toString());
|
||||
Book b = booksEvent.getBook();
|
||||
if (installedBooksList.contains(b)) {
|
||||
installedBooksList.remove(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.
|
||||
*/
|
||||
public boolean removeBook(Book b) {
|
||||
try {
|
||||
// This worked in the past, but isn't now...
|
||||
// installedBooks.remove(b);
|
||||
Book realBook = installedBooks.getBook(b.getInitials());
|
||||
b.getDriver().delete(realBook);
|
||||
return true;
|
||||
} catch (BookException e) {
|
||||
Log.e("InstalledManager", "Unable to remove book (already uninstalled?): " + e.getLocalizedMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user