Minor cleanup

This commit is contained in:
arkon 2022-10-14 16:13:50 -04:00
parent 7e92921f84
commit bc1fbfac9d
21 changed files with 60 additions and 75 deletions

View File

@ -109,9 +109,9 @@ class MangaRepositoryImpl(
}
}
override suspend fun updateAll(values: List<MangaUpdate>): Boolean {
override suspend fun updateAll(mangaUpdates: List<MangaUpdate>): Boolean {
return try {
partialUpdate(*values.toTypedArray())
partialUpdate(*mangaUpdates.toTypedArray())
true
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
@ -119,9 +119,9 @@ class MangaRepositoryImpl(
}
}
private suspend fun partialUpdate(vararg values: MangaUpdate) {
private suspend fun partialUpdate(vararg mangaUpdates: MangaUpdate) {
handler.await(inTransaction = true) {
values.forEach { value ->
mangaUpdates.forEach { value ->
mangasQueries.update(
source = value.source,
url = value.url,

View File

@ -44,9 +44,9 @@ class TrackRepositoryImpl(
insertValues(*tracks.toTypedArray())
}
private suspend fun insertValues(vararg values: Track) {
private suspend fun insertValues(vararg tracks: Track) {
handler.await(inTransaction = true) {
values.forEach { mangaTrack ->
tracks.forEach { mangaTrack ->
manga_syncQueries.insert(
mangaId = mangaTrack.mangaId,
syncId = mangaTrack.syncId,

View File

@ -32,7 +32,7 @@ import eu.kanade.domain.download.interactor.DeleteDownload
import eu.kanade.domain.extension.interactor.GetExtensionLanguages
import eu.kanade.domain.extension.interactor.GetExtensionSources
import eu.kanade.domain.extension.interactor.GetExtensionsByType
import eu.kanade.domain.history.interactor.DeleteHistoryTable
import eu.kanade.domain.history.interactor.DeleteAllHistory
import eu.kanade.domain.history.interactor.GetHistory
import eu.kanade.domain.history.interactor.GetNextChapter
import eu.kanade.domain.history.interactor.RemoveHistoryById
@ -117,7 +117,7 @@ class DomainModule : InjektModule {
addFactory { SyncChaptersWithTrackServiceTwoWay(get(), get()) }
addSingletonFactory<HistoryRepository> { HistoryRepositoryImpl(get()) }
addFactory { DeleteHistoryTable(get()) }
addFactory { DeleteAllHistory(get()) }
addFactory { GetHistory(get()) }
addFactory { UpsertHistory(get()) }
addFactory { RemoveHistoryById(get()) }

View File

@ -4,9 +4,8 @@ import eu.kanade.domain.category.model.Category
import eu.kanade.domain.category.model.anyWithName
import eu.kanade.domain.category.repository.CategoryRepository
import eu.kanade.domain.library.service.LibraryPreferences
import eu.kanade.tachiyomi.util.lang.withNonCancellableContext
import eu.kanade.tachiyomi.util.system.logcat
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.withContext
import logcat.LogPriority
class CreateCategoryWithName(
@ -22,10 +21,10 @@ class CreateCategoryWithName(
sort.direction.flag
}
suspend fun await(name: String): Result = withContext(NonCancellable) {
suspend fun await(name: String): Result = withNonCancellableContext {
val categories = categoryRepository.getAll()
if (categories.anyWithName(name)) {
return@withContext Result.NameAlreadyExistsError
return@withNonCancellableContext Result.NameAlreadyExistsError
}
val nextOrder = categories.maxOfOrNull { it.order }?.plus(1) ?: 0

View File

@ -2,21 +2,20 @@ package eu.kanade.domain.category.interactor
import eu.kanade.domain.category.model.CategoryUpdate
import eu.kanade.domain.category.repository.CategoryRepository
import eu.kanade.tachiyomi.util.lang.withNonCancellableContext
import eu.kanade.tachiyomi.util.system.logcat
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.withContext
import logcat.LogPriority
class DeleteCategory(
private val categoryRepository: CategoryRepository,
) {
suspend fun await(categoryId: Long) = withContext(NonCancellable) {
suspend fun await(categoryId: Long) = withNonCancellableContext {
try {
categoryRepository.delete(categoryId)
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
return@withContext Result.InternalError(e)
return@withNonCancellableContext Result.InternalError(e)
}
val categories = categoryRepository.getAll()

View File

@ -4,19 +4,18 @@ import eu.kanade.domain.category.model.Category
import eu.kanade.domain.category.model.CategoryUpdate
import eu.kanade.domain.category.model.anyWithName
import eu.kanade.domain.category.repository.CategoryRepository
import eu.kanade.tachiyomi.util.lang.withNonCancellableContext
import eu.kanade.tachiyomi.util.system.logcat
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.withContext
import logcat.LogPriority
class RenameCategory(
private val categoryRepository: CategoryRepository,
) {
suspend fun await(categoryId: Long, name: String) = withContext(NonCancellable) {
suspend fun await(categoryId: Long, name: String) = withNonCancellableContext {
val categories = categoryRepository.getAll()
if (categories.anyWithName(name)) {
return@withContext Result.NameAlreadyExistsError
return@withNonCancellableContext Result.NameAlreadyExistsError
}
val update = CategoryUpdate(

View File

@ -3,21 +3,20 @@ package eu.kanade.domain.category.interactor
import eu.kanade.domain.category.model.Category
import eu.kanade.domain.category.model.CategoryUpdate
import eu.kanade.domain.category.repository.CategoryRepository
import eu.kanade.tachiyomi.util.lang.withNonCancellableContext
import eu.kanade.tachiyomi.util.system.logcat
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.withContext
import logcat.LogPriority
class ReorderCategory(
private val categoryRepository: CategoryRepository,
) {
suspend fun await(categoryId: Long, newPosition: Int) = withContext(NonCancellable) {
suspend fun await(categoryId: Long, newPosition: Int) = withNonCancellableContext {
val categories = categoryRepository.getAll().filterNot(Category::isSystemCategory)
val currentIndex = categories.indexOfFirst { it.id == categoryId }
if (currentIndex == newPosition) {
return@withContext Result.Unchanged
return@withNonCancellableContext Result.Unchanged
}
val reorderedCategories = categories.toMutableList()

View File

@ -2,14 +2,13 @@ package eu.kanade.domain.category.interactor
import eu.kanade.domain.category.model.CategoryUpdate
import eu.kanade.domain.category.repository.CategoryRepository
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.withContext
import eu.kanade.tachiyomi.util.lang.withNonCancellableContext
class UpdateCategory(
private val categoryRepository: CategoryRepository,
) {
suspend fun await(payload: CategoryUpdate): Result = withContext(NonCancellable) {
suspend fun await(payload: CategoryUpdate): Result = withNonCancellableContext {
try {
categoryRepository.updatePartial(payload)
Result.Success

View File

@ -7,9 +7,8 @@ import eu.kanade.domain.download.interactor.DeleteDownload
import eu.kanade.domain.download.service.DownloadPreferences
import eu.kanade.domain.manga.model.Manga
import eu.kanade.domain.manga.repository.MangaRepository
import eu.kanade.tachiyomi.util.lang.withNonCancellableContext
import eu.kanade.tachiyomi.util.system.logcat
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.withContext
import logcat.LogPriority
class SetReadStatus(
@ -27,38 +26,28 @@ class SetReadStatus(
)
}
suspend fun await(read: Boolean, vararg values: Chapter): Result = withContext(NonCancellable) {
val chapters = values.filterNot { it.read == read }
if (chapters.isEmpty()) {
return@withContext Result.NoChapters
}
val manga = chapters.fold(mutableSetOf<Manga>()) { acc, chapter ->
if (acc.all { it.id != chapter.mangaId }) {
acc += mangaRepository.getMangaById(chapter.mangaId)
}
acc
suspend fun await(read: Boolean, vararg chapters: Chapter): Result = withNonCancellableContext {
val chaptersToUpdate = chapters.filterNot { it.read == read }
if (chaptersToUpdate.isEmpty()) {
return@withNonCancellableContext Result.NoChapters
}
try {
chapterRepository.updateAll(
chapters.map { chapter ->
mapper(chapter, read)
},
chaptersToUpdate.map { mapper(it, read) },
)
} catch (e: Exception) {
logcat(LogPriority.ERROR, e)
return@withContext Result.InternalError(e)
return@withNonCancellableContext Result.InternalError(e)
}
if (read && downloadPreferences.removeAfterMarkedAsRead().get()) {
manga.forEach {
chaptersToUpdate
.groupBy { it.mangaId }
.forEach { (mangaId, chapters) ->
deleteDownload.awaitAll(
manga = it,
values = chapters
.filter { chapter -> it.id == chapter.mangaId }
.toTypedArray(),
manga = mangaRepository.getMangaById(mangaId),
chapters = chapters.toTypedArray(),
)
}
}
@ -66,10 +55,10 @@ class SetReadStatus(
Result.Success
}
suspend fun await(mangaId: Long, read: Boolean): Result = withContext(NonCancellable) {
suspend fun await(mangaId: Long, read: Boolean): Result = withNonCancellableContext {
await(
read = read,
values = chapterRepository
chapters = chapterRepository
.getChapterByMangaId(mangaId)
.toTypedArray(),
)

View File

@ -9,8 +9,8 @@ import eu.kanade.domain.chapter.repository.ChapterRepository
import eu.kanade.domain.manga.interactor.UpdateManga
import eu.kanade.domain.manga.model.Manga
import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.isLocal
import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.util.chapter.ChapterRecognition
@ -42,7 +42,7 @@ class SyncChaptersWithSource(
manga: Manga,
source: Source,
): List<Chapter> {
if (rawSourceChapters.isEmpty() && source.id != LocalSource.ID) {
if (rawSourceChapters.isEmpty() && !source.isLocal()) {
throw NoChaptersException()
}

View File

@ -5,17 +5,16 @@ import eu.kanade.domain.chapter.model.toDbChapter
import eu.kanade.domain.manga.model.Manga
import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.source.SourceManager
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.withContext
import eu.kanade.tachiyomi.util.lang.withNonCancellableContext
class DeleteDownload(
private val sourceManager: SourceManager,
private val downloadManager: DownloadManager,
) {
suspend fun awaitAll(manga: Manga, vararg values: Chapter) = withContext(NonCancellable) {
suspend fun awaitAll(manga: Manga, vararg chapters: Chapter) = withNonCancellableContext {
sourceManager.get(manga.source)?.let { source ->
downloadManager.deleteChapters(values.map { it.toDbChapter() }, manga, source)
downloadManager.deleteChapters(chapters.map { it.toDbChapter() }, manga, source)
}
}
}

View File

@ -2,7 +2,7 @@ package eu.kanade.domain.history.interactor
import eu.kanade.domain.history.repository.HistoryRepository
class DeleteHistoryTable(
class DeleteAllHistory(
private val repository: HistoryRepository,
) {

View File

@ -20,8 +20,8 @@ class UpdateManga(
return mangaRepository.update(mangaUpdate)
}
suspend fun awaitAll(values: List<MangaUpdate>): Boolean {
return mangaRepository.updateAll(values)
suspend fun awaitAll(mangaUpdates: List<MangaUpdate>): Boolean {
return mangaRepository.updateAll(mangaUpdates)
}
suspend fun awaitUpdateFromSource(

View File

@ -33,5 +33,5 @@ interface MangaRepository {
suspend fun update(update: MangaUpdate): Boolean
suspend fun updateAll(values: List<MangaUpdate>): Boolean
suspend fun updateAll(mangaUpdates: List<MangaUpdate>): Boolean
}

View File

@ -18,7 +18,7 @@ class GetUpdates(
return repository.subscribeAll(after)
.onEach { updates ->
// Set unread chapter count for bottom bar badge
preferences.unreadUpdatesCount().set(updates.count { it.read.not() })
preferences.unreadUpdatesCount().set(updates.count { !it.read })
}
}
}

View File

@ -10,6 +10,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalUriHandler
import eu.kanade.domain.category.model.Category
import eu.kanade.domain.library.model.display
import eu.kanade.domain.manga.model.isLocal
import eu.kanade.presentation.components.EmptyScreen
import eu.kanade.presentation.components.EmptyScreenAction
import eu.kanade.presentation.components.LibraryBottomActionMenu
@ -18,7 +19,6 @@ import eu.kanade.presentation.components.Scaffold
import eu.kanade.presentation.library.components.LibraryContent
import eu.kanade.presentation.library.components.LibraryToolbar
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.LocalSource
import eu.kanade.tachiyomi.ui.library.LibraryPresenter
import eu.kanade.tachiyomi.widget.TachiyomiBottomNavigationView
@ -64,7 +64,7 @@ fun LibraryScreen(
onChangeCategoryClicked = onChangeCategoryClicked,
onMarkAsReadClicked = onMarkAsReadClicked,
onMarkAsUnreadClicked = onMarkAsUnreadClicked,
onDownloadClicked = onDownloadClicked.takeIf { presenter.selection.none { it.manga.source == LocalSource.ID } },
onDownloadClicked = onDownloadClicked.takeIf { presenter.selection.none { it.manga.isLocal() } },
onDeleteClicked = onDeleteClicked,
)
},

View File

@ -395,7 +395,7 @@ class Downloader(
// When the page is ready, set page path, progress (just in case) and status
.doOnNext { file ->
val success = splitTallImageIfNeeded(page, tmpDir)
if (success.not()) {
if (!success) {
notifier.onError(context.getString(R.string.download_notifier_split_failed), download.chapter.name, download.manga.title)
}
page.uri = file.uri

View File

@ -28,4 +28,6 @@ fun Source.getNameForMangaInfo(): String {
}
}
fun Source.isLocalOrStub(): Boolean = id == LocalSource.ID || this is SourceManager.StubSource
fun Source.isLocal(): Boolean = id == LocalSource.ID
fun Source.isLocalOrStub(): Boolean = isLocal() || this is SourceManager.StubSource

View File

@ -631,7 +631,7 @@ class MangaPresenter(
presenterScope.launchIO {
setReadStatus.await(
read = read,
values = chapters.toTypedArray(),
chapters = chapters.toTypedArray(),
)
}
toggleAllSelection(false)

View File

@ -9,7 +9,7 @@ import androidx.compose.runtime.setValue
import eu.kanade.core.util.insertSeparators
import eu.kanade.domain.base.BasePreferences
import eu.kanade.domain.chapter.model.Chapter
import eu.kanade.domain.history.interactor.DeleteHistoryTable
import eu.kanade.domain.history.interactor.DeleteAllHistory
import eu.kanade.domain.history.interactor.GetHistory
import eu.kanade.domain.history.interactor.GetNextChapter
import eu.kanade.domain.history.interactor.RemoveHistoryById
@ -38,7 +38,7 @@ class HistoryPresenter(
private val state: HistoryStateImpl = HistoryState() as HistoryStateImpl,
private val getHistory: GetHistory = Injekt.get(),
private val getNextChapter: GetNextChapter = Injekt.get(),
private val deleteHistoryTable: DeleteHistoryTable = Injekt.get(),
private val deleteAllHistory: DeleteAllHistory = Injekt.get(),
private val removeHistoryById: RemoveHistoryById = Injekt.get(),
private val removeHistoryByMangaId: RemoveHistoryByMangaId = Injekt.get(),
preferences: BasePreferences = Injekt.get(),
@ -101,7 +101,7 @@ class HistoryPresenter(
fun deleteAllHistory() {
presenterScope.launchIO {
val result = deleteHistoryTable.await()
val result = deleteAllHistory.await()
if (!result) return@launchIO
withUIContext {
view?.activity?.toast(R.string.clear_history_completed)

View File

@ -215,7 +215,7 @@ class UpdatesPresenter(
presenterScope.launchIO {
setReadStatus.await(
read = read,
values = updates
chapters = updates
.mapNotNull { getChapter.await(it.update.chapterId) }
.toTypedArray(),
)