Add bones for registering download progress

This commit is contained in:
Bradlee Speice 2014-05-19 23:20:47 -04:00
parent d10626efe6
commit f7f3b56db8
4 changed files with 99 additions and 9 deletions

View File

@ -18,7 +18,8 @@ import de.greenrobot.event.EventBus;
injects = {
BookListFragment.class,
DownloadManager.class,
BookRefreshTask.class
BookRefreshTask.class,
BookListAdapter.BookItemHolder.class
}
)
public class ActivityDownloaderModule {

View File

@ -9,11 +9,17 @@ import android.widget.ImageButton;
import android.widget.TextView;
import com.todddavies.components.progressbar.ProgressWheel;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
import org.bspeice.minimalbible.activities.downloader.manager.DownloadProgressEvent;
import org.crosswire.jsword.book.Book;
import java.util.List;
import javax.inject.Inject;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
@ -53,38 +59,54 @@ public class BookListAdapter extends BaseAdapter {
// and you'll get some really strange issues
if (convertView == null || convertView.getTag() == null) {
convertView = inflater.inflate(R.layout.list_download_items, null);
viewHolder = new BookItemHolder(convertView);
viewHolder = new BookItemHolder(convertView, getItem(position));
} else {
viewHolder = (BookItemHolder) convertView.getTag();
}
viewHolder.bindHolder(position);
viewHolder.bindHolder();
return convertView;
}
public class BookItemHolder {
public static class BookItemHolder {
@InjectView(R.id.download_txt_item_acronym) TextView acronym;
@InjectView(R.id.txt_download_item_name) TextView itemName;
@InjectView(R.id.download_ibtn_download) ImageButton isDownloaded;
@InjectView(R.id.download_prg_download) ProgressWheel downloadProgress;
public BookItemHolder(View v) {
@Inject DownloadManager downloadManager;
Book b;
public BookItemHolder(View v, Book b) {
ButterKnife.inject(this, v);
MinimalBible.getApplication().inject(this);
this.b = b;
}
public void bindHolder(int position) {
Book b = BookListAdapter.this.getItem(position);
public void bindHolder() {
acronym.setText(b.getInitials());
itemName.setText(b.getName());
DownloadProgressEvent downloadProgress = downloadManager.getInProgressDownloadProgress(b);
if (downloadProgress != null) {
displayProgress((int) downloadProgress.toCircular());
}
}
@OnClick(R.id.download_ibtn_download)
public void onDownloadItem(View v) {
Log.d("BookListAdapter", v.toString());
displayProgress(0); // Can assume 0 since the download is now starting
// TODO: Kick off a service to actually do the downloading, rather than simulate
downloadManager.getDownloadBus().post(new DownloadProgressEvent(47, b));
}
private void displayProgress(int progress) {
isDownloaded.setVisibility(View.GONE);
downloadProgress.setVisibility(View.VISIBLE);
downloadProgress.setProgress(75); // Out of 360
downloadProgress.setProgress(progress);
}
}
}

View File

@ -8,6 +8,8 @@ import org.crosswire.jsword.book.BookCategory;
import org.crosswire.jsword.book.install.InstallManager;
import org.crosswire.jsword.book.install.Installer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -24,7 +26,12 @@ public class DownloadManager {
/**
* Cached copy of modules that are available so we don't refresh for everyone who requests it.
*/
private List<Book> availableModules = null;
private List<Book> availableModules;
/**
* Cached copy of downloads in progress so views displaying this info can get it quickly.
*/
private Map<Book, DownloadProgressEvent> inProgressDownloads;
@Inject
protected EventBus downloadBus;
@ -40,6 +47,7 @@ public class DownloadManager {
MinimalBible.getApplication().inject(this);
setDownloadDir();
refreshModules();
inProgressDownloads = new HashMap<Book, DownloadProgressEvent>();
}
/**
@ -107,4 +115,31 @@ public class DownloadManager {
public EventBus getDownloadBus() {
return this.downloadBus;
}
/**
* Handle a book download progress event.
* Mostly responsible for caching the in progress status to check on it easily
* @param event
*/
public void onEvent(DownloadProgressEvent event) {
if (event.isComplete() && inProgressDownloads.containsKey(event.getB())) {
inProgressDownloads.remove(event.getB());
} else {
inProgressDownloads.put(event.getB(), event);
}
}
/**
* Check the status of a book download in progress.
* @param b
* @return The most recent DownloadProgressEvent for the book, or null if not downloading
*/
public DownloadProgressEvent getInProgressDownloadProgress(Book b) {
if (inProgressDownloads.containsKey(b)) {
return inProgressDownloads.get(b);
} else {
return null;
}
}
}

View File

@ -0,0 +1,32 @@
package org.bspeice.minimalbible.activities.downloader.manager;
import org.crosswire.jsword.book.Book;
/**
* Created by bspeice on 5/19/14.
*/
public class DownloadProgressEvent {
private int progress;
private Book b;
public DownloadProgressEvent(int progress, Book b) {
this.progress = progress;
this.b = b;
}
public int getProgress() {
return progress;
}
public Book getB() {
return b;
}
public float toCircular() {
return ((float)progress) * 360 / 100;
}
public boolean isComplete() {
return progress >= 100;
}
}