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 lastUsedCategory() = flowPrefs.getInt(Keys.lastUsedCategory, 0)
fun lastCategoriesAddedTo() = flowPrefs.getStringSet("last_category_added_to", emptySet())
fun lastUsedSources() = flowPrefs.getStringSet("last_used_sources", emptySet()) fun lastUsedSources() = flowPrefs.getStringSet("last_used_sources", emptySet())
fun lastVersionCode() = flowPrefs.getInt("last_version_code", 0) 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 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) 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.Category
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.MangaCategory 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.databinding.SetCategoriesSheetBinding
import eu.kanade.tachiyomi.ui.category.ManageCategoryDialog import eu.kanade.tachiyomi.ui.category.ManageCategoryDialog
import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.main.MainActivity
@ -67,6 +68,7 @@ class SetCategoriesSheet(
private val itemAdapter = ItemAdapter<AddCategoryItem>() private val itemAdapter = ItemAdapter<AddCategoryItem>()
private val db: DatabaseHelper by injectLazy() private val db: DatabaseHelper by injectLazy()
private val preferences: PreferencesHelper by injectLazy()
override var recyclerView: RecyclerView? = binding.categoryRecyclerView override var recyclerView: RecyclerView? = binding.categoryRecyclerView
private val preCheckedCategories = categories.mapIndexedNotNull { index, category -> private val preCheckedCategories = categories.mapIndexedNotNull { index, category ->
@ -177,8 +179,8 @@ class SetCategoriesSheet(
val items = when { val items = when {
addingToLibrary -> checkedItems.map { it.category } addingToLibrary -> checkedItems.map { it.category }
addingMore -> checkedItems.map { it.category }.subtract(preCheckedCategories) addingMore -> checkedItems.map { it.category }.subtract(preCheckedCategories.toSet())
removing -> selectedCategories.subtract(selectedItems.map { it.category }) removing -> selectedCategories.subtract(selectedItems.map { it.category }.toSet())
nothingChanged -> selectedItems.map { it.category } nothingChanged -> selectedItems.map { it.category }
else -> checkedItems.map { it.category } else -> checkedItems.map { it.category }
} }
@ -236,7 +238,7 @@ class SetCategoriesSheet(
binding.newCategoryButton.setOnClickListener { binding.newCategoryButton.setOnClickListener {
ManageCategoryDialog(null) { ManageCategoryDialog(null) {
categories = db.getCategories().executeAsBlocking() 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( itemAdapter.set(
categories.mapIndexed { index, category -> categories.mapIndexed { index, category ->
AddCategoryItem(category).apply { AddCategoryItem(category).apply {
@ -270,9 +272,15 @@ class SetCategoriesSheet(
val removeCategories = uncheckedItems.map(AddCategoryItem::category) val removeCategories = uncheckedItems.map(AddCategoryItem::category)
val mangaCategories = listManga.map { manga -> val mangaCategories = listManga.map { manga ->
val categories = db.getCategoriesForManga(manga).executeAsBlocking() val categories = db.getCategoriesForManga(manga).executeAsBlocking()
.subtract(removeCategories).plus(addCategories).distinct() .subtract(removeCategories.toSet()).plus(addCategories).distinct()
categories.map { MangaCategory.create(manga, it) } categories.map { MangaCategory.create(manga, it) }
}.flatten() }.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) db.setMangaCategories(mangaCategories, listManga)
onMangaAdded() onMangaAdded()
} }

View file

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

View file

@ -40,6 +40,7 @@ import timber.log.Timber
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import java.util.Date import java.util.Date
import java.util.Locale
fun Manga.isLocal() = source == LocalSource.ID fun Manga.isLocal() = source == LocalSource.ID
@ -173,6 +174,9 @@ fun Manga.addOrRemoveToFavorites(
val categories = db.getCategories().executeAsBlocking() val categories = db.getCategories().executeAsBlocking()
val defaultCategoryId = preferences.defaultCategory() val defaultCategoryId = preferences.defaultCategory()
val defaultCategory = categories.find { it.id == defaultCategoryId } val defaultCategory = categories.find { it.id == defaultCategoryId }
val lastUsedCategories = preferences.lastCategoriesAddedTo().get().mapNotNull { catId ->
categories.find { it.id == catId.toIntOrNull() }
}
when { when {
defaultCategory != null -> { defaultCategory != null -> {
favorite = true 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 defaultCategoryId == 0 || categories.isEmpty() -> { // 'Default' or no category
favorite = true favorite = true
date_added = Date().time date_added = Date().time
@ -207,21 +244,8 @@ fun Manga.addOrRemoveToFavorites(
view.snack(R.string.added_to_library) view.snack(R.string.added_to_library)
} }
} }
else -> { else -> { // Always ask
val categoriesForManga = db.getCategoriesForManga(this).executeAsBlocking() showSetCategoriesSheet(db, activity, categories, onMangaAdded, onMangaMoved)
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 { } else {
@ -252,6 +276,29 @@ fun Manga.addOrRemoveToFavorites(
return null 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( private fun showAddDuplicateDialog(
newManga: Manga, newManga: Manga,
libraryManga: Manga, libraryManga: Manga,