Move default category into database (#7676)

This commit is contained in:
Andreas
2022-08-05 15:32:10 +02:00
committed by GitHub
parent 5315467908
commit 914831d51f
23 changed files with 269 additions and 216 deletions

View File

@@ -5,6 +5,8 @@ import android.net.Uri
import com.hippo.unifile.UniFile
import data.Manga_sync
import data.Mangas
import eu.kanade.data.category.categoryMapper
import eu.kanade.domain.category.model.Category
import eu.kanade.domain.history.model.HistoryUpdate
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.backup.AbstractBackupManager
@@ -138,7 +140,9 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
private suspend fun backupCategories(options: Int): List<BackupCategory> {
// Check if user wants category information in backup
return if (options and BACKUP_CATEGORY_MASK == BACKUP_CATEGORY) {
handler.awaitList { categoriesQueries.getCategories(backupCategoryMapper) }
handler.awaitList { categoriesQueries.getCategories(categoryMapper) }
.filterNot(Category::isSystemCategory)
.map(backupCategoryMapper)
} else {
emptyList()
}
@@ -224,34 +228,37 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
*/
internal suspend fun restoreCategories(backupCategories: List<BackupCategory>) {
// Get categories from file and from db
val dbCategories = handler.awaitList { categoriesQueries.getCategories() }
val dbCategories = handler.awaitList { categoriesQueries.getCategories(categoryMapper) }
// Iterate over them
backupCategories
.map { it.getCategoryImpl() }
.forEach { category ->
// Used to know if the category is already in the db
var found = false
for (dbCategory in dbCategories) {
// If the category is already in the db, assign the id to the file's category
// and do nothing
if (category.name == dbCategory.name) {
category.id = dbCategory.id.toInt()
found = true
break
}
}
// If the category isn't in the db, remove the id and insert a new category
// Store the inserted id in the category
if (!found) {
// Let the db assign the id
category.id = null
category.id = handler.awaitOne {
categoriesQueries.insert(category.name, category.order.toLong(), category.flags.toLong())
categoriesQueries.selectLastInsertedRowId()
}.toInt()
val categories = backupCategories.map {
var category = it.getCategory()
var found = false
for (dbCategory in dbCategories) {
// If the category is already in the db, assign the id to the file's category
// and do nothing
if (category.name == dbCategory.name) {
category = category.copy(id = dbCategory.id)
found = true
break
}
}
if (!found) {
// Let the db assign the id
val id = handler.awaitOne {
categoriesQueries.insert(category.name, category.order, category.flags)
categoriesQueries.selectLastInsertedRowId()
}
category = category.copy(id = id)
}
category
}
preferences.categorizedDisplaySettings().set(
(dbCategories + categories)
.distinctBy { it.flags }
.size > 1,
)
}
/**

View File

@@ -1,6 +1,6 @@
package eu.kanade.tachiyomi.data.backup.full.models
import eu.kanade.tachiyomi.data.database.models.CategoryImpl
import eu.kanade.domain.category.model.Category
import kotlinx.serialization.Serializable
import kotlinx.serialization.protobuf.ProtoNumber
@@ -12,19 +12,20 @@ class BackupCategory(
// Bump by 100 to specify this is a 0.x value
@ProtoNumber(100) var flags: Long = 0,
) {
fun getCategoryImpl(): CategoryImpl {
return CategoryImpl().apply {
name = this@BackupCategory.name
flags = this@BackupCategory.flags.toInt()
order = this@BackupCategory.order.toInt()
}
fun getCategory(): Category {
return Category(
id = 0,
name = this@BackupCategory.name,
flags = this@BackupCategory.flags,
order = this@BackupCategory.order,
)
}
}
val backupCategoryMapper = { _: Long, name: String, order: Long, flags: Long ->
val backupCategoryMapper = { category: Category ->
BackupCategory(
name = name,
order = order,
flags = flags,
name = category.name,
order = category.order,
flags = category.flags,
)
}

View File

@@ -1,44 +0,0 @@
package eu.kanade.tachiyomi.data.database.models
import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting
import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting
import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting
import java.io.Serializable
import eu.kanade.domain.category.model.Category as DomainCategory
interface Category : Serializable {
var id: Int?
var name: String
var order: Int
var flags: Int
private fun setFlags(flag: Int, mask: Int) {
flags = flags and mask.inv() or (flag and mask)
}
var displayMode: Int
get() = flags and DisplayModeSetting.MASK.toInt()
set(mode) = setFlags(mode, DisplayModeSetting.MASK.toInt())
var sortMode: Int
get() = flags and SortModeSetting.MASK.toInt()
set(mode) = setFlags(mode, SortModeSetting.MASK.toInt())
var sortDirection: Int
get() = flags and SortDirectionSetting.MASK.toInt()
set(mode) = setFlags(mode, SortDirectionSetting.MASK.toInt())
}
fun Category.toDomainCategory(): DomainCategory? {
val categoryId = id ?: return null
return DomainCategory(
id = categoryId.toLong(),
name = this.name,
order = this.order.toLong(),
flags = this.flags.toLong(),
)
}

View File

@@ -1,24 +0,0 @@
package eu.kanade.tachiyomi.data.database.models
class CategoryImpl : Category {
override var id: Int? = null
override lateinit var name: String
override var order: Int = 0
override var flags: Int = 0
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || javaClass != other.javaClass) return false
val category = other as Category
return name == category.name
}
override fun hashCode(): Int {
return name.hashCode()
}
}