From 0b69793d7c31ce82e1b0ace7636bd74fc616f427 Mon Sep 17 00:00:00 2001 From: Luna712 <142361265+Luna712@users.noreply.github.com> Date: Sun, 30 Jun 2024 22:02:45 -0600 Subject: [PATCH] Major performance and bug fixes to downloads I tracked down underlying issues to setPersistentId. So I instead removed that entire function here and instead we send the byte data from the view model to it. This also does minor improvements to view model. This also fixes the underlying cause of many bugs like the byte mismatch and icon bug, while my previous fix for that was just a patch-job, this should fix the underlying cause of them as well. Overall when testing this makes downloads load with 100% zero lag now. --- .../ui/download/DownloadAdapter.kt | 2 + .../ui/download/DownloadViewModel.kt | 11 +++-- .../ui/download/button/BaseFetchButton.kt | 45 +++++-------------- .../ui/download/button/PieFetchButton.kt | 1 - 4 files changed, 17 insertions(+), 42 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 8f496b3c..317b378a 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 @@ -114,6 +114,7 @@ class DownloadAdapter( downloadHeaderGotoChild.isVisible = false downloadButton.setDefaultClickListener(card.child, downloadHeaderInfo, mediaClickCallback) + downloadButton.setProgress(card.currentBytes, card.totalBytes) downloadButton.isVisible = true episodeHolder.setOnClickListener { @@ -167,6 +168,7 @@ class DownloadAdapter( } downloadButton.setDefaultClickListener(card.data, downloadChildEpisodeTextExtra, mediaClickCallback) + downloadButton.setProgress(card.currentBytes, card.totalBytes) downloadChildEpisodeText.apply { text = context.getNameFull(d.name, d.episode, d.season) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadViewModel.kt index 380430e1..bb19d4ab 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadViewModel.kt @@ -18,7 +18,6 @@ import com.lagradost.cloudstream3.utils.DataStore.getKeys import com.lagradost.cloudstream3.utils.VideoDownloadHelper import com.lagradost.cloudstream3.utils.VideoDownloadManager import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class DownloadViewModel : ViewModel() { @@ -43,8 +42,8 @@ class DownloadViewModel : ViewModel() { fun updateList(context: Context) = viewModelScope.launchSafe { val children = withContext(Dispatchers.IO) { - val headers = context.getKeys(DOWNLOAD_EPISODE_CACHE) - headers.mapNotNull { context.getKey(it) } + context.getKeys(DOWNLOAD_EPISODE_CACHE) + .mapNotNull { context.getKey(it) } .distinctBy { it.id } // Remove duplicates } @@ -57,10 +56,10 @@ class DownloadViewModel : ViewModel() { // Gets all children downloads withContext(Dispatchers.IO) { - for (c in children) { - val childFile = VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(context, c.id) ?: continue + children.forEach { c -> + val childFile = VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(context, c.id) ?: return@forEach - if (childFile.fileLength <= 1) continue + if (childFile.fileLength <= 1) return@forEach val len = childFile.totalBytes val flen = childFile.fileLength diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/download/button/BaseFetchButton.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/download/button/BaseFetchButton.kt index b43f1aac..6dc1df2b 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/download/button/BaseFetchButton.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/download/button/BaseFetchButton.kt @@ -64,42 +64,18 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) : var currentMetaData: DownloadMetadata = DownloadMetadata(0, 0, 0, null) - fun setPersistentId(id: Int) { - persistentId = id - currentMetaData.id = id - - VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(context, id)?.let { savedData -> - val downloadedBytes = savedData.fileLength - val totalBytes = savedData.totalBytes - - /*lastRequest = savedData.uriRequest - files = savedData.files - - var totalBytes: Long = 0 - var downloadedBytes: Long = 0 - for (file in savedData.files) { - downloadedBytes += file.completedLength - totalBytes += file.length - }*/ - setProgress(downloadedBytes, totalBytes) - // some extra padding for just in case - val status = VideoDownloadManager.downloadStatus[id] - ?: if (downloadedBytes > 1024L && downloadedBytes + 1024L >= totalBytes) DownloadStatusTell.IsDone else DownloadStatusTell.IsPaused - currentMetaData.apply { - this.id = id - this.downloadedLength = downloadedBytes - this.totalLength = totalBytes - this.status = status - } - setStatus(status) - } ?: run { - resetView() - } - } - abstract fun setStatus(status: VideoDownloadManager.DownloadType?) open fun setProgress(downloadedBytes: Long, totalBytes: Long) { + val status = VideoDownloadManager.downloadStatus[id] + ?: if (downloadedBytes > 1024L && downloadedBytes + 1024L >= totalBytes) DownloadStatusTell.IsDone else DownloadStatusTell.IsPaused + currentMetaData.apply { + this.id = id + this.downloadedLength = downloadedBytes + this.totalLength = totalBytes + this.status = status + } + setStatus(status) isZeroBytes = downloadedBytes == 0L progressBar.post { val steps = 10000L @@ -174,7 +150,7 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) : val pid = persistentId if (pid != null) { // refresh in case of onDetachedFromWindow -> onAttachedToWindow while still being ??????? - setPersistentId(pid) + currentMetaData.id = pid } super.onAttachedToWindow() @@ -198,5 +174,4 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) : * Get a clean slate again, might be useful in recyclerview? * */ abstract fun resetView() - } \ 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 f1031c24..f9b8a6ee 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 @@ -167,7 +167,6 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) : callback: (DownloadClickEvent) -> Unit ) { this.progressText = textView - this.setPersistentId(card.id) view.setOnClickListener { if (isZeroBytes) { removeKey(KEY_RESUME_PACKAGES, card.id.toString())