2020-11-20 15:38:08 +01:00
|
|
|
package com.cappielloantonio.play.adapter;
|
|
|
|
|
|
2021-12-29 10:22:10 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2020-11-20 15:38:08 +01:00
|
|
|
import android.content.Context;
|
2020-12-01 20:04:54 +01:00
|
|
|
import android.os.Bundle;
|
2021-08-17 16:17:41 +02: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;
|
|
|
|
|
|
2021-08-14 17:37:23 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2021-12-29 10:22:10 +01:00
|
|
|
import androidx.media3.session.MediaController;
|
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;
|
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;
|
2021-04-27 10:44:55 +02:00
|
|
|
import com.cappielloantonio.play.ui.activity.MainActivity;
|
2021-12-29 10:22:10 +01:00
|
|
|
import com.cappielloantonio.play.util.MappingUtil;
|
2021-08-01 19:08:40 +02:00
|
|
|
import com.cappielloantonio.play.util.MusicUtil;
|
2021-12-29 10:22:10 +01:00
|
|
|
import com.google.common.util.concurrent.MoreExecutors;
|
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-08-17 16:17:41 +02:00
|
|
|
public class SimilarTrackAdapter extends RecyclerView.Adapter<SimilarTrackAdapter.ViewHolder> {
|
|
|
|
|
private static final String TAG = "SimilarTrackAdapter";
|
2020-11-28 14:50:15 +01:00
|
|
|
|
2021-08-14 17:37:23 +02:00
|
|
|
private final MainActivity mainActivity;
|
|
|
|
|
private final Context context;
|
|
|
|
|
private final LayoutInflater mInflater;
|
|
|
|
|
|
2020-11-20 15:38:08 +01:00
|
|
|
private List<Song> songs;
|
|
|
|
|
|
2021-08-17 16:17:41 +02:00
|
|
|
public SimilarTrackAdapter(MainActivity mainActivity, Context context) {
|
2020-12-05 21:31:12 +01:00
|
|
|
this.mainActivity = mainActivity;
|
2020-11-20 15:38:08 +01:00
|
|
|
this.context = context;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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) {
|
2021-08-19 11:46:09 +02:00
|
|
|
View view = mInflater.inflate(R.layout.item_home_similar_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);
|
|
|
|
|
|
2021-08-07 15:09:20 +02:00
|
|
|
holder.textTitle.setText(MusicUtil.getReadableString(song.getTitle()));
|
2020-11-26 16:05:58 +01:00
|
|
|
|
|
|
|
|
CustomGlideRequest.Builder
|
2021-08-11 13:05:10 +02:00
|
|
|
.from(context, song.getPrimary(), CustomGlideRequest.SONG_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 songs.size();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 11:01:02 +02:00
|
|
|
public void setItems(List<Song> songs) {
|
|
|
|
|
this.songs = songs;
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 10:22:10 +01:00
|
|
|
@SuppressLint("UnsafeOptInUsageError")
|
2021-09-03 09:25:33 +02:00
|
|
|
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
2020-11-20 15:38:08 +01:00
|
|
|
TextView textTitle;
|
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-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);
|
2021-09-03 09:25:33 +02:00
|
|
|
itemView.setOnLongClickListener(this);
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View view) {
|
2021-12-29 10:22:10 +01:00
|
|
|
// List<Song> opener = new ArrayList<>();
|
|
|
|
|
// opener.add(songs.get(getBindingAdapterPosition()));
|
|
|
|
|
// MusicPlayerRemote.openQueue(opener, 0, true);
|
2021-08-17 16:17:41 +02:00
|
|
|
|
2021-12-29 10:22:10 +01:00
|
|
|
// MediaManager.startQueue(mainActivity.getMediaControllerInstance(), MappingUtil.mapMediaItem(context, opener), opener);
|
|
|
|
|
|
|
|
|
|
mainActivity.mediaControllerListenableFuture.addListener(() -> {
|
|
|
|
|
try {
|
|
|
|
|
if (mainActivity.mediaControllerListenableFuture.isDone()) {
|
|
|
|
|
MediaController mediaController = mainActivity.mediaControllerListenableFuture.get();
|
|
|
|
|
|
|
|
|
|
mediaController.setMediaItem(MappingUtil.mapMediaItem(context, songs.get(getBindingAdapterPosition())));
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}, MoreExecutors.directExecutor());
|
|
|
|
|
|
|
|
|
|
// QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
|
|
|
|
// queueRepository.insertAllAndStartNew(opener);
|
2020-12-05 21:31:12 +01:00
|
|
|
|
2021-12-22 16:22:01 +01:00
|
|
|
mainActivity.setBottomSheetInPeek(true);
|
2021-12-29 10:22:10 +01:00
|
|
|
// mainActivity.setBottomSheetMusicInfo(songs.get(getBindingAdapterPosition()));
|
2020-12-08 11:12:44 +01:00
|
|
|
|
2021-12-29 10:22:10 +01:00
|
|
|
/*SongRepository songRepository = new SongRepository(App.getInstance());
|
2021-08-17 16:17:41 +02:00
|
|
|
songRepository.getInstantMix(songs.get(getBindingAdapterPosition()), 20, new MediaCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onError(Exception exception) {
|
2021-09-04 19:00:41 +02:00
|
|
|
Log.e(TAG, "onError() " + exception.getMessage());
|
2021-08-17 16:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onLoadMedia(List<?> media) {
|
2021-12-29 10:22:10 +01:00
|
|
|
// MusicPlayerRemote.enqueue((List<Song>) media);
|
|
|
|
|
MediaManager.enqueue(mainActivity.getMediaControllerInstance(), MappingUtil.mapMediaItem(context, (List<Song>) media), (List<Song>) media);
|
2021-08-17 16:17:41 +02:00
|
|
|
}
|
2021-12-29 10:22:10 +01:00
|
|
|
});*/
|
2020-12-01 20:04:54 +01:00
|
|
|
}
|
2021-09-03 09:25:33 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onLongClick(View view) {
|
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
|
bundle.putParcelable("song_object", songs.get(getBindingAdapterPosition()));
|
|
|
|
|
Navigation.findNavController(view).navigate(R.id.songBottomSheetDialog, bundle);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
}
|