Use SQLDelight for a Category related queries (#7438)

This commit is contained in:
Andreas
2022-07-02 22:12:06 +02:00
committed by GitHub
parent 21771e62aa
commit 2674570792
14 changed files with 96 additions and 99 deletions

View File

@@ -372,9 +372,9 @@ open class BrowseSourcePresenter(
* @param manga the manga to get categories from.
* @return Array of category ids the manga is in, if none returns default id
*/
fun getMangaCategoryIds(manga: Manga): Array<Long?> {
val categories = db.getCategoriesForManga(manga.id!!).executeAsBlocking()
return categories.mapNotNull { it?.id?.toLong() }.toTypedArray()
suspend fun getMangaCategoryIds(manga: Manga): Array<Long?> {
val categories = getCategories.await(manga.id!!)
return categories.map { it.id }.toTypedArray()
}
/**

View File

@@ -58,6 +58,8 @@ import eu.kanade.tachiyomi.ui.reader.ReaderActivity
import eu.kanade.tachiyomi.ui.recent.history.HistoryController
import eu.kanade.tachiyomi.ui.recent.updates.UpdatesController
import eu.kanade.tachiyomi.ui.webview.WebViewActivity
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.system.logcat
import eu.kanade.tachiyomi.util.system.toast
import eu.kanade.tachiyomi.widget.materialdialogs.QuadStateTextView
@@ -249,18 +251,22 @@ class MangaController :
}
private fun onCategoriesClick() {
val manga = presenter.manga ?: return
val categories = presenter.getCategories()
viewScope.launchIO {
val manga = presenter.manga ?: return@launchIO
val categories = presenter.getCategories()
val ids = presenter.getMangaCategoryIds(manga)
val preselected = categories.map {
if (it.id in ids) {
QuadStateTextView.State.CHECKED.ordinal
} else {
QuadStateTextView.State.UNCHECKED.ordinal
val ids = presenter.getMangaCategoryIds(manga)
val preselected = categories.map {
if (it.id in ids) {
QuadStateTextView.State.CHECKED.ordinal
} else {
QuadStateTextView.State.UNCHECKED.ordinal
}
}.toTypedArray()
launchUI {
showChangeCategoryDialog(manga.toDbManga(), categories, preselected)
}
}.toTypedArray()
showChangeCategoryDialog(manga.toDbManga(), categories, preselected)
}
}
private fun showChangeCategoryDialog(manga: Manga, categories: List<Category>, preselected: Array<Int>) {

View File

@@ -4,6 +4,7 @@ import android.os.Bundle
import androidx.compose.runtime.Immutable
import eu.kanade.domain.category.interactor.GetCategories
import eu.kanade.domain.category.interactor.SetMangaCategories
import eu.kanade.domain.category.model.toDbCategory
import eu.kanade.domain.chapter.interactor.SyncChaptersWithSource
import eu.kanade.domain.chapter.interactor.SyncChaptersWithTrackServiceTwoWay
import eu.kanade.domain.chapter.interactor.UpdateChapter
@@ -22,7 +23,6 @@ import eu.kanade.domain.track.interactor.GetTracks
import eu.kanade.domain.track.interactor.InsertTrack
import eu.kanade.domain.track.model.toDbTrack
import eu.kanade.domain.track.model.toDomainTrack
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
@@ -78,7 +78,6 @@ class MangaPresenter(
val mangaId: Long,
val isFromSource: Boolean,
private val preferences: PreferencesHelper = Injekt.get(),
private val db: DatabaseHelper = Injekt.get(),
private val trackManager: TrackManager = Injekt.get(),
private val downloadManager: DownloadManager = Injekt.get(),
private val getMangaAndChapters: GetMangaWithChapters = Injekt.get(),
@@ -326,8 +325,8 @@ class MangaPresenter(
*
* @return List of categories, not including the default category
*/
fun getCategories(): List<Category> {
return db.getCategories().executeAsBlocking()
suspend fun getCategories(): List<Category> {
return getCategories.await().map { it.toDbCategory() }
}
/**
@@ -597,9 +596,12 @@ class MangaPresenter(
}
private fun downloadNewChapters(chapters: List<Chapter>) {
val manga = successState?.manga ?: return
if (chapters.isEmpty() || !manga.shouldDownloadNewChapters(db, preferences)) return
downloadChapters(chapters.map { it.toDomainChapter()!! })
presenterScope.launchIO {
val manga = successState?.manga ?: return@launchIO
val categories = getCategories.await(manga.id).map { it.id }
if (chapters.isEmpty() || !manga.shouldDownloadNewChapters(categories, preferences)) return@launchIO
downloadChapters(chapters.map { it.toDomainChapter()!! })
}
}
/**

View File

@@ -11,9 +11,9 @@ import androidx.core.text.buildSpannedString
import androidx.preference.PreferenceScreen
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.hippo.unifile.UniFile
import eu.kanade.domain.category.interactor.GetCategories
import eu.kanade.domain.category.model.Category
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.util.preference.bindTo
@@ -32,6 +32,7 @@ import eu.kanade.tachiyomi.widget.materialdialogs.QuadStateTextView
import eu.kanade.tachiyomi.widget.materialdialogs.setQuadStateMultiChoiceItems
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.runBlocking
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
@@ -40,13 +41,13 @@ import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
class SettingsDownloadController : SettingsController() {
private val db: DatabaseHelper by injectLazy()
private val getCategories: GetCategories by injectLazy()
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.pref_category_downloads
val dbCategories = db.getCategories().executeAsBlocking()
val categories = listOf(Category.createDefault(context)) + dbCategories
val dbCategories = runBlocking { getCategories.await() }
val categories = listOf(Category.default(context)) + dbCategories
preference {
bindTo(preferences.downloadsDirectory())
@@ -116,7 +117,7 @@ class SettingsDownloadController : SettingsController() {
preferences.removeExcludeCategories().asFlow()
.onEach { mutable ->
val selected = mutable
.mapNotNull { id -> categories.find { it.id == id.toInt() } }
.mapNotNull { id -> categories.find { it.id == id.toLong() } }
.sortedBy { it.order }
summary = if (selected.isEmpty()) {
@@ -146,7 +147,7 @@ class SettingsDownloadController : SettingsController() {
fun updateSummary() {
val selectedCategories = preferences.downloadNewChapterCategories().get()
.mapNotNull { id -> categories.find { it.id == id.toInt() } }
.mapNotNull { id -> categories.find { it.id == id.toLong() } }
.sortedBy { it.order }
val includedItemsText = if (selectedCategories.isEmpty()) {
context.getString(R.string.all)
@@ -155,7 +156,7 @@ class SettingsDownloadController : SettingsController() {
}
val excludedCategories = preferences.downloadNewChapterCategoriesExclude().get()
.mapNotNull { id -> categories.find { it.id == id.toInt() } }
.mapNotNull { id -> categories.find { it.id == id.toLong() } }
.sortedBy { it.order }
val excludedItemsText = if (excludedCategories.isEmpty()) {
context.getString(R.string.none)
@@ -251,11 +252,11 @@ class SettingsDownloadController : SettingsController() {
class DownloadCategoriesDialog : DialogController() {
private val preferences: PreferencesHelper = Injekt.get()
private val db: DatabaseHelper = Injekt.get()
private val getCategories: GetCategories = Injekt.get()
override fun onCreateDialog(savedViewState: Bundle?): Dialog {
val dbCategories = db.getCategories().executeAsBlocking()
val categories = listOf(Category.createDefault(activity!!)) + dbCategories
val dbCategories = runBlocking { getCategories.await() }
val categories = listOf(Category.default(activity!!)) + dbCategories
val items = categories.map { it.name }
var selected = categories

View File

@@ -7,9 +7,9 @@ import androidx.core.content.ContextCompat
import androidx.core.text.buildSpannedString
import androidx.preference.PreferenceScreen
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import eu.kanade.domain.category.interactor.GetCategories
import eu.kanade.domain.category.model.Category
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.data.preference.DEVICE_BATTERY_NOT_LOW
import eu.kanade.tachiyomi.data.preference.DEVICE_CHARGING
@@ -41,6 +41,7 @@ import eu.kanade.tachiyomi.widget.materialdialogs.setQuadStateMultiChoiceItems
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.runBlocking
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
@@ -48,14 +49,14 @@ import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
class SettingsLibraryController : SettingsController() {
private val db: DatabaseHelper = Injekt.get()
private val getCategories: GetCategories by injectLazy()
private val trackManager: TrackManager by injectLazy()
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
titleRes = R.string.pref_category_library
val dbCategories = db.getCategories().executeAsBlocking()
val categories = listOf(Category.createDefault(context)) + dbCategories
val dbCategories = runBlocking { getCategories.await() }
val categories = listOf(Category.default(context)) + dbCategories
preferenceCategory {
titleRes = R.string.pref_category_display
@@ -110,12 +111,12 @@ class SettingsLibraryController : SettingsController() {
entryValues = arrayOf("-1") + categories.map { it.id.toString() }.toTypedArray()
defaultValue = "-1"
val selectedCategory = categories.find { it.id == preferences.defaultCategory() }
val selectedCategory = categories.find { it.id == preferences.defaultCategory().toLong() }
summary = selectedCategory?.name
?: context.getString(R.string.default_category_summary)
onChange { newValue ->
summary = categories.find {
it.id == (newValue as String).toInt()
it.id == (newValue as String).toLong()
}?.name ?: context.getString(R.string.default_category_summary)
true
}
@@ -228,10 +229,10 @@ class SettingsLibraryController : SettingsController() {
fun updateSummary() {
val includedCategories = preferences.libraryUpdateCategories().get()
.mapNotNull { id -> categories.find { it.id == id.toInt() } }
.mapNotNull { id -> categories.find { it.id == id.toLong() } }
.sortedBy { it.order }
val excludedCategories = preferences.libraryUpdateCategoriesExclude().get()
.mapNotNull { id -> categories.find { it.id == id.toInt() } }
.mapNotNull { id -> categories.find { it.id == id.toLong() } }
.sortedBy { it.order }
val allExcluded = excludedCategories.size == categories.size
@@ -327,11 +328,11 @@ class SettingsLibraryController : SettingsController() {
class LibraryGlobalUpdateCategoriesDialog : DialogController() {
private val preferences: PreferencesHelper = Injekt.get()
private val db: DatabaseHelper = Injekt.get()
private val getCategories: GetCategories = Injekt.get()
override fun onCreateDialog(savedViewState: Bundle?): Dialog {
val dbCategories = db.getCategories().executeAsBlocking()
val categories = listOf(Category.createDefault(activity!!)) + dbCategories
val dbCategories = runBlocking { getCategories.await() }
val categories = listOf(Category.default(activity!!)) + dbCategories
val items = categories.map { it.name }
var selected = categories