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.
This commit is contained in:
Luna712 2024-06-30 22:02:45 -06:00 committed by GitHub
parent 1a05651510
commit 0b69793d7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 17 additions and 42 deletions

View file

@ -114,6 +114,7 @@ class DownloadAdapter(
downloadHeaderGotoChild.isVisible = false downloadHeaderGotoChild.isVisible = false
downloadButton.setDefaultClickListener(card.child, downloadHeaderInfo, mediaClickCallback) downloadButton.setDefaultClickListener(card.child, downloadHeaderInfo, mediaClickCallback)
downloadButton.setProgress(card.currentBytes, card.totalBytes)
downloadButton.isVisible = true downloadButton.isVisible = true
episodeHolder.setOnClickListener { episodeHolder.setOnClickListener {
@ -167,6 +168,7 @@ class DownloadAdapter(
} }
downloadButton.setDefaultClickListener(card.data, downloadChildEpisodeTextExtra, mediaClickCallback) downloadButton.setDefaultClickListener(card.data, downloadChildEpisodeTextExtra, mediaClickCallback)
downloadButton.setProgress(card.currentBytes, card.totalBytes)
downloadChildEpisodeText.apply { downloadChildEpisodeText.apply {
text = context.getNameFull(d.name, d.episode, d.season) text = context.getNameFull(d.name, d.episode, d.season)

View file

@ -18,7 +18,6 @@ import com.lagradost.cloudstream3.utils.DataStore.getKeys
import com.lagradost.cloudstream3.utils.VideoDownloadHelper import com.lagradost.cloudstream3.utils.VideoDownloadHelper
import com.lagradost.cloudstream3.utils.VideoDownloadManager import com.lagradost.cloudstream3.utils.VideoDownloadManager
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class DownloadViewModel : ViewModel() { class DownloadViewModel : ViewModel() {
@ -43,8 +42,8 @@ class DownloadViewModel : ViewModel() {
fun updateList(context: Context) = viewModelScope.launchSafe { fun updateList(context: Context) = viewModelScope.launchSafe {
val children = withContext(Dispatchers.IO) { val children = withContext(Dispatchers.IO) {
val headers = context.getKeys(DOWNLOAD_EPISODE_CACHE) context.getKeys(DOWNLOAD_EPISODE_CACHE)
headers.mapNotNull { context.getKey<VideoDownloadHelper.DownloadEpisodeCached>(it) } .mapNotNull { context.getKey<VideoDownloadHelper.DownloadEpisodeCached>(it) }
.distinctBy { it.id } // Remove duplicates .distinctBy { it.id } // Remove duplicates
} }
@ -57,10 +56,10 @@ class DownloadViewModel : ViewModel() {
// Gets all children downloads // Gets all children downloads
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
for (c in children) { children.forEach { c ->
val childFile = VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(context, c.id) ?: continue 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 len = childFile.totalBytes
val flen = childFile.fileLength val flen = childFile.fileLength

View file

@ -64,25 +64,9 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) :
var currentMetaData: DownloadMetadata = var currentMetaData: DownloadMetadata =
DownloadMetadata(0, 0, 0, null) DownloadMetadata(0, 0, 0, null)
fun setPersistentId(id: Int) { abstract fun setStatus(status: VideoDownloadManager.DownloadType?)
persistentId = id
currentMetaData.id = id
VideoDownloadManager.getDownloadFileInfoAndUpdateSettings(context, id)?.let { savedData -> open fun setProgress(downloadedBytes: Long, totalBytes: Long) {
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] val status = VideoDownloadManager.downloadStatus[id]
?: if (downloadedBytes > 1024L && downloadedBytes + 1024L >= totalBytes) DownloadStatusTell.IsDone else DownloadStatusTell.IsPaused ?: if (downloadedBytes > 1024L && downloadedBytes + 1024L >= totalBytes) DownloadStatusTell.IsDone else DownloadStatusTell.IsPaused
currentMetaData.apply { currentMetaData.apply {
@ -92,14 +76,6 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) :
this.status = status this.status = status
} }
setStatus(status) setStatus(status)
} ?: run {
resetView()
}
}
abstract fun setStatus(status: VideoDownloadManager.DownloadType?)
open fun setProgress(downloadedBytes: Long, totalBytes: Long) {
isZeroBytes = downloadedBytes == 0L isZeroBytes = downloadedBytes == 0L
progressBar.post { progressBar.post {
val steps = 10000L val steps = 10000L
@ -174,7 +150,7 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) :
val pid = persistentId val pid = persistentId
if (pid != null) { if (pid != null) {
// refresh in case of onDetachedFromWindow -> onAttachedToWindow while still being ??????? // refresh in case of onDetachedFromWindow -> onAttachedToWindow while still being ???????
setPersistentId(pid) currentMetaData.id = pid
} }
super.onAttachedToWindow() super.onAttachedToWindow()
@ -198,5 +174,4 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) :
* Get a clean slate again, might be useful in recyclerview? * Get a clean slate again, might be useful in recyclerview?
* */ * */
abstract fun resetView() abstract fun resetView()
} }

View file

@ -167,7 +167,6 @@ open class PieFetchButton(context: Context, attributeSet: AttributeSet) :
callback: (DownloadClickEvent) -> Unit callback: (DownloadClickEvent) -> Unit
) { ) {
this.progressText = textView this.progressText = textView
this.setPersistentId(card.id)
view.setOnClickListener { view.setOnClickListener {
if (isZeroBytes) { if (isZeroBytes) {
removeKey(KEY_RESUME_PACKAGES, card.id.toString()) removeKey(KEY_RESUME_PACKAGES, card.id.toString())