From 9ab76ba47b47e114a72c93c622167f27177700a3 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 17 Nov 2024 21:00:59 +0100 Subject: [PATCH] feat: option to only delete watched videos --- .../com/github/libretube/db/DatabaseHelper.kt | 26 +++++++------------ .../libretube/ui/adapters/DownloadsAdapter.kt | 2 ++ .../ui/fragments/DownloadsFragment.kt | 16 +++++++++--- app/src/main/res/values/strings.xml | 1 + 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt index bae0a5c9ae..26b3b2900c 100644 --- a/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt +++ b/app/src/main/java/com/github/libretube/db/DatabaseHelper.kt @@ -87,16 +87,17 @@ object DatabaseHelper { } } + suspend fun isVideoWatched(videoId: String, duration: Long): Boolean = withContext(Dispatchers.IO) { + val historyItem = Database.watchPositionDao() + .findById(videoId) ?: return@withContext false + val progress = historyItem.position / 1000 + // show video only in feed when watched less than 90% + return@withContext progress > 0.9f * duration + } + suspend fun filterUnwatched(streams: List): List { return streams.filter { - withContext(Dispatchers.IO) { - val historyItem = Database.watchPositionDao() - .findById(it.url.orEmpty().toID()) ?: return@withContext true - val progress = historyItem.position / 1000 - val duration = it.duration ?: 0 - // show video only in feed when watched less than 90% - progress < 0.9f * duration - } + !isVideoWatched(it.url.orEmpty().toID(), it.duration ?: 0) } } @@ -105,14 +106,7 @@ object DatabaseHelper { unfinished: Boolean = true ): List { return streams.filter { - withContext(Dispatchers.IO) { - val historyItem = Database.watchPositionDao() - .findById(it.videoId) ?: return@withContext true - val progress = historyItem.position / 1000 - val duration = it.duration ?: 0 - // show video only in feed when watched less than 90% - if (unfinished) progress < 0.9f * duration else progress > 0.9f * duration - } + unfinished xor isVideoWatched(it.videoId, it.duration ?: 0) } } diff --git a/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt index 29cf3a967d..35bfca46f4 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt @@ -148,6 +148,8 @@ class DownloadsAdapter( .show() } + fun itemAt(index: Int) = downloads[index] + fun deleteDownload(position: Int) { val download = downloads[position].download val items = downloads[position].downloadItems diff --git a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt index cc8bad9eb6..bce0eeb173 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt @@ -24,6 +24,7 @@ import com.github.libretube.constants.IntentData import com.github.libretube.constants.PreferenceKeys import com.github.libretube.databinding.FragmentDownloadContentBinding import com.github.libretube.databinding.FragmentDownloadsBinding +import com.github.libretube.db.DatabaseHelper import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.obj.DownloadWithItems import com.github.libretube.db.obj.filterByTab @@ -270,12 +271,21 @@ class DownloadsFragmentPage : DynamicLayoutManagerFragment() { } private fun showDeleteAllDialog(context: Context, adapter: DownloadsAdapter) { + var onlyDeleteWatchedVideos = false + MaterialAlertDialogBuilder(context) .setTitle(R.string.delete_all) - .setMessage(R.string.irreversible) + .setMultiChoiceItems(arrayOf(getString(R.string.delete_only_watched_videos)), null) { _, _, selected -> + onlyDeleteWatchedVideos = selected + } .setPositiveButton(R.string.okay) { _, _ -> - for (downloadIndex in downloads.size - 1 downTo 0) { - adapter.deleteDownload(downloadIndex) + lifecycleScope.launch { + for (downloadIndex in downloads.size - 1 downTo 0) { + val download = adapter.itemAt(downloadIndex).download + if (!onlyDeleteWatchedVideos || DatabaseHelper.isVideoWatched(download.videoId, download.duration ?: 0)) { + adapter.deleteDownload(downloadIndex) + } + } } } .setNegativeButton(R.string.cancel, null) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9d1f07f4dd..09244cebef 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -521,6 +521,7 @@ A local version of this video is available. Would you like to play the video from the download folder? %1$s views + Only delete already watched videos Download Service