PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/adapter/HomeAdapter.kt

202 lines
7.1 KiB
Kotlin
Raw Normal View History

2019-04-20 05:29:45 +00:00
package code.name.monkey.retromusic.adapter
import android.util.DisplayMetrics
2019-12-01 11:28:57 +00:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
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
import androidx.recyclerview.widget.RecyclerView
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
import code.name.monkey.retromusic.util.PreferenceUtil
class HomeAdapter(
2019-12-01 11:28:57 +00:00
private val activity: AppCompatActivity, private val displayMetrics: DisplayMetrics
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
2019-12-01 11:28:57 +00:00
private var list = ArrayList<Home>()
override fun getItemViewType(position: Int): Int {
return list[position].homeSection
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val layout = LayoutInflater.from(activity)
.inflate(R.layout.section_recycler_view, parent, false)
return when (viewType) {
RECENT_ARTISTS, TOP_ARTISTS -> ArtistViewHolder(layout)
PLAYLISTS -> PlaylistViewHolder(layout)
else -> {
AlbumViewHolder(LayoutInflater.from(activity).inflate(R.layout.metal_section_recycler_view, parent, false))
}
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
println("ViewType ${getItemViewType(position)}")
when (getItemViewType(position)) {
RECENT_ALBUMS -> {
val viewHolder = holder as AlbumViewHolder
viewHolder.bindView(
list[position].arrayList.toAlbums(),
R.string.recent_albums,
R.string.recent_added_albums
)
}
TOP_ALBUMS -> {
val viewHolder = holder as AlbumViewHolder
viewHolder.bindView(
list[position].arrayList.toAlbums(),
R.string.top_albums,
R.string.most_played_albums
)
}
RECENT_ARTISTS -> {
val viewHolder = holder as ArtistViewHolder
viewHolder.bindView(
list[position].arrayList.toArtists(),
R.string.recent_artists,
R.string.recent_added_artists
)
}
TOP_ARTISTS -> {
val viewHolder = holder as ArtistViewHolder
viewHolder.bindView(
list[position].arrayList.toArtists(),
R.string.top_artists,
R.string.most_played_artists
)
}
PLAYLISTS -> {
val viewHolder = holder as PlaylistViewHolder
viewHolder.bindView(
list[position].arrayList.toPlaylist(),
R.string.favorites,
R.string.favorites_songs
)
}
}
}
override fun getItemCount(): Int {
return list.size
}
fun swapData(sections: ArrayList<Home>) {
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) {
fun bindView(list: ArrayList<Album>, titleRes: Int, subtitleRes: Int) {
if (list.isNotEmpty()) {
recyclerView.apply {
show()
adapter = AlbumFullWidthAdapter(activity, list, displayMetrics)
}
title.text = activity.getString(titleRes)
}
}
}
inner class ArtistViewHolder(view: View) : AbsHomeViewItem(view) {
fun bindView(list: ArrayList<Artist>, titleRes: Int, subtitleRes: Int) {
if (list.isNotEmpty()) {
recyclerView.apply {
show()
layoutManager = GridLayoutManager(
activity, 1, GridLayoutManager.HORIZONTAL, false
)
val artistAdapter = ArtistAdapter(
activity,
list,
PreferenceUtil.getInstance(activity).getHomeGridStyle(activity),
false,
null
)
adapter = artistAdapter
}
title.text = activity.getString(titleRes)
}
}
}
private inner class PlaylistViewHolder(view: View) : AbsHomeViewItem(view) {
fun bindView(arrayList: ArrayList<Playlist>, titleRes: Int, subtitleRes: Int) {
if (arrayList.isNotEmpty()) {
val songs = PlaylistSongsLoader.getPlaylistSongList(activity, arrayList[0])
if (songs.isNotEmpty()) {
recyclerView.apply {
show()
val songAdapter = SongAdapter(
activity, songs, R.layout.item_album_card, false, null
)
layoutManager = GridLayoutManager(
activity, 1, GridLayoutManager.HORIZONTAL, false
)
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)
}
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)
}
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)
}
return arrayList;
2019-10-26 08:11:08 +00:00
}