Skip to content

Commit

Permalink
[android] player refactor. #WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gubatron committed Feb 5, 2016
1 parent 044263f commit 5c4fe1f
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 568 deletions.
30 changes: 17 additions & 13 deletions android/apollo/src/com/andrew/apollo/adapters/AlbumAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,32 @@ public View getView(final int position, View convertView, final ViewGroup parent
holder.mLineTwo.get().setText(dataHolder.mLineTwo);
}
}
// Asynchronously load the album images into the adapter
mImageFetcher.loadAlbumImage(dataHolder.mLineTwo, dataHolder.mLineOne, dataHolder.mItemId,
holder.mImage.get());

if (mImageFetcher == null) {
LOGGER.warn("ArtistAdapter has null image fetcher");
}

if (mImageFetcher != null) {
// Asynchronously load the album images into the adapter
mImageFetcher.loadAlbumImage(dataHolder.mLineTwo, dataHolder.mLineOne, dataHolder.mItemId,
holder.mImage.get());
}

// List view only items
if (mLoadExtraData) {
// Make sure the background layer gets set
holder.mOverlay.get().setBackgroundColor(mOverlay);
// Set the number of songs (line three)
holder.mLineThree.get().setText(dataHolder.mLineThree);
// Asynchronously load the artist image on the background view
mImageFetcher.loadArtistImage(dataHolder.mLineTwo, holder.mBackground.get());

if (mImageFetcher != null) {
// Asynchronously load the artist image on the background view
mImageFetcher.loadArtistImage(dataHolder.mLineTwo, holder.mBackground.get());
}
}
if (mTouchPlay) {
// Play the album when the artwork is touched
playAlbum(holder.mImage.get(), position);
initAlbumPlayOnClick(holder.mImage.get(), position);
}
return convertView;
}
Expand Down Expand Up @@ -171,13 +182,6 @@ public void removeFromCache(final Album album) {
}
}

/**
* Flushes the disk cache.
*/
public void flush() {
mImageFetcher.flush();
}

/**
* @param extra True to load line three and the background image, false
* otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public interface Cacheable {

static Logger LOGGER = Logger.getLogger(ApolloFragmentAdapter.class);

/**
* The header view
*/
protected static final int ITEM_VIEW_TYPE_HEADER = -1;

/**
* * The data in the list.
*/
protected static final int ITEM_VIEW_TYPE_MUSIC = 0;


/**
* Used to set the size of the data in the adapter
*/
Expand Down Expand Up @@ -85,7 +96,9 @@ public ApolloFragmentAdapter(Context context, int mLayoutId) {
* Method that unloads and clears the items in the adapter
*/
public void unload() {
mData = null;
if (this instanceof Cacheable) {
mData = null;
}
mDataList.clear();
clear();
}
Expand All @@ -107,10 +120,10 @@ public void setDataList(final List<I> data) {
/**
* Starts playing an album if the user touches the artwork in the list.
*
* @param album The {@link ImageView} holding the album
* @param album The {@link ImageView} holding the album
* @param position The position of the album to play.
*/
protected void playAlbum(final ImageView album, final int position) {
protected void initAlbumPlayOnClick(final ImageView album, final int position) {
if (album != null) {
album.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -130,7 +143,7 @@ public static MusicHolder prepareMusicHolder(int mLayoutId, Context context, Vie
holder = new MusicHolder(convertView);
convertView.setTag(holder);
} else {
holder = (MusicHolder)convertView.getTag();
holder = (MusicHolder) convertView.getTag();
}
return holder;
}
Expand Down Expand Up @@ -163,47 +176,74 @@ public boolean hasStableIds() {
}

/**
* {@inheritDoc}
* Some views have multiple view types.
* If the View type for the adapter is a ITEM_VIEW_TYPE_HEADER
* Our elements are actually displayed starting with an offset of 1 on the listView, gridView.
*
* @return
*/
public int getOffset() {
int offset = 0;
if (getItemViewType(0) == ITEM_VIEW_TYPE_HEADER) {
offset = 1;
}
return offset;
}

/**
* The adapter's element count
*/
@Override
public int getCount() {
final int size = mDataList.size();
return size == 0 ? 0 : size + 1;
if (mDataList != null) {
return mDataList.size();
}
return super.getCount();
}

/**
* The view count, including a header element if present for this adapter.
*
* @return
*/
public int getViewCount() {
return getCount() + getOffset();
}

/**
* @param position - The ACTUAL position in the model container. If you're using an offset based on a list view that has a header element at 0, you must substract to the position you might have.
* @return The element at the indexed position in the model container (not the view). null if position is out of bounds.
*/
public I getItem(int position) {
if (position == 0) {
LOGGER.info("getItem(0) -> null.");
if (position < 0 || (mDataList != null && position >= mDataList.size())) {
return null;
}

I result;

if (mDataList != null && !mDataList.isEmpty()) {
int realPosition = position - 1;
result = mDataList.get(realPosition);
LOGGER.info("getItem(" + position + " -> " + realPosition + ") => " + result);
} else {
result = super.getItem(position);
}

return result;
return (mDataList != null && !mDataList.isEmpty()) ? mDataList.get(position) : super.getItem(position);
}

/**
*
* @param position The ACTUAL position in the model container.
* If you have an extra header element, substract the offset before invoking this method.
* @return the object id. if out of bound, returns -1.
*/
@Override
public long getItemId(int position) {
if (position == 0) {
LOGGER.info("position == 0 -> -1");
if (position < 0) {
return -1;
}

int realPosition = position-1;
if (mData != null && realPosition < mData.length) {
LOGGER.info("using mData[]. "+realPosition+" -> " + mData[realPosition].mItemId);
return mData[realPosition].mItemId;
} else if (!mDataList.isEmpty() && realPosition < mDataList.size()) {
I item = mDataList.get(realPosition);
long id=-1;
if (this instanceof Cacheable && mData != null) {
if (position < mData.length) {
return mData[position].mItemId;
} else {
return -1;
}
}

if (mDataList != null && !mDataList.isEmpty() && position < mDataList.size()) {
I item = mDataList.get(position);
long id = -1;
if (item instanceof Song) {
id = ((Song) item).mSongId;
} else if (item instanceof Album) {
Expand All @@ -215,17 +255,15 @@ public long getItemId(int position) {
} else if (item instanceof Artist) {
id = ((Artist) item).mArtistId;
}

LOGGER.info("using mDataList. "+realPosition+" -> " + id);
return id;
}

return - 1;
return -1;
}

/**
* @param extra True to load line three and the background image, false
* otherwise.
* otherwise.
*/
public void setLoadExtraData(final boolean extra) {
mLoadExtraData = extra;
Expand Down
39 changes: 24 additions & 15 deletions android/apollo/src/com/andrew/apollo/adapters/ArtistAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,17 @@ public View getView(final int position, View convertView, final ViewGroup parent
holder.mLineOne.get().setText(dataHolder.mLineOne);
// Set the number of albums (line two)
holder.mLineTwo.get().setText(dataHolder.mLineTwo);
// Asynchronously load the artist image into the adapter
mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mImage.get());

if (mLoadExtraData) {
if (mImageFetcher == null) {
LOGGER.warn("ArtistAdapter has null image fetcher");
}

if (mImageFetcher != null) {
// Asynchronously load the artist image into the adapter
mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mImage.get());
}

if (mLoadExtraData && mImageFetcher != null) {
// Make sure the background layer gets set
holder.mOverlay.get().setBackgroundColor(mOverlayColor);
// Set the number of songs (line three)
Expand Down Expand Up @@ -114,18 +121,20 @@ public void buildCache() {
// Build the artist
final Artist artist = getItem(i);

// Build the data holder
mData[i] = new DataHolder();
// Artist Id
mData[i].mItemId = artist.mArtistId;
// Artist names (line one)
mData[i].mLineOne = artist.mArtistName;
// Number of albums (line two)
mData[i].mLineTwo = MusicUtils.makeLabel(getContext(),
R.plurals.Nalbums, artist.mAlbumNumber);
// Number of songs (line three)
mData[i].mLineThree = MusicUtils.makeLabel(getContext(),
R.plurals.Nsongs, artist.mSongNumber);
if (artist != null) {
// Build the data holder
mData[i] = new DataHolder();
// Artist Id
mData[i].mItemId = artist.mArtistId;
// Artist names (line one)
mData[i].mLineOne = artist.mArtistName;
// Number of albums (line two)
mData[i].mLineTwo = MusicUtils.makeLabel(getContext(),
R.plurals.Nalbums, artist.mAlbumNumber);
// Number of songs (line three)
mData[i].mLineThree = MusicUtils.makeLabel(getContext(),
R.plurals.Nsongs, artist.mSongNumber);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@
*/
public class ArtistAlbumAdapter extends ApolloFragmentAdapter<Album> {

/**
* The header view
*/
private static final int ITEM_VIEW_TYPE_HEADER = 0;

/**
* * The data in the list.
*/
private static final int ITEM_VIEW_TYPE_MUSIC = 1;

/**
* Number of views (ImageView, TextView, header)
*/
Expand Down Expand Up @@ -114,8 +104,9 @@ public View getView(final int position, View convertView, final ViewGroup parent
mImageFetcher.loadAlbumImage(album.mArtistName,
albumName, album.mAlbumId,
holder.mImage.get());

// Play the album when the artwork is touched
playAlbum(holder.mImage.get(), position);
initAlbumPlayOnClick(holder.mImage.get(), position);
return convertView;
}

Expand Down Expand Up @@ -144,7 +135,7 @@ public int getItemViewType(final int position) {
* @param album The {@link ImageView} holding the album
* @param position The position of the album to play.
*/
protected void playAlbum(final ImageView album, final int position) {
protected void initAlbumPlayOnClick(final ImageView album, final int position) {
if (album != null) {
album.setOnClickListener(new OnClickListener() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ public class ProfileSongAdapter extends ApolloFragmentAdapter<Song> {
*/
public static final int DISPLAY_ALBUM_SETTING = 2;

/**
* The header view
*/
private static final int ITEM_VIEW_TYPE_HEADER = 0;

/**
* * The data in the list.
*/
private static final int ITEM_VIEW_TYPE_MUSIC = 1;

/**
* Number of views (ImageView, TextView, header)
*/
Expand Down Expand Up @@ -206,25 +196,6 @@ public View getView(final int position, View convertView, final ViewGroup parent
return convertView;
}

/**
* {@inheritDoc}
*/
@Override
public boolean hasStableIds() {
return true;
}

/**
* {@inheritDoc}
*/
@Override
public long getItemId(final int position) {
if (position == 0) {
return -1;
}
return position - 1;
}

/**
* {@inheritDoc}
*/
Expand Down
18 changes: 1 addition & 17 deletions android/apollo/src/com/andrew/apollo/adapters/SongAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @author Andrew Neal (andrewdneal@gmail.com)
*/
public class SongAdapter extends ApolloFragmentAdapter<Song> {
public class SongAdapter extends ApolloFragmentAdapter<Song> implements ApolloFragmentAdapter.Cacheable {

/**
* Number of views (TextView)
Expand Down Expand Up @@ -77,14 +77,6 @@ public View getView(final int position, View convertView, final ViewGroup parent
return convertView;
}

/**
* {@inheritDoc}
*/
@Override
public boolean hasStableIds() {
return true;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -117,14 +109,6 @@ public void buildCache() {
}
}

/**
* Method that unloads and clears the items in the adapter
*/
public void unload() {
clear();
mData = null;
}

@Override
public long getItemId(int position) {
return getItem(position).mSongId;
Expand Down

0 comments on commit 5c4fe1f

Please sign in to comment.