Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lagradost.api.Log
import com.lagradost.cloudstream3.CloudStreamApp
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.isEpisodeBased
import com.lagradost.cloudstream3.mvvm.Resource
Expand All @@ -36,6 +37,7 @@ import com.lagradost.cloudstream3.utils.ResourceLiveData
import com.lagradost.cloudstream3.utils.downloader.DownloadObjects
import com.lagradost.cloudstream3.utils.downloader.DownloadQueueManager
import com.lagradost.cloudstream3.utils.downloader.VideoDownloadManager.deleteFilesAndUpdateSettings
import com.lagradost.cloudstream3.utils.downloader.VideoDownloadManager.downloadDeleteEvent
import com.lagradost.cloudstream3.utils.downloader.VideoDownloadManager.getDownloadFileInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -67,6 +69,17 @@ class DownloadViewModel : ViewModel() {
private val _selectedItemIds = ConsistentLiveData<Set<Int>?>(null)
val selectedItemIds: LiveData<Set<Int>?> = _selectedItemIds

init {
// Keep the Downloads list in sync when a download is deleted/cancelled from
// anywhere in the app (result page button, queue, notification, etc.). See
// onDownloadDeleted for the rationale (issue #1227).
downloadDeleteEvent += ::onDownloadDeleted
}

override fun onCleared() {
downloadDeleteEvent -= ::onDownloadDeleted
super.onCleared()
}

fun cancelSelection() {
updateSelectedItems { null }
Expand Down Expand Up @@ -389,6 +402,18 @@ class DownloadViewModel : ViewModel() {
postChildren(_childCards.success?.filter { it.data.id !in idsToRemove })
}

/**
* Refreshes the Downloads screen in real time when a download is deleted/cancelled.
*/
private fun onDownloadDeleted(id: Int) {
// Keep multi-select state consistent: forget the removed id if it was selected.
updateSelectedItems { it?.minus(id) }

val context = CloudStreamApp.context ?: return
updateHeaderList(context)
postChildren(_childCards.success?.filterNot { it.data.id == id })
}

private fun updateStorageStats(visual: List<VisualDownloadCached.Header>) {
try {
val stat = StatFs(Environment.getExternalStorageDirectory().path)
Expand Down Expand Up @@ -584,4 +609,4 @@ class DownloadViewModel : ViewModel() {
val names: List<String>,
val parentName: String?
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) :
}
}

/*fun downloadDeleteEvent(data: Int) {

}*/
fun downloadDeleteEvent(data: Int) {
if (data == persistentId) {
resetView()
}
}

/*fun downloadEvent(data: Pair<Int, VideoDownloadManager.DownloadActionType>) {
val (id, action) = data
Expand All @@ -185,7 +187,7 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) :

override fun onAttachedToWindow() {
VideoDownloadManager.downloadStatusEvent += ::downloadStatusEvent
// VideoDownloadManager.downloadDeleteEvent += ::downloadDeleteEvent
VideoDownloadManager.downloadDeleteEvent += ::downloadDeleteEvent
// VideoDownloadManager.downloadEvent += ::downloadEvent
VideoDownloadManager.downloadProgressEvent += ::downloadProgressEvent

Expand All @@ -200,7 +202,7 @@ abstract class BaseFetchButton(context: Context, attributeSet: AttributeSet) :

override fun onDetachedFromWindow() {
VideoDownloadManager.downloadStatusEvent -= ::downloadStatusEvent
// VideoDownloadManager.downloadDeleteEvent -= ::downloadDeleteEvent
VideoDownloadManager.downloadDeleteEvent -= ::downloadDeleteEvent
// VideoDownloadManager.downloadEvent -= ::downloadEvent
VideoDownloadManager.downloadProgressEvent -= ::downloadProgressEvent

Expand Down