Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to keep read manga in Clear Database #1382

Merged
merged 1 commit into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ interface MangaQueries : DbProvider {
)
.prepare()

fun deleteMangasNotInLibraryAndNotReadBySourceIds(sourceIds: List<Long>) = db.delete()
.byQuery(
DeleteQuery.builder()
.table(MangaTable.TABLE)
.where(
"""
${MangaTable.COL_FAVORITE} = ? AND ${MangaTable.COL_SOURCE} IN (${Queries.placeholders(sourceIds.size)}) AND ${MangaTable.COL_ID} NOT IN (
SELECT ${ChapterTable.COL_MANGA_ID} FROM ${ChapterTable.TABLE} WHERE ${ChapterTable.COL_READ} = 1 OR ${ChapterTable.COL_LAST_PAGE_READ} != 0
)
""".trimIndent(),
)
.whereArgs(0, *sourceIds.toTypedArray())
.build(),
)
.prepare()

fun deleteMangas() = db.delete()
.byQuery(
DeleteQuery.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import eu.kanade.tachiyomi.ui.base.controller.BaseCoroutineController
import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.util.system.materialAlertDialog
import eu.kanade.tachiyomi.util.system.rootWindowInsetsCompat
import eu.kanade.tachiyomi.util.system.setCustomTitleAndMessage
import eu.kanade.tachiyomi.util.view.activityBinding
import eu.kanade.tachiyomi.util.view.fullAppBarHeight
import eu.kanade.tachiyomi.util.view.scrollViewWith
Expand Down Expand Up @@ -229,23 +230,28 @@ class ClearDatabaseController :

class ClearDatabaseSourcesDialog : DialogController() {
override fun onCreateDialog(savedViewState: Bundle?): Dialog {
val item = arrayOf(activity!!.getString(R.string.clear_db_exclude_read))
val selected = booleanArrayOf(true)
return activity!!.materialAlertDialog()
.setMessage(R.string.clear_database_confirmation)
.setCustomTitleAndMessage(0, activity!!.getString(R.string.clear_database_confirmation))
.setMultiChoiceItems(item, selected) { _, which, checked ->
selected[which] = checked
}
.setPositiveButton(android.R.string.ok) { _, _ ->
(targetController as? ClearDatabaseController)?.clearDatabaseForSelectedSources()
(targetController as? ClearDatabaseController)?.clearDatabaseForSelectedSources(selected.last())
}
.setNegativeButton(android.R.string.cancel, null)
.create()
}
}

@SuppressLint("NotifyDataSetChanged")
private fun clearDatabaseForSelectedSources() {
private fun clearDatabaseForSelectedSources(keepReadManga: Boolean) {
val adapter = adapter ?: return
val selectedSourceIds = adapter.selectedPositions.mapNotNull { position ->
adapter.getItem(position)?.source?.id
}
presenter.clearDatabaseForSourceIds(selectedSourceIds)
presenter.clearDatabaseForSourceIds(selectedSourceIds, keepReadManga)
binding.fab.isVisible = false
adapter.clearSelection()
adapter.notifyDataSetChanged()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ class ClearDatabasePresenter : BaseCoroutinePresenter<ClearDatabaseController>()
getDatabaseSources()
}

fun clearDatabaseForSourceIds(sources: List<Long>) {
db.deleteMangasNotInLibraryBySourceIds(sources).executeAsBlocking()
fun clearDatabaseForSourceIds(sources: List<Long>, keepReadManga: Boolean) {
if (keepReadManga) {
db.deleteMangasNotInLibraryAndNotReadBySourceIds(sources).executeAsBlocking()
} else {
db.deleteMangasNotInLibraryBySourceIds(sources).executeAsBlocking()
}
db.deleteHistoryNoLastRead().executeAsBlocking()
getDatabaseSources()
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@
<string name="clear_database_source_item_count">%1$d non-library manga in database</string>
<string name="clear_database_summary">Delete manga and chapters that are not in your library</string>
<string name="clear_database_confirmation">Are you sure? Read chapters and progress of non-library manga will be lost</string>
<string name="clear_db_exclude_read">Keep manga with read chapters</string>
<string name="clear_database_completed">Entries deleted</string>
<string name="database_clean">Database clean</string>
<string name="refresh_library_metadata">Refresh library metadata</string>
Expand Down