tempus/app/src/main/java/com/cappielloantonio/play/adapter/ArtistAdapter.java

106 lines
3.7 KiB
Java
Raw Normal View History

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;
2021-08-01 11:26:06 +02:00
import android.text.Html;
2020-11-20 15:38:08 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.navigation.Navigation;
2020-11-20 15:38:08 +01:00
import androidx.recyclerview.widget.RecyclerView;
import com.cappielloantonio.play.R;
import com.cappielloantonio.play.glide.CustomGlideRequest;
2020-11-20 15:38:08 +01:00
import com.cappielloantonio.play.model.Artist;
import com.cappielloantonio.play.util.MusicUtil;
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;
2021-04-20 11:53:51 +02:00
import java.util.Objects;
2020-11-20 15:38:08 +01:00
public class ArtistAdapter extends RecyclerView.Adapter<ArtistAdapter.ViewHolder> {
private static final String TAG = "ArtistAdapter";
2020-11-28 14:50:15 +01:00
2020-11-20 15:38:08 +01:00
private List<Artist> artists;
2020-11-28 14:50:15 +01:00
private LayoutInflater inflater;
2020-11-20 15:38:08 +01:00
private Context context;
2020-11-28 14:50:15 +01:00
public ArtistAdapter(Context context) {
2020-11-20 15:38:08 +01:00
this.context = context;
2020-11-28 14:50:15 +01:00
this.inflater = LayoutInflater.from(context);
2020-11-30 20:54:05 +01:00
this.artists = new ArrayList<>();
2020-11-20 15:38:08 +01:00
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
2020-11-28 14:50:15 +01:00
View view = inflater.inflate(R.layout.item_library_artist, parent, false);
2020-11-20 15:38:08 +01:00
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Artist artist = artists.get(position);
holder.textArtistName.setText(MusicUtil.getReadableInfo(artist.getName()));
CustomGlideRequest.Builder
2021-08-05 14:04:34 +02:00
.from(context, artist.getId(), CustomGlideRequest.ARTIST_PIC)
.build()
.into(holder.cover);
2020-11-20 15:38:08 +01:00
}
@Override
public int getItemCount() {
return artists.size();
}
public Artist getItem(int position) {
return artists.get(position);
}
public void setItems(List<Artist> artists) {
this.artists = artists;
notifyDataSetChanged();
}
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 textArtistName;
ImageView cover;
2020-11-20 15:38:08 +01:00
ViewHolder(View itemView) {
super(itemView);
textArtistName = itemView.findViewById(R.id.artist_name_label);
cover = itemView.findViewById(R.id.artist_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-12-01 20:04:54 +01:00
Bundle bundle = new Bundle();
bundle.putParcelable("artist_object", artists.get(getBindingAdapterPosition()));
2021-04-20 11:53:51 +02:00
if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.searchFragment) {
Navigation.findNavController(view).navigate(R.id.action_searchFragment_to_artistPageFragment, bundle);
} else if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.libraryFragment) {
2021-04-20 11:53:51 +02:00
Navigation.findNavController(view).navigate(R.id.action_libraryFragment_to_artistPageFragment, bundle);
} else if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.artistCatalogueFragment) {
2021-04-20 11:53:51 +02:00
Navigation.findNavController(view).navigate(R.id.action_artistCatalogueFragment_to_artistPageFragment, bundle);
}
2020-12-01 20:04:54 +01:00
}
@Override
public boolean onLongClick(View v) {
Bundle bundle = new Bundle();
bundle.putParcelable("artist_object", artists.get(getBindingAdapterPosition()));
2020-12-01 20:04:54 +01:00
Navigation.findNavController(v).navigate(R.id.artistBottomSheetDialog, bundle);
return true;
2020-11-20 15:38:08 +01:00
}
}
}