Add a clear button to the watch history

This commit is contained in:
Bnyro 2022-12-28 17:07:35 +01:00
parent 8b39c9924d
commit 2945f3c27a
5 changed files with 99 additions and 23 deletions

View File

@ -9,5 +9,6 @@ object IntentData {
const val position = "position"
const val fileName = "fileName"
const val openQueueOnce = "openQueue"
const val keepQueue = "keepQueue"
const val playlistType = "playlistType"
}

View File

@ -127,11 +127,16 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
private val commentsViewModel: CommentsViewModel by activityViewModels()
/**
* video information
* Video information passed by the intent
*/
private var videoId: String? = null
private var playlistId: String? = null
private var channelId: String? = null
private var keepQueue: Boolean = false
/**
* Video information fetched at runtime
*/
private var isLive = false
private lateinit var streams: Streams
@ -179,6 +184,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
videoId = it.getString(IntentData.videoId)!!.toID()
playlistId = it.getString(IntentData.playlistId)
channelId = it.getString(IntentData.channelId)
keepQueue = it.getBoolean(IntentData.keepQueue, false)
}
}
@ -202,7 +208,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
context?.hideKeyboard(view)
// clear the playing queue
PlayingQueue.resetToDefaults()
if (!keepQueue) PlayingQueue.resetToDefaults()
changeOrientationMode()

View File

@ -9,14 +9,17 @@ import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.databinding.FragmentWatchHistoryBinding
import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toPixel
import com.github.libretube.ui.adapters.WatchHistoryAdapter
import com.github.libretube.ui.base.BaseFragment
import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.util.ProxyHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class WatchHistoryFragment : BaseFragment() {
private lateinit var binding: FragmentWatchHistoryBinding
@ -41,6 +44,21 @@ class WatchHistoryFragment : BaseFragment() {
)
}
binding.clear.setOnClickListener {
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.clear_history)
.setMessage(R.string.irreversible)
.setPositiveButton(R.string.okay) { _, _ ->
binding.historyScrollView.visibility = View.GONE
binding.historyEmpty.visibility = View.VISIBLE
query {
Database.watchHistoryDao().deleteAll()
}
}
.setNegativeButton(R.string.cancel, null)
.show()
}
val watchHistory = awaitQuery {
Database.watchHistoryDao().getAll()
}
@ -88,17 +106,17 @@ class WatchHistoryFragment : BaseFragment() {
// observe changes
watchHistoryAdapter.registerAdapterDataObserver(object :
RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
if (watchHistoryAdapter.itemCount == 0) {
binding.watchHistoryRecView.visibility = View.GONE
binding.historyEmpty.visibility = View.VISIBLE
RecyclerView.AdapterDataObserver() {
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
if (watchHistoryAdapter.itemCount == 0) {
binding.historyScrollView.visibility = View.GONE
binding.historyEmpty.visibility = View.VISIBLE
}
}
}
})
})
binding.watchHistoryRecView.adapter = watchHistoryAdapter
binding.historyEmpty.visibility = View.GONE
binding.watchHistoryRecView.visibility = View.VISIBLE
binding.historyScrollView.visibility = View.VISIBLE
}
}

View File

@ -49,23 +49,29 @@ object NavigationHelper {
context: Context,
videoId: String?,
playlistId: String? = null,
channelId: String? = null
channelId: String? = null,
keepQueue: Boolean = false
) {
if (videoId == null) return
val bundle = Bundle()
bundle.putString(IntentData.videoId, videoId.toID())
bundle.putString(IntentData.playlistId, playlistId)
bundle.putString(IntentData.channelId, channelId)
val bundle = Bundle().apply {
putString(IntentData.videoId, videoId.toID())
putString(IntentData.playlistId, playlistId)
putString(IntentData.channelId, channelId)
putBoolean(IntentData.keepQueue, keepQueue)
}
val frag = PlayerFragment()
frag.arguments = bundle
val activity = context as AppCompatActivity
activity.supportFragmentManager.beginTransaction()
.remove(PlayerFragment())
.commit()
activity.supportFragmentManager.beginTransaction()
.replace(R.id.container, frag)
.replace(
R.id.container,
PlayerFragment().apply {
arguments = bundle
}
)
.commitNow()
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -26,12 +27,56 @@
android:textStyle="bold" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/watchHistoryRecView"
<ScrollView
android:id="@+id/historyScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:nestedScrollingEnabled="false"
android:visibility="gone" />
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/play_all"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:maxLines="1"
android:text="@string/play_all"
app:icon="@drawable/ic_playlist" />
<com.google.android.material.button.MaterialButton
android:id="@+id/clear"
style="@style/Widget.Material3.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:maxLines="1"
android:text="@string/clear_history"
app:icon="@drawable/ic_delete" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/watchHistoryRecView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:nestedScrollingEnabled="false" />
</LinearLayout>
</ScrollView>
</FrameLayout>