Use Set for ids

This commit is contained in:
Luna712 2024-07-18 17:07:37 -06:00 committed by GitHub
parent 529ffab5bc
commit dc582e852b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 16 deletions

View file

@ -32,7 +32,7 @@ object DownloadButtonSetup {
DialogInterface.BUTTON_POSITIVE -> {
VideoDownloadManager.deleteFilesAndUpdateSettings(
ctx,
listOf(id),
setOf(id),
MainScope()
)
}

View file

@ -230,7 +230,7 @@ class DownloadViewModel : ViewModel() {
}
}
private fun removeItems(idsToRemove: List<Int>) = viewModelScope.launchSafe {
private fun removeItems(idsToRemove: Set<Int>) = viewModelScope.launchSafe {
val updatedHeaders = headerCards.value.orEmpty().filter { it.data.id !in idsToRemove }
val updatedChildren = childCards.value.orEmpty().filter { it.data.id !in idsToRemove }
_headerCards.postValue(updatedHeaders)
@ -274,10 +274,12 @@ class DownloadViewModel : ViewModel() {
context: Context,
selectedItemsList: List<VisualDownloadCached>
): DeleteData {
val ids = mutableListOf<Int>()
val parentIds = mutableListOf<Int>()
val seriesNames = mutableListOf<String>()
val names = mutableListOf<String>()
val seriesNames = mutableListOf<String>()
val ids = mutableSetOf<Int>()
val parentIds = mutableSetOf<Int>()
var parentName: String? = null
selectedItemsList.forEach { item ->
@ -368,8 +370,8 @@ class DownloadViewModel : ViewModel() {
private fun showDeleteConfirmationDialog(
context: Context,
message: String,
ids: List<Int>,
parentIds: List<Int>
ids: Set<Int>,
parentIds: Set<Int>
) {
val builder = AlertDialog.Builder(context)
val dialogClickListener =
@ -424,8 +426,8 @@ class DownloadViewModel : ViewModel() {
}
private data class DeleteData(
val ids: List<Int>,
val parentIds: List<Int>,
val ids: Set<Int>,
val parentIds: Set<Int>,
val seriesNames: List<String>,
val names: List<String>,
val parentName: String?

View file

@ -1740,9 +1740,9 @@ object VideoDownloadManager {
fun deleteFilesAndUpdateSettings(
context: Context,
ids: List<Int>,
ids: Set<Int>,
scope: CoroutineScope,
onComplete: (List<Int>) -> Unit = {}
onComplete: (Set<Int>) -> Unit = {}
) {
scope.launchSafe(Dispatchers.IO) {
val deleteJobs = ids.map { id ->
@ -1752,11 +1752,11 @@ object VideoDownloadManager {
}
val results = deleteJobs.awaitAll()
val successfulDeletes = results.filter { it.second }.map { it.first }
val failedDeletes = results.filterNot { it.second }
val (successfulResults, failedResults) = results.partition { it.second }
val successfulIds = successfulResults.map { it.first }.toSet()
if (failedDeletes.isNotEmpty()) {
failedDeletes.forEach { (id, _) ->
if (failedResults.isNotEmpty()) {
failedResults.forEach { (id, _) ->
// TODO show a toast if some failed?
Log.e("FileDeletion", "Failed to delete file with ID: $id")
}
@ -1764,7 +1764,7 @@ object VideoDownloadManager {
Log.i("FileDeletion", "All files deleted successfully")
}
onComplete.invoke(successfulDeletes)
onComplete.invoke(successfulIds)
}
}