优化new playlist 代码逻辑

This commit is contained in:
ocean 2024-06-28 17:59:17 +08:00
parent fe89030ad0
commit 0487ed7713
7 changed files with 477 additions and 576 deletions

View File

@ -34,11 +34,14 @@ import melody.offline.music.service.MyDownloadService
import melody.offline.music.service.ViewModelMain import melody.offline.music.service.ViewModelMain
import melody.offline.music.util.AnalysisUtil import melody.offline.music.util.AnalysisUtil
import melody.offline.music.util.DownloadUtil import melody.offline.music.util.DownloadUtil
import melody.offline.music.util.FileSizeConverter
import melody.offline.music.util.LogTag import melody.offline.music.util.LogTag
import melody.offline.music.view.ListMoreBottomSheetDialog
import org.json.JSONObject import org.json.JSONObject
@OptIn(UnstableApi::class) @OptIn(UnstableApi::class)
class MoPlaylistSongsActivity : MoBaseActivity() { class MoPlaylistSongsActivity : MoBaseActivity(), ListMoreBottomSheetDialog.ListMoreViewListener,
ListMoreBottomSheetDialog.UpdateAdapterListener {
companion object { companion object {
const val PLAYLIST_ID = "playlist_id" const val PLAYLIST_ID = "playlist_id"
const val PLAYLIST_TITLE = "playlist_title" const val PLAYLIST_TITLE = "playlist_title"
@ -58,8 +61,8 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
val bean: PlaylistItem val bean: PlaylistItem
) : Request() ) : Request()
data class OnDownloadRemove(val id: String) : Request() data class OnDownloadRemove(val playlistItem: PlaylistItem) : Request()
data class OnUpdateDownloadUi(val id: String) : Request() data class OnUpdateDownloadUi(val playlistItem: PlaylistItem) : Request()
data class OnRemovePlaylist( data class OnRemovePlaylist(
val bean: PlaylistItem val bean: PlaylistItem
) : Request() ) : Request()
@ -69,10 +72,9 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
private lateinit var binding: ActivityPlaylistSongsBinding private lateinit var binding: ActivityPlaylistSongsBinding
private var adapter: PlaylistSongsAdapter? = null private var adapter: PlaylistSongsAdapter? = null
private var playlistItems: MutableList<PlaylistItem> = mutableListOf() private var playlistItems: MutableList<PlaylistItem> = mutableListOf()
private var currentPosition = -1
private var playlistId = -1 private var playlistId = -1
private var playlistTitle = "" private var playlistTitle = ""
private var isAnimationRunning = false//动画是否正在进行 private var moreDialog: ListMoreBottomSheetDialog? = null
override suspend fun main() { override suspend fun main() {
binding = ActivityPlaylistSongsBinding.inflate(layoutInflater) binding = ActivityPlaylistSongsBinding.inflate(layoutInflater)
@ -84,7 +86,6 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
initView() initView()
initAdapter() initAdapter()
LolAdWrapper.shared.loadAdIfNotCached(this, AdPlacement.INST_ME_PAGE_LIST) LolAdWrapper.shared.loadAdIfNotCached(this, AdPlacement.INST_ME_PAGE_LIST)
initDownloadFlow()
initData() initData()
onReceive() onReceive()
} }
@ -127,14 +128,12 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
) )
) )
val currentFavoriteBean = val currentFavoriteBean =
App.appFavoriteDBManager.getFavoriteBeanByID(it.bean.videoId!!) App.appFavoriteDBManager.getFavoriteBeanByID(it.bean.videoId)
if (currentFavoriteBean != null) { if (currentFavoriteBean != null) {
currentFavoriteBean.isFavorite = !currentFavoriteBean.isFavorite currentFavoriteBean.isFavorite = !currentFavoriteBean.isFavorite
App.appFavoriteDBManager.updateFavoriteBean(currentFavoriteBean) App.appFavoriteDBManager.updateFavoriteBean(currentFavoriteBean)
if (currentFavoriteBean.isFavorite) { if (currentFavoriteBean.isFavorite) {
AnalysisUtil.logEvent( AnalysisUtil.logEvent(AnalysisUtil.PLAYER_B_LOVE_CLICK, songMap)
AnalysisUtil.PLAYER_B_LOVE_CLICK, songMap
)
} else { } else {
AnalysisUtil.logEvent( AnalysisUtil.logEvent(
AnalysisUtil.PLAYER_B_UN_LOVE_CLICK, songMap AnalysisUtil.PLAYER_B_UN_LOVE_CLICK, songMap
@ -156,14 +155,13 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
is Request.OnDownload -> { is Request.OnDownload -> {
val id = it.bean.videoId val id = it.bean.videoId
val offBean = App.appOfflineDBManager.getOfflineBeanByID(id) val offBean =
App.appOfflineDBManager.getOfflineBeanByID(id)//得到当前ID的本地数据
if (offBean != null && offBean.bytesDownloaded?.let { bytes -> bytes > 0 } == true) {//判断当前数据库是否有这条数据。 if (offBean != null && offBean.bytesDownloaded?.let { bytes -> bytes > 0 } == true) {//判断当前数据库是否有这条数据。
if (currentPosition >= 0) { showRemoveDownloadDialogHint(it.bean)
showRemoveDownloadDialogHint(id)
}
} else { } else {
val isFavorite = val isFavorite =
App.appFavoriteDBManager.getFavoriteBeanByID(it.bean.videoId.toString()) != null App.appFavoriteDBManager.getFavoriteBeanByID(it.bean.videoId)
//判断是否已经下载了这条数据,已经下载,就直接进行数据库数据存储,反之走下载流程。 //判断是否已经下载了这条数据,已经下载,就直接进行数据库数据存储,反之走下载流程。
if (DownloadUtil.downloadResourceExist(id)) { if (DownloadUtil.downloadResourceExist(id)) {
val favoriteBean = FavoriteBean( val favoriteBean = FavoriteBean(
@ -171,10 +169,11 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
it.bean.title, it.bean.title,
it.bean.name, it.bean.name,
it.bean.thumbnail, it.bean.thumbnail,
isFavorite isFavorite?.isFavorite ?: false
) )
insertOfflineData(favoriteBean) insertOfflineData(favoriteBean)
requests.trySend(Request.OnUpdateDownloadUi(id)) it.bean.isOffline = true//更改状态
requests.trySend(Request.OnUpdateDownloadUi(it.bean))
} else { } else {
val downloadRequest = DownloadRequest.Builder(id, id.toUri()) val downloadRequest = DownloadRequest.Builder(id, id.toUri())
.setCustomCacheKey(id).build() .setCustomCacheKey(id).build()
@ -202,7 +201,7 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
it.bean.title, it.bean.title,
it.bean.name, it.bean.name,
it.bean.thumbnail, it.bean.thumbnail,
isFavorite isFavorite?.isFavorite ?: false
) )
insertOfflineData(favoriteBean) insertOfflineData(favoriteBean)
val jsonObject = JSONObject() val jsonObject = JSONObject()
@ -228,34 +227,29 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
is Request.OnDownloadRemove -> { is Request.OnDownloadRemove -> {
val currentOfflineBean = val currentOfflineBean =
App.appOfflineDBManager.getOfflineBeanByID(it.id) App.appOfflineDBManager.getOfflineBeanByID(it.playlistItem.videoId)
if (currentOfflineBean != null) { if (currentOfflineBean != null) {
App.appOfflineDBManager.deleteOfflineBean(currentOfflineBean) App.appOfflineDBManager.deleteOfflineBean(currentOfflineBean)
it.playlistItem.isOffline = false
} }
requests.trySend(Request.OnUpdateDownloadUi(it.id)) requests.trySend(Request.OnUpdateDownloadUi(it.playlistItem))
} }
is Request.OnUpdateDownloadUi -> { is Request.OnUpdateDownloadUi -> {
val offBean = App.appOfflineDBManager.getOfflineBeanByID(it.id) moreDialog?.updateDownloadBtnUi(it.playlistItem.isOffline)//更新对话框的ui
if (offBean != null && offBean.bytesDownloaded?.let { bytes -> bytes > 0 } == true) { runOnUiThread {
binding.downloadLoading.visibility = View.GONE adapter?.notifyDataSetChanged()//更新adapter
binding.downloadImg.visibility = View.VISIBLE
binding.downloadImg.setImageResource(R.drawable.more_downloaded_icon)
binding.downloadTv.text =
getString(R.string.download_remove_offline)
} else {
binding.downloadLoading.visibility = View.GONE
binding.downloadImg.visibility = View.VISIBLE
binding.downloadImg.setImageResource(R.drawable.more_download_icon)
binding.downloadTv.text = getString(R.string.download_save_offline)
} }
} }
is Request.OnRemovePlaylist -> { is Request.OnRemovePlaylist -> {
playlistItems.remove(it.bean)
adapter?.notifyDataSetChanged()
App.appPlaylistDBManager.deletePlaylistItem(it.bean) App.appPlaylistDBManager.deletePlaylistItem(it.bean)
playlistItems.remove(it.bean)
runOnUiThread {
adapter?.notifyDataSetChanged()//更新adapter
}
} }
} }
} }
@ -302,46 +296,37 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
private fun initView() { private fun initView() {
binding.titleText.text = playlistTitle binding.titleText.text = playlistTitle
binding.layoutInfo.setOnClickListener { }
binding.backBtn.setOnClickListener { binding.backBtn.setOnClickListener {
finish() finish()
} }
binding.tryAgainBtn.setOnClickListener { binding.tryAgainBtn.setOnClickListener {
requests.trySend(Request.TryAgain) requests.trySend(Request.TryAgain)
} }
binding.bottomCloseBtn.setOnClickListener {
hideBottomLayout()
} }
binding.bottomBlankLayout.setOnClickListener {
hideBottomLayout()
override fun onFavoritesClicked(playlistItem: PlaylistItem) {
requests.trySend(Request.OnFavorites(playlistItem))
} }
binding.favoritesBtn.setOnClickListener {
if (currentPosition >= 0) { override fun onDownloadClicked(playlistItem: PlaylistItem) {
requests.trySend(Request.OnDownload(playlistItem))
}
override fun onAddToPlaylistClicked(playlistItem: PlaylistItem) {
requests.trySend( requests.trySend(
Request.OnFavorites( Request.OnRemovePlaylist(playlistItem)
playlistItems[currentPosition]
)
) )
} }
}
binding.moreDownloadBtn.setOnClickListener { @SuppressLint("NotifyDataSetChanged")
if (currentPosition >= 0) { override fun onUpdateAdapterListener(download: Download, playlistItem: PlaylistItem) {
requests.trySend( if (playlistItem.videoId == download.request.id) {
Request.OnDownload( playlistItem.isOffline = true
playlistItems[currentPosition] playlistItem.bytesDownloaded = download.bytesDownloaded
) playlistItem.size = FileSizeConverter(download.bytesDownloaded).formattedSize()
) //更新adapter
} adapter?.notifyDataSetChanged()
}
binding.moreAddPlaylistBtn.setOnClickListener {
if (currentPosition >= 0) {
requests.trySend(
Request.OnRemovePlaylist(
playlistItems[currentPosition]
)
)
hideBottomLayout()
}
} }
} }
@ -349,10 +334,15 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
adapter = PlaylistSongsAdapter(this, playlistItems) adapter = PlaylistSongsAdapter(this, playlistItems)
adapter?.setOnMoreClickListener(object : PlaylistSongsAdapter.OnMoreClickListener { adapter?.setOnMoreClickListener(object : PlaylistSongsAdapter.OnMoreClickListener {
override fun onMoreClick(position: Int) { override fun onMoreClick(position: Int) {
currentPosition = position moreDialog =
if (!isAnimationRunning) { ListMoreBottomSheetDialog(
toggleBottomLayout(position) this@MoPlaylistSongsActivity,
} playlistItems[position],
this@MoPlaylistSongsActivity,
this@MoPlaylistSongsActivity
)
moreDialog?.setListMoreViewListener(this@MoPlaylistSongsActivity)
moreDialog?.show()
} }
}) })
binding.rv.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) binding.rv.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
@ -362,8 +352,28 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")
private suspend fun initData() { private suspend fun initData() {
showLoadingUi() showLoadingUi()
//根据list id获取所有的item数据
val items = App.appPlaylistDBManager.getPlaylistItems(playlistId)
App.appFavoriteDBManager.getAllFavoriteBeans().map {
LogTag.LogD(TAG, "getAllFavoriteBeans->$it")
}
//更新收藏与喜欢
val updatedPlaylistItems = items.map { item ->
val offlineBean = App.appOfflineDBManager.getOfflineBeanByID(item.videoId)
val favoriteBean = App.appFavoriteDBManager.getFavoriteBeanByID(item.videoId)
item.copy(
isOffline = offlineBean?.isOffline ?: false,
isFavorite = favoriteBean?.isFavorite ?: false,
bytesDownloaded = offlineBean?.bytesDownloaded,
size = offlineBean?.size
)
}
App.appPlaylistDBManager.updatePlaylistItems(updatedPlaylistItems)
playlistItems.clear() playlistItems.clear()
playlistItems.addAll(App.appPlaylistDBManager.getPlaylistItems(playlistId)) playlistItems.addAll(updatedPlaylistItems)
if (playlistItems.size > 0) { if (playlistItems.size > 0) {
showDataUi() showDataUi()
} else { } else {
@ -374,7 +384,7 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
} }
} }
private fun showRemoveDownloadDialogHint(id: String) { private fun showRemoveDownloadDialogHint(playlistItem: PlaylistItem) {
val inflater = LayoutInflater.from(this) val inflater = LayoutInflater.from(this)
val dialogView = inflater.inflate(R.layout.dialog_hint, null) val dialogView = inflater.inflate(R.layout.dialog_hint, null)
val okBtn = dialogView.findViewById<TextView>(R.id.dialog_ok_btn) val okBtn = dialogView.findViewById<TextView>(R.id.dialog_ok_btn)
@ -385,181 +395,19 @@ class MoPlaylistSongsActivity : MoBaseActivity() {
dialog.show() dialog.show()
okBtn.setOnClickListener { okBtn.setOnClickListener {
dialog.dismiss() dialog.dismiss()
requests.trySend(Request.OnDownloadRemove(id)) requests.trySend(Request.OnDownloadRemove(playlistItem))
} }
cancelBtn.setOnClickListener { cancelBtn.setOnClickListener {
dialog.dismiss() dialog.dismiss()
} }
} }
private fun initDownloadFlow() {
ViewModelMain.modelDownloadsFlow.observe(this) { downloads ->
if (currentPosition >= 0) {
val id = playlistItems[currentPosition].videoId
val currentScreenDownloads = downloads[id]
if (currentScreenDownloads != null) {
updateDownloadUI(currentScreenDownloads)
}
}
}
}
private fun updateDownloadUI(download: Download) {
LogTag.LogD(TAG, "download.state->${download.state}")
when (download.state) {
Download.STATE_DOWNLOADING -> {
binding.downloadLoading.visibility = View.VISIBLE
binding.downloadImg.setImageResource(R.drawable.more_download_icon)
binding.downloadImg.visibility = View.GONE
binding.downloadTv.text = getString(R.string.download_saving)
binding.moreDownloadBtn.isClickable = false
binding.moreDownloadBtn.isEnabled = false
}
Download.STATE_COMPLETED -> {
binding.downloadLoading.visibility = View.GONE
binding.downloadImg.setImageResource(R.drawable.more_downloaded_icon)
binding.downloadImg.visibility = View.VISIBLE
binding.downloadTv.text = getString(R.string.download_remove_offline)
binding.moreDownloadBtn.isClickable = true
binding.moreDownloadBtn.isEnabled = true
val jsonObject = JSONObject()
jsonObject.put(
"download_id", download.request.id
)
val songMap = mutableMapOf(
Pair(
AnalysisUtil.PARAM_VALUE, jsonObject.toString()
)
)
AnalysisUtil.logEvent(AnalysisUtil.PLAYER_B_DOWNLOAD_SUCCESS_ACTION, songMap)
}
Download.STATE_FAILED -> {
binding.downloadLoading.visibility = View.GONE
binding.downloadImg.setImageResource(R.drawable.error)
binding.downloadImg.visibility = View.VISIBLE
binding.downloadTv.text = getString(R.string.download_save_offline)
binding.moreDownloadBtn.isClickable = true
binding.moreDownloadBtn.isEnabled = true
}
else -> {
binding.downloadLoading.visibility = View.GONE
binding.downloadImg.setImageResource(R.drawable.more_download_icon)
binding.downloadImg.visibility = View.VISIBLE
binding.downloadTv.text = getString(R.string.download_save_offline)
binding.moreDownloadBtn.isClickable = true
binding.moreDownloadBtn.isEnabled = true
}
}
}
private fun updateFavoriteUi(b: Boolean) { private fun updateFavoriteUi(b: Boolean) {
if (b) { if (moreDialog != null) {
binding.favoritesImg.setImageResource(R.drawable.favorited_icon) moreDialog?.updateFavoriteUi(b)
} else {
binding.favoritesImg.setImageResource(R.drawable.not_favorited_icon)
} }
} }
private fun toggleBottomLayout(position: Int) {
if (binding.bottomLayout.visibility == View.VISIBLE) {
hideBottomLayout()
} else {
showBottomLayout(position)
}
}
private fun showBottomLayout(position: Int) {
val slideUpAnimation = AnimationUtils.loadAnimation(this, R.anim.slide_up)
slideUpAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation) {
isAnimationRunning = true
// 动画开始时禁用按钮
animationLoadingNotClick(false)
}
override fun onAnimationEnd(animation: Animation) {
isAnimationRunning = false
// 动画结束时启用按钮
animationLoadingNotClick(true)
}
override fun onAnimationRepeat(animation: Animation) {
// 可选:处理动画重复的情况
}
})
binding.bottomLayout.startAnimation(slideUpAnimation)
binding.bottomLayout.visibility = View.VISIBLE
val bean = playlistItems[position]
Glide.with(this).load(bean.thumbnail).into(binding.songImg)
binding.title.text = bean.title
if (bean.name.isEmpty()) {
binding.name.visibility = View.GONE
} else {
binding.name.visibility = View.VISIBLE
binding.name.text = bean.name
}
requests.trySend(Request.UpdateFavorite(bean))
val currentDownload = DownloadUtil.getCurrentIdDownload(bean.videoId)
if (currentDownload != null) {
//是否下载过资源下载过资源就不走download的ui更新因为 它肯定状态为3
if (DownloadUtil.downloadResourceExist(bean.videoId)) {
requests.trySend(Request.OnUpdateDownloadUi(bean.videoId))
} else {
updateDownloadUI(currentDownload)
}
} else {
requests.trySend(Request.OnUpdateDownloadUi(bean.videoId))
}
}
private fun hideBottomLayout() {
val slideDownAnimation = AnimationUtils.loadAnimation(this, R.anim.slide_down)
slideDownAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation?) {
isAnimationRunning = true
animationLoadingNotClick(false)
}
override fun onAnimationEnd(animation: Animation?) {
isAnimationRunning = false
animationLoadingNotClick(true)
}
override fun onAnimationRepeat(animation: Animation?) {
}
})
binding.bottomLayout.startAnimation(slideDownAnimation)
binding.bottomLayout.visibility = View.GONE
//隐藏布局后则,解除点击限制
binding.moreDownloadBtn.isClickable = true
binding.moreDownloadBtn.isEnabled = true
}
private fun animationLoadingNotClick(b: Boolean) {
binding.layoutInfo.isClickable = b
binding.bottomCloseBtn.isClickable = b
binding.bottomBlankLayout.isClickable = b
binding.moreDownloadBtn.isClickable = b
binding.moreAddPlaylistBtn.isClickable = b
binding.favoritesBtn.isClickable = b
}
private fun showDataUi() { private fun showDataUi() {
binding.loadingLayout.visibility = View.GONE binding.loadingLayout.visibility = View.GONE
binding.noContentLayout.visibility = View.GONE binding.noContentLayout.visibility = View.GONE

View File

@ -72,17 +72,15 @@ class PlaylistSongsAdapter(
} }
if (bean.size?.isNotEmpty() == true) { if (bean.size?.isNotEmpty() == true) {
size.text = bean.size size.text = bean.size
size.visibility = View.VISIBLE
} else {
size.visibility = View.GONE
} }
if (bean.isOffline) { if (bean.isOffline) {
size.visibility = View.VISIBLE
downloadCoImg.visibility = View.VISIBLE downloadCoImg.visibility = View.VISIBLE
downloadImg.setImageResource(R.drawable.download_done_icon) downloadImg.setImageResource(R.drawable.download_done_icon)
} else { } else {
downloadCoImg.visibility = View.GONE downloadCoImg.visibility = View.GONE
downloadImg.setImageResource(R.drawable.download_icon) downloadImg.setImageResource(R.drawable.download_icon)
size.visibility = View.GONE
} }
} }
} }

View File

@ -49,6 +49,12 @@ class AppPlaylistDBManager private constructor(context: Context) {
} }
} }
suspend fun updatePlaylistItems(playlistItems: List<PlaylistItem>){
return withContext(Dispatchers.IO){
playlistDao.updatePlaylistItems(playlistItems)
}
}
suspend fun getPlaylistByTitle(title: String): Playlist? { suspend fun getPlaylistByTitle(title: String): Playlist? {
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
playlistDao.getPlaylistByTitle(title) playlistDao.getPlaylistByTitle(title)
@ -66,7 +72,6 @@ class AppPlaylistDBManager private constructor(context: Context) {
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
playlistDao.getPlaylistItems(playlistId) playlistDao.getPlaylistItems(playlistId)
} }
} }
suspend fun deletePlaylist(playlist: Playlist) { suspend fun deletePlaylist(playlist: Playlist) {

View File

@ -33,6 +33,9 @@ interface PlaylistDao {
@Update @Update
suspend fun updatePlaylistItem(playlistItem: PlaylistItem) suspend fun updatePlaylistItem(playlistItem: PlaylistItem)
@Update
suspend fun updatePlaylistItems(playlistItems: List<PlaylistItem>)
@Delete @Delete
suspend fun deletePlaylist(playlist: Playlist) suspend fun deletePlaylist(playlist: Playlist)

View File

@ -0,0 +1,235 @@
package melody.offline.music.view
import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.annotation.OptIn
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.offline.Download
import com.bumptech.glide.Glide
import com.google.android.material.bottomsheet.BottomSheetDialog
import melody.offline.music.App
import melody.offline.music.R
import melody.offline.music.activity.MoPlaylistSongsActivity
import melody.offline.music.bean.PlaylistItem
import melody.offline.music.service.ViewModelMain
import melody.offline.music.util.AnalysisUtil
import melody.offline.music.util.DownloadUtil
import melody.offline.music.util.FileSizeConverter
import melody.offline.music.util.LogTag
import org.json.JSONObject
@OptIn(UnstableApi::class)
class ListMoreBottomSheetDialog(
private val context: Activity,
private val playlistItem: PlaylistItem,
private val lifecycleOwner: LifecycleOwner,
private val updateAdapterListener: UpdateAdapterListener
) : BottomSheetDialog(context) {
private var bottomDialogBottomLayout: LinearLayout? = null
private var bottomDialogBottomCloseBtn: LinearLayout? = null
private var bottomDialogSongImg: ImageView? = null
private var bottomDialogTitle: TextView? = null
private var bottomDialogName: TextView? = null
private var bottomDialogFavoritesBtn: LinearLayout? = null
private var bottomDialogFavoritesImg: ImageView? = null
private var bottomDialogMoreDownloadBtn: LinearLayout? = null
private var bottomDialogDownloadImg: ImageView? = null
private var bottomDialogDownloadLoading: ProgressBar? = null
private var bottomDialogDownloadTv: TextView? = null
private var bottomDialogMoreAddPlaylistBtn: LinearLayout? = null
private var bottomDialogAddPlaylistTv: TextView? = null
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.list_more_layout)
window?.setBackgroundDrawableResource(android.R.color.transparent)
window?.navigationBarColor = ContextCompat.getColor(context, R.color.main_bg_color)
initView()
initInfoUi()
initClick()
initDownloadFlow()
}
private fun initView() {
bottomDialogBottomLayout = findViewById(R.id.bottomDialogBottomLayout)
bottomDialogBottomCloseBtn = findViewById(R.id.bottomDialogBottomCloseBtn)
bottomDialogSongImg = findViewById(R.id.bottomDialogSongImg)
bottomDialogTitle = findViewById(R.id.bottomDialogTitle)
bottomDialogName = findViewById(R.id.bottomDialogName)
bottomDialogFavoritesBtn = findViewById(R.id.bottomDialogFavoritesBtn)
bottomDialogFavoritesImg = findViewById(R.id.bottomDialogFavoritesImg)
bottomDialogMoreDownloadBtn = findViewById(R.id.bottomDialogMoreDownloadBtn)
bottomDialogDownloadImg = findViewById(R.id.bottomDialogDownloadImg)
bottomDialogDownloadLoading = findViewById(R.id.bottomDialogDownloadLoading)
bottomDialogDownloadTv = findViewById(R.id.bottomDialogDownloadTv)
bottomDialogMoreAddPlaylistBtn = findViewById(R.id.bottomDialogMoreAddPlaylistBtn)
bottomDialogAddPlaylistTv = findViewById(R.id.bottomDialogAddPlaylistTv)
}
private fun initInfoUi() {
val bean = playlistItem
Glide.with(context).load(bean.thumbnail).into(bottomDialogSongImg!!)
bottomDialogTitle?.text = bean.title
if (bean.name.isEmpty()) {
bottomDialogName?.visibility = View.GONE
} else {
bottomDialogName?.visibility = View.VISIBLE
bottomDialogName?.text = bean.name
}
LogTag.LogD(LogTag.VO_TEST_ONLY, "$playlistItem")
updateFavoriteUi(playlistItem.isFavorite)
//获取当前ID的下载记录
val currentDownload = DownloadUtil.getCurrentIdDownload(bean.videoId)
if (currentDownload != null) {
//是否下载过资源下载过资源就不走download的ui更新因为 它肯定状态为3
if (DownloadUtil.downloadResourceExist(bean.videoId)) {
updateDownloadBtnUi(playlistItem.isOffline)
} else {
updateDownloadUI(currentDownload)
}
} else {
updateDownloadBtnUi(playlistItem.isOffline)
}
}
private fun initClick() {
bottomDialogBottomCloseBtn?.setOnClickListener {
dismiss()
}
bottomDialogFavoritesBtn?.setOnClickListener {
if (listenerMoreView != null) {
listenerMoreView?.onFavoritesClicked(playlistItem)
}
}
bottomDialogMoreDownloadBtn?.setOnClickListener {
if (listenerMoreView != null) {
listenerMoreView?.onDownloadClicked(playlistItem)
}
}
bottomDialogMoreAddPlaylistBtn?.setOnClickListener {
if (listenerMoreView != null) {
listenerMoreView?.onAddToPlaylistClicked(playlistItem)
}
}
}
fun updateFavoriteUi(b: Boolean) {
if (b) {
bottomDialogFavoritesImg?.setImageResource(R.drawable.favorited_icon)
} else {
bottomDialogFavoritesImg?.setImageResource(R.drawable.not_favorited_icon)
}
}
fun updateDownloadBtnUi(b: Boolean) {
if (b) {
bottomDialogDownloadLoading?.visibility = View.GONE
bottomDialogDownloadImg?.visibility = View.VISIBLE
bottomDialogDownloadImg?.setImageResource(R.drawable.more_downloaded_icon)
bottomDialogDownloadTv?.text = context.getString(R.string.download_remove_offline)
} else {
bottomDialogDownloadLoading?.visibility = View.GONE
bottomDialogDownloadImg?.visibility = View.VISIBLE
bottomDialogDownloadImg?.setImageResource(R.drawable.more_download_icon)
bottomDialogDownloadTv?.text = context.getString(R.string.download_save_offline)
}
}
private fun initDownloadFlow() {
ViewModelMain.modelDownloadsFlow.observe(lifecycleOwner) { downloads ->
val id = playlistItem.videoId
val currentScreenDownloads = downloads[id]
if (currentScreenDownloads != null) {
updateDownloadUI(currentScreenDownloads)
}
}
}
private fun updateDownloadUI(download: Download) {
when (download.state) {
Download.STATE_DOWNLOADING -> {
bottomDialogDownloadLoading?.visibility = View.VISIBLE
bottomDialogDownloadImg?.setImageResource(R.drawable.more_download_icon)
bottomDialogDownloadImg?.visibility = View.GONE
bottomDialogDownloadTv?.text = context.getString(R.string.download_saving)
bottomDialogMoreDownloadBtn?.isClickable = false
bottomDialogMoreDownloadBtn?.isEnabled = false
}
Download.STATE_COMPLETED -> {
updateAdapterListener.onUpdateAdapterListener(download, playlistItem)
bottomDialogDownloadLoading?.visibility = View.GONE
bottomDialogDownloadImg?.setImageResource(R.drawable.more_downloaded_icon)
bottomDialogDownloadImg?.visibility = View.VISIBLE
bottomDialogDownloadTv?.text = context.getString(R.string.download_remove_offline)
bottomDialogMoreDownloadBtn?.isClickable = true
bottomDialogMoreDownloadBtn?.isEnabled = true
val jsonObject = JSONObject()
jsonObject.put(
"download_id", download.request.id
)
val songMap = mutableMapOf(
Pair(
AnalysisUtil.PARAM_VALUE, jsonObject.toString()
)
)
AnalysisUtil.logEvent(AnalysisUtil.PLAYER_B_DOWNLOAD_SUCCESS_ACTION, songMap)
}
Download.STATE_FAILED -> {
bottomDialogDownloadLoading?.visibility = View.GONE
bottomDialogDownloadImg?.setImageResource(R.drawable.error)
bottomDialogDownloadImg?.visibility = View.VISIBLE
bottomDialogDownloadTv?.text = context.getString(R.string.download_save_offline)
bottomDialogMoreDownloadBtn?.isClickable = true
bottomDialogMoreDownloadBtn?.isEnabled = true
}
else -> {
bottomDialogDownloadLoading?.visibility = View.GONE
bottomDialogDownloadImg?.setImageResource(R.drawable.more_download_icon)
bottomDialogDownloadImg?.visibility = View.VISIBLE
bottomDialogDownloadTv?.text = context.getString(R.string.download_save_offline)
bottomDialogMoreDownloadBtn?.isClickable = true
bottomDialogMoreDownloadBtn?.isEnabled = true
}
}
}
// 定义一个接口用于回调事件
interface ListMoreViewListener {
fun onFavoritesClicked(playlistItem: PlaylistItem) {}
fun onDownloadClicked(playlistItem: PlaylistItem) {}
fun onAddToPlaylistClicked(playlistItem: PlaylistItem) {}
}
private var listenerMoreView: ListMoreViewListener? = null
// 设置监听器
fun setListMoreViewListener(listener: ListMoreViewListener) {
this.listenerMoreView = listener
}
interface UpdateAdapterListener {
fun onUpdateAdapterListener(download: Download, playlistItem: PlaylistItem)
}
}

View File

@ -164,200 +164,4 @@
android:orientation="vertical" /> android:orientation="vertical" />
</RelativeLayout> </RelativeLayout>
</LinearLayout> </LinearLayout>
<LinearLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:visibility="gone">
<View
android:id="@+id/bottom_blank_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="68dp"
android:layout_weight="3" />
<LinearLayout
android:id="@+id/layout_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/drw_bottom_layout_bg"
android:orientation="vertical"
android:paddingBottom="32dp"
android:visibility="visible">
<LinearLayout
android:id="@+id/bottomCloseBtn"
android:layout_width="match_parent"
android:layout_height="32dp"
android:gravity="center">
<TextView
android:layout_width="30dp"
android:layout_height="4dp"
android:background="@drawable/drw_bottom_close_btn_bg"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingStart="18dp"
android:paddingEnd="18dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="0dp"
app:cardBackgroundColor="@color/transparent"
app:cardCornerRadius="10dp"
app:cardElevation="0dp">
<ImageView
android:id="@+id/songImg"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/app_logo" />
</androidx.cardview.widget.CardView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<melody.offline.music.view.MarqueeTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/regular_font"
android:maxLines="1"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="14dp" />
<melody.offline.music.view.MarqueeTextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/regular_font"
android:maxLines="1"
android:text="@string/app_name"
android:textColor="@color/white_60"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/favoritesBtn"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="@drawable/drw_round_48_bg"
android:gravity="center">
<ImageView
android:id="@+id/favoritesImg"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="@drawable/not_favorited_icon" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="16dp"
android:background="@color/liner_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/moreDownloadBtn"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="4dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/downloadImg"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/more_downloaded_icon" />
<ProgressBar
android:id="@+id/downloadLoading"
android:layout_width="24dp"
android:layout_height="24dp"
android:indeterminateTint="@color/green"
android:progressBackgroundTint="@color/green"
android:progressTint="@color/green"
android:visibility="gone" />
</RelativeLayout>
<TextView
android:id="@+id/downloadTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:text="@string/download_remove_offline"
android:textColor="@color/white"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/moreAddPlaylistBtn"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="4dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/more_add_playlist_icon" />
<TextView
android:id="@+id/addPlaylistTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:text="@string/remove_from_playlist"
android:textColor="@color/white"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout> </RelativeLayout>

View File

@ -1,20 +1,27 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content">
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout <LinearLayout
android:id="@+id/layout_info" android:id="@+id/bottomDialogBottomLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:background="@drawable/drw_bottom_layout_bg" android:background="@drawable/drw_bottom_layout_bg"
android:orientation="vertical" android:orientation="vertical"
android:visibility="visible">
<LinearLayout
android:id="@+id/bottomDialogLayoutInfo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="32dp" android:paddingBottom="32dp"
android:visibility="visible"> android:visibility="visible">
<LinearLayout <LinearLayout
android:id="@+id/bottomCloseBtn" android:id="@+id/bottomDialogBottomCloseBtn"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="32dp" android:layout_height="32dp"
android:gravity="center"> android:gravity="center">
@ -49,7 +56,7 @@
app:cardElevation="0dp"> app:cardElevation="0dp">
<ImageView <ImageView
android:id="@+id/songImg" android:id="@+id/bottomDialogSongImg"
android:layout_width="50dp" android:layout_width="50dp"
android:layout_height="50dp" android:layout_height="50dp"
android:src="@mipmap/app_logo" /> android:src="@mipmap/app_logo" />
@ -66,7 +73,7 @@
android:orientation="vertical"> android:orientation="vertical">
<melody.offline.music.view.MarqueeTextView <melody.offline.music.view.MarqueeTextView
android:id="@+id/title" android:id="@+id/bottomDialogTitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:fontFamily="@font/regular_font" android:fontFamily="@font/regular_font"
@ -76,7 +83,7 @@
android:textSize="14dp" /> android:textSize="14dp" />
<melody.offline.music.view.MarqueeTextView <melody.offline.music.view.MarqueeTextView
android:id="@+id/name" android:id="@+id/bottomDialogName"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:fontFamily="@font/regular_font" android:fontFamily="@font/regular_font"
@ -88,14 +95,14 @@
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/favoritesBtn" android:id="@+id/bottomDialogFavoritesBtn"
android:layout_width="32dp" android:layout_width="32dp"
android:layout_height="32dp" android:layout_height="32dp"
android:background="@drawable/drw_round_48_bg" android:background="@drawable/drw_round_48_bg"
android:gravity="center"> android:gravity="center">
<ImageView <ImageView
android:id="@+id/favoritesImg" android:id="@+id/bottomDialogFavoritesImg"
android:layout_width="16dp" android:layout_width="16dp"
android:layout_height="16dp" android:layout_height="16dp"
android:src="@drawable/not_favorited_icon" /> android:src="@drawable/not_favorited_icon" />
@ -114,7 +121,7 @@
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout <LinearLayout
android:id="@+id/moreDownloadBtn" android:id="@+id/bottomDialogMoreDownloadBtn"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="48dp" android:layout_height="48dp"
android:layout_marginTop="4dp" android:layout_marginTop="4dp"
@ -126,13 +133,13 @@
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<ImageView <ImageView
android:id="@+id/downloadImg" android:id="@+id/bottomDialogDownloadImg"
android:layout_width="24dp" android:layout_width="24dp"
android:layout_height="24dp" android:layout_height="24dp"
android:src="@drawable/more_download_icon" /> android:src="@drawable/more_downloaded_icon" />
<ProgressBar <ProgressBar
android:id="@+id/downloadLoading" android:id="@+id/bottomDialogDownloadLoading"
android:layout_width="24dp" android:layout_width="24dp"
android:layout_height="24dp" android:layout_height="24dp"
android:indeterminateTint="@color/green" android:indeterminateTint="@color/green"
@ -144,18 +151,18 @@
</RelativeLayout> </RelativeLayout>
<TextView <TextView
android:id="@+id/downloadTv" android:id="@+id/bottomDialogDownloadTv"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="12dp" android:layout_marginStart="12dp"
android:text="@string/download" android:text="@string/download_remove_offline"
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="14dp" /> android:textSize="14dp" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/moreAddPlaylistBtn" android:id="@+id/bottomDialogMoreAddPlaylistBtn"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="48dp" android:layout_height="48dp"
android:layout_marginTop="4dp" android:layout_marginTop="4dp"
@ -168,11 +175,11 @@
android:src="@drawable/more_add_playlist_icon" /> android:src="@drawable/more_add_playlist_icon" />
<TextView <TextView
android:id="@+id/addPlaylistTv" android:id="@+id/bottomDialogAddPlaylistTv"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="12dp" android:layout_marginStart="12dp"
android:text="@string/add_to_playlist" android:text="@string/remove_from_playlist"
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="14dp" /> android:textSize="14dp" />
@ -182,3 +189,4 @@
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</LinearLayout>