2020-11-20 15:38:08 +01:00
|
|
|
package com.cappielloantonio.play.adapter;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2020-12-01 20:04:54 +01:00
|
|
|
import android.os.Bundle;
|
2020-11-20 15:38:08 +01:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
2020-11-26 16:05:58 +01:00
|
|
|
import android.widget.ImageView;
|
2020-11-20 15:38:08 +01:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
2020-12-01 20:04:54 +01:00
|
|
|
import androidx.fragment.app.FragmentManager;
|
|
|
|
|
import androidx.navigation.Navigation;
|
2020-11-20 15:38:08 +01:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
2020-11-22 19:11:38 +01:00
|
|
|
import com.cappielloantonio.play.App;
|
2020-11-20 15:38:08 +01:00
|
|
|
import com.cappielloantonio.play.R;
|
2020-11-26 16:05:58 +01:00
|
|
|
import com.cappielloantonio.play.glide.CustomGlideRequest;
|
2020-11-20 15:38:08 +01:00
|
|
|
import com.cappielloantonio.play.model.Song;
|
2020-11-22 19:11:38 +01:00
|
|
|
import com.cappielloantonio.play.repository.SongRepository;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2020-11-30 20:54:05 +01:00
|
|
|
import java.util.ArrayList;
|
2020-11-20 15:38:08 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
2020-11-21 13:54:49 +01:00
|
|
|
/**
|
|
|
|
|
* Adapter per i brani recenti in home
|
|
|
|
|
*/
|
2020-11-20 15:38:08 +01:00
|
|
|
public class RecentMusicAdapter extends RecyclerView.Adapter<RecentMusicAdapter.ViewHolder> {
|
|
|
|
|
private static final String TAG = "RecentMusicAdapter";
|
2020-11-28 14:50:15 +01:00
|
|
|
|
2020-11-20 15:38:08 +01:00
|
|
|
private List<Song> songs;
|
|
|
|
|
private LayoutInflater mInflater;
|
|
|
|
|
private Context context;
|
2020-12-01 20:04:54 +01:00
|
|
|
private FragmentManager fragmentManager;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2020-12-01 20:04:54 +01:00
|
|
|
public RecentMusicAdapter(Context context, FragmentManager fragmentManager) {
|
2020-11-20 15:38:08 +01:00
|
|
|
this.context = context;
|
2020-12-01 20:04:54 +01:00
|
|
|
this.fragmentManager = fragmentManager;
|
2020-11-20 15:38:08 +01:00
|
|
|
this.mInflater = LayoutInflater.from(context);
|
2020-11-30 20:54:05 +01:00
|
|
|
this.songs = new ArrayList<>();
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
2020-11-26 16:05:58 +01:00
|
|
|
View view = mInflater.inflate(R.layout.item_home_track, parent, false);
|
2020-11-20 15:38:08 +01:00
|
|
|
return new ViewHolder(view);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onBindViewHolder(ViewHolder holder, int position) {
|
|
|
|
|
Song song = songs.get(position);
|
|
|
|
|
|
|
|
|
|
holder.textTitle.setText(song.getTitle());
|
2020-11-25 15:12:07 +01:00
|
|
|
holder.textAlbum.setText(song.getAlbumName());
|
2020-11-26 16:05:58 +01:00
|
|
|
|
|
|
|
|
CustomGlideRequest.Builder
|
2020-11-28 14:50:15 +01:00
|
|
|
.from(context, song.getPrimary(), song.getBlurHash(), CustomGlideRequest.PRIMARY, CustomGlideRequest.TOP_QUALITY)
|
2020-11-26 16:05:58 +01:00
|
|
|
.build()
|
|
|
|
|
.into(holder.cover);
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getItemCount() {
|
|
|
|
|
return songs.size();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 20:04:54 +01:00
|
|
|
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
2020-11-20 15:38:08 +01:00
|
|
|
TextView textTitle;
|
2020-11-25 15:12:07 +01:00
|
|
|
TextView textAlbum;
|
2020-11-26 16:05:58 +01:00
|
|
|
ImageView cover;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
|
|
|
|
ViewHolder(View itemView) {
|
|
|
|
|
super(itemView);
|
|
|
|
|
|
|
|
|
|
textTitle = itemView.findViewById(R.id.title_track_label);
|
2020-11-25 15:12:07 +01:00
|
|
|
textAlbum = itemView.findViewById(R.id.album_track_label);
|
2020-11-26 16:05:58 +01:00
|
|
|
cover = itemView.findViewById(R.id.track_cover_image_view);
|
2020-11-20 15:38:08 +01:00
|
|
|
|
|
|
|
|
itemView.setOnClickListener(this);
|
2020-12-01 20:04:54 +01:00
|
|
|
itemView.setOnLongClickListener(this);
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View view) {
|
2020-11-22 19:11:38 +01:00
|
|
|
SongRepository songRepository = new SongRepository(App.getInstance());
|
2020-11-30 20:54:05 +01:00
|
|
|
songRepository.increasePlayCount(songs.get(getAdapterPosition()));
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
2020-12-01 20:04:54 +01:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onLongClick(View view) {
|
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
|
bundle.putParcelable("song_object", songs.get(getAdapterPosition()));
|
|
|
|
|
Navigation.findNavController(view).navigate(R.id.songBottomSheetDialog, bundle);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
2020-11-21 18:41:35 +01:00
|
|
|
public void setItems(List<Song> songs) {
|
|
|
|
|
this.songs = songs;
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|