Fix selection in library (#8233)

This commit is contained in:
AntsyLich 2022-10-18 19:32:34 +06:00 committed by GitHub
parent 9c4051a5ba
commit ea092fa175
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 31 deletions

View File

@ -11,9 +11,9 @@ data class LibraryManga(
val chapterFetchedAt: Long,
val lastRead: Long,
) {
val totalChapters
get() = readCount + unreadCount
val id: Long = manga.id
val hasStarted
get() = readCount > 0
val totalChapters = readCount + unreadCount
val hasStarted = readCount > 0
}

View File

@ -13,6 +13,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.util.fastAny
import eu.kanade.domain.library.model.LibraryManga
import eu.kanade.domain.manga.model.MangaCover
import eu.kanade.tachiyomi.ui.library.LibraryItem
@ -49,7 +50,7 @@ fun LibraryComfortableGrid(
showUnreadBadge = showUnreadBadges,
showLocalBadge = showLocalBadges,
showLanguageBadge = showLanguageBadges,
isSelected = libraryItem.libraryManga in selection,
isSelected = selection.fastAny { it.id == libraryItem.libraryManga.id },
onClick = onClick,
onLongClick = onLongClick,
)

View File

@ -23,6 +23,7 @@ import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.util.fastAny
import eu.kanade.domain.library.model.LibraryManga
import eu.kanade.tachiyomi.ui.library.LibraryItem
@ -58,7 +59,7 @@ fun LibraryCompactGrid(
showUnreadBadge = showUnreadBadges,
showLocalBadge = showLocalBadges,
showLanguageBadge = showLanguageBadges,
isSelected = libraryItem.libraryManga in selection,
isSelected = selection.fastAny { it.id == libraryItem.libraryManga.id },
onClick = onClick,
onLongClick = onLongClick,
)

View File

@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.util.fastAny
import eu.kanade.domain.library.model.LibraryManga
import eu.kanade.tachiyomi.ui.library.LibraryItem
@ -41,7 +42,7 @@ fun LibraryCoverOnlyGrid(
showUnreadBadge = showUnreadBadges,
showLocalBadge = showLocalBadges,
showLanguageBadge = showLanguageBadges,
isSelected = libraryItem.libraryManga in selection,
isSelected = selection.fastAny { it.id == libraryItem.libraryManga.id },
onClick = onClick,
onLongClick = onLongClick,
)

View File

@ -18,6 +18,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.fastAny
import androidx.compose.ui.zIndex
import eu.kanade.domain.library.model.LibraryManga
import eu.kanade.domain.manga.model.MangaCover
@ -25,7 +26,6 @@ import eu.kanade.presentation.components.BadgeGroup
import eu.kanade.presentation.components.FastScrollLazyColumn
import eu.kanade.presentation.components.MangaCover.Square
import eu.kanade.presentation.util.horizontalPadding
import eu.kanade.presentation.util.plus
import eu.kanade.presentation.util.selectedBackground
import eu.kanade.presentation.util.verticalPadding
import eu.kanade.tachiyomi.R
@ -70,7 +70,7 @@ fun LibraryList(
showUnreadBadge = showUnreadBadges,
showLocalBadge = showLocalBadges,
showLanguageBadge = showLanguageBadges,
isSelected = libraryItem.libraryManga in selection,
isSelected = selection.fastAny { it.id == libraryItem.libraryManga.id },
onClick = onClick,
onLongClick = onLongClick,
)

View File

@ -588,19 +588,14 @@ class LibraryPresenter(
state.selection = emptyList()
}
private fun removeSelected(mutableList: MutableList<LibraryManga>, manga: LibraryManga): Boolean {
if (selection.fastAny { it.manga.id == manga.manga.id }) {
return mutableList.remove(manga)
}
return false
}
fun toggleSelection(manga: LibraryManga) {
val mutableList = state.selection.toMutableList()
if (!removeSelected(mutableList, manga)) {
mutableList.add(manga)
state.selection = selection.toMutableList().apply {
if (fastAny { it.id == manga.id }) {
removeAll { it.id == manga.id }
} else {
add(manga)
}
}
state.selection = mutableList
}
/**
@ -608,22 +603,22 @@ class LibraryPresenter(
* same category as the given manga
*/
fun toggleRangeSelection(manga: LibraryManga) {
val mutableList = state.selection.toMutableList()
if (!removeSelected(mutableList, manga) && mutableList.fastAny
{ it.category == manga.category }
) {
state.selection = selection.toMutableList().apply {
val lastSelected = lastOrNull()
if (lastSelected == null || lastSelected.category != manga.category) {
add(manga)
return@apply
}
val items = (loadedManga[manga.category] ?: emptyList()).map { it.libraryManga }
val lastMangaIndex = items.indexOf(mutableList.findLast { it.category == manga.category })
val lastMangaIndex = items.indexOf(lastSelected)
val curMangaIndex = items.indexOf(manga)
val newList = when (lastMangaIndex >= curMangaIndex + 1) {
val selectedIds = map { it.id }
val newSelections = when (lastMangaIndex >= curMangaIndex + 1) {
true -> items.subList(curMangaIndex, lastMangaIndex)
false -> items.subList(lastMangaIndex, curMangaIndex + 1)
}
mutableList.addAll(newList.filterNot { it in selection })
} else {
mutableList.add(manga)
}.filterNot { it.id in selectedIds }
addAll(newSelections)
}
state.selection = mutableList
}
fun selectAll(index: Int) {