From 2573c3732220f263bf8bb50d65b09c89c5e878b2 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 4 Jun 2023 16:59:25 +0200 Subject: [PATCH] Show 'mark as unwatched' in video options sheet if already watched --- .../libretube/db/dao/WatchHistoryDao.kt | 7 +++++++ .../libretube/db/dao/WatchPositionDao.kt | 3 +++ .../ui/sheets/VideoOptionsBottomSheet.kt | 21 ++++++++++++++++--- app/src/main/res/values/strings.xml | 1 + 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/github/libretube/db/dao/WatchHistoryDao.kt b/app/src/main/java/com/github/libretube/db/dao/WatchHistoryDao.kt index 6857fb432..4cbf90b5e 100644 --- a/app/src/main/java/com/github/libretube/db/dao/WatchHistoryDao.kt +++ b/app/src/main/java/com/github/libretube/db/dao/WatchHistoryDao.kt @@ -6,12 +6,16 @@ import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query import com.github.libretube.db.obj.WatchHistoryItem +import com.github.libretube.db.obj.WatchPosition @Dao interface WatchHistoryDao { @Query("SELECT * FROM watchHistoryItem") suspend fun getAll(): List + @Query("SELECT * FROM watchHistoryItem WHERE videoId LIKE :videoId LIMIT 1") + suspend fun findById(videoId: String): WatchHistoryItem? + @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun insert(watchHistoryItem: WatchHistoryItem) @@ -21,6 +25,9 @@ interface WatchHistoryDao { @Delete suspend fun delete(watchHistoryItem: WatchHistoryItem) + @Query("DELETE FROM watchHistoryItem WHERE videoId = :id") + fun deleteByVideoId(id: String) + @Query("DELETE FROM watchHistoryItem") suspend fun deleteAll() } diff --git a/app/src/main/java/com/github/libretube/db/dao/WatchPositionDao.kt b/app/src/main/java/com/github/libretube/db/dao/WatchPositionDao.kt index 3f4d898a9..9cbf800e5 100644 --- a/app/src/main/java/com/github/libretube/db/dao/WatchPositionDao.kt +++ b/app/src/main/java/com/github/libretube/db/dao/WatchPositionDao.kt @@ -20,6 +20,9 @@ interface WatchPositionDao { @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun insertAll(watchPositions: List) + @Query("DELETE FROM watchPosition WHERE videoId = :id") + fun deleteByVideoId(id: String) + @Query("DELETE FROM watchPosition") suspend fun deleteAll() } diff --git a/app/src/main/java/com/github/libretube/ui/sheets/VideoOptionsBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/VideoOptionsBottomSheet.kt index 4dbb94338..07e4e86d3 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/VideoOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/VideoOptionsBottomSheet.kt @@ -20,6 +20,7 @@ import com.github.libretube.ui.dialogs.ShareDialog import com.github.libretube.ui.fragments.SubscriptionsFragment import com.github.libretube.util.PlayingQueue import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext /** @@ -47,9 +48,17 @@ class VideoOptionsBottomSheet( optionsList += getString(R.string.add_to_queue) } - // show the mark as watched option if watch positions are enabled - if (PlayerHelper.watchPositionsVideo) { - optionsList += getString(R.string.mark_as_watched) + // show the mark as watched or unwatched option if watch positions are enabled + if (PlayerHelper.watchPositionsVideo || PlayerHelper.watchHistoryEnabled) { + val watchPositionEntry = runBlocking(Dispatchers.IO) { + DatabaseHolder.Database.watchPositionDao().findById(videoId) + } + val watchHistoryEntry = runBlocking(Dispatchers.IO) { + DatabaseHolder.Database.watchHistoryDao().findById(videoId) + } + optionsList += if (watchHistoryEntry != null || watchPositionEntry != null) { + getString(R.string.mark_as_unwatched) + } else getString(R.string.mark_as_watched) } setSimpleItems(optionsList) { which -> @@ -116,6 +125,12 @@ class VideoOptionsBottomSheet( ) } } + getString(R.string.mark_as_unwatched) -> { + withContext(Dispatchers.IO) { + DatabaseHolder.Database.watchPositionDao().deleteByVideoId(videoId) + DatabaseHolder.Database.watchHistoryDao().deleteByVideoId(videoId) + } + } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 35448a110..a48a5fc7e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -412,6 +412,7 @@ Creation date (reversed) Alphabetic Alphabetic (reversed) + Mark as unwatched Import subscriptions from