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

View file

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