New option when adding manga: last used categories

Default option is now when adding to add manga based on the last categories or categories set.
This commit is contained in:
Jays2Kings 2023-07-31 18:25:18 -04:00
parent c71c11431a
commit 01a6f3619c
4 changed files with 91 additions and 29 deletions

View file

@ -207,6 +207,8 @@ class PreferencesHelper(val context: Context) {
fun lastUsedCategory() = flowPrefs.getInt(Keys.lastUsedCategory, 0)
fun lastCategoriesAddedTo() = flowPrefs.getStringSet("last_category_added_to", emptySet())
fun lastUsedSources() = flowPrefs.getStringSet("last_used_sources", emptySet())
fun lastVersionCode() = flowPrefs.getInt("last_version_code", 0)
@ -329,7 +331,7 @@ class PreferencesHelper(val context: Context) {
fun autoDownloadWhileReading() = flowPrefs.getInt("auto_download_while_reading", 0)
fun defaultCategory() = prefs.getInt(Keys.defaultCategory, -1)
fun defaultCategory() = prefs.getInt(Keys.defaultCategory, -2)
fun skipRead() = prefs.getBoolean(Keys.skipRead, false)

View file

@ -18,6 +18,7 @@ import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.MangaCategory
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.databinding.SetCategoriesSheetBinding
import eu.kanade.tachiyomi.ui.category.ManageCategoryDialog
import eu.kanade.tachiyomi.ui.main.MainActivity
@ -67,6 +68,7 @@ class SetCategoriesSheet(
private val itemAdapter = ItemAdapter<AddCategoryItem>()
private val db: DatabaseHelper by injectLazy()
private val preferences: PreferencesHelper by injectLazy()
override var recyclerView: RecyclerView? = binding.categoryRecyclerView
private val preCheckedCategories = categories.mapIndexedNotNull { index, category ->
@ -177,8 +179,8 @@ class SetCategoriesSheet(
val items = when {
addingToLibrary -> checkedItems.map { it.category }
addingMore -> checkedItems.map { it.category }.subtract(preCheckedCategories)
removing -> selectedCategories.subtract(selectedItems.map { it.category })
addingMore -> checkedItems.map { it.category }.subtract(preCheckedCategories.toSet())
removing -> selectedCategories.subtract(selectedItems.map { it.category }.toSet())
nothingChanged -> selectedItems.map { it.category }
else -> checkedItems.map { it.category }
}
@ -236,7 +238,7 @@ class SetCategoriesSheet(
binding.newCategoryButton.setOnClickListener {
ManageCategoryDialog(null) {
categories = db.getCategories().executeAsBlocking()
val map = itemAdapter.adapterItems.map { it.category.id to it.state }.toMap()
val map = itemAdapter.adapterItems.associate { it.category.id to it.state }
itemAdapter.set(
categories.mapIndexed { index, category ->
AddCategoryItem(category).apply {
@ -270,9 +272,15 @@ class SetCategoriesSheet(
val removeCategories = uncheckedItems.map(AddCategoryItem::category)
val mangaCategories = listManga.map { manga ->
val categories = db.getCategoriesForManga(manga).executeAsBlocking()
.subtract(removeCategories).plus(addCategories).distinct()
.subtract(removeCategories.toSet()).plus(addCategories).distinct()
categories.map { MangaCategory.create(manga, it) }
}.flatten()
if (addCategories.isNotEmpty() || listManga.size == 1) {
preferences.lastCategoriesAddedTo().set(
addCategories.mapNotNull { it.id?.toString() }.toSet().takeIf { it.isNotEmpty() }
?: setOf("0"),
)
}
db.setMangaCategories(mangaCategories, listManga)
onMangaAdded()
}

View file

@ -88,17 +88,22 @@ class SettingsLibraryController : SettingsController() {
val categories = listOf(Category.createDefault(context)) + dbCategories
entries =
listOf(context.getString(R.string.always_ask)) + categories.map { it.name }.toTypedArray()
entryValues = listOf(-1) + categories.mapNotNull { it.id }.toList()
defaultValue = "-1"
listOf(context.getString(R.string.last_used), context.getString(R.string.always_ask)) +
categories.map { it.name }.toTypedArray()
entryValues = listOf(-2, -1) + categories.mapNotNull { it.id }.toList()
defaultValue = "-2"
val selectedCategory = categories.find { it.id == preferences.defaultCategory() }
summary =
selectedCategory?.name ?: context.getString(R.string.always_ask)
val categoryName: (Int) -> String = { catId ->
when (catId) {
-2 -> context.getString(R.string.last_used)
-1 -> context.getString(R.string.always_ask)
else -> categories.find { it.id == preferences.defaultCategory() }?.name
?: context.getString(R.string.last_used)
}
}
summary = categoryName(preferences.defaultCategory())
onChange { newValue ->
summary = categories.find {
it.id == newValue as Int
}?.name ?: context.getString(R.string.always_ask)
summary = categoryName(newValue as Int)
true
}
}

View file

@ -40,6 +40,7 @@ import timber.log.Timber
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.Date
import java.util.Locale
fun Manga.isLocal() = source == LocalSource.ID
@ -173,6 +174,9 @@ fun Manga.addOrRemoveToFavorites(
val categories = db.getCategories().executeAsBlocking()
val defaultCategoryId = preferences.defaultCategory()
val defaultCategory = categories.find { it.id == defaultCategoryId }
val lastUsedCategories = preferences.lastCategoriesAddedTo().get().mapNotNull { catId ->
categories.find { it.id == catId.toIntOrNull() }
}
when {
defaultCategory != null -> {
favorite = true
@ -189,6 +193,39 @@ fun Manga.addOrRemoveToFavorites(
}
}
}
defaultCategoryId == -2 && (
lastUsedCategories.isNotEmpty() ||
preferences.lastCategoriesAddedTo().get().firstOrNull() == "0"
) -> { // last used category(s)
favorite = true
date_added = Date().time
autoAddTrack(db, onMangaMoved)
db.insertManga(this).executeAsBlocking()
db.setMangaCategories(
lastUsedCategories.map { MangaCategory.create(this, it) },
listOf(this),
)
(activity as? MainActivity)?.showNotificationPermissionPrompt()
onMangaMoved()
return view.snack(
activity.getString(
R.string.added_to_,
when (lastUsedCategories.size) {
0 -> activity.getString(R.string.default_category).lowercase(Locale.ROOT)
1 -> lastUsedCategories.firstOrNull()?.name ?: ""
else -> activity.resources.getQuantityString(
R.plurals.category_plural,
lastUsedCategories.size,
lastUsedCategories.size,
)
},
),
) {
setAction(R.string.change) {
moveCategories(db, activity, onMangaMoved)
}
}
}
defaultCategoryId == 0 || categories.isEmpty() -> { // 'Default' or no category
favorite = true
date_added = Date().time
@ -207,21 +244,8 @@ fun Manga.addOrRemoveToFavorites(
view.snack(R.string.added_to_library)
}
}
else -> {
val categoriesForManga = db.getCategoriesForManga(this).executeAsBlocking()
val ids = categoriesForManga.mapNotNull { it.id }.toTypedArray()
SetCategoriesSheet(
activity,
this,
categories.toMutableList(),
ids,
true,
) {
(activity as? MainActivity)?.showNotificationPermissionPrompt()
onMangaAdded(null)
autoAddTrack(db, onMangaMoved)
}.show()
else -> { // Always ask
showSetCategoriesSheet(db, activity, categories, onMangaAdded, onMangaMoved)
}
}
} else {
@ -252,6 +276,29 @@ fun Manga.addOrRemoveToFavorites(
return null
}
private fun Manga.showSetCategoriesSheet(
db: DatabaseHelper,
activity: Activity,
categories: List<Category>,
onMangaAdded: (Pair<Long, Boolean>?) -> Unit,
onMangaMoved: () -> Unit,
) {
val categoriesForManga = db.getCategoriesForManga(this).executeAsBlocking()
val ids = categoriesForManga.mapNotNull { it.id }.toTypedArray()
SetCategoriesSheet(
activity,
this,
categories.toMutableList(),
ids,
true,
) {
(activity as? MainActivity)?.showNotificationPermissionPrompt()
onMangaAdded(null)
autoAddTrack(db, onMangaMoved)
}.show()
}
private fun showAddDuplicateDialog(
newManga: Manga,
libraryManga: Manga,