2019-04-20 05:29:45 +00:00
|
|
|
package code.name.monkey.retromusic.adapter
|
2019-01-19 06:18:26 +00:00
|
|
|
|
|
|
|
import android.util.DisplayMetrics
|
2019-12-01 11:28:57 +00:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2019-01-19 06:18:26 +00:00
|
|
|
import androidx.annotation.IntDef
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2019-11-21 16:31:39 +00:00
|
|
|
import androidx.appcompat.widget.AppCompatTextView
|
2019-12-01 11:28:57 +00:00
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
2020-01-09 20:53:18 +00:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
2019-12-01 11:28:57 +00:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2019-01-19 06:18:26 +00:00
|
|
|
import code.name.monkey.retromusic.R
|
2019-04-20 05:29:45 +00:00
|
|
|
import code.name.monkey.retromusic.adapter.album.AlbumFullWidthAdapter
|
|
|
|
import code.name.monkey.retromusic.adapter.artist.ArtistAdapter
|
|
|
|
import code.name.monkey.retromusic.adapter.song.SongAdapter
|
2019-10-12 10:28:07 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.show
|
2019-05-15 21:01:20 +00:00
|
|
|
import code.name.monkey.retromusic.loaders.PlaylistSongsLoader
|
2019-12-01 11:28:57 +00:00
|
|
|
import code.name.monkey.retromusic.model.Album
|
|
|
|
import code.name.monkey.retromusic.model.Artist
|
|
|
|
import code.name.monkey.retromusic.model.Home
|
|
|
|
import code.name.monkey.retromusic.model.Playlist
|
2019-01-19 06:18:26 +00:00
|
|
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
|
|
|
|
2019-09-09 13:04:53 +00:00
|
|
|
class HomeAdapter(
|
2020-01-17 17:19:06 +00:00
|
|
|
private val activity: AppCompatActivity,
|
|
|
|
private val displayMetrics: DisplayMetrics
|
2019-09-09 13:04:53 +00:00
|
|
|
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
2019-01-19 06:18:26 +00:00
|
|
|
|
2020-02-17 11:20:08 +00:00
|
|
|
private var list = listOf<Home>()
|
2019-12-01 11:28:57 +00:00
|
|
|
|
|
|
|
override fun getItemViewType(position: Int): Int {
|
|
|
|
return list[position].homeSection
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
|
|
|
val layout = LayoutInflater.from(activity)
|
2020-01-09 20:53:18 +00:00
|
|
|
.inflate(R.layout.section_recycler_view, parent, false)
|
2019-12-01 11:28:57 +00:00
|
|
|
return when (viewType) {
|
|
|
|
RECENT_ARTISTS, TOP_ARTISTS -> ArtistViewHolder(layout)
|
|
|
|
PLAYLISTS -> PlaylistViewHolder(layout)
|
|
|
|
else -> {
|
2020-01-09 20:53:18 +00:00
|
|
|
AlbumViewHolder(
|
|
|
|
LayoutInflater.from(activity).inflate(
|
|
|
|
R.layout.metal_section_recycler_view,
|
|
|
|
parent,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
)
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
|
|
|
when (getItemViewType(position)) {
|
|
|
|
RECENT_ALBUMS -> {
|
|
|
|
val viewHolder = holder as AlbumViewHolder
|
2019-12-01 15:27:01 +00:00
|
|
|
viewHolder.bindView(list[position].arrayList.toAlbums(), R.string.recent_albums)
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
|
|
|
TOP_ALBUMS -> {
|
|
|
|
val viewHolder = holder as AlbumViewHolder
|
2019-12-01 15:27:01 +00:00
|
|
|
viewHolder.bindView(list[position].arrayList.toAlbums(), R.string.top_albums)
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
|
|
|
RECENT_ARTISTS -> {
|
|
|
|
val viewHolder = holder as ArtistViewHolder
|
2019-12-01 15:27:01 +00:00
|
|
|
viewHolder.bindView(list[position].arrayList.toArtists(), R.string.recent_artists)
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
|
|
|
TOP_ARTISTS -> {
|
|
|
|
val viewHolder = holder as ArtistViewHolder
|
2019-12-01 15:27:01 +00:00
|
|
|
viewHolder.bindView(list[position].arrayList.toArtists(), R.string.top_artists)
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
|
|
|
PLAYLISTS -> {
|
|
|
|
val viewHolder = holder as PlaylistViewHolder
|
2019-12-01 15:27:01 +00:00
|
|
|
viewHolder.bindView(list[position].arrayList.toPlaylist(), R.string.favorites)
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItemCount(): Int {
|
|
|
|
return list.size
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:20:08 +00:00
|
|
|
fun swapData(sections: List<Home>) {
|
2019-12-01 11:28:57 +00:00
|
|
|
list = sections
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
@IntDef(RECENT_ALBUMS, TOP_ALBUMS, RECENT_ARTISTS, TOP_ARTISTS, PLAYLISTS)
|
|
|
|
@Retention(AnnotationRetention.SOURCE)
|
|
|
|
annotation class HomeSection
|
|
|
|
|
|
|
|
const val RECENT_ALBUMS = 3
|
|
|
|
const val TOP_ALBUMS = 1
|
|
|
|
const val RECENT_ARTISTS = 2
|
|
|
|
const val TOP_ARTISTS = 0
|
|
|
|
const val PLAYLISTS = 4
|
|
|
|
}
|
|
|
|
|
|
|
|
private inner class AlbumViewHolder(view: View) : AbsHomeViewItem(view) {
|
2019-12-01 15:27:01 +00:00
|
|
|
fun bindView(list: ArrayList<Album>, titleRes: Int) {
|
2019-12-01 11:28:57 +00:00
|
|
|
if (list.isNotEmpty()) {
|
|
|
|
recyclerView.apply {
|
|
|
|
show()
|
|
|
|
adapter = AlbumFullWidthAdapter(activity, list, displayMetrics)
|
|
|
|
}
|
|
|
|
title.text = activity.getString(titleRes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inner class ArtistViewHolder(view: View) : AbsHomeViewItem(view) {
|
2019-12-01 15:27:01 +00:00
|
|
|
fun bindView(list: ArrayList<Artist>, titleRes: Int) {
|
2019-12-01 11:28:57 +00:00
|
|
|
if (list.isNotEmpty()) {
|
|
|
|
recyclerView.apply {
|
|
|
|
show()
|
2020-02-25 13:15:23 +00:00
|
|
|
layoutManager =
|
|
|
|
LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
|
2019-12-01 11:28:57 +00:00
|
|
|
val artistAdapter = ArtistAdapter(
|
2020-01-09 20:53:18 +00:00
|
|
|
activity,
|
|
|
|
list,
|
|
|
|
PreferenceUtil.getInstance(activity).getHomeGridStyle(activity),
|
|
|
|
null
|
2019-12-01 11:28:57 +00:00
|
|
|
)
|
|
|
|
adapter = artistAdapter
|
|
|
|
}
|
|
|
|
title.text = activity.getString(titleRes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private inner class PlaylistViewHolder(view: View) : AbsHomeViewItem(view) {
|
2019-12-01 15:27:01 +00:00
|
|
|
fun bindView(arrayList: ArrayList<Playlist>, titleRes: Int) {
|
2019-12-01 11:28:57 +00:00
|
|
|
if (arrayList.isNotEmpty()) {
|
|
|
|
val songs = PlaylistSongsLoader.getPlaylistSongList(activity, arrayList[0])
|
|
|
|
if (songs.isNotEmpty()) {
|
|
|
|
recyclerView.apply {
|
|
|
|
show()
|
2020-02-25 13:15:23 +00:00
|
|
|
val songAdapter =
|
|
|
|
SongAdapter(activity, songs, R.layout.item_album_card, null)
|
|
|
|
layoutManager =
|
|
|
|
GridLayoutManager(activity, 1, GridLayoutManager.HORIZONTAL, false)
|
2019-12-01 11:28:57 +00:00
|
|
|
adapter = songAdapter
|
|
|
|
}
|
|
|
|
title.text = activity.getString(titleRes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
open inner class AbsHomeViewItem(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
|
|
|
val recyclerView: RecyclerView = itemView.findViewById(R.id.recyclerView)
|
|
|
|
val title: AppCompatTextView = itemView.findViewById(R.id.title)
|
|
|
|
}
|
2019-10-24 20:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun <E> ArrayList<E>.toAlbums(): ArrayList<Album> {
|
2019-12-01 11:28:57 +00:00
|
|
|
val arrayList = ArrayList<Album>()
|
|
|
|
for (x in this) {
|
|
|
|
arrayList.add(x as Album)
|
|
|
|
}
|
2020-01-09 20:53:18 +00:00
|
|
|
return arrayList
|
2019-10-24 20:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun <E> ArrayList<E>.toArtists(): ArrayList<Artist> {
|
2019-12-01 11:28:57 +00:00
|
|
|
val arrayList = ArrayList<Artist>()
|
|
|
|
for (x in this) {
|
|
|
|
arrayList.add(x as Artist)
|
|
|
|
}
|
2020-01-09 20:53:18 +00:00
|
|
|
return arrayList
|
2019-10-24 20:08:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 08:11:08 +00:00
|
|
|
private fun <E> ArrayList<E>.toPlaylist(): ArrayList<Playlist> {
|
2019-12-01 11:28:57 +00:00
|
|
|
val arrayList = ArrayList<Playlist>()
|
|
|
|
for (x in this) {
|
|
|
|
arrayList.add(x as Playlist)
|
|
|
|
}
|
2020-01-09 20:53:18 +00:00
|
|
|
return arrayList
|
2019-10-26 08:11:08 +00:00
|
|
|
}
|
|
|
|
|