Extra refactoring and cleanup on BookItemHolder

Also, rules for displaying everything and layouts are fleshed out.
ugly-unit-test
Bradlee Speice 2014-05-20 22:21:59 -04:00
parent 135832859c
commit d7a9ad3c82
3 changed files with 116 additions and 63 deletions

View File

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

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}