Add initial selected bytes to view model

This commit is contained in:
Luna712 2024-07-09 00:17:08 -06:00 committed by GitHub
parent 4966398206
commit 3ce6b8074e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -44,6 +44,9 @@ class DownloadViewModel : ViewModel() {
private val _downloadBytes = MutableLiveData<Long>() private val _downloadBytes = MutableLiveData<Long>()
val downloadBytes: LiveData<Long> = _downloadBytes val downloadBytes: LiveData<Long> = _downloadBytes
private val _selectedBytes = MutableLiveData<Long>(0)
val selectedBytes: LiveData<Long> = _selectedBytes
private val _isMultiDeleteState = MutableLiveData(false) private val _isMultiDeleteState = MutableLiveData(false)
val isMultiDeleteState: LiveData<Boolean> = _isMultiDeleteState val isMultiDeleteState: LiveData<Boolean> = _isMultiDeleteState
@ -61,6 +64,7 @@ class DownloadViewModel : ViewModel() {
if (!currentSelected.contains(item)) { if (!currentSelected.contains(item)) {
currentSelected.add(item) currentSelected.add(item)
_selectedItems.postValue(currentSelected) _selectedItems.postValue(currentSelected)
updateSelectedBytes()
} }
} }
@ -68,6 +72,7 @@ class DownloadViewModel : ViewModel() {
selectedItems.value?.let { selected -> selectedItems.value?.let { selected ->
selected.remove(item) selected.remove(item)
_selectedItems.postValue(selected) _selectedItems.postValue(selected)
updateSelectedBytes()
} }
} }
@ -81,6 +86,7 @@ class DownloadViewModel : ViewModel() {
} }
} }
_selectedItems.postValue(currentSelected) _selectedItems.postValue(currentSelected)
updateSelectedBytes()
} }
fun clearSelectedItems() { fun clearSelectedItems() {
@ -104,6 +110,25 @@ class DownloadViewModel : ViewModel() {
return false return false
} }
private fun updateSelectedBytes() = viewModelScope.launchSafe {
val selectedItemsList = selectedItems.value ?: return@launchSafe
var totalSelectedBytes = 0L
selectedItemsList.forEach { item ->
totalSelectedBytes += when (item) {
is VisualDownloadCached.Header -> {
item.totalBytes
}
is VisualDownloadCached.Child -> {
item.totalBytes
}
}
}
_selectedBytes.postValue(totalSelectedBytes)
}
fun updateList(context: Context) = viewModelScope.launchSafe { fun updateList(context: Context) = viewModelScope.launchSafe {
val children = withContext(Dispatchers.IO) { val children = withContext(Dispatchers.IO) {
context.getKeys(DOWNLOAD_EPISODE_CACHE) context.getKeys(DOWNLOAD_EPISODE_CACHE)