Use immutable collections in more places

This commit is contained in:
arkon
2023-11-11 22:43:50 -05:00
parent dd998be1e7
commit 336221a972
35 changed files with 252 additions and 152 deletions
@@ -21,6 +21,10 @@ import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.util.lang.toDateKey
import eu.kanade.tachiyomi.util.lang.toRelativeString
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.mutate
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
@@ -106,27 +110,29 @@ class UpdatesScreenModel(
}
}
private fun List<UpdatesWithRelations>.toUpdateItems(): List<UpdatesItem> {
return this.map { update ->
val activeDownload = downloadManager.getQueuedDownloadOrNull(update.chapterId)
val downloaded = downloadManager.isChapterDownloaded(
update.chapterName,
update.scanlator,
update.mangaTitle,
update.sourceId,
)
val downloadState = when {
activeDownload != null -> activeDownload.status
downloaded -> Download.State.DOWNLOADED
else -> Download.State.NOT_DOWNLOADED
private fun List<UpdatesWithRelations>.toUpdateItems(): PersistentList<UpdatesItem> {
return this
.map { update ->
val activeDownload = downloadManager.getQueuedDownloadOrNull(update.chapterId)
val downloaded = downloadManager.isChapterDownloaded(
update.chapterName,
update.scanlator,
update.mangaTitle,
update.sourceId,
)
val downloadState = when {
activeDownload != null -> activeDownload.status
downloaded -> Download.State.DOWNLOADED
else -> Download.State.NOT_DOWNLOADED
}
UpdatesItem(
update = update,
downloadStateProvider = { downloadState },
downloadProgressProvider = { activeDownload?.progress ?: 0 },
selected = update.chapterId in selectedChapterIds,
)
}
UpdatesItem(
update = update,
downloadStateProvider = { downloadState },
downloadProgressProvider = { activeDownload?.progress ?: 0 },
selected = update.chapterId in selectedChapterIds,
)
}
.toPersistentList()
}
fun updateLibrary(): Boolean {
@@ -144,17 +150,14 @@ class UpdatesScreenModel(
*/
private fun updateDownloadState(download: Download) {
mutableState.update { state ->
val newItems = state.items.toMutableList().apply {
val modifiedIndex = indexOfFirst { it.update.chapterId == download.chapter.id }
if (modifiedIndex < 0) return@apply
val newItems = state.items.mutate { list ->
val modifiedIndex = list.indexOfFirst { it.update.chapterId == download.chapter.id }
if (modifiedIndex < 0) return@mutate
val item = get(modifiedIndex)
set(
modifiedIndex,
item.copy(
downloadStateProvider = { download.status },
downloadProgressProvider = { download.progress },
),
val item = list[modifiedIndex]
list[modifiedIndex] = item.copy(
downloadStateProvider = { download.status },
downloadProgressProvider = { download.progress },
)
}
state.copy(items = newItems)
@@ -330,7 +333,7 @@ class UpdatesScreenModel(
}
}
}
state.copy(items = newItems)
state.copy(items = newItems.toPersistentList())
}
}
@@ -340,7 +343,7 @@ class UpdatesScreenModel(
selectedChapterIds.addOrRemove(it.update.chapterId, selected)
it.copy(selected = selected)
}
state.copy(items = newItems)
state.copy(items = newItems.toPersistentList())
}
selectedPositions[0] = -1
@@ -353,7 +356,7 @@ class UpdatesScreenModel(
selectedChapterIds.addOrRemove(it.update.chapterId, !it.selected)
it.copy(selected = !it.selected)
}
state.copy(items = newItems)
state.copy(items = newItems.toPersistentList())
}
selectedPositions[0] = -1
selectedPositions[1] = -1
@@ -370,7 +373,7 @@ class UpdatesScreenModel(
@Immutable
data class State(
val isLoading: Boolean = true,
val items: List<UpdatesItem> = emptyList(),
val items: PersistentList<UpdatesItem> = persistentListOf(),
val dialog: Dialog? = null,
) {
val selected = items.filter { it.selected }