Skip to content

Commit

Permalink
New option when adding manga: last used categories
Browse files Browse the repository at this point in the history
Default option is now when adding to add manga based on the last categories or categories set.
  • Loading branch information
Jays2Kings committed Jul 31, 2023
1 parent c71c114 commit 01a6f36
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

val selectedCategory = categories.find { it.id == preferences.defaultCategory() }
summary =
selectedCategory?.name ?: context.getString(R.string.always_ask)
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 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
}
}
Expand Down
77 changes: 62 additions & 15 deletions app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 01a6f36

Please sign in to comment.