Skip to content

Commit

Permalink
[android] more refactors. brought Ref.class to apollo to be able to s…
Browse files Browse the repository at this point in the history
…ubmit this work to cyanogen's project later on.
  • Loading branch information
gubatron committed Feb 6, 2016
1 parent fceef38 commit 573fc47
Show file tree
Hide file tree
Showing 20 changed files with 317 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import com.andrew.apollo.utils.ApolloUtils;
import com.andrew.apollo.utils.Lists;
import com.andrew.apollo.utils.MusicUtils;
import com.frostwire.util.Ref;
import com.andrew.apollo.utils.Ref;

import java.io.IOException;
import java.lang.ref.WeakReference;
Expand Down
49 changes: 21 additions & 28 deletions android/apollo/src/com/andrew/apollo/adapters/AlbumAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
import android.widget.ArrayAdapter;
import com.andrew.apollo.cache.ImageFetcher;
import com.andrew.apollo.model.Album;
import com.andrew.apollo.ui.MusicHolder;
import com.andrew.apollo.ui.MusicHolder.DataHolder;
import com.andrew.apollo.utils.ApolloUtils;
import com.andrew.apollo.ui.MusicViewHolder;
import com.andrew.apollo.ui.MusicViewHolder.DataHolder;
import com.andrew.apollo.utils.MusicUtils;
import com.andrew.apollo.utils.Ref;
import com.frostwire.android.R;
import com.frostwire.logging.Logger;
import com.frostwire.util.Ref;

/**
* This {@link ArrayAdapter} is used to display all of the albums on a user's
Expand All @@ -40,21 +39,11 @@ public class AlbumAdapter extends ApolloFragmentAdapter<Album> implements Apollo
*/
private static final int VIEW_TYPE_COUNT = 2;

/**
* Image cache and image fetcher
*/
private final ImageFetcher mImageFetcher;

/**
* Semi-transparent overlay
*/
private final int mOverlay;

/**
* Determines if the grid or list should be the default style
*/
private boolean mLoadExtraData = false;

/**
* Sets the album art on click listener to start playing them album when
* touched.
Expand All @@ -66,8 +55,6 @@ public class AlbumAdapter extends ApolloFragmentAdapter<Album> implements Apollo
*/
public AlbumAdapter(final Activity context, final int layoutId) {
super(context, layoutId);
// Initialize the cache & image fetcher
mImageFetcher = ApolloUtils.getImageFetcher(context);
// Cache the transparent overlay
mOverlay = context.getResources().getColor(R.color.list_item_background);
}
Expand All @@ -78,11 +65,11 @@ public AlbumAdapter(final Activity context, final int layoutId) {
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// Recycle ViewHolder's items
MusicHolder holder = prepareMusicHolder(mLayoutId, getContext(), convertView, parent);
MusicViewHolder holder = prepareMusicViewHolder(mLayoutId, getContext(), convertView, parent);
// Retrieve the data holder
final DataHolder dataHolder = mData[position];

if (holder != null) {
if (holder != null && dataHolder != null) {
// Set each album name (line one)
if (Ref.alive(holder.mLineOne)) {
holder.mLineOne.get().setText(dataHolder.mLineOne);
Expand All @@ -97,28 +84,34 @@ public View getView(final int position, View convertView, final ViewGroup parent
LOGGER.warn("ArtistAdapter has null image fetcher");
}

if (mImageFetcher != null) {
if (mImageFetcher != null && dataHolder != null && Ref.alive(holder.mImage)) {
// Asynchronously load the album images into the adapter
mImageFetcher.loadAlbumImage(dataHolder.mLineTwo, dataHolder.mLineOne, dataHolder.mItemId,
holder.mImage.get());
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);
if (mLoadExtraData && holder != null && dataHolder != null) {
if (Ref.alive(holder.mOverlay)) {
// Make sure the background layer gets set
holder.mOverlay.get().setBackgroundColor(mOverlay);
}

if (Ref.alive(holder.mLineThree)) {
// Set the number of songs (line three)
holder.mLineThree.get().setText(dataHolder.mLineThree);
}

if (mImageFetcher != null) {
if (mImageFetcher != null && Ref.alive(holder.mBackground)) {
// Asynchronously load the artist image on the background view
mImageFetcher.loadArtistImage(dataHolder.mLineTwo, holder.mBackground.get());
}
}
if (mTouchPlay) {

if (mTouchPlay && holder != null && Ref.alive(holder.mImage)) {
// Play the album when the artwork is touched
initAlbumPlayOnClick(holder.mImage.get(), position);
}

return convertView;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import android.widget.ImageView;
import com.andrew.apollo.cache.ImageFetcher;
import com.andrew.apollo.model.*;
import com.andrew.apollo.ui.MusicHolder;
import com.andrew.apollo.ui.MusicViewHolder;
import com.andrew.apollo.utils.ApolloUtils;
import com.andrew.apollo.utils.Lists;
import com.andrew.apollo.utils.MusicUtils;
Expand Down Expand Up @@ -73,7 +73,7 @@ public interface Cacheable {
/**
* Used to cache the album info
*/
protected MusicHolder.DataHolder[] mData;
protected MusicViewHolder.DataHolder[] mData;

/**
* Loads line three and the background image if the user decides to.
Expand Down Expand Up @@ -136,14 +136,24 @@ public void onClick(final View v) {
}
}

public static MusicHolder prepareMusicHolder(int mLayoutId, Context context, View convertView, final ViewGroup parent) {
MusicHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(mLayoutId, parent, false);
holder = new MusicHolder(convertView);
convertView.setTag(holder);
public static MusicViewHolder prepareMusicViewHolder(int mLayoutId, Context context, View convertView, final ViewGroup parent) {
MusicViewHolder holder = null;
if (convertView != null) {
holder = (MusicViewHolder) convertView.getTag();
} else {
holder = (MusicHolder) convertView.getTag();
try {
convertView = LayoutInflater.from(context).inflate(mLayoutId, parent, false);
} catch (Throwable t) {
holder = null;
}
}

if (holder == null && convertView != null) {
holder = new MusicViewHolder(convertView);
}

if (convertView != null && holder != null) {
convertView.setTag(holder);
}
return holder;
}
Expand All @@ -157,16 +167,6 @@ public void setPauseDiskCache(final boolean pause) {
}
}

// /**
// * @param album The key used to find the cached album to remove
// */
// public void removeFromCache(final Album album) {
// if (mImageFetcher != null) {
// mImageFetcher.removeFromCache(
// ImageFetcher.generateAlbumCacheKey(album.mAlbumName, album.mArtistName));
// }
// }

/**
* {@inheritDoc}
*/
Expand Down
56 changes: 38 additions & 18 deletions android/apollo/src/com/andrew/apollo/adapters/ArtistAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
import android.view.ViewGroup;
import android.widget.ImageView;
import com.andrew.apollo.model.Artist;
import com.andrew.apollo.ui.MusicHolder;
import com.andrew.apollo.ui.MusicHolder.DataHolder;
import com.andrew.apollo.ui.MusicViewHolder;
import com.andrew.apollo.ui.MusicViewHolder.DataHolder;
import com.andrew.apollo.utils.MusicUtils;
import com.andrew.apollo.utils.Ref;
import com.frostwire.android.R;

/**
Expand Down Expand Up @@ -62,34 +63,53 @@ public ArtistAdapter(final Activity context, final int layoutId) {
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// Recycle ViewHolder's items
MusicHolder holder = prepareMusicHolder(mLayoutId, getContext(), convertView, parent);
MusicViewHolder holder = prepareMusicViewHolder(mLayoutId, getContext(), convertView, parent);

// Retrieve the data holder
final DataHolder dataHolder = mData[position];

// Set each artist name (line one)
holder.mLineOne.get().setText(dataHolder.mLineOne);
// Set the number of albums (line two)
holder.mLineTwo.get().setText(dataHolder.mLineTwo);
if (holder != null && dataHolder != null) {
// Set each artist name (line one)
if (Ref.alive(holder.mLineOne)) {
holder.mLineOne.get().setText(dataHolder.mLineOne);
}

// Set the number of albums (line two)
if (Ref.alive(holder.mLineTwo)) {
holder.mLineTwo.get().setText(dataHolder.mLineTwo);
}
}

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

if (mImageFetcher != null) {
if (mImageFetcher != null && dataHolder != null && Ref.alive(holder.mImage)) {
// 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)
holder.mLineThree.get().setText(dataHolder.mLineThree);
// Set the background image
mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mBackground.get());
// Play the artist when the artwork is touched
playArtist(holder.mImage.get(), position);
if (mLoadExtraData && mImageFetcher != null && holder != null) {
if (Ref.alive(holder.mOverlay)) {
// Make sure the background layer gets set
holder.mOverlay.get().setBackgroundColor(mOverlayColor);
}

if (Ref.alive(holder.mLineThree)) {
// Set the number of songs (line three)
holder.mLineThree.get().setText(dataHolder.mLineThree);
}


if (Ref.alive(holder.mBackground)) {
// Set the background image
mImageFetcher.loadArtistImage(dataHolder.mLineOne, holder.mBackground.get());
}

if (Ref.alive(holder.mImage)) {
// Play the artist when the artwork is touched
initArtistPlayOnClick(holder.mImage.get(), position);
}
}
return convertView;
}
Expand Down Expand Up @@ -145,7 +165,7 @@ public void buildCache() {
* @param artist The {@link ImageView} holding the aritst image
* @param position The position of the artist to play.
*/
private void playArtist(final ImageView artist, final int position) {
private void initArtistPlayOnClick(final ImageView artist, final int position) {
artist.setOnClickListener(new OnClickListener() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import com.andrew.apollo.model.Album;
import com.andrew.apollo.ui.MusicHolder;
import com.andrew.apollo.ui.MusicViewHolder;
import com.andrew.apollo.ui.fragments.profile.ArtistAlbumFragment;
import com.andrew.apollo.utils.MusicUtils;
import com.andrew.apollo.utils.Ref;
import com.frostwire.android.R;
import com.frostwire.util.Ref;


/**
* This {@link ArrayAdapter} is used to display the albums for a particular
Expand Down Expand Up @@ -75,13 +76,13 @@ public View getView(final int position, View convertView, final ViewGroup parent
}

// Recycle MusicHolder's items
MusicHolder holder = prepareMusicHolder(mLayoutId, getContext(), convertView, parent);
MusicViewHolder holder = prepareMusicViewHolder(mLayoutId, getContext(), convertView, parent);
if (holder != null && Ref.alive(holder.mOverlay)) {
holder.mOverlay.get().setBackgroundColor(0);
}

// Retrieve the album
final Album album = getItem(position - 1);
final Album album = getItem(position - getOffset());
final String albumName = album.mAlbumName;

if (holder != null) {
Expand All @@ -100,13 +101,18 @@ public View getView(final int position, View convertView, final ViewGroup parent
}
}

// Asynchronously load the album images into the adapter
mImageFetcher.loadAlbumImage(album.mArtistName,
albumName, album.mAlbumId,
holder.mImage.get());
if (mImageFetcher != null && Ref.alive(holder.mImage)) {
// Asynchronously load the album images into the adapter
mImageFetcher.loadAlbumImage(album.mArtistName,
albumName, album.mAlbumId,
holder.mImage.get());
}

// Play the album when the artwork is touched
initAlbumPlayOnClick(holder.mImage.get(), position);
if (holder != null && Ref.alive(holder.mImage)) {
initAlbumPlayOnClick(holder.mImage.get(), position - getOffset());
}

return convertView;
}

Expand All @@ -133,15 +139,15 @@ public int getItemViewType(final int position) {
* Starts playing an album if the user touches the artwork in the list.
*
* @param album The {@link ImageView} holding the album
* @param position The position of the album to play.
* @param pos The position of the album to play.
*/
protected void initAlbumPlayOnClick(final ImageView album, final int position) {
protected void initAlbumPlayOnClick(final ImageView album, final int pos) {
if (album != null) {
album.setOnClickListener(new OnClickListener() {

@Override
public void onClick(final View v) {
final long id = getItem(position - 1).mAlbumId;
final long id = getItem(pos).mAlbumId;
final long[] list = MusicUtils.getSongListForAlbum(getContext(), id);
MusicUtils.playAll(list, 0, false);
}
Expand Down

0 comments on commit 573fc47

Please sign in to comment.