mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
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:
parent
1a05651510
commit
0b69793d7c
4 changed files with 17 additions and 42 deletions
|
@ -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)
|
||||
|
|
|
@ -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<VideoDownloadHelper.DownloadEpisodeCached>(it) }
|
||||
context.getKeys(DOWNLOAD_EPISODE_CACHE)
|
||||
.mapNotNull { context.getKey<VideoDownloadHelper.DownloadEpisodeCached>(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
|
||||
|
||||
|
|
|
@ -64,25 +64,9 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) :
|
|||
var currentMetaData: DownloadMetadata =
|
||||
DownloadMetadata(0, 0, 0, null)
|
||||
|
||||
fun setPersistentId(id: Int) {
|
||||
persistentId = id
|
||||
currentMetaData.id = id
|
||||
abstract fun setStatus(status: VideoDownloadManager.DownloadType?)
|
||||
|
||||
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
|
||||
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 {
|
||||
|
@ -92,14 +76,6 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) :
|
|||
this.status = status
|
||||
}
|
||||
setStatus(status)
|
||||
} ?: run {
|
||||
resetView()
|
||||
}
|
||||
}
|
||||
|
||||
abstract fun setStatus(status: VideoDownloadManager.DownloadType?)
|
||||
|
||||
open fun setProgress(downloadedBytes: Long, totalBytes: Long) {
|
||||
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()
|
||||
|
||||
}
|
|
@ -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())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue