prepare the AlbumsAdapter for showing individual songs too

This commit is contained in:
tibbi 2020-10-05 11:30:25 +02:00
parent a2cc181ee5
commit 26a87b52dd
5 changed files with 18 additions and 11 deletions

View file

@ -12,6 +12,7 @@ import com.simplemobiletools.musicplayer.helpers.ALBUM
import com.simplemobiletools.musicplayer.helpers.ARTIST
import com.simplemobiletools.musicplayer.models.Album
import com.simplemobiletools.musicplayer.models.Artist
import com.simplemobiletools.musicplayer.models.ListItem
import com.simplemobiletools.musicplayer.models.Song
import kotlinx.android.synthetic.main.activity_albums.*
@ -26,8 +27,9 @@ class AlbumsActivity : SimpleActivity() {
title = artist.title
getAlbums(artist) { albums ->
val items = albums.toMutableList() as ArrayList<ListItem>
runOnUiThread {
AlbumsAdapter(this, albums, albums_list) {
AlbumsAdapter(this, items, albums_list) {
Intent(this, SongsActivity::class.java).apply {
putExtra(ALBUM, Gson().toJson(it as Album))
startActivity(this)

View file

@ -14,10 +14,12 @@ import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.activities.SimpleActivity
import com.simplemobiletools.musicplayer.models.Album
import com.simplemobiletools.musicplayer.models.ListItem
import kotlinx.android.synthetic.main.item_album.view.*
import java.util.*
class AlbumsAdapter(activity: SimpleActivity, val albums: ArrayList<Album>, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) :
// we show both albums and individual songs here
class AlbumsAdapter(activity: SimpleActivity, val items: ArrayList<ListItem>, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) :
MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
init {
@ -29,32 +31,32 @@ class AlbumsAdapter(activity: SimpleActivity, val albums: ArrayList<Album>, recy
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_album, parent)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val album = albums.getOrNull(position) ?: return
val album = items.getOrNull(position) ?: return
holder.bindView(album, true, true) { itemView, layoutPosition ->
setupView(itemView, album)
setupView(itemView, album as Album)
}
bindViewHolder(holder)
}
override fun getItemCount() = albums.size
override fun getItemCount() = items.size
override fun prepareActionMode(menu: Menu) {}
override fun actionItemPressed(id: Int) {}
override fun getSelectableItemCount() = albums.size
override fun getSelectableItemCount() = items.size
override fun getIsItemSelectable(position: Int) = true
override fun getItemSelectionKey(position: Int) = albums.getOrNull(position)?.id
override fun getItemSelectionKey(position: Int) = (items.getOrNull(position) as? Album)?.id
override fun getItemKeyPosition(key: Int) = albums.indexOfFirst { it.id == key }
override fun getItemKeyPosition(key: Int) = items.indexOfFirst { (it as Album).id == key }
override fun onActionModeCreated() {}
override fun onActionModeDestroyed() {}
private fun getItemWithKey(key: Int): Album? = albums.firstOrNull { it.id == key }
private fun getItemWithKey(key: Int): ListItem? = items.firstOrNull { (it as Album).id == key }
private fun setupView(view: View, album: Album) {
view.apply {

View file

@ -1,3 +1,3 @@
package com.simplemobiletools.musicplayer.models
data class Album(val id: Int, val artist: String, val title: String)
data class Album(val id: Int, val artist: String, val title: String): ListItem()

View file

@ -0,0 +1,3 @@
package com.simplemobiletools.musicplayer.models
open class ListItem

View file

@ -22,7 +22,7 @@ data class Song(
@ColumnInfo(name = "duration") val duration: Int,
@ColumnInfo(name = "album") val album: String,
@ColumnInfo(name = "playlist_id") val playListId: Int,
@ColumnInfo(name = "track_id") val trackId: Int) : Serializable, Comparable<Song> {
@ColumnInfo(name = "track_id") val trackId: Int) : Serializable, Comparable<Song>, ListItem() {
companion object {
private const val serialVersionUID = 6717978793256852245L