From d7a9ad3c828c0a4577a9bc6874da1acba8a04763 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 20 May 2014 22:21:59 -0400 Subject: [PATCH] Extra refactoring and cleanup on BookItemHolder Also, rules for displaying everything and layouts are fleshed out. --- .../downloader/ActivityDownloaderModule.java | 2 +- .../activities/downloader/BookItemHolder.java | 115 ++++++++++++++++++ .../downloader/BookListAdapter.java | 62 ---------- 3 files changed, 116 insertions(+), 63 deletions(-) create mode 100644 MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java 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 337dee5..78e64eb 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 @@ -19,7 +19,7 @@ import de.greenrobot.event.EventBus; BookListFragment.class, DownloadManager.class, BookRefreshTask.class, - BookListAdapter.BookItemHolder.class + BookItemHolder.class } ) public class ActivityDownloaderModule { diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java new file mode 100644 index 0000000..3d1461c --- /dev/null +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/downloader/BookItemHolder.java @@ -0,0 +1,115 @@ +package org.bspeice.minimalbible.activities.downloader; + +import android.util.Log; +import android.view.View; +import android.widget.ImageButton; +import android.widget.RelativeLayout; +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 javax.inject.Inject; + +import butterknife.ButterKnife; +import butterknife.InjectView; +import butterknife.OnClick; + +/** +* Created by bspeice on 5/20/14. +*/ +public class BookItemHolder { + + // TODO: The holder should register and unregister itself for DownloadProgress events + // so that we can display live updates. + + @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; + + @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() { + acronym.setText(b.getInitials()); + itemName.setText(b.getName()); + DownloadProgressEvent downloadProgressEvent = downloadManager.getInProgressDownloadProgress(b); + if (downloadProgressEvent != null) { + displayProgress((int) downloadProgressEvent.toCircular()); + } + // TODO: Display a remove icon if the book has been downloaded. + } + + @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: Write a unit test to make sure that this is called - displayProgress() assumes it + + // TODO: Kick off a service to actually do the downloading, rather than simulate + downloadManager.getDownloadBus().post(new DownloadProgressEvent(47, b)); + } + + /** + * Display the current progress of this download + * @param progress The progress out of 360 (degrees of a circle) + */ + private void displayProgress(int progress) { + + + if (progress <= 0) { + // Download starting + RelativeLayout.LayoutParams acronymParams = + (RelativeLayout.LayoutParams)acronym.getLayoutParams(); + acronymParams.addRule(RelativeLayout.LEFT_OF, downloadProgress.getId()); + + RelativeLayout.LayoutParams nameParams = + (RelativeLayout.LayoutParams)itemName.getLayoutParams(); + nameParams.addRule(RelativeLayout.LEFT_OF, downloadProgress.getId()); + + isDownloaded.setVisibility(View.GONE); + downloadProgress.setVisibility(View.VISIBLE); + + downloadProgress.spin(); + } else if (progress < 360) { + // Download in progress + RelativeLayout.LayoutParams acronymParams = + (RelativeLayout.LayoutParams)acronym.getLayoutParams(); + acronymParams.addRule(RelativeLayout.LEFT_OF, downloadProgress.getId()); + + RelativeLayout.LayoutParams nameParams = + (RelativeLayout.LayoutParams)itemName.getLayoutParams(); + nameParams.addRule(RelativeLayout.LEFT_OF, downloadProgress.getId()); + + isDownloaded.setVisibility(View.GONE); + downloadProgress.setVisibility(View.VISIBLE); + + downloadProgress.setProgress(progress); + } else { + // Download complete + RelativeLayout.LayoutParams acronymParams = + (RelativeLayout.LayoutParams)acronym.getLayoutParams(); + acronymParams.addRule(RelativeLayout.LEFT_OF, isDownloaded.getId()); + + RelativeLayout.LayoutParams nameParams = + (RelativeLayout.LayoutParams)itemName.getLayoutParams(); + nameParams.addRule(RelativeLayout.LEFT_OF, isDownloaded.getId()); + + isDownloaded.setVisibility(View.VISIBLE); + downloadProgress.setVisibility(View.GONE); + } + } +} 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 09d1a9a..92d43ab 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 @@ -1,29 +1,15 @@ package org.bspeice.minimalbible.activities.downloader; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; -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; - /** * Adapter to inflate list_download_items.xml */ @@ -67,52 +53,4 @@ public class BookListAdapter extends BaseAdapter { viewHolder.bindHolder(); return convertView; } - - public static class BookItemHolder { - - // TODO: The holder should register and unregister itself for DownloadProgress events - // so that we can display live updates. - // TODO: Do we need to worry about lifecycle since this will be a static class? - // TODO: Switch this to its own class (non-static) so we can inject it, - // and it doesn't persist in memory - - @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; - - @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() { - 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(progress); - } - } }