From d01e1c89e0675ed6ff66984e6807f56ef65dbcb4 Mon Sep 17 00:00:00 2001 From: Ankit Grai Date: Wed, 20 Nov 2024 23:27:03 +0530 Subject: [PATCH] feat: Added anime clear progress (#531) * feat: Added anime clear progress * more stuff added --- .../dantotsu/media/anime/AnimeWatchAdapter.kt | 44 +++++++++++++++++ .../dantotsu/media/manga/MangaReadAdapter.kt | 37 ++++++++++---- .../dantotsu/settings/saving/PrefManager.kt | 11 ++++- app/src/main/res/drawable/ic_delete.xml | 9 ++++ app/src/main/res/layout/dialog_layout.xml | 48 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + 6 files changed, 142 insertions(+), 9 deletions(-) create mode 100644 app/src/main/res/drawable/ic_delete.xml diff --git a/app/src/main/java/ani/dantotsu/media/anime/AnimeWatchAdapter.kt b/app/src/main/java/ani/dantotsu/media/anime/AnimeWatchAdapter.kt index e1c77312..d53880bf 100644 --- a/app/src/main/java/ani/dantotsu/media/anime/AnimeWatchAdapter.kt +++ b/app/src/main/java/ani/dantotsu/media/anime/AnimeWatchAdapter.kt @@ -9,6 +9,7 @@ import android.widget.ArrayAdapter import android.widget.ImageButton import android.widget.LinearLayout import androidx.core.content.ContextCompat +import androidx.core.content.ContextCompat.getString import androidx.core.content.ContextCompat.startActivity import androidx.core.view.isGone import androidx.core.view.isVisible @@ -17,6 +18,7 @@ import androidx.recyclerview.widget.RecyclerView import ani.dantotsu.FileUrl import ani.dantotsu.R import ani.dantotsu.currActivity +import ani.dantotsu.currContext import ani.dantotsu.databinding.DialogLayoutBinding import ani.dantotsu.databinding.ItemMediaSourceBinding import ani.dantotsu.databinding.ItemChipBinding @@ -38,6 +40,7 @@ import ani.dantotsu.px import ani.dantotsu.settings.FAQActivity import ani.dantotsu.settings.saving.PrefManager import ani.dantotsu.settings.saving.PrefName +import ani.dantotsu.snackString import ani.dantotsu.toast import ani.dantotsu.util.customAlertDialog import com.google.android.material.chip.Chip @@ -278,6 +281,26 @@ class AnimeWatchAdapter( } } } + resetProgress.setOnClickListener { + fragment.requireContext().customAlertDialog().apply { + setTitle(" Delete Progress for all episodes of ${media.nameRomaji}") + setMessage("This will delete all the locally stored progress for all episodes") + setPosButton(R.string.ok){ + val prefix = "${media.id}_" + val regex = Regex("^${prefix}\\d+$") + + PrefManager.getAllCustomValsForMedia(prefix) + .keys + .filter { it.matches(regex) } + .onEach { key -> PrefManager.removeCustomVal(key) } + snackString("Deleted the progress of all Episodes for ${media.nameRomaji}") + } + setNegButton(R.string.no) + show() + } + } + + resetProgressDef.text = getString(currContext()!!,R.string.clear_stored_episode) // Hidden mangaScanlatorContainer.visibility = View.GONE @@ -298,6 +321,27 @@ class AnimeWatchAdapter( } // Episode Handling handleEpisodes() + + //clear progress + binding.sourceTitle.setOnLongClickListener { + fragment.requireContext().customAlertDialog().apply { + setTitle(" Delete Progress for all episodes of ${media.nameRomaji}") + setMessage("This will delete all the locally stored progress for all episodes") + setPosButton(R.string.ok){ + val prefix = "${media.id}_" + val regex = Regex("^${prefix}\\d+$") + + PrefManager.getAllCustomValsForMedia(prefix) + .keys + .filter { it.matches(regex) } + .onEach { key -> PrefManager.removeCustomVal(key) } + snackString("Deleted the progress of all Episodes for ${media.nameRomaji}") + } + setNegButton(R.string.no) + show() + } + true + } } fun subscribeButton(enabled: Boolean) { diff --git a/app/src/main/java/ani/dantotsu/media/manga/MangaReadAdapter.kt b/app/src/main/java/ani/dantotsu/media/manga/MangaReadAdapter.kt index e75b1aa9..39aef576 100644 --- a/app/src/main/java/ani/dantotsu/media/manga/MangaReadAdapter.kt +++ b/app/src/main/java/ani/dantotsu/media/manga/MangaReadAdapter.kt @@ -1,6 +1,7 @@ package ani.dantotsu.media.manga import android.content.Intent +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -10,6 +11,7 @@ import android.widget.ImageButton import android.widget.LinearLayout import android.widget.NumberPicker import androidx.core.content.ContextCompat +import androidx.core.content.ContextCompat.getString import androidx.core.content.ContextCompat.startActivity import androidx.core.view.isGone import androidx.core.view.isVisible @@ -63,6 +65,15 @@ class MangaReadAdapter( var scanlatorSelectionListener: ScanlatorSelectionListener? = null var options = listOf() + private fun clearCustomValsForMedia(mediaId: String, suffix: String) { + val customVals = PrefManager.getAllCustomValsForMedia("$mediaId$suffix") + customVals.forEach { (key) -> + PrefManager.removeCustomVal(key) + Log.d("PrefManager", "Removed key: $key") + } + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val bind = ItemMediaSourceBinding.inflate(LayoutInflater.from(parent.context), parent, false) return ViewHolder(bind) @@ -93,14 +104,8 @@ class MangaReadAdapter( setTitle(" Delete Progress for all chapters of ${media.nameRomaji}") setMessage("This will delete all the locally stored progress for chapters") setPosButton(R.string.ok){ - val currentChapters = PrefManager.getAllCustomValsForMedia("${media.id}_Chapter") - currentChapters.forEach { (key) -> - PrefManager.removeCustomVal(key) - } - val currentChapterWithVolume = PrefManager.getAllCustomValsForMedia("${media.id}_Vol") - currentChapterWithVolume.forEach { (key) -> - PrefManager.removeCustomVal(key) - } + clearCustomValsForMedia("${media.id}", "_Chapter") + clearCustomValsForMedia("${media.id}", "_Vol") snackString("Deleted the progress of Chapters for ${media.nameRomaji}") } setNegButton(R.string.no) @@ -277,6 +282,22 @@ class MangaReadAdapter( show() } } + resetProgress.setOnClickListener { + fragment.requireContext().customAlertDialog().apply { + setTitle(" Delete Progress for all chapters of ${media.nameRomaji}") + setMessage("This will delete all the locally stored progress for chapters") + setPosButton(R.string.ok){ +// Usage + clearCustomValsForMedia("${media.id}", "_Chapter") + clearCustomValsForMedia("${media.id}", "_Vol") + + snackString("Deleted the progress of Chapters for ${media.nameRomaji}") + } + setNegButton(R.string.no) + show() + } + } + resetProgressDef.text = getString(currContext()!!,R.string.clear_stored_chapter) // Scanlator mangaScanlatorContainer.isVisible = options.count() > 1 diff --git a/app/src/main/java/ani/dantotsu/settings/saving/PrefManager.kt b/app/src/main/java/ani/dantotsu/settings/saving/PrefManager.kt index e173231f..c0833ee2 100644 --- a/app/src/main/java/ani/dantotsu/settings/saving/PrefManager.kt +++ b/app/src/main/java/ani/dantotsu/settings/saving/PrefManager.kt @@ -235,10 +235,18 @@ object PrefManager { apply() } } + + /** + * Retrieves all SharedPreferences entries with keys starting with the specified prefix. + * + * @param prefix The prefix to filter keys. + * @return A map containing key-value pairs that match the prefix. + */ fun getAllCustomValsForMedia(prefix: String): Map { + val prefs = irrelevantPreferences ?: return emptyMap() val allEntries = mutableMapOf() - irrelevantPreferences?.all?.forEach { (key, value) -> + prefs.all.forEach { (key, value) -> if (key.startsWith(prefix)) { allEntries[key] = value } @@ -249,6 +257,7 @@ object PrefManager { + @Suppress("UNCHECKED_CAST") fun getLiveVal(prefName: PrefName, default: T): SharedPreferenceLiveData { val pref = getPrefLocation(prefName.data.prefLocation) diff --git a/app/src/main/res/drawable/ic_delete.xml b/app/src/main/res/drawable/ic_delete.xml new file mode 100644 index 00000000..0b9c4378 --- /dev/null +++ b/app/src/main/res/drawable/ic_delete.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/dialog_layout.xml b/app/src/main/res/layout/dialog_layout.xml index eab3f479..3db06395 100644 --- a/app/src/main/res/layout/dialog_layout.xml +++ b/app/src/main/res/layout/dialog_layout.xml @@ -292,5 +292,53 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8a2217ac..f4ef3697 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1083,6 +1083,8 @@ Non quae tempore quo provident laudantium qui illo dolor vel quia dolor et exerc Route All Your Network Traffic Through a Socks5 Proxy Proxy Setup Configure your Socks5 Proxy + Clear Stored Episode Data + Clear Stored Chapter Data