Replace CategoryUpdate with dedicated update queries (#3693)
Assisted-by: Claude:claude-opus-5
This commit is contained in:
@@ -55,7 +55,6 @@ import tachiyomi.domain.category.interactor.ResetCategoryFlags
|
|||||||
import tachiyomi.domain.category.interactor.SetDisplayMode
|
import tachiyomi.domain.category.interactor.SetDisplayMode
|
||||||
import tachiyomi.domain.category.interactor.SetMangaCategories
|
import tachiyomi.domain.category.interactor.SetMangaCategories
|
||||||
import tachiyomi.domain.category.interactor.SetSortModeForCategory
|
import tachiyomi.domain.category.interactor.SetSortModeForCategory
|
||||||
import tachiyomi.domain.category.interactor.UpdateCategory
|
|
||||||
import tachiyomi.domain.category.repository.CategoryRepository
|
import tachiyomi.domain.category.repository.CategoryRepository
|
||||||
import tachiyomi.domain.chapter.interactor.GetBookmarkedChaptersByMangaId
|
import tachiyomi.domain.chapter.interactor.GetBookmarkedChaptersByMangaId
|
||||||
import tachiyomi.domain.chapter.interactor.GetChapter
|
import tachiyomi.domain.chapter.interactor.GetChapter
|
||||||
@@ -113,7 +112,6 @@ class DomainModule : InjektModule {
|
|||||||
addFactory { CreateCategoryWithName(get(), get()) }
|
addFactory { CreateCategoryWithName(get(), get()) }
|
||||||
addFactory { RenameCategory(get()) }
|
addFactory { RenameCategory(get()) }
|
||||||
addFactory { ReorderCategory(get()) }
|
addFactory { ReorderCategory(get()) }
|
||||||
addFactory { UpdateCategory(get()) }
|
|
||||||
addFactory { DeleteCategory(get(), get(), get()) }
|
addFactory { DeleteCategory(get(), get(), get()) }
|
||||||
|
|
||||||
addSingletonFactory<MangaRepository> { MangaRepositoryImpl(get()) }
|
addSingletonFactory<MangaRepository> { MangaRepositoryImpl(get()) }
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import kotlinx.coroutines.flow.Flow
|
|||||||
import tachiyomi.data.Database
|
import tachiyomi.data.Database
|
||||||
import tachiyomi.data.subscribeToList
|
import tachiyomi.data.subscribeToList
|
||||||
import tachiyomi.domain.category.model.Category
|
import tachiyomi.domain.category.model.Category
|
||||||
import tachiyomi.domain.category.model.CategoryUpdate
|
|
||||||
import tachiyomi.domain.category.repository.CategoryRepository
|
import tachiyomi.domain.category.repository.CategoryRepository
|
||||||
|
|
||||||
class CategoryRepositoryImpl(
|
class CategoryRepositoryImpl(
|
||||||
@@ -51,23 +50,24 @@ class CategoryRepositoryImpl(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun updatePartial(update: CategoryUpdate) {
|
override suspend fun updateName(categoryId: Long, name: String) {
|
||||||
database.categoriesQueries.update(
|
database.categoriesQueries.updateName(name = name, categoryId = categoryId)
|
||||||
name = update.name,
|
|
||||||
order = update.order,
|
|
||||||
flags = update.flags,
|
|
||||||
categoryId = update.id,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun updatePartial(updates: List<CategoryUpdate>) {
|
override suspend fun updateFlags(categoryId: Long, flags: Long) {
|
||||||
database.transaction {
|
database.categoriesQueries.updateFlags(flags = flags, categoryId = categoryId)
|
||||||
updates.forEach { updatePartial(it) }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun updateAllFlags(flags: Long?) {
|
override suspend fun updateAllFlags(flags: Long?) {
|
||||||
database.categoriesQueries.updateAllFlags(flags)
|
database.categoriesQueries.updateAllFlags(flags = flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun updateAllOrders(orderedIds: List<Long>) {
|
||||||
|
database.transaction {
|
||||||
|
orderedIds.forEachIndexed { index, categoryId ->
|
||||||
|
database.categoriesQueries.updateOrder(order = index.toLong(), categoryId = categoryId)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun delete(categoryId: Long) {
|
override suspend fun delete(categoryId: Long) {
|
||||||
|
|||||||
@@ -50,13 +50,21 @@ delete:
|
|||||||
DELETE FROM categories
|
DELETE FROM categories
|
||||||
WHERE _id = :categoryId;
|
WHERE _id = :categoryId;
|
||||||
|
|
||||||
update:
|
updateName:
|
||||||
UPDATE categories
|
UPDATE categories
|
||||||
SET name = coalesce(:name, name),
|
SET name = :name
|
||||||
sort = coalesce(:order, sort),
|
WHERE _id = :categoryId;
|
||||||
flags = coalesce(:flags, flags)
|
|
||||||
|
updateOrder:
|
||||||
|
UPDATE categories
|
||||||
|
SET sort = :order
|
||||||
|
WHERE _id = :categoryId;
|
||||||
|
|
||||||
|
updateFlags:
|
||||||
|
UPDATE categories
|
||||||
|
SET flags = :flags
|
||||||
WHERE _id = :categoryId;
|
WHERE _id = :categoryId;
|
||||||
|
|
||||||
updateAllFlags:
|
updateAllFlags:
|
||||||
UPDATE categories SET
|
UPDATE categories SET
|
||||||
flags = coalesce(?, flags);
|
flags = coalesce(:flags, flags);
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package tachiyomi.domain.category.interactor
|
|||||||
import logcat.LogPriority
|
import logcat.LogPriority
|
||||||
import tachiyomi.core.common.util.lang.withNonCancellableContext
|
import tachiyomi.core.common.util.lang.withNonCancellableContext
|
||||||
import tachiyomi.core.common.util.system.logcat
|
import tachiyomi.core.common.util.system.logcat
|
||||||
import tachiyomi.domain.category.model.CategoryUpdate
|
|
||||||
import tachiyomi.domain.category.repository.CategoryRepository
|
import tachiyomi.domain.category.repository.CategoryRepository
|
||||||
import tachiyomi.domain.download.service.DownloadPreferences
|
import tachiyomi.domain.download.service.DownloadPreferences
|
||||||
import tachiyomi.domain.library.service.LibraryPreferences
|
import tachiyomi.domain.library.service.LibraryPreferences
|
||||||
@@ -22,13 +21,7 @@ class DeleteCategory(
|
|||||||
return@withNonCancellableContext Result.InternalError(e)
|
return@withNonCancellableContext Result.InternalError(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
val categories = categoryRepository.getAll()
|
val orderedIds = categoryRepository.getAll().map { it.id }
|
||||||
val updates = categories.mapIndexed { index, category ->
|
|
||||||
CategoryUpdate(
|
|
||||||
id = category.id,
|
|
||||||
order = index.toLong(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
val defaultCategory = libraryPreferences.defaultCategory.get()
|
val defaultCategory = libraryPreferences.defaultCategory.get()
|
||||||
if (defaultCategory == categoryId.toInt()) {
|
if (defaultCategory == categoryId.toInt()) {
|
||||||
@@ -50,7 +43,7 @@ class DeleteCategory(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
categoryRepository.updatePartial(updates)
|
categoryRepository.updateAllOrders(orderedIds = orderedIds)
|
||||||
Result.Success
|
Result.Success
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logcat(LogPriority.ERROR, e)
|
logcat(LogPriority.ERROR, e)
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import logcat.LogPriority
|
|||||||
import tachiyomi.core.common.util.lang.withNonCancellableContext
|
import tachiyomi.core.common.util.lang.withNonCancellableContext
|
||||||
import tachiyomi.core.common.util.system.logcat
|
import tachiyomi.core.common.util.system.logcat
|
||||||
import tachiyomi.domain.category.model.Category
|
import tachiyomi.domain.category.model.Category
|
||||||
import tachiyomi.domain.category.model.CategoryUpdate
|
|
||||||
import tachiyomi.domain.category.repository.CategoryRepository
|
import tachiyomi.domain.category.repository.CategoryRepository
|
||||||
|
|
||||||
class RenameCategory(
|
class RenameCategory(
|
||||||
@@ -12,13 +11,8 @@ class RenameCategory(
|
|||||||
) {
|
) {
|
||||||
|
|
||||||
suspend fun await(categoryId: Long, name: String) = withNonCancellableContext {
|
suspend fun await(categoryId: Long, name: String) = withNonCancellableContext {
|
||||||
val update = CategoryUpdate(
|
|
||||||
id = categoryId,
|
|
||||||
name = name,
|
|
||||||
)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
categoryRepository.updatePartial(update)
|
categoryRepository.updateName(categoryId = categoryId, name = name)
|
||||||
Result.Success
|
Result.Success
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logcat(LogPriority.ERROR, e)
|
logcat(LogPriority.ERROR, e)
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import logcat.LogPriority
|
|||||||
import tachiyomi.core.common.util.lang.withNonCancellableContext
|
import tachiyomi.core.common.util.lang.withNonCancellableContext
|
||||||
import tachiyomi.core.common.util.system.logcat
|
import tachiyomi.core.common.util.system.logcat
|
||||||
import tachiyomi.domain.category.model.Category
|
import tachiyomi.domain.category.model.Category
|
||||||
import tachiyomi.domain.category.model.CategoryUpdate
|
|
||||||
import tachiyomi.domain.category.repository.CategoryRepository
|
import tachiyomi.domain.category.repository.CategoryRepository
|
||||||
|
|
||||||
class ReorderCategory(
|
class ReorderCategory(
|
||||||
@@ -28,14 +27,7 @@ class ReorderCategory(
|
|||||||
try {
|
try {
|
||||||
categories.add(newIndex, categories.removeAt(currentIndex))
|
categories.add(newIndex, categories.removeAt(currentIndex))
|
||||||
|
|
||||||
val updates = categories.mapIndexed { index, category ->
|
categoryRepository.updateAllOrders(orderedIds = categories.map { it.id })
|
||||||
CategoryUpdate(
|
|
||||||
id = category.id,
|
|
||||||
order = index.toLong(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
categoryRepository.updatePartial(updates)
|
|
||||||
Result.Success
|
Result.Success
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logcat(LogPriority.ERROR, e)
|
logcat(LogPriority.ERROR, e)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package tachiyomi.domain.category.interactor
|
package tachiyomi.domain.category.interactor
|
||||||
|
|
||||||
import tachiyomi.domain.category.model.Category
|
import tachiyomi.domain.category.model.Category
|
||||||
import tachiyomi.domain.category.model.CategoryUpdate
|
|
||||||
import tachiyomi.domain.category.repository.CategoryRepository
|
import tachiyomi.domain.category.repository.CategoryRepository
|
||||||
import tachiyomi.domain.library.model.LibrarySort
|
import tachiyomi.domain.library.model.LibrarySort
|
||||||
import tachiyomi.domain.library.model.plus
|
import tachiyomi.domain.library.model.plus
|
||||||
@@ -20,12 +19,7 @@ class SetSortModeForCategory(
|
|||||||
preferences.randomSortSeed.set(Random.nextInt())
|
preferences.randomSortSeed.set(Random.nextInt())
|
||||||
}
|
}
|
||||||
if (category != null && preferences.categorizedDisplaySettings.get()) {
|
if (category != null && preferences.categorizedDisplaySettings.get()) {
|
||||||
categoryRepository.updatePartial(
|
categoryRepository.updateFlags(categoryId = category.id, flags = flags)
|
||||||
CategoryUpdate(
|
|
||||||
id = category.id,
|
|
||||||
flags = flags,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
preferences.sortingMode.set(LibrarySort(type, direction))
|
preferences.sortingMode.set(LibrarySort(type, direction))
|
||||||
categoryRepository.updateAllFlags(flags)
|
categoryRepository.updateAllFlags(flags)
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
package tachiyomi.domain.category.interactor
|
|
||||||
|
|
||||||
import tachiyomi.core.common.util.lang.withNonCancellableContext
|
|
||||||
import tachiyomi.domain.category.model.CategoryUpdate
|
|
||||||
import tachiyomi.domain.category.repository.CategoryRepository
|
|
||||||
|
|
||||||
class UpdateCategory(
|
|
||||||
private val categoryRepository: CategoryRepository,
|
|
||||||
) {
|
|
||||||
|
|
||||||
suspend fun await(payload: CategoryUpdate): Result = withNonCancellableContext {
|
|
||||||
try {
|
|
||||||
categoryRepository.updatePartial(payload)
|
|
||||||
Result.Success
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Result.Error(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sealed interface Result {
|
|
||||||
data object Success : Result
|
|
||||||
data class Error(val error: Exception) : Result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package tachiyomi.domain.category.model
|
|
||||||
|
|
||||||
data class CategoryUpdate(
|
|
||||||
val id: Long,
|
|
||||||
val name: String? = null,
|
|
||||||
val order: Long? = null,
|
|
||||||
val flags: Long? = null,
|
|
||||||
)
|
|
||||||
@@ -2,7 +2,6 @@ package tachiyomi.domain.category.repository
|
|||||||
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import tachiyomi.domain.category.model.Category
|
import tachiyomi.domain.category.model.Category
|
||||||
import tachiyomi.domain.category.model.CategoryUpdate
|
|
||||||
|
|
||||||
interface CategoryRepository {
|
interface CategoryRepository {
|
||||||
|
|
||||||
@@ -18,11 +17,13 @@ interface CategoryRepository {
|
|||||||
|
|
||||||
suspend fun insert(category: Category)
|
suspend fun insert(category: Category)
|
||||||
|
|
||||||
suspend fun updatePartial(update: CategoryUpdate)
|
suspend fun updateName(categoryId: Long, name: String)
|
||||||
|
|
||||||
suspend fun updatePartial(updates: List<CategoryUpdate>)
|
suspend fun updateFlags(categoryId: Long, flags: Long)
|
||||||
|
|
||||||
suspend fun updateAllFlags(flags: Long?)
|
suspend fun updateAllFlags(flags: Long?)
|
||||||
|
|
||||||
|
suspend fun updateAllOrders(orderedIds: List<Long>)
|
||||||
|
|
||||||
suspend fun delete(categoryId: Long)
|
suspend fun delete(categoryId: Long)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user