Support batch chapter update api

This commit is contained in:
Syer10
2022-11-15 14:27:46 -05:00
parent 49bee53a67
commit fab3907d08
14 changed files with 473 additions and 65 deletions

View File

@@ -87,6 +87,10 @@ data class ChapterDownloadItem(
_downloadState.value = ChapterDownloadState.NotDownloaded
}
fun setNotDownloaded() {
_downloadState.value = ChapterDownloadState.NotDownloaded
}
fun isSelected(selectedItems: List<Long>): Boolean {
return (chapter.id in selectedItems).also { _isSelected.value = it }
}

View File

@@ -13,12 +13,11 @@ import ca.gosyer.jui.domain.category.interactor.GetCategories
import ca.gosyer.jui.domain.category.interactor.GetMangaCategories
import ca.gosyer.jui.domain.category.interactor.RemoveMangaFromCategory
import ca.gosyer.jui.domain.category.model.Category
import ca.gosyer.jui.domain.chapter.interactor.BatchUpdateChapter
import ca.gosyer.jui.domain.chapter.interactor.DeleteChapterDownload
import ca.gosyer.jui.domain.chapter.interactor.GetChapters
import ca.gosyer.jui.domain.chapter.interactor.RefreshChapters
import ca.gosyer.jui.domain.chapter.interactor.UpdateChapterBookmarked
import ca.gosyer.jui.domain.chapter.interactor.UpdateChapterMarkPreviousRead
import ca.gosyer.jui.domain.chapter.interactor.UpdateChapterRead
import ca.gosyer.jui.domain.chapter.model.Chapter
import ca.gosyer.jui.domain.download.interactor.BatchChapterDownload
import ca.gosyer.jui.domain.download.interactor.QueueChapterDownload
@@ -60,8 +59,7 @@ class MangaScreenViewModel @Inject constructor(
private val refreshManga: RefreshManga,
private val getChapters: GetChapters,
private val refreshChapters: RefreshChapters,
private val updateChapterRead: UpdateChapterRead,
private val updateChapterBookmarked: UpdateChapterBookmarked,
private val batchUpdateChapter: BatchUpdateChapter,
private val updateChapterMarkPreviousRead: UpdateChapterMarkPreviousRead,
private val queueChapterDownload: QueueChapterDownload,
private val stopChapterDownload: StopChapterDownload,
@@ -229,35 +227,29 @@ class MangaScreenViewModel @Inject constructor(
}
}
private fun findChapter(index: Int) = chapters.value.find { it.chapter.index == index }?.chapter
private fun setRead(index: Int, read: Boolean) {
val chapter = findChapter(index) ?: return
if (chapter.read == read) return
private fun setRead(chapterIds: List<Long>, read: Boolean) {
scope.launch {
manga.value?.let { manga ->
updateChapterRead.await(manga, index, read = read, onError = { toast(it.message.orEmpty()) })
batchUpdateChapter.await(manga, chapterIds, isRead = read, onError = { toast(it.message.orEmpty()) })
refreshChaptersAsync(manga.id).await()
_selectedIds.value = _selectedIds.value.minus(chapter.id).toImmutableList()
_selectedIds.value = persistentListOf()
}
}
}
fun markRead(index: Int) = setRead(index, true)
fun markUnread(index: Int) = setRead(index, false)
fun markRead(id: Long?) = setRead(listOfNotNull(id).ifEmpty { _selectedIds.value }, true)
fun markUnread(id: Long?) = setRead(listOfNotNull(id).ifEmpty { _selectedIds.value }, false)
private fun setBookmarked(index: Int, bookmark: Boolean) {
val chapter = findChapter(index) ?: return
if (chapter.bookmarked == bookmark) return
private fun setBookmarked(chapterIds: List<Long>, bookmark: Boolean) {
scope.launch {
manga.value?.let { manga ->
updateChapterBookmarked.await(manga, index, bookmarked = bookmark, onError = { toast(it.message.orEmpty()) })
batchUpdateChapter.await(manga, chapterIds, isBookmarked = bookmark, onError = { toast(it.message.orEmpty()) })
refreshChaptersAsync(manga.id).await()
_selectedIds.value = _selectedIds.value.minus(chapter.id).toImmutableList()
_selectedIds.value = persistentListOf()
}
}
}
fun bookmarkChapter(index: Int) = setBookmarked(index, true)
fun unBookmarkChapter(index: Int) = setBookmarked(index, false)
fun bookmarkChapter(id: Long?) = setBookmarked(listOfNotNull(id).ifEmpty { _selectedIds.value }, true)
fun unBookmarkChapter(id: Long?) = setBookmarked(listOfNotNull(id).ifEmpty { _selectedIds.value }, false)
fun markPreviousRead(index: Int) {
scope.launch {
@@ -275,10 +267,20 @@ class MangaScreenViewModel @Inject constructor(
}
}
fun deleteDownload(index: Int) {
fun deleteDownload(id: Long?) {
scope.launch {
chapters.value.find { it.chapter.index == index }
?.deleteDownload(deleteChapterDownload)
if (id == null) {
val manga = _manga.value ?: return@launch
val chapterIds = _selectedIds.value
batchUpdateChapter.await(manga, chapterIds, delete = true, onError = { toast(it.message.orEmpty()) })
chapterIds.forEach { id ->
chapters.value.find { it.chapter.id == id }?.setNotDownloaded()
}
_selectedIds.value = persistentListOf()
} else {
chapters.value.find { it.chapter.id == id }
?.deleteDownload(deleteChapterDownload)
}
}
}

View File

@@ -59,14 +59,14 @@ fun ChapterItem(
chapterDownload: ChapterDownloadItem,
format: (Instant) -> String,
onClick: (Int) -> Unit,
markRead: (Int) -> Unit,
markUnread: (Int) -> Unit,
bookmarkChapter: (Int) -> Unit,
unBookmarkChapter: (Int) -> Unit,
markRead: (Long) -> Unit,
markUnread: (Long) -> Unit,
bookmarkChapter: (Long) -> Unit,
unBookmarkChapter: (Long) -> Unit,
markPreviousAsRead: (Int) -> Unit,
onClickDownload: (Int) -> Unit,
onClickStopDownload: (Int) -> Unit,
onClickDeleteChapter: (Int) -> Unit,
onClickDeleteChapter: (Long) -> Unit,
onSelectChapter: (Int) -> Unit,
onUnselectChapter: (Int) -> Unit
) {
@@ -79,10 +79,10 @@ fun ChapterItem(
.selectedBackground(isSelected)
.chapterItemModifier(
onClick = { onClick(chapter.index) },
markRead = { markRead(chapter.index) }.takeUnless { chapter.read },
markUnread = { markUnread(chapter.index) }.takeIf { chapter.read },
bookmarkChapter = { bookmarkChapter(chapter.index) }.takeUnless { chapter.bookmarked },
unBookmarkChapter = { unBookmarkChapter(chapter.index) }.takeIf { chapter.bookmarked },
markRead = { markRead(chapter.id) }.takeUnless { chapter.read },
markUnread = { markUnread(chapter.id) }.takeIf { chapter.read },
bookmarkChapter = { bookmarkChapter(chapter.id) }.takeUnless { chapter.bookmarked },
unBookmarkChapter = { unBookmarkChapter(chapter.id) }.takeIf { chapter.bookmarked },
markPreviousAsRead = { markPreviousAsRead(chapter.index) },
onSelectChapter = { onSelectChapter(chapter.index) }.takeUnless { chapterDownload.isSelected.value },
onUnselectChapter = { onUnselectChapter(chapter.index) }.takeIf { chapterDownload.isSelected.value }
@@ -157,7 +157,7 @@ fun ChapterItem(
chapterDownload,
{ onClickDownload(it.index) },
{ onClickStopDownload(it.index) },
{ onClickDeleteChapter(it.index) }
{ onClickDeleteChapter(it.id) }
)
}
}

View File

@@ -97,13 +97,13 @@ fun MangaScreenContent(
downloadNext: (Int) -> Unit,
downloadUnread: () -> Unit,
downloadAll: () -> Unit,
markRead: (Int) -> Unit,
markUnread: (Int) -> Unit,
bookmarkChapter: (Int) -> Unit,
unBookmarkChapter: (Int) -> Unit,
markRead: (Long?) -> Unit,
markUnread: (Long?) -> Unit,
bookmarkChapter: (Long?) -> Unit,
unBookmarkChapter: (Long?) -> Unit,
markPreviousRead: (Int) -> Unit,
downloadChapter: (Int) -> Unit,
deleteDownload: (Int) -> Unit,
deleteDownload: (Long?) -> Unit,
stopDownloadingChapter: (Int) -> Unit,
onSelectChapter: (Int) -> Unit,
onUnselectChapter: (Int) -> Unit,
@@ -178,12 +178,12 @@ fun MangaScreenContent(
visible = inActionMode,
items = getBottomActionItems(
selectedItems = selectedItems,
markRead = markRead,
markUnread = markUnread,
bookmarkChapter = bookmarkChapter,
unBookmarkChapter = unBookmarkChapter,
markRead = { markRead(null) },
markUnread = { markUnread(null) },
bookmarkChapter = { bookmarkChapter(null) },
unBookmarkChapter = { unBookmarkChapter(null) },
markPreviousAsRead = markPreviousRead,
deleteChapter = deleteDownload,
deleteChapter = { deleteDownload(null) },
downloadChapters = downloadChapters
)
)
@@ -366,35 +366,35 @@ private fun getActionModeActionItems(
@Stable
private fun getBottomActionItems(
selectedItems: ImmutableList<ChapterDownloadItem>,
markRead: (Int) -> Unit,
markUnread: (Int) -> Unit,
bookmarkChapter: (Int) -> Unit,
unBookmarkChapter: (Int) -> Unit,
markRead: () -> Unit,
markUnread: () -> Unit,
bookmarkChapter: () -> Unit,
unBookmarkChapter: () -> Unit,
markPreviousAsRead: (Int) -> Unit,
deleteChapter: (Int) -> Unit,
deleteChapter: () -> Unit,
downloadChapters: () -> Unit
): ImmutableList<BottomActionItem> {
return listOfNotNull(
BottomActionItem(
name = stringResource(MR.strings.action_bookmark),
icon = Icons.Rounded.BookmarkAdd,
onClick = { bookmarkChapter(selectedItems.first().chapter.index) }
).takeIf { selectedItems.fastAny { !it.chapter.bookmarked } && selectedItems.size == 1 },
onClick = bookmarkChapter
).takeIf { selectedItems.fastAny { !it.chapter.bookmarked } },
BottomActionItem(
name = stringResource(MR.strings.action_remove_bookmark),
icon = Icons.Rounded.BookmarkRemove,
onClick = { unBookmarkChapter(selectedItems.first().chapter.index) }
).takeIf { selectedItems.fastAny { it.chapter.bookmarked } && selectedItems.size == 1 },
onClick = unBookmarkChapter
).takeIf { selectedItems.fastAny { it.chapter.bookmarked } },
BottomActionItem(
name = stringResource(MR.strings.action_mark_as_read),
icon = Icons.Rounded.DoneAll,
onClick = { markRead(selectedItems.first().chapter.index) }
).takeIf { selectedItems.fastAny { !it.chapter.read } && selectedItems.size == 1 },
onClick = markRead
).takeIf { selectedItems.fastAny { !it.chapter.read } },
BottomActionItem(
name = stringResource(MR.strings.action_mark_as_unread),
icon = Icons.Rounded.RemoveDone,
onClick = { markUnread(selectedItems.first().chapter.index) }
).takeIf { selectedItems.fastAny { it.chapter.read } && selectedItems.size == 1 },
onClick = markUnread
).takeIf { selectedItems.fastAny { it.chapter.read } },
BottomActionItem(
name = stringResource(MR.strings.action_mark_previous_read),
icon = JuiAssets.DonePrev,
@@ -408,7 +408,7 @@ private fun getBottomActionItems(
BottomActionItem(
name = stringResource(MR.strings.action_delete),
icon = Icons.Rounded.Delete,
onClick = { deleteChapter(selectedItems.first().chapter.index) }
).takeIf { selectedItems.fastAny { it.downloadState.value == ChapterDownloadState.Downloaded } && selectedItems.size == 1 }
onClick = deleteChapter
).takeIf { selectedItems.fastAny { it.downloadState.value == ChapterDownloadState.Downloaded } }
).toImmutableList()
}