2020-11-20 15:38:08 +01:00
|
|
|
package com.cappielloantonio.play.adapter;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2021-04-18 20:23:09 +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;
|
|
|
|
|
|
2020-11-24 10:52:00 +01:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
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;
|
2021-04-18 20:23:09 +02:00
|
|
|
import com.cappielloantonio.play.interfaces.MediaCallback;
|
2020-11-20 15:38:08 +01:00
|
|
|
import com.cappielloantonio.play.model.Song;
|
2021-04-18 20:23:09 +02:00
|
|
|
import com.cappielloantonio.play.repository.QueueRepository;
|
2021-07-27 14:52:37 +02:00
|
|
|
import com.cappielloantonio.play.repository.SongRepository;
|
2021-04-27 11:01:02 +02:00
|
|
|
import com.cappielloantonio.play.service.MusicPlayerRemote;
|
2021-04-27 10:44:55 +02:00
|
|
|
import com.cappielloantonio.play.ui.activity.MainActivity;
|
2021-04-19 14:20:00 +02:00
|
|
|
import com.cappielloantonio.play.util.PreferenceUtil;
|
2021-04-18 20:23:09 +02:00
|
|
|
import com.cappielloantonio.play.util.SyncUtil;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2021-04-18 20:23:09 +02:00
|
|
|
import java.util.ArrayList;
|
2020-11-20 15:38:08 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
2020-11-25 15:12:07 +01:00
|
|
|
public class DiscoverSongAdapter extends RecyclerView.Adapter<DiscoverSongAdapter.ViewHolder> {
|
2020-11-22 19:11:38 +01:00
|
|
|
private static final String TAG = "DiscoverSongAdapter";
|
2020-11-20 15:38:08 +01:00
|
|
|
|
|
|
|
|
private List<Song> songs;
|
2020-11-28 14:50:15 +01:00
|
|
|
private LayoutInflater inflater;
|
2020-11-20 15:38:08 +01:00
|
|
|
private Context context;
|
2021-04-18 20:23:09 +02:00
|
|
|
private MainActivity activity;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2021-07-28 15:46:05 +02:00
|
|
|
public DiscoverSongAdapter(MainActivity activity, Context context) {
|
2021-04-18 20:23:09 +02:00
|
|
|
this.activity = activity;
|
2020-11-20 15:38:08 +01:00
|
|
|
this.context = context;
|
2020-11-28 14:50:15 +01:00
|
|
|
this.inflater = LayoutInflater.from(context);
|
2021-07-28 15:46:05 +02:00
|
|
|
this.songs = new ArrayList<>();
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2020-11-25 15:12:07 +01:00
|
|
|
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
2020-11-28 14:50:15 +01:00
|
|
|
View view = inflater.inflate(R.layout.item_home_discover_song, parent, false);
|
2020-11-25 15:12:07 +01:00
|
|
|
return new ViewHolder(view);
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2020-11-25 15:12:07 +01:00
|
|
|
public void onBindViewHolder(ViewHolder holder, int position) {
|
|
|
|
|
Song song = songs.get(position);
|
|
|
|
|
|
|
|
|
|
holder.textTitle.setText(song.getTitle());
|
|
|
|
|
holder.textAlbum.setText(song.getAlbumName());
|
2020-11-26 16:05:58 +01:00
|
|
|
|
|
|
|
|
CustomGlideRequest.Builder
|
2021-07-27 12:10:28 +02:00
|
|
|
.from(context, song.getPrimary(), song.getBlurHash(), CustomGlideRequest.SONG_PIC)
|
2020-11-26 16:05:58 +01:00
|
|
|
.build()
|
|
|
|
|
.into(holder.cover);
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2020-11-25 15:12:07 +01:00
|
|
|
public int getItemCount() {
|
|
|
|
|
return songs.size();
|
|
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2021-04-27 11:01:02 +02:00
|
|
|
public void setItems(List<Song> songs) {
|
|
|
|
|
this.songs = songs;
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-25 15:12:07 +01:00
|
|
|
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
|
|
|
|
TextView textTitle;
|
|
|
|
|
TextView textAlbum;
|
2020-11-26 16:05:58 +01:00
|
|
|
ImageView cover;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2020-11-25 15:12:07 +01:00
|
|
|
ViewHolder(View itemView) {
|
|
|
|
|
super(itemView);
|
|
|
|
|
|
|
|
|
|
textTitle = itemView.findViewById(R.id.title_discover_song_label);
|
|
|
|
|
textAlbum = itemView.findViewById(R.id.album_discover_song_label);
|
2020-11-26 16:05:58 +01:00
|
|
|
cover = itemView.findViewById(R.id.discover_song_cover_image_view);
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2020-11-25 15:12:07 +01:00
|
|
|
itemView.setOnClickListener(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View view) {
|
2021-07-28 18:29:26 +02:00
|
|
|
List<Song> opener = new ArrayList<>();
|
|
|
|
|
opener.add(songs.get(getBindingAdapterPosition()));
|
|
|
|
|
MusicPlayerRemote.openQueue(opener, 0, true);
|
|
|
|
|
|
|
|
|
|
QueueRepository queueRepository = new QueueRepository(App.getInstance());
|
|
|
|
|
queueRepository.insertAllAndStartNew(opener);
|
|
|
|
|
|
|
|
|
|
activity.isBottomSheetInPeek(true);
|
|
|
|
|
activity.setBottomSheetMusicInfo(songs.get(getBindingAdapterPosition()));
|
|
|
|
|
|
2021-07-27 14:52:37 +02:00
|
|
|
SongRepository songRepository = new SongRepository(App.getInstance());
|
|
|
|
|
songRepository.getInstantMix(songs.get(getBindingAdapterPosition()), 20, new MediaCallback() {
|
2021-04-18 20:23:09 +02:00
|
|
|
@Override
|
|
|
|
|
public void onError(Exception exception) {
|
|
|
|
|
Log.e(TAG, "onError: " + exception.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onLoadMedia(List<?> media) {
|
2021-07-28 18:29:26 +02:00
|
|
|
MusicPlayerRemote.enqueue((List<Song>) media);
|
2021-04-18 20:23:09 +02:00
|
|
|
}
|
2021-07-27 14:52:37 +02:00
|
|
|
});
|
2020-11-25 15:12:07 +01:00
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
}
|