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;
|
2022-01-17 17:32:59 +01:00
|
|
|
import android.util.Log;
|
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;
|
2022-01-17 17:32:59 +01:00
|
|
|
import android.widget.Toast;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2021-08-14 17:37:23 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2022-01-17 17:32:59 +01:00
|
|
|
import androidx.media3.session.MediaBrowser;
|
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;
|
|
|
|
|
|
2021-08-09 10:48:52 +02:00
|
|
|
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
|
2022-01-17 17:32:59 +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;
|
2022-01-17 17:32:59 +01:00
|
|
|
import com.cappielloantonio.play.interfaces.MediaCallback;
|
2020-11-20 15:38:08 +01:00
|
|
|
import com.cappielloantonio.play.model.Artist;
|
2022-01-17 17:32:59 +01:00
|
|
|
import com.cappielloantonio.play.model.Song;
|
|
|
|
|
import com.cappielloantonio.play.repository.ArtistRepository;
|
|
|
|
|
import com.cappielloantonio.play.service.MediaManager;
|
|
|
|
|
import com.cappielloantonio.play.ui.activity.MainActivity;
|
2021-08-01 19:08:40 +02:00
|
|
|
import com.cappielloantonio.play.util.MusicUtil;
|
2022-01-17 17:32:59 +01:00
|
|
|
import com.google.common.util.concurrent.ListenableFuture;
|
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
|
|
|
|
2021-08-14 17:37:23 +02:00
|
|
|
private final LayoutInflater inflater;
|
2022-01-17 17:32:59 +01:00
|
|
|
private final MainActivity mainActivity;
|
2021-08-14 17:37:23 +02:00
|
|
|
private final Context context;
|
|
|
|
|
|
2020-11-20 15:38:08 +01:00
|
|
|
private List<Artist> artists;
|
|
|
|
|
|
2022-01-17 17:32:59 +01:00
|
|
|
private ListenableFuture<MediaBrowser> mediaBrowserListenableFuture;
|
|
|
|
|
|
|
|
|
|
public ArtistAdapter(MainActivity mainActivity, Context context) {
|
|
|
|
|
this.mainActivity = mainActivity;
|
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
|
|
|
}
|
|
|
|
|
|
2021-08-14 17:37:23 +02:00
|
|
|
@NonNull
|
2020-11-20 15:38:08 +01:00
|
|
|
@Override
|
2021-08-14 17:37:23 +02:00
|
|
|
public ViewHolder onCreateViewHolder(@NonNull 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);
|
|
|
|
|
|
2021-08-07 15:09:20 +02:00
|
|
|
holder.textArtistName.setText(MusicUtil.getReadableString(artist.getName()));
|
2020-11-26 16:05:58 +01:00
|
|
|
|
|
|
|
|
CustomGlideRequest.Builder
|
2021-08-11 13:05:10 +02:00
|
|
|
.from(context, artist.getId(), CustomGlideRequest.ARTIST_PIC, null)
|
2020-11-26 16:05:58 +01:00
|
|
|
.build()
|
2021-08-09 10:48:52 +02:00
|
|
|
.transform(new RoundedCorners(CustomGlideRequest.CORNER_RADIUS))
|
2020-11-26 16:05:58 +01:00
|
|
|
.into(holder.cover);
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getItemCount() {
|
|
|
|
|
return artists.size();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 11:01:02 +02:00
|
|
|
public Artist getItem(int position) {
|
|
|
|
|
return artists.get(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setItems(List<Artist> artists) {
|
|
|
|
|
this.artists = artists;
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 17:32:59 +01:00
|
|
|
public void setMediaBrowserListenableFuture(ListenableFuture<MediaBrowser> mediaBrowserListenableFuture) {
|
|
|
|
|
this.mediaBrowserListenableFuture = mediaBrowserListenableFuture;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2020-11-26 16:05:58 +01:00
|
|
|
ImageView cover;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
|
|
|
|
ViewHolder(View itemView) {
|
|
|
|
|
super(itemView);
|
|
|
|
|
|
|
|
|
|
textArtistName = itemView.findViewById(R.id.artist_name_label);
|
2020-11-26 16:05:58 +01:00
|
|
|
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();
|
2021-04-12 14:51:25 +02:00
|
|
|
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);
|
2021-04-27 11:01:02 +02:00
|
|
|
} 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);
|
2021-04-27 11:01:02 +02:00
|
|
|
} 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);
|
2022-01-17 17:32:59 +01:00
|
|
|
} else if (Objects.requireNonNull(Navigation.findNavController(view).getCurrentDestination()).getId() == R.id.homeFragment) {
|
|
|
|
|
if (mediaBrowserListenableFuture != null) {
|
|
|
|
|
ArtistRepository artistRepository = new ArtistRepository(App.getInstance());
|
|
|
|
|
artistRepository.getInstantMix(artists.get(getBindingAdapterPosition()), 20, new MediaCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onError(Exception exception) {
|
|
|
|
|
Log.e(TAG, "onError() " + exception.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onLoadMedia(List<?> media) {
|
|
|
|
|
if (media.size() > 0) {
|
|
|
|
|
MediaManager.startQueue(mediaBrowserListenableFuture, context, (ArrayList<Song>) media, 0);
|
|
|
|
|
mainActivity.setBottomSheetInPeek(true);
|
|
|
|
|
} else {
|
|
|
|
|
Toast.makeText(context, context.getString(R.string.artist_error_retrieving_radio), Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-04-20 11:53:51 +02:00
|
|
|
}
|
2020-12-01 20:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onLongClick(View v) {
|
|
|
|
|
Bundle bundle = new Bundle();
|
2021-04-12 14:51:25 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|