Retain previous selected state when updating list states

Fixes #8417
This commit is contained in:
arkon
2022-11-13 22:27:59 -05:00
parent b1ccebf329
commit 10e349f76e
7 changed files with 79 additions and 56 deletions
@@ -4,6 +4,7 @@ import android.os.Bundle
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import eu.kanade.core.util.addOrRemove
import eu.kanade.domain.base.BasePreferences
import eu.kanade.domain.chapter.interactor.GetChapter
import eu.kanade.domain.chapter.interactor.SetReadStatus
@@ -72,6 +73,7 @@ class UpdatesPresenter(
// First and last selected index in list
private val selectedPositions: Array<Int> = arrayOf(-1, -1)
private val selectedChapterIds: HashSet<Long> = HashSet()
override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState)
@@ -100,7 +102,7 @@ class UpdatesPresenter(
presenterScope.launchIO {
downloadManager.queue.statusFlow()
.catch { error -> logcat(LogPriority.ERROR, error) }
.catch { logcat(LogPriority.ERROR, it) }
.collect {
withUIContext {
updateDownloadState(it)
@@ -110,7 +112,7 @@ class UpdatesPresenter(
presenterScope.launchIO {
downloadManager.queue.progressFlow()
.catch { error -> logcat(LogPriority.ERROR, error) }
.catch { logcat(LogPriority.ERROR, it) }
.collect {
withUIContext {
updateDownloadState(it)
@@ -120,27 +122,26 @@ class UpdatesPresenter(
}
private fun List<UpdatesWithRelations>.toUpdateItems(): List<UpdatesItem> {
return this
.distinctBy { it.chapterId }
.map {
val activeDownload = downloadManager.queue.find { download -> it.chapterId == download.chapter.id }
val downloaded = downloadManager.isChapterDownloaded(
it.chapterName,
it.scanlator,
it.mangaTitle,
it.sourceId,
)
val downloadState = when {
activeDownload != null -> activeDownload.status
downloaded -> Download.State.DOWNLOADED
else -> Download.State.NOT_DOWNLOADED
}
UpdatesItem(
update = it,
downloadStateProvider = { downloadState },
downloadProgressProvider = { activeDownload?.progress ?: 0 },
)
return this.map {
val activeDownload = downloadManager.queue.find { download -> it.chapterId == download.chapter.id }
val downloaded = downloadManager.isChapterDownloaded(
it.chapterName,
it.scanlator,
it.mangaTitle,
it.sourceId,
)
val downloadState = when {
activeDownload != null -> activeDownload.status
downloaded -> Download.State.DOWNLOADED
else -> Download.State.NOT_DOWNLOADED
}
UpdatesItem(
update = it,
downloadStateProvider = { downloadState },
downloadProgressProvider = { activeDownload?.progress ?: 0 },
selected = it.chapterId in selectedChapterIds,
)
}
}
/**
@@ -155,12 +156,14 @@ class UpdatesPresenter(
}
if (modifiedIndex < 0) return@apply
val item = removeAt(modifiedIndex)
.copy(
val item = get(modifiedIndex)
set(
modifiedIndex,
item.copy(
downloadStateProvider = { download.status },
downloadProgressProvider = { download.progress },
)
add(modifiedIndex, item)
),
)
}
}
@@ -281,6 +284,7 @@ class UpdatesPresenter(
val firstSelection = none { it.selected }
set(selectedIndex, selectedItem.copy(selected = selected))
selectedChapterIds.addOrRemove(item.update.chapterId, selected)
if (selected && userSelected && fromLongPress) {
if (firstSelection) {
@@ -303,6 +307,7 @@ class UpdatesPresenter(
range.forEach {
val inbetweenItem = get(it)
if (!inbetweenItem.selected) {
selectedChapterIds.add(inbetweenItem.update.chapterId)
set(it, inbetweenItem.copy(selected = true))
}
}
@@ -327,6 +332,7 @@ class UpdatesPresenter(
fun toggleAllSelection(selected: Boolean) {
state.items = items.map {
selectedChapterIds.addOrRemove(it.update.chapterId, selected)
it.copy(selected = selected)
}
selectedPositions[0] = -1
@@ -335,6 +341,7 @@ class UpdatesPresenter(
fun invertSelection() {
state.items = items.map {
selectedChapterIds.addOrRemove(it.update.chapterId, !it.selected)
it.copy(selected = !it.selected)
}
selectedPositions[0] = -1