204 lines
6.8 KiB
Kotlin
204 lines
6.8 KiB
Kotlin
/*
|
|
* Copyright (c) 2020 Hemanth Savarla.
|
|
*
|
|
* Licensed under the GNU General Public License v3
|
|
*
|
|
* This is free software: you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
*
|
|
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
* See the GNU General Public License for more details.
|
|
*
|
|
*/
|
|
package code.name.monkey.retromusic.adapter.album
|
|
|
|
import android.content.res.ColorStateList
|
|
import android.content.res.Resources
|
|
import android.view.LayoutInflater
|
|
import android.view.MenuItem
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import androidx.core.view.ViewCompat
|
|
import androidx.fragment.app.FragmentActivity
|
|
import code.name.monkey.retromusic.R
|
|
import code.name.monkey.retromusic.adapter.base.AbsMultiSelectAdapter
|
|
import code.name.monkey.retromusic.adapter.base.MediaEntryViewHolder
|
|
import code.name.monkey.retromusic.glide.AlbumGlideRequest
|
|
import code.name.monkey.retromusic.glide.RetroMusicColoredTarget
|
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
|
import code.name.monkey.retromusic.helper.SortOrder
|
|
import code.name.monkey.retromusic.helper.menu.SongsMenuHelper
|
|
import code.name.monkey.retromusic.interfaces.IAlbumClickListener
|
|
import code.name.monkey.retromusic.interfaces.ICabHolder
|
|
import code.name.monkey.retromusic.model.Album
|
|
import code.name.monkey.retromusic.model.Song
|
|
import code.name.monkey.retromusic.util.MusicUtil
|
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
|
import code.name.monkey.retromusic.util.color.MediaNotificationProcessor
|
|
import com.bumptech.glide.Glide
|
|
import me.zhanghai.android.fastscroll.PopupTextProvider
|
|
|
|
open class AlbumAdapter(
|
|
protected val activity: FragmentActivity,
|
|
var dataSet: List<Album>,
|
|
protected var itemLayoutRes: Int,
|
|
ICabHolder: ICabHolder?,
|
|
private val albumClickListener: IAlbumClickListener?
|
|
) : AbsMultiSelectAdapter<AlbumAdapter.ViewHolder, Album>(
|
|
activity,
|
|
ICabHolder,
|
|
R.menu.menu_media_selection
|
|
), PopupTextProvider {
|
|
|
|
init {
|
|
this.setHasStableIds(true)
|
|
}
|
|
|
|
fun swapDataSet(dataSet: List<Album>) {
|
|
this.dataSet = dataSet
|
|
notifyDataSetChanged()
|
|
}
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
|
val view =
|
|
try {
|
|
LayoutInflater.from(activity).inflate(itemLayoutRes, parent, false)
|
|
} catch (e: Resources.NotFoundException) {
|
|
LayoutInflater.from(activity).inflate(R.layout.item_grid, parent, false)
|
|
}
|
|
return createViewHolder(view, viewType)
|
|
}
|
|
|
|
protected open fun createViewHolder(view: View, viewType: Int): ViewHolder {
|
|
return ViewHolder(view)
|
|
}
|
|
|
|
private fun getAlbumTitle(album: Album): String? {
|
|
return album.title
|
|
}
|
|
|
|
protected open fun getAlbumText(album: Album): String? {
|
|
return album.artistName
|
|
}
|
|
|
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
|
val album = dataSet[position]
|
|
val isChecked = isChecked(album)
|
|
holder.itemView.isActivated = isChecked
|
|
holder.title?.text = getAlbumTitle(album)
|
|
holder.text?.text = getAlbumText(album)
|
|
holder.playSongs?.setOnClickListener {
|
|
album.songs.let { songs ->
|
|
MusicPlayerRemote.openQueue(
|
|
songs,
|
|
0,
|
|
true
|
|
)
|
|
}
|
|
}
|
|
loadAlbumCover(album, holder)
|
|
}
|
|
|
|
protected open fun setColors(color: MediaNotificationProcessor, holder: ViewHolder) {
|
|
if (holder.paletteColorContainer != null) {
|
|
holder.title?.setTextColor(color.primaryTextColor)
|
|
holder.text?.setTextColor(color.secondaryTextColor)
|
|
holder.paletteColorContainer?.setBackgroundColor(color.backgroundColor)
|
|
}
|
|
holder.mask?.backgroundTintList = ColorStateList.valueOf(color.primaryTextColor)
|
|
}
|
|
|
|
protected open fun loadAlbumCover(album: Album, holder: ViewHolder) {
|
|
if (holder.image == null) {
|
|
return
|
|
}
|
|
|
|
AlbumGlideRequest.Builder.from(Glide.with(activity), album.safeGetFirstSong())
|
|
.checkIgnoreMediaStore()
|
|
.generatePalette(activity)
|
|
.build()
|
|
.into(object : RetroMusicColoredTarget(holder.image!!) {
|
|
override fun onColorReady(colors: MediaNotificationProcessor) {
|
|
setColors(colors, holder)
|
|
}
|
|
})
|
|
}
|
|
|
|
override fun getItemCount(): Int {
|
|
return dataSet.size
|
|
}
|
|
|
|
override fun getItemId(position: Int): Long {
|
|
return dataSet[position].id
|
|
}
|
|
|
|
override fun getIdentifier(position: Int): Album? {
|
|
return dataSet[position]
|
|
}
|
|
|
|
override fun getName(album: Album): String {
|
|
return album.title!!
|
|
}
|
|
|
|
override fun onMultipleItemAction(
|
|
menuItem: MenuItem,
|
|
selection: List<Album>
|
|
) {
|
|
SongsMenuHelper.handleMenuClick(activity, getSongList(selection), menuItem.itemId)
|
|
}
|
|
|
|
private fun getSongList(albums: List<Album>): List<Song> {
|
|
val songs = ArrayList<Song>()
|
|
for (album in albums) {
|
|
songs.addAll(album.songs)
|
|
}
|
|
return songs
|
|
}
|
|
|
|
override fun getPopupText(position: Int): String {
|
|
return getSectionName(position)
|
|
}
|
|
|
|
private fun getSectionName(position: Int): String {
|
|
var sectionName: String? = null
|
|
when (PreferenceUtil.albumSortOrder) {
|
|
SortOrder.AlbumSortOrder.ALBUM_A_Z, SortOrder.AlbumSortOrder.ALBUM_Z_A -> sectionName =
|
|
dataSet[position].title
|
|
SortOrder.AlbumSortOrder.ALBUM_ARTIST -> sectionName = dataSet[position].artistName
|
|
SortOrder.AlbumSortOrder.ALBUM_YEAR -> return MusicUtil.getYearString(
|
|
dataSet[position].year
|
|
)
|
|
}
|
|
return MusicUtil.getSectionName(sectionName)
|
|
}
|
|
|
|
inner class ViewHolder(itemView: View) : MediaEntryViewHolder(itemView) {
|
|
|
|
init {
|
|
setImageTransitionName("Album")
|
|
menu?.visibility = View.GONE
|
|
}
|
|
|
|
override fun onClick(v: View?) {
|
|
super.onClick(v)
|
|
if (isInQuickSelectMode) {
|
|
toggleChecked(layoutPosition)
|
|
} else {
|
|
ViewCompat.setTransitionName(itemView, "album")
|
|
albumClickListener?.onAlbumClick(dataSet[layoutPosition].id, itemView)
|
|
}
|
|
}
|
|
|
|
override fun onLongClick(v: View?): Boolean {
|
|
toggleChecked(layoutPosition)
|
|
return super.onLongClick(v)
|
|
}
|
|
}
|
|
|
|
companion object {
|
|
val TAG: String = AlbumAdapter::class.java.simpleName
|
|
}
|
|
}
|