Skip to content

Commit

Permalink
feat: option to only delete watched videos
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Nov 17, 2024
1 parent 9012a58 commit 9ab76ba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
26 changes: 10 additions & 16 deletions app/src/main/java/com/github/libretube/db/DatabaseHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<StreamItem>): List<StreamItem> {
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)
}
}

Expand All @@ -105,14 +106,7 @@ object DatabaseHelper {
unfinished: Boolean = true
): List<WatchHistoryItem> {
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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
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 @@ -521,6 +521,7 @@
<string name="dialog_play_offline_title">A local version of this video is available.</string>
<string name="dialog_play_offline_body">Would you like to play the video from the download folder?</string>
<string name="view_count">%1$s views</string>
<string name="delete_only_watched_videos">Only delete already watched videos</string>

<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>
Expand Down

0 comments on commit 9ab76ba

Please sign in to comment.