Use data class and minor performance improvements

This commit is contained in:
Luna712 2024-07-10 15:16:33 -06:00 committed by GitHub
parent df603b0838
commit a26aeb6534
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -28,6 +28,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class DownloadViewModel : ViewModel() { class DownloadViewModel : ViewModel() {
private val _headerCards = private val _headerCards =
MutableLiveData<List<VisualDownloadCached.Header>>().apply { listOf<VisualDownloadCached.Header>() } MutableLiveData<List<VisualDownloadCached.Header>>().apply { listOf<VisualDownloadCached.Header>() }
val headerCards: LiveData<List<VisualDownloadCached.Header>> = _headerCards val headerCards: LiveData<List<VisualDownloadCached.Header>> = _headerCards
@ -249,23 +250,23 @@ class DownloadViewModel : ViewModel() {
val selectedItemsList = selectedItems.value ?: emptyList() val selectedItemsList = selectedItems.value ?: emptyList()
val ids = mutableListOf<Int>() val ids = mutableListOf<Int>()
var parentName: String? = null
val seriesNames = mutableListOf<String>() val seriesNames = mutableListOf<String>()
val names = mutableListOf<String>() val names = mutableListOf<String>()
var parentName: String? = null
selectedItemsList.forEach { item -> selectedItemsList.forEach { item ->
when (item) { when (item) {
is VisualDownloadCached.Header -> { is VisualDownloadCached.Header -> {
if (item.data.type.isEpisodeBased()) { if (item.data.type.isEpisodeBased()) {
context.getKeys(DOWNLOAD_EPISODE_CACHE) val episodes = context.getKeys(DOWNLOAD_EPISODE_CACHE)
.mapNotNull { .mapNotNull {
context.getKey<VideoDownloadHelper.DownloadEpisodeCached>( context.getKey<VideoDownloadHelper.DownloadEpisodeCached>(
it it
) )
} }
.filter { it.parentId == item.data.id } .filter { it.parentId == item.data.id }
.mapTo(ids) { it.id } .map { it.id }
ids.addAll(episodes)
val episodeInfo = "${item.data.name} (${item.totalDownloads} ${ val episodeInfo = "${item.data.name} (${item.totalDownloads} ${
context.resources.getQuantityString( context.resources.getQuantityString(
@ -298,14 +299,14 @@ class DownloadViewModel : ViewModel() {
} }
} }
val data = Triple(parentName, seriesNames.toList(), names.toList()) val data = DeleteConfirmationData(parentName, seriesNames.toList(), names.toList())
showDeleteConfirmationDialog(context, ids, data, onDeleteConfirm) showDeleteConfirmationDialog(context, ids, data, onDeleteConfirm)
} }
private fun showDeleteConfirmationDialog( private fun showDeleteConfirmationDialog(
context: Context, context: Context,
ids: List<Int>, ids: List<Int>,
data: Triple<String?, List<String>, List<String>>, data: DeleteConfirmationData,
onDeleteConfirm: () -> Unit onDeleteConfirm: () -> Unit
) { ) {
val (parentName, seriesNames, names) = data val (parentName, seriesNames, names) = data
@ -317,19 +318,16 @@ class DownloadViewModel : ViewModel() {
seriesNames.isNotEmpty() && names.isEmpty() -> { seriesNames.isNotEmpty() && names.isEmpty() -> {
context.getString(R.string.delete_message_series_only).format(formattedSeriesNames) context.getString(R.string.delete_message_series_only).format(formattedSeriesNames)
} }
parentName != null && names.isNotEmpty() -> { parentName != null && names.isNotEmpty() -> {
context.getString(R.string.delete_message_series_episodes) context.getString(R.string.delete_message_series_episodes)
.format(parentName, formattedNames) .format(parentName, formattedNames)
} }
seriesNames.isNotEmpty() -> { seriesNames.isNotEmpty() -> {
val seriesSection = context.getString(R.string.delete_message_series_section) val seriesSection = context.getString(R.string.delete_message_series_section)
.format(formattedSeriesNames) .format(formattedSeriesNames)
context.getString(R.string.delete_message_multiple) context.getString(R.string.delete_message_multiple)
.format(formattedNames) + "\n\n" + seriesSection .format(formattedNames) + "\n\n" + seriesSection
} }
else -> context.getString(R.string.delete_message_multiple).format(formattedNames) else -> context.getString(R.string.delete_message_multiple).format(formattedNames)
} }
@ -344,7 +342,6 @@ class DownloadViewModel : ViewModel() {
onDeleteConfirm.invoke() onDeleteConfirm.invoke()
} }
} }
DialogInterface.BUTTON_NEGATIVE -> { DialogInterface.BUTTON_NEGATIVE -> {
// Do nothing on cancel // Do nothing on cancel
} }
@ -361,4 +358,10 @@ class DownloadViewModel : ViewModel() {
logError(e) logError(e)
} }
} }
private data class DeleteConfirmationData(
val parentName: String?,
val seriesNames: List<String>,
val names: List<String>
)
} }