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

109 lines
3.9 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;
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.Album;
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 AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder> {
2020-11-28 14:50:15 +01:00
private static final String TAG = "AlbumAdapter";
2020-11-20 15:38:08 +01:00
private List<Album> albums;
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 AlbumAdapter(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.albums = 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_album, parent, false);
2020-11-20 15:38:08 +01:00
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Album album = albums.get(position);
holder.textAlbumName.setText(album.getTitle());
holder.textArtistName.setText(album.getArtistName());
CustomGlideRequest.Builder
.from(context, album.getPrimary(), album.getBlurHash(), CustomGlideRequest.PRIMARY, CustomGlideRequest.TOP_QUALITY, CustomGlideRequest.ALBUM_PIC)
.build()
.into(holder.cover);
2020-11-20 15:38:08 +01:00
}
@Override
public int getItemCount() {
return albums.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 textAlbumName;
TextView textArtistName;
ImageView cover;
2020-11-20 15:38:08 +01:00
ViewHolder(View itemView) {
super(itemView);
textAlbumName = itemView.findViewById(R.id.album_name_label);
textArtistName = itemView.findViewById(R.id.artist_name_label);
cover = itemView.findViewById(R.id.album_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("album_object", albums.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_albumPageFragment, bundle);
}
else if(Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.libraryFragment) {
Navigation.findNavController(view).navigate(R.id.action_libraryFragment_to_albumPageFragment, bundle);
}
else if(Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.albumCatalogueFragment) {
Navigation.findNavController(view).navigate(R.id.action_albumCatalogueFragment_to_albumPageFragment, bundle);
}
2020-12-01 20:04:54 +01:00
}
@Override
public boolean onLongClick(View v) {
Bundle bundle = new Bundle();
bundle.putParcelable("album_object", albums.get(getBindingAdapterPosition()));
2020-12-01 20:04:54 +01:00
Navigation.findNavController(v).navigate(R.id.albumBottomSheetDialog, bundle);
return true;
2020-11-20 15:38:08 +01:00
}
}
2020-11-23 09:28:20 +01:00
public Album getItem(int position) {
return albums.get(position);
}
2020-11-20 15:38:08 +01:00
public void setItems(List<Album> albums) {
this.albums = albums;
notifyDataSetChanged();
}
}