/*
* Copyright (C) 2015 Naman Dwivedi
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
package com.naman14.timber.adapters;
import android.app.Activity;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import com.naman14.timber.MusicPlayer;
import com.naman14.timber.R;
import com.naman14.timber.models.Song;
import com.naman14.timber.utils.TimberUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
public class SlidingQueueAdapter extends RecyclerView.Adapter<SlidingQueueAdapter.ItemHolder> {
public static int currentlyPlayingPosition;
private List<Song> arraylist;
private Activity mContext;
private int lastPosition = -1;
public SlidingQueueAdapter(Activity context, List<Song> arraylist) {
this.arraylist = arraylist;
this.mContext = context;
currentlyPlayingPosition = MusicPlayer.getQueuePosition();
}
@Override
public ItemHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_song_sliding_queue, null);
ItemHolder ml = new ItemHolder(v);
return ml;
}
@Override
public void onBindViewHolder(ItemHolder itemHolder, int i) {
// setAnimation(itemHolder.itemView, i);
Song localItem = arraylist.get(i);
ImageLoader.getInstance().displayImage(TimberUtils.getAlbumArtUri(localItem.albumId).toString(), itemHolder.albumArt, new DisplayImageOptions.Builder().cacheInMemory(true).showImageOnFail(R.drawable.ic_empty_music2).resetViewBeforeLoading(true).build());
}
@Override
public int getItemCount() {
return (null != arraylist ? arraylist.size() : 0);
}
public long[] getSongIds() {
long[] ret = new long[getItemCount()];
for (int i = 0; i < getItemCount(); i++) {
ret[i] = arraylist.get(i).id;
}
return ret;
}
private void setAnimation(View viewToAnimate, int position) {
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition) {
Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.scale);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
public class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected ImageView albumArt;
public ItemHolder(View view) {
super(view);
this.albumArt = (ImageView) view.findViewById(R.id.album_art);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
MusicPlayer.setQueuePosition(getAdapterPosition());
Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
notifyItemChanged(currentlyPlayingPosition);
notifyItemChanged(getAdapterPosition());
Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
}
}, 50);
}
}, 50);
}
}, 100);
}
}
}