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;
|
2021-08-15 16:40:43 +02:00
|
|
|
import android.view.animation.AccelerateDecelerateInterpolator;
|
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 19:10:55 +01:00
|
|
|
import androidx.media3.session.MediaBrowser;
|
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;
|
2022-02-07 09:47:46 +01:00
|
|
|
import com.cappielloantonio.play.model.Media;
|
2021-07-27 14:52:37 +02:00
|
|
|
import com.cappielloantonio.play.repository.SongRepository;
|
2021-12-29 19:10:55 +01:00
|
|
|
import com.cappielloantonio.play.service.MediaManager;
|
2021-04-27 10:44:55 +02:00
|
|
|
import com.cappielloantonio.play.ui.activity.MainActivity;
|
2021-08-01 19:08:40 +02:00
|
|
|
import com.cappielloantonio.play.util.MusicUtil;
|
2021-12-29 19:10:55 +01:00
|
|
|
import com.google.common.util.concurrent.ListenableFuture;
|
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
|
|
|
|
2021-08-14 17:37:23 +02:00
|
|
|
private final LayoutInflater inflater;
|
|
|
|
|
private final Context context;
|
|
|
|
|
private final MainActivity activity;
|
2021-12-29 19:10:55 +01:00
|
|
|
private ListenableFuture<MediaBrowser> mediaBrowserListenableFuture;
|
2021-08-14 17:37:23 +02:00
|
|
|
|
2022-02-07 09:47:46 +01:00
|
|
|
private List<Media> songs;
|
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
|
|
|
}
|
|
|
|
|
|
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_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) {
|
2022-02-07 09:47:46 +01:00
|
|
|
Media song = songs.get(position);
|
2020-11-25 15:12:07 +01:00
|
|
|
|
2021-08-07 15:09:20 +02:00
|
|
|
holder.textTitle.setText(MusicUtil.getReadableString(song.getTitle()));
|
|
|
|
|
holder.textAlbum.setText(MusicUtil.getReadableString(song.getAlbumName()));
|
2020-11-26 16:05:58 +01:00
|
|
|
|
|
|
|
|
CustomGlideRequest.Builder
|
2022-02-07 17:30:33 +01:00
|
|
|
.from(context, song.getCoverArtId(), CustomGlideRequest.SONG_PIC, null)
|
2020-11-26 16:05:58 +01:00
|
|
|
.build()
|
|
|
|
|
.into(holder.cover);
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-15 16:40:43 +02:00
|
|
|
@Override
|
|
|
|
|
public void onViewAttachedToWindow(@NonNull ViewHolder holder) {
|
|
|
|
|
super.onViewAttachedToWindow(holder);
|
|
|
|
|
startAnimation(holder);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2022-02-07 09:47:46 +01:00
|
|
|
public void setItems(List<Media> songs) {
|
2021-04-27 11:01:02 +02:00
|
|
|
this.songs = songs;
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 19:10:55 +01:00
|
|
|
public void setMediaBrowserListenableFuture(ListenableFuture<MediaBrowser> mediaBrowserListenableFuture) {
|
|
|
|
|
this.mediaBrowserListenableFuture = mediaBrowserListenableFuture;
|
|
|
|
|
}
|
|
|
|
|
|
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-12-29 19:10:55 +01:00
|
|
|
MediaManager.startQueue(mediaBrowserListenableFuture, context, songs.get(getBindingAdapterPosition()));
|
2021-12-29 20:13:34 +01:00
|
|
|
activity.setBottomSheetInPeek(true);
|
2021-07-28 18:29:26 +02:00
|
|
|
|
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) {
|
2021-09-04 19:00:41 +02:00
|
|
|
Log.e(TAG, "onError() " + exception.getMessage());
|
2021-04-18 20:23:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onLoadMedia(List<?> media) {
|
2022-02-07 09:47:46 +01:00
|
|
|
MediaManager.enqueue(mediaBrowserListenableFuture, context, (List<Media>) media,false);
|
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
|
|
|
}
|
2021-08-15 16:40:43 +02:00
|
|
|
|
|
|
|
|
private void startAnimation(ViewHolder holder) {
|
|
|
|
|
holder.cover.animate()
|
|
|
|
|
.setDuration(20000)
|
|
|
|
|
.setStartDelay(10)
|
|
|
|
|
.setInterpolator(new AccelerateDecelerateInterpolator())
|
|
|
|
|
.scaleX(1.4f)
|
|
|
|
|
.scaleY(1.4f)
|
|
|
|
|
.start();
|
|
|
|
|
}
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|