Add play all from watch history button

This commit is contained in:
Bnyro 2022-12-28 17:16:00 +01:00
parent 2945f3c27a
commit 1e7b38e209

View File

@ -10,6 +10,7 @@ import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.databinding.FragmentWatchHistoryBinding import com.github.libretube.databinding.FragmentWatchHistoryBinding
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.awaitQuery import com.github.libretube.extensions.awaitQuery
@ -18,6 +19,8 @@ import com.github.libretube.extensions.toPixel
import com.github.libretube.ui.adapters.WatchHistoryAdapter import com.github.libretube.ui.adapters.WatchHistoryAdapter
import com.github.libretube.ui.base.BaseFragment import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.ui.models.PlayerViewModel import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.PlayingQueue
import com.github.libretube.util.ProxyHelper import com.github.libretube.util.ProxyHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -44,6 +47,17 @@ class WatchHistoryFragment : BaseFragment() {
) )
} }
val watchHistory = awaitQuery {
Database.watchHistoryDao().getAll()
}
if (watchHistory.isEmpty()) return
watchHistory.forEach {
it.thumbnailUrl = ProxyHelper.rewriteUrl(it.thumbnailUrl)
it.uploaderAvatar = ProxyHelper.rewriteUrl(it.uploaderAvatar)
}
binding.clear.setOnClickListener { binding.clear.setOnClickListener {
MaterialAlertDialogBuilder(requireContext()) MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.clear_history) .setTitle(R.string.clear_history)
@ -59,15 +73,23 @@ class WatchHistoryFragment : BaseFragment() {
.show() .show()
} }
val watchHistory = awaitQuery { binding.playAll.setOnClickListener {
Database.watchHistoryDao().getAll() PlayingQueue.resetToDefaults()
} PlayingQueue.add(
*watchHistory.reversed().map {
if (watchHistory.isEmpty()) return StreamItem(
url = "/watch?v=${it.videoId}",
watchHistory.forEach { title = it.title,
it.thumbnailUrl = ProxyHelper.rewriteUrl(it.thumbnailUrl) thumbnail = it.thumbnailUrl,
it.uploaderAvatar = ProxyHelper.rewriteUrl(it.uploaderAvatar) uploaderName = it.uploader,
uploaderUrl = it.uploaderUrl,
uploaderAvatar = it.uploaderAvatar,
uploadedDate = it.uploadDate,
duration = it.duration
)
}.toTypedArray()
)
NavigationHelper.navigateVideo(requireContext(), watchHistory.last().videoId, keepQueue = true)
} }
// reversed order // reversed order