diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java index 13a7f6f..337dee5 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/ActivityDownloaderModule.java @@ -18,7 +18,8 @@ import de.greenrobot.event.EventBus; injects = { BookListFragment.class, DownloadManager.class, - BookRefreshTask.class + BookRefreshTask.class, + BookListAdapter.BookItemHolder.class } ) public class ActivityDownloaderModule { diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookListAdapter.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookListAdapter.java index 7e95be9..c375968 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookListAdapter.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookListAdapter.java @@ -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); } } } diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java index ba73abb..56b3b95 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadManager.java @@ -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 availableModules = null; + private List availableModules; + + /** + * Cached copy of downloads in progress so views displaying this info can get it quickly. + */ + private Map inProgressDownloads; @Inject protected EventBus downloadBus; @@ -40,6 +47,7 @@ public class DownloadManager { MinimalBible.getApplication().inject(this); setDownloadDir(); refreshModules(); + inProgressDownloads = new HashMap(); } /** @@ -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; + } + } + } diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadProgressEvent.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadProgressEvent.java new file mode 100644 index 0000000..0170400 --- /dev/null +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/manager/DownloadProgressEvent.java @@ -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; + } +}