From c1b5f5c128859158a5ef022d0f16eb129b963651 Mon Sep 17 00:00:00 2001 From: Luna712 <142361265+Luna712@users.noreply.github.com> Date: Thu, 4 Jul 2024 15:51:07 -0600 Subject: [PATCH] Fix download button display bug in adapter (#1175) --- .../ui/download/DownloadAdapter.kt | 22 ++++++-- .../ui/download/DownloadFragment.kt | 3 +- .../ui/download/button/PieFetchButton.kt | 50 +++++++++---------- 3 files changed, 42 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadAdapter.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadAdapter.kt index b4a16a66..9a026334 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadAdapter.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadAdapter.kt @@ -4,6 +4,7 @@ import android.annotation.SuppressLint import android.text.format.Formatter.formatShortFileSize import android.view.LayoutInflater import android.view.ViewGroup +import androidx.core.content.ContextCompat import androidx.core.view.isVisible import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter @@ -13,9 +14,8 @@ import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.databinding.DownloadChildEpisodeBinding import com.lagradost.cloudstream3.databinding.DownloadHeaderEpisodeBinding import com.lagradost.cloudstream3.mvvm.logError -import com.lagradost.cloudstream3.utils.AppContextUtils.getNameFull -import com.lagradost.cloudstream3.utils.DataStoreHelper import com.lagradost.cloudstream3.ui.download.button.DownloadStatusTell +import com.lagradost.cloudstream3.utils.AppContextUtils.getNameFull import com.lagradost.cloudstream3.utils.DataStoreHelper.fixVisual import com.lagradost.cloudstream3.utils.DataStoreHelper.getViewPos import com.lagradost.cloudstream3.utils.UIHelper.setImage @@ -135,8 +135,15 @@ class DownloadAdapter( downloadButton.applyMetaData(card.child.id, card.currentBytes, card.totalBytes) // We will let the view model handle this downloadButton.doSetProgress = false + downloadButton.progressBar.progressDrawable = + downloadButton.getDrawableFromStatus(status) + ?.let { ContextCompat.getDrawable(downloadButton.context, it) } downloadHeaderInfo.text = formattedSizeString - } else downloadButton.doSetProgress = true + } else { + downloadButton.doSetProgress = true + downloadButton.progressBar.progressDrawable = + ContextCompat.getDrawable(downloadButton.context, downloadButton.progressDrawable) + } downloadButton.setDefaultClickListener(card.child, downloadHeaderInfo, mediaClickCallback) downloadButton.isVisible = true @@ -197,8 +204,15 @@ class DownloadAdapter( downloadButton.applyMetaData(d.id, card.currentBytes, card.totalBytes) // We will let the view model handle this downloadButton.doSetProgress = false + downloadButton.progressBar.progressDrawable = + downloadButton.getDrawableFromStatus(status) + ?.let { ContextCompat.getDrawable(downloadButton.context, it) } downloadChildEpisodeTextExtra.text = formatShortFileSize(downloadChildEpisodeTextExtra.context, card.totalBytes) - } else downloadButton.doSetProgress = true + } else { + downloadButton.doSetProgress = true + downloadButton.progressBar.progressDrawable = + ContextCompat.getDrawable(downloadButton.context, downloadButton.progressDrawable) + } downloadButton.setDefaultClickListener(d, downloadChildEpisodeTextExtra, mediaClickCallback) downloadButton.isVisible = true diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadFragment.kt index 82c5ffb8..23d546e1 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadFragment.kt @@ -40,7 +40,6 @@ import com.lagradost.cloudstream3.ui.result.setLinearListLayout import com.lagradost.cloudstream3.ui.settings.Globals.TV import com.lagradost.cloudstream3.ui.settings.Globals.isLayout import com.lagradost.cloudstream3.utils.AppContextUtils.loadResult -import com.lagradost.cloudstream3.utils.Coroutines.main import com.lagradost.cloudstream3.utils.DOWNLOAD_EPISODE_CACHE import com.lagradost.cloudstream3.utils.DataStore import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe @@ -273,4 +272,4 @@ class DownloadFragment : Fragment() { val selectedVideoUri = result?.data?.data ?: return@registerForActivityResult playUri(activity ?: return@registerForActivityResult, selectedVideoUri) } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/download/button/PieFetchButton.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/download/button/PieFetchButton.kt index a6dc5c56..abc159d0 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/download/button/PieFetchButton.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/download/button/PieFetchButton.kt @@ -1,7 +1,6 @@ package com.lagradost.cloudstream3.ui.download.button import android.content.Context -import android.graphics.drawable.Drawable import android.os.Looper import android.util.AttributeSet import android.util.Log @@ -45,6 +44,8 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) : private var iconPaused: Int = 0 private var hideWhenIcon: Boolean = true + var progressDrawable: Int = 0 + var overrideLayout: Int? = null companion object { @@ -115,10 +116,10 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) : R.styleable.PieFetchButton_download_icon_complete, R.drawable.download_icon_done ) iconPaused = getResourceId( - R.styleable.PieFetchButton_download_icon_paused, 0//R.drawable.download_icon_pause + R.styleable.PieFetchButton_download_icon_paused, 0 // R.drawable.download_icon_pause ) iconActive = getResourceId( - R.styleable.PieFetchButton_download_icon_active, 0 //R.drawable.download_icon_load + R.styleable.PieFetchButton_download_icon_active, 0 // R.drawable.download_icon_load ) iconWaiting = getResourceId( R.styleable.PieFetchButton_download_icon_waiting, 0 @@ -129,7 +130,7 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) : val fillIndex = getInt(R.styleable.PieFetchButton_download_fill, 0) - val progressDrawable = getResourceId( + progressDrawable = getResourceId( R.styleable.PieFetchButton_download_fill_override, fillArray[fillIndex] ) @@ -170,7 +171,7 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) : if (isZeroBytes) { removeKey(KEY_RESUME_PACKAGES, card.id.toString()) callback(DownloadClickEvent(DOWNLOAD_ACTION_DOWNLOAD, card)) - //callback.invoke(DownloadClickEvent(DOWNLOAD_ACTION_DOWNLOAD, data)) + // callback.invoke(DownloadClickEvent(DOWNLOAD_ACTION_DOWNLOAD, data)) } else { val list = arrayListOf( Pair(DOWNLOAD_ACTION_PLAY_FILE, R.string.popup_play_file), @@ -197,7 +198,7 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) : list ) { callback(DownloadClickEvent(itemId, card)) - //callback.invoke(DownloadClickEvent(itemId, data)) + // callback.invoke(DownloadClickEvent(itemId, data)) } } } @@ -205,7 +206,7 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) : view.setOnLongClickListener { callback(DownloadClickEvent(DOWNLOAD_ACTION_LONG_CLICK, card)) - //clickCallback.invoke(DownloadClickEvent(DOWNLOAD_ACTION_LONG_CLICK, data)) + // clickCallback.invoke(DownloadClickEvent(DOWNLOAD_ACTION_LONG_CLICK, data)) return@setOnLongClickListener true } } @@ -218,7 +219,7 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) : setDefaultClickListener(this, textView, card, callback) } - /*open fun setDefaultClickListener(requestGetter: suspend BaseFetchButton.() -> List) { + /* open fun setDefaultClickListener(requestGetter: suspend BaseFetchButton.() -> List) { this.setOnClickListener { when (this.currentStatus) { null -> { @@ -244,7 +245,7 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) : else -> {} } } - }*/ + } */ @MainThread private fun setStatusInternal(status : DownloadStatusTell?) { @@ -262,7 +263,8 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) : progressBarBackground.background = ContextCompat.getDrawable(context, progressDrawable) - val drawable = getDrawableFromStatus(status) + val drawable = + getDrawableFromStatus(status)?.let { ContextCompat.getDrawable(this.context, it) } statusView.setImageDrawable(drawable) val isDrawable = drawable != null @@ -280,12 +282,12 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) : override fun setStatus(status: DownloadStatusTell?) { currentStatus = status - // runs on the main thread, but also instant if it already is + // Runs on the main thread, but also instant if it already is if (Looper.myLooper() == Looper.getMainLooper()) { try { setStatusInternal(status) } catch (t : Throwable) { - logError(t) // just in case setStatusInternal throws because thread + logError(t) // Just in case setStatusInternal throws because thread progressBarBackground.post { setStatusInternal(status) } @@ -325,19 +327,13 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) : } } - open fun getDrawableFromStatus(status: DownloadStatusTell?): Drawable? { - val drawableInt = when (status) { - DownloadStatusTell.IsPaused -> iconPaused - DownloadStatusTell.IsPending -> iconWaiting - DownloadStatusTell.IsDownloading -> iconActive - DownloadStatusTell.IsFailed -> iconError - DownloadStatusTell.IsDone -> iconComplete - DownloadStatusTell.IsStopped -> iconRemoved - null -> iconInit - } - if (drawableInt == 0) { - return null - } - return ContextCompat.getDrawable(this.context, drawableInt) - } + open fun getDrawableFromStatus(status: DownloadStatusTell?): Int? = when (status) { + DownloadStatusTell.IsPaused -> iconPaused + DownloadStatusTell.IsPending -> iconWaiting + DownloadStatusTell.IsDownloading -> iconActive + DownloadStatusTell.IsFailed -> iconError + DownloadStatusTell.IsDone -> iconComplete + DownloadStatusTell.IsStopped -> iconRemoved + else -> iconInit + }.takeIf { it != 0 } } \ No newline at end of file