mirror of
https://github.com/MinimalBible/MinimalBible-Legacy
synced 2024-12-22 14:48:25 -05:00
Refactor so we don't re-download the book list every time
Code is still pretty ugly, but I'm not sure on how to prettify it. Get to that later.
This commit is contained in:
parent
0f98338504
commit
e29d4546db
@ -120,18 +120,17 @@ public class BookListFragment extends Fragment {
|
|||||||
f = null;
|
f = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
DownloadManager dm = new DownloadManager();
|
DownloadManager dm = DownloadManager.getInstance();
|
||||||
|
|
||||||
|
if (!dm.isLoaded()) {
|
||||||
ProgressDialog refreshDialog = new ProgressDialog(getActivity());
|
ProgressDialog refreshDialog = new ProgressDialog(getActivity());
|
||||||
if (dm.willRefresh()) {
|
|
||||||
refreshDialog.setMessage("Refreshing available modules...");
|
refreshDialog.setMessage("Refreshing available modules...");
|
||||||
} else {
|
|
||||||
refreshDialog
|
|
||||||
.setMessage("Fetching available modules from cache...");
|
|
||||||
}
|
|
||||||
refreshDialog.setCancelable(false);
|
refreshDialog.setCancelable(false);
|
||||||
refreshDialog.show();
|
refreshDialog.show();
|
||||||
dm.fetchAvailableBooks(f, new DlBookRefreshListener(refreshDialog));
|
dm.fetchAvailableBooks(f, new DlBookRefreshListener(refreshDialog));
|
||||||
|
} else {
|
||||||
|
dm.fetchAvailableBooks(f, new DlBookRefreshListener(null));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class DlBookRefreshListener implements
|
private class DlBookRefreshListener implements
|
||||||
|
@ -3,11 +3,15 @@ package org.bspeice.minimalbible.activities.downloader;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.bspeice.minimalbible.MinimalBible;
|
||||||
|
import org.bspeice.minimalbible.MinimalBibleConstants;
|
||||||
import org.crosswire.jsword.book.Book;
|
import org.crosswire.jsword.book.Book;
|
||||||
import org.crosswire.jsword.book.BookFilter;
|
import org.crosswire.jsword.book.BookFilter;
|
||||||
import org.crosswire.jsword.book.install.InstallException;
|
import org.crosswire.jsword.book.install.InstallException;
|
||||||
import org.crosswire.jsword.book.install.Installer;
|
import org.crosswire.jsword.book.install.Installer;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@ -16,16 +20,14 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
|
|||||||
private static final String TAG = "BookRefreshTask";
|
private static final String TAG = "BookRefreshTask";
|
||||||
|
|
||||||
private BookRefreshListener listener;
|
private BookRefreshListener listener;
|
||||||
private boolean refresh;
|
|
||||||
private BookFilter filter;
|
private BookFilter filter;
|
||||||
|
|
||||||
public BookRefreshTask(boolean refresh, BookRefreshListener listener) {
|
public BookRefreshTask(BookRefreshListener listener) {
|
||||||
this.refresh = refresh;
|
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BookRefreshTask(boolean refresh, BookFilter f, BookRefreshListener listener) {
|
public BookRefreshTask(BookFilter f,
|
||||||
this.refresh = refresh;
|
BookRefreshListener listener) {
|
||||||
this.filter = f;
|
this.filter = f;
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
@ -35,11 +37,13 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
|
|||||||
List<Book> books = new LinkedList<Book>();
|
List<Book> books = new LinkedList<Book>();
|
||||||
|
|
||||||
for (Installer i : params) {
|
for (Installer i : params) {
|
||||||
if (refresh) {
|
if (doRefresh()) {
|
||||||
try {
|
try {
|
||||||
i.reloadBookList();
|
i.reloadBookList();
|
||||||
} catch (InstallException e) {
|
} catch (InstallException e) {
|
||||||
Log.e(TAG, "Error downloading books from installer: " + i.toString(), e);
|
Log.e(TAG,
|
||||||
|
"Error downloading books from installer: "
|
||||||
|
+ i.toString(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,6 +57,21 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
|
|||||||
return books;
|
return books;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean doRefresh() {
|
||||||
|
// Check if we should refresh over the internet, or use the local copy
|
||||||
|
// TODO: Discover if we need to refresh over Internet, or use a cached
|
||||||
|
// copy - likely something time-based, also check network state.
|
||||||
|
// Fun fact - jSword handles the caching for us.
|
||||||
|
|
||||||
|
SharedPreferences prefs = MinimalBible.getAppContext()
|
||||||
|
.getSharedPreferences(
|
||||||
|
MinimalBibleConstants.DOWNLOAD_PREFS_FILE,
|
||||||
|
Context.MODE_PRIVATE);
|
||||||
|
|
||||||
|
// Refresh if download enabled
|
||||||
|
return prefs.getBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED, false);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(List<Book> result) {
|
protected void onPostExecute(List<Book> result) {
|
||||||
super.onPostExecute(result);
|
super.onPostExecute(result);
|
||||||
@ -63,5 +82,3 @@ public class BookRefreshTask extends AsyncTask<Installer, Integer, List<Book>> {
|
|||||||
public void onRefreshComplete(List<Book> results);
|
public void onRefreshComplete(List<Book> results);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,29 +1,36 @@
|
|||||||
package org.bspeice.minimalbible.activities.downloader;
|
package org.bspeice.minimalbible.activities.downloader;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.bspeice.minimalbible.MinimalBible;
|
import org.bspeice.minimalbible.MinimalBible;
|
||||||
import org.bspeice.minimalbible.MinimalBibleConstants;
|
import org.bspeice.minimalbible.activities.downloader.BookRefreshTask.BookRefreshListener;
|
||||||
|
import org.crosswire.jsword.book.Book;
|
||||||
import org.crosswire.jsword.book.BookCategory;
|
import org.crosswire.jsword.book.BookCategory;
|
||||||
import org.crosswire.jsword.book.BookFilter;
|
import org.crosswire.jsword.book.BookFilter;
|
||||||
import org.crosswire.jsword.book.install.InstallManager;
|
import org.crosswire.jsword.book.install.InstallManager;
|
||||||
import org.crosswire.jsword.book.install.Installer;
|
import org.crosswire.jsword.book.install.Installer;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
public class DownloadManager {
|
public class DownloadManager {
|
||||||
|
|
||||||
// TODO: Probably should be a singleton.
|
|
||||||
|
|
||||||
private final String TAG = "DownloadManager";
|
private final String TAG = "DownloadManager";
|
||||||
|
private static DownloadManager instance;
|
||||||
|
private List<Book> books;
|
||||||
|
|
||||||
public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE,
|
public static final BookCategory[] VALID_CATEGORIES = { BookCategory.BIBLE,
|
||||||
BookCategory.COMMENTARY, BookCategory.DICTIONARY,
|
BookCategory.COMMENTARY, BookCategory.DICTIONARY,
|
||||||
BookCategory.IMAGES, BookCategory.MAPS };
|
BookCategory.IMAGES, BookCategory.MAPS };
|
||||||
|
|
||||||
public DownloadManager() {
|
public static DownloadManager getInstance() {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new DownloadManager();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private DownloadManager() {
|
||||||
setDownloadDir();
|
setDownloadDir();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,32 +47,30 @@ public class DownloadManager {
|
|||||||
private BookRefreshTask _fetchAvailableBooks(BookFilter f,
|
private BookRefreshTask _fetchAvailableBooks(BookFilter f,
|
||||||
BookRefreshTask.BookRefreshListener bookRefreshListener) {
|
BookRefreshTask.BookRefreshListener bookRefreshListener) {
|
||||||
|
|
||||||
Map<String, Installer> installers = getInstallers();
|
if (!isLoaded()) {
|
||||||
|
return (BookRefreshTask) new BookRefreshTask(
|
||||||
return (BookRefreshTask) new BookRefreshTask(willRefresh(),
|
new DmBookRefreshListener(bookRefreshListener))
|
||||||
bookRefreshListener).execute(installers.values().toArray(
|
.execute(getInstallersArray());
|
||||||
new Installer[installers.size()]));
|
} else {
|
||||||
|
return (BookRefreshTask) new NoopBookRefreshTask(books,
|
||||||
|
bookRefreshListener).execute(getInstallersArray());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean willRefresh() {
|
public boolean isLoaded() {
|
||||||
// Method to determine if we need a refresh
|
// Let methods know if we're going to take a while to reload everything
|
||||||
// Public, so other modules can predict and take action accordingly.
|
return (books != null);
|
||||||
// TODO: Discover if we need to refresh over Internet, or use a cached
|
|
||||||
// copy - likely something time-based, also check network state.
|
|
||||||
// Fun fact - jSword handles the caching for us.
|
|
||||||
|
|
||||||
SharedPreferences prefs = MinimalBible.getAppContext()
|
|
||||||
.getSharedPreferences(
|
|
||||||
MinimalBibleConstants.DOWNLOAD_PREFS_FILE,
|
|
||||||
Context.MODE_PRIVATE);
|
|
||||||
|
|
||||||
return (prefs.getBoolean(MinimalBibleConstants.KEY_DOWNLOAD_ENABLED, false));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Installer> getInstallers() {
|
public Map<String, Installer> getInstallers() {
|
||||||
return new InstallManager().getInstallers();
|
return new InstallManager().getInstallers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Installer[] getInstallersArray() {
|
||||||
|
Map<String, Installer> installers = getInstallers();
|
||||||
|
return installers.values().toArray(new Installer[installers.size()]);
|
||||||
|
}
|
||||||
|
|
||||||
private void setDownloadDir() {
|
private void setDownloadDir() {
|
||||||
// We need to set the download directory for jSword to stick with
|
// We need to set the download directory for jSword to stick with
|
||||||
// Android.
|
// Android.
|
||||||
@ -74,4 +79,19 @@ public class DownloadManager {
|
|||||||
System.setProperty("jsword.home", home);
|
System.setProperty("jsword.home", home);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create our own refresh listener to save a reference to the books
|
||||||
|
private class DmBookRefreshListener implements BookRefreshListener {
|
||||||
|
private BookRefreshListener listener;
|
||||||
|
|
||||||
|
public DmBookRefreshListener(BookRefreshListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRefreshComplete(List<Book> results) {
|
||||||
|
books = results;
|
||||||
|
listener.onRefreshComplete(results);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
package org.bspeice.minimalbible.activities.downloader;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.crosswire.jsword.book.Book;
|
||||||
|
import org.crosswire.jsword.book.install.Installer;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* There's probably a better way of doing this, but this allows me to avoid networking,
|
||||||
|
* while still maintaining the code structure of being asynchronous.
|
||||||
|
*/
|
||||||
|
public class NoopBookRefreshTask extends BookRefreshTask {
|
||||||
|
BookRefreshListener listener;
|
||||||
|
List<Book> books;
|
||||||
|
|
||||||
|
public NoopBookRefreshTask(List<Book> books, BookRefreshListener listener) {
|
||||||
|
super(listener);
|
||||||
|
this.books = books;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<Book> doInBackground(Installer... params) {
|
||||||
|
return books;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user