2020-11-20 15:38:08 +01:00
|
|
|
package com.cappielloantonio.play.adapter;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2020-11-24 10:52:00 +01:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2020-11-20 15:38:08 +01:00
|
|
|
import androidx.viewpager.widget.PagerAdapter;
|
|
|
|
|
|
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;
|
|
|
|
|
import com.cappielloantonio.play.model.Song;
|
2020-11-22 19:11:38 +01:00
|
|
|
import com.cappielloantonio.play.repository.SongRepository;
|
2020-11-24 10:52:00 +01:00
|
|
|
import com.cappielloantonio.play.util.Util;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class DiscoverSongAdapter extends PagerAdapter {
|
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;
|
|
|
|
|
private LayoutInflater layoutInflater;
|
|
|
|
|
private Context context;
|
2020-11-22 19:11:38 +01:00
|
|
|
private View view;
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2020-11-22 19:11:38 +01:00
|
|
|
public DiscoverSongAdapter(Context context, List<Song> songs) {
|
2020-11-20 15:38:08 +01:00
|
|
|
this.context = context;
|
2020-11-22 19:11:38 +01:00
|
|
|
this.songs = songs;
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getCount() {
|
|
|
|
|
return songs.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
|
|
|
|
|
return view.equals(object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
|
@Override
|
|
|
|
|
public Object instantiateItem(@NonNull ViewGroup container, final int position) {
|
|
|
|
|
layoutInflater = LayoutInflater.from(context);
|
2020-11-22 19:11:38 +01:00
|
|
|
view = layoutInflater.inflate(R.layout.item_home_discover_song, container, false);
|
2020-11-20 15:38:08 +01:00
|
|
|
|
2020-11-22 19:11:38 +01:00
|
|
|
TextView title = view.findViewById(R.id.title_discover_song_label);
|
2020-11-24 10:52:00 +01:00
|
|
|
TextView desc = view.findViewById(R.id.album_discover_song_label);
|
2020-11-20 15:38:08 +01:00
|
|
|
title.setText(songs.get(position).getTitle());
|
|
|
|
|
desc.setText(songs.get(position).getAlbumName());
|
|
|
|
|
|
2020-11-22 19:11:38 +01:00
|
|
|
view.setOnClickListener(v -> {
|
|
|
|
|
SongRepository songRepository = new SongRepository(App.getInstance());
|
|
|
|
|
songRepository.update(songs.get(position));
|
|
|
|
|
});
|
2020-11-20 15:38:08 +01:00
|
|
|
|
|
|
|
|
container.addView(view, 0);
|
|
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-21 18:41:35 +01:00
|
|
|
public void setItems(List<Song> songs) {
|
|
|
|
|
this.songs = songs;
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-20 15:38:08 +01:00
|
|
|
@Override
|
|
|
|
|
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
|
2020-11-22 19:11:38 +01:00
|
|
|
container.removeView((View) object);
|
2020-11-20 15:38:08 +01:00
|
|
|
}
|
|
|
|
|
}
|