diff --git a/app/src/main/java/melody/offline/music/activity/MoPlaylistSongsActivity.kt b/app/src/main/java/melody/offline/music/activity/MoPlaylistSongsActivity.kt index f8cf0cf..1d476bb 100644 --- a/app/src/main/java/melody/offline/music/activity/MoPlaylistSongsActivity.kt +++ b/app/src/main/java/melody/offline/music/activity/MoPlaylistSongsActivity.kt @@ -34,11 +34,14 @@ import melody.offline.music.service.MyDownloadService 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 melody.offline.music.view.ListMoreBottomSheetDialog import org.json.JSONObject @OptIn(UnstableApi::class) -class MoPlaylistSongsActivity : MoBaseActivity() { +class MoPlaylistSongsActivity : MoBaseActivity(), ListMoreBottomSheetDialog.ListMoreViewListener, + ListMoreBottomSheetDialog.UpdateAdapterListener { companion object { const val PLAYLIST_ID = "playlist_id" const val PLAYLIST_TITLE = "playlist_title" @@ -58,8 +61,8 @@ class MoPlaylistSongsActivity : MoBaseActivity() { val bean: PlaylistItem ) : Request() - data class OnDownloadRemove(val id: String) : Request() - data class OnUpdateDownloadUi(val id: String) : Request() + data class OnDownloadRemove(val playlistItem: PlaylistItem) : Request() + data class OnUpdateDownloadUi(val playlistItem: PlaylistItem) : Request() data class OnRemovePlaylist( val bean: PlaylistItem ) : Request() @@ -69,10 +72,9 @@ class MoPlaylistSongsActivity : MoBaseActivity() { private lateinit var binding: ActivityPlaylistSongsBinding private var adapter: PlaylistSongsAdapter? = null private var playlistItems: MutableList = mutableListOf() - private var currentPosition = -1 private var playlistId = -1 private var playlistTitle = "" - private var isAnimationRunning = false//动画是否正在进行 + private var moreDialog: ListMoreBottomSheetDialog? = null override suspend fun main() { binding = ActivityPlaylistSongsBinding.inflate(layoutInflater) @@ -84,7 +86,6 @@ class MoPlaylistSongsActivity : MoBaseActivity() { initView() initAdapter() LolAdWrapper.shared.loadAdIfNotCached(this, AdPlacement.INST_ME_PAGE_LIST) - initDownloadFlow() initData() onReceive() } @@ -127,14 +128,12 @@ class MoPlaylistSongsActivity : MoBaseActivity() { ) ) val currentFavoriteBean = - App.appFavoriteDBManager.getFavoriteBeanByID(it.bean.videoId!!) + App.appFavoriteDBManager.getFavoriteBeanByID(it.bean.videoId) if (currentFavoriteBean != null) { currentFavoriteBean.isFavorite = !currentFavoriteBean.isFavorite App.appFavoriteDBManager.updateFavoriteBean(currentFavoriteBean) if (currentFavoriteBean.isFavorite) { - AnalysisUtil.logEvent( - AnalysisUtil.PLAYER_B_LOVE_CLICK, songMap - ) + AnalysisUtil.logEvent(AnalysisUtil.PLAYER_B_LOVE_CLICK, songMap) } else { AnalysisUtil.logEvent( AnalysisUtil.PLAYER_B_UN_LOVE_CLICK, songMap @@ -156,14 +155,13 @@ class MoPlaylistSongsActivity : MoBaseActivity() { is Request.OnDownload -> { 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 (currentPosition >= 0) { - showRemoveDownloadDialogHint(id) - } + showRemoveDownloadDialogHint(it.bean) } else { val isFavorite = - App.appFavoriteDBManager.getFavoriteBeanByID(it.bean.videoId.toString()) != null + App.appFavoriteDBManager.getFavoriteBeanByID(it.bean.videoId) //判断是否已经下载了这条数据,已经下载,就直接进行数据库数据存储,反之走下载流程。 if (DownloadUtil.downloadResourceExist(id)) { val favoriteBean = FavoriteBean( @@ -171,10 +169,11 @@ class MoPlaylistSongsActivity : MoBaseActivity() { it.bean.title, it.bean.name, it.bean.thumbnail, - isFavorite + isFavorite?.isFavorite ?: false ) insertOfflineData(favoriteBean) - requests.trySend(Request.OnUpdateDownloadUi(id)) + it.bean.isOffline = true//更改状态 + requests.trySend(Request.OnUpdateDownloadUi(it.bean)) } else { val downloadRequest = DownloadRequest.Builder(id, id.toUri()) .setCustomCacheKey(id).build() @@ -202,7 +201,7 @@ class MoPlaylistSongsActivity : MoBaseActivity() { it.bean.title, it.bean.name, it.bean.thumbnail, - isFavorite + isFavorite?.isFavorite ?: false ) insertOfflineData(favoriteBean) val jsonObject = JSONObject() @@ -228,34 +227,29 @@ class MoPlaylistSongsActivity : MoBaseActivity() { is Request.OnDownloadRemove -> { val currentOfflineBean = - App.appOfflineDBManager.getOfflineBeanByID(it.id) + App.appOfflineDBManager.getOfflineBeanByID(it.playlistItem.videoId) if (currentOfflineBean != null) { App.appOfflineDBManager.deleteOfflineBean(currentOfflineBean) + + it.playlistItem.isOffline = false } - requests.trySend(Request.OnUpdateDownloadUi(it.id)) + requests.trySend(Request.OnUpdateDownloadUi(it.playlistItem)) } is Request.OnUpdateDownloadUi -> { - val offBean = App.appOfflineDBManager.getOfflineBeanByID(it.id) - if (offBean != null && offBean.bytesDownloaded?.let { bytes -> bytes > 0 } == true) { - binding.downloadLoading.visibility = View.GONE - 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) + moreDialog?.updateDownloadBtnUi(it.playlistItem.isOffline)//更新对话框的ui + runOnUiThread { + adapter?.notifyDataSetChanged()//更新adapter } } is Request.OnRemovePlaylist -> { - playlistItems.remove(it.bean) - adapter?.notifyDataSetChanged() App.appPlaylistDBManager.deletePlaylistItem(it.bean) + playlistItems.remove(it.bean) + runOnUiThread { + adapter?.notifyDataSetChanged()//更新adapter + } } } } @@ -302,46 +296,37 @@ class MoPlaylistSongsActivity : MoBaseActivity() { private fun initView() { binding.titleText.text = playlistTitle - binding.layoutInfo.setOnClickListener { } binding.backBtn.setOnClickListener { finish() } binding.tryAgainBtn.setOnClickListener { requests.trySend(Request.TryAgain) } - binding.bottomCloseBtn.setOnClickListener { - hideBottomLayout() - } - binding.bottomBlankLayout.setOnClickListener { - hideBottomLayout() - } - binding.favoritesBtn.setOnClickListener { - if (currentPosition >= 0) { - requests.trySend( - Request.OnFavorites( - playlistItems[currentPosition] - ) - ) - } - } - binding.moreDownloadBtn.setOnClickListener { - if (currentPosition >= 0) { - requests.trySend( - Request.OnDownload( - playlistItems[currentPosition] - ) - ) - } - } - binding.moreAddPlaylistBtn.setOnClickListener { - if (currentPosition >= 0) { - requests.trySend( - Request.OnRemovePlaylist( - playlistItems[currentPosition] - ) - ) - hideBottomLayout() - } + } + + + override fun onFavoritesClicked(playlistItem: PlaylistItem) { + requests.trySend(Request.OnFavorites(playlistItem)) + } + + override fun onDownloadClicked(playlistItem: PlaylistItem) { + requests.trySend(Request.OnDownload(playlistItem)) + } + + override fun onAddToPlaylistClicked(playlistItem: PlaylistItem) { + requests.trySend( + Request.OnRemovePlaylist(playlistItem) + ) + } + + @SuppressLint("NotifyDataSetChanged") + override fun onUpdateAdapterListener(download: Download, playlistItem: PlaylistItem) { + if (playlistItem.videoId == download.request.id) { + playlistItem.isOffline = true + playlistItem.bytesDownloaded = download.bytesDownloaded + playlistItem.size = FileSizeConverter(download.bytesDownloaded).formattedSize() + //更新adapter + adapter?.notifyDataSetChanged() } } @@ -349,10 +334,15 @@ class MoPlaylistSongsActivity : MoBaseActivity() { adapter = PlaylistSongsAdapter(this, playlistItems) adapter?.setOnMoreClickListener(object : PlaylistSongsAdapter.OnMoreClickListener { override fun onMoreClick(position: Int) { - currentPosition = position - if (!isAnimationRunning) { - toggleBottomLayout(position) - } + moreDialog = + ListMoreBottomSheetDialog( + this@MoPlaylistSongsActivity, + playlistItems[position], + this@MoPlaylistSongsActivity, + this@MoPlaylistSongsActivity + ) + moreDialog?.setListMoreViewListener(this@MoPlaylistSongsActivity) + moreDialog?.show() } }) binding.rv.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) @@ -362,8 +352,28 @@ class MoPlaylistSongsActivity : MoBaseActivity() { @SuppressLint("NotifyDataSetChanged") private suspend fun initData() { 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.addAll(App.appPlaylistDBManager.getPlaylistItems(playlistId)) + playlistItems.addAll(updatedPlaylistItems) if (playlistItems.size > 0) { showDataUi() } else { @@ -374,7 +384,7 @@ class MoPlaylistSongsActivity : MoBaseActivity() { } } - private fun showRemoveDownloadDialogHint(id: String) { + private fun showRemoveDownloadDialogHint(playlistItem: PlaylistItem) { val inflater = LayoutInflater.from(this) val dialogView = inflater.inflate(R.layout.dialog_hint, null) val okBtn = dialogView.findViewById(R.id.dialog_ok_btn) @@ -385,181 +395,19 @@ class MoPlaylistSongsActivity : MoBaseActivity() { dialog.show() okBtn.setOnClickListener { dialog.dismiss() - requests.trySend(Request.OnDownloadRemove(id)) + requests.trySend(Request.OnDownloadRemove(playlistItem)) } cancelBtn.setOnClickListener { 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) { - if (b) { - binding.favoritesImg.setImageResource(R.drawable.favorited_icon) - } else { - binding.favoritesImg.setImageResource(R.drawable.not_favorited_icon) + if (moreDialog != null) { + moreDialog?.updateFavoriteUi(b) } } - 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() { binding.loadingLayout.visibility = View.GONE binding.noContentLayout.visibility = View.GONE diff --git a/app/src/main/java/melody/offline/music/adapter/PlaylistSongsAdapter.kt b/app/src/main/java/melody/offline/music/adapter/PlaylistSongsAdapter.kt index 472e692..6bdc264 100644 --- a/app/src/main/java/melody/offline/music/adapter/PlaylistSongsAdapter.kt +++ b/app/src/main/java/melody/offline/music/adapter/PlaylistSongsAdapter.kt @@ -72,17 +72,15 @@ class PlaylistSongsAdapter( } if (bean.size?.isNotEmpty() == true) { size.text = bean.size - size.visibility = View.VISIBLE - } else { - size.visibility = View.GONE } - if (bean.isOffline) { + size.visibility = View.VISIBLE downloadCoImg.visibility = View.VISIBLE downloadImg.setImageResource(R.drawable.download_done_icon) } else { downloadCoImg.visibility = View.GONE downloadImg.setImageResource(R.drawable.download_icon) + size.visibility = View.GONE } } } diff --git a/app/src/main/java/melody/offline/music/database/AppPlaylistDBManager.kt b/app/src/main/java/melody/offline/music/database/AppPlaylistDBManager.kt index b25e056..832e90c 100644 --- a/app/src/main/java/melody/offline/music/database/AppPlaylistDBManager.kt +++ b/app/src/main/java/melody/offline/music/database/AppPlaylistDBManager.kt @@ -49,6 +49,12 @@ class AppPlaylistDBManager private constructor(context: Context) { } } + suspend fun updatePlaylistItems(playlistItems: List){ + return withContext(Dispatchers.IO){ + playlistDao.updatePlaylistItems(playlistItems) + } + } + suspend fun getPlaylistByTitle(title: String): Playlist? { return withContext(Dispatchers.IO) { playlistDao.getPlaylistByTitle(title) @@ -66,7 +72,6 @@ class AppPlaylistDBManager private constructor(context: Context) { return withContext(Dispatchers.IO) { playlistDao.getPlaylistItems(playlistId) } - } suspend fun deletePlaylist(playlist: Playlist) { diff --git a/app/src/main/java/melody/offline/music/database/PlaylistDao.kt b/app/src/main/java/melody/offline/music/database/PlaylistDao.kt index 7ca6c07..25aab8a 100644 --- a/app/src/main/java/melody/offline/music/database/PlaylistDao.kt +++ b/app/src/main/java/melody/offline/music/database/PlaylistDao.kt @@ -33,6 +33,9 @@ interface PlaylistDao { @Update suspend fun updatePlaylistItem(playlistItem: PlaylistItem) + @Update + suspend fun updatePlaylistItems(playlistItems: List) + @Delete suspend fun deletePlaylist(playlist: Playlist) diff --git a/app/src/main/java/melody/offline/music/view/ListMoreBottomSheetDialog.kt b/app/src/main/java/melody/offline/music/view/ListMoreBottomSheetDialog.kt new file mode 100644 index 0000000..bd3cfe8 --- /dev/null +++ b/app/src/main/java/melody/offline/music/view/ListMoreBottomSheetDialog.kt @@ -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) + } +} diff --git a/app/src/main/res/layout/activity_playlist_songs.xml b/app/src/main/res/layout/activity_playlist_songs.xml index 28e0d31..267c443 100644 --- a/app/src/main/res/layout/activity_playlist_songs.xml +++ b/app/src/main/res/layout/activity_playlist_songs.xml @@ -164,200 +164,4 @@ android:orientation="vertical" /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/list_more_layout.xml b/app/src/main/res/layout/list_more_layout.xml index 8135645..3482829 100644 --- a/app/src/main/res/layout/list_more_layout.xml +++ b/app/src/main/res/layout/list_more_layout.xml @@ -1,183 +1,191 @@ + android:layout_height="wrap_content"> - - - - - - + android:paddingBottom="32dp" + android:visibility="visible"> + android:layout_height="32dp" + android:gravity="center"> - + - - - - - - - - - - - - - - - - - - + android:orientation="vertical" + android:paddingStart="18dp" + android:paddingEnd="18dp"> - + android:layout_height="wrap_content" + android:elevation="0dp" + app:cardBackgroundColor="@color/transparent" + app:cardCornerRadius="10dp" + app:cardElevation="0dp"> + android:id="@+id/bottomDialogSongImg" + android:layout_width="50dp" + android:layout_height="50dp" + android:src="@mipmap/app_logo" /> - + - - - - + android:layout_marginEnd="12dp" + android:layout_weight="1" + android:gravity="center_vertical" + android:orientation="vertical"> + + + + + + + + + + + + + android:layout_height="wrap_content" + android:orientation="vertical"> - + - + + + + + + + + + + + + + + + + + + + + -