logic and a bit of cleanup

This commit is contained in:
Deltashot 2022-10-09 22:27:44 +02:00
parent 50f959e5f4
commit 1bee855766
3 changed files with 31 additions and 2 deletions

View File

@ -70,6 +70,7 @@ fun LibraryScreen(
onChangeCurrentPage = { presenter.activeCategory = it }, onChangeCurrentPage = { presenter.activeCategory = it },
onMangaClicked = onMangaClicked, onMangaClicked = onMangaClicked,
onToggleSelection = { presenter.toggleSelection(it) }, onToggleSelection = { presenter.toggleSelection(it) },
onToggleMultiSelection = { presenter.toggleMultiSelection(it) },
onRefresh = onClickRefresh, onRefresh = onClickRefresh,
onGlobalSearchClicked = onGlobalSearchClicked, onGlobalSearchClicked = onGlobalSearchClicked,
getNumberOfMangaForCategory = { presenter.getMangaCountForCategory(it) }, getNumberOfMangaForCategory = { presenter.getMangaCountForCategory(it) },

View File

@ -45,6 +45,7 @@ fun LibraryContent(
onChangeCurrentPage: (Int) -> Unit, onChangeCurrentPage: (Int) -> Unit,
onMangaClicked: (Long) -> Unit, onMangaClicked: (Long) -> Unit,
onToggleSelection: (LibraryManga) -> Unit, onToggleSelection: (LibraryManga) -> Unit,
onToggleMultiSelection: (LibraryManga) -> Unit,
onRefresh: (Category?) -> Boolean, onRefresh: (Category?) -> Boolean,
onGlobalSearchClicked: () -> Unit, onGlobalSearchClicked: () -> Unit,
getNumberOfMangaForCategory: @Composable (Long) -> State<Int?>, getNumberOfMangaForCategory: @Composable (Long) -> State<Int?>,
@ -85,7 +86,7 @@ fun LibraryContent(
} }
} }
val onLongClickManga = { manga: LibraryManga -> val onLongClickManga = { manga: LibraryManga ->
onToggleSelection(manga) onToggleMultiSelection(manga)
} }
SwipeRefresh( SwipeRefresh(

View File

@ -667,11 +667,38 @@ class LibraryPresenter(
val mutableList = state.selection.toMutableList() val mutableList = state.selection.toMutableList()
if (selection.fastAny { it.manga.id == manga.manga.id }) { if (selection.fastAny { it.manga.id == manga.manga.id }) {
mutableList.remove(manga) mutableList.remove(manga)
state.selection = mutableList
} else { } else {
mutableList.add(manga) mutableList.add(manga)
}
state.selection = mutableList state.selection = mutableList
} }
}
fun toggleMultiSelection(manga: LibraryManga) {
val mutableList = state.selection.toMutableList()
if (selection.fastAny { it.manga.id == manga.manga.id }) {
mutableList.remove(manga)
state.selection = mutableList
} else if (mutableList.fastAny{ it.category == manga.category }) {
val items = (loadedManga[manga.category] ?: emptyList()).map { it.libraryManga }
val lastMangaIndex =
items.indexOf(mutableList.findLast { it.category == manga.category })
val curMangaIndex = items.indexOf(manga)
val newList = when (lastMangaIndex >= curMangaIndex + 1) {
true -> items.subList(curMangaIndex, lastMangaIndex)
false -> items.subList(lastMangaIndex, curMangaIndex + 1)
}
state.selection = state.selection.toMutableList().apply {
addAll(newList.filterNot { it in selection }.map { it })
}
} else {
mutableList.add(manga)
state.selection = mutableList
}
}
fun selectAll(index: Int) { fun selectAll(index: Int) {
val category = categories[index] val category = categories[index]