mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
swipe to left to delete from history
This commit is contained in:
parent
21a5f17a19
commit
47ed98ffc3
@ -20,6 +20,12 @@ class WatchHistoryAdapter(
|
|||||||
RecyclerView.Adapter<WatchHistoryViewHolder>() {
|
RecyclerView.Adapter<WatchHistoryViewHolder>() {
|
||||||
private val TAG = "WatchHistoryAdapter"
|
private val TAG = "WatchHistoryAdapter"
|
||||||
|
|
||||||
|
fun removeFromWatchHistory(position: Int) {
|
||||||
|
PreferenceHelper.removeFromWatchHistory(position)
|
||||||
|
watchHistory.removeAt(position)
|
||||||
|
notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WatchHistoryViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WatchHistoryViewHolder {
|
||||||
val layoutInflater = LayoutInflater.from(parent.context)
|
val layoutInflater = LayoutInflater.from(parent.context)
|
||||||
val binding = WatchHistoryRowBinding.inflate(layoutInflater, parent, false)
|
val binding = WatchHistoryRowBinding.inflate(layoutInflater, parent, false)
|
||||||
@ -41,9 +47,7 @@ class WatchHistoryAdapter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
deleteBTN.setOnClickListener {
|
deleteBTN.setOnClickListener {
|
||||||
PreferenceHelper.removeFromWatchHistory(video.videoId!!)
|
removeFromWatchHistory(position)
|
||||||
watchHistory.removeAt(position)
|
|
||||||
notifyDataSetChanged()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
root.setOnClickListener {
|
root.setOnClickListener {
|
||||||
|
@ -4,7 +4,9 @@ import android.os.Bundle
|
|||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
import androidx.recyclerview.widget.ItemTouchHelper
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.github.libretube.adapters.WatchHistoryAdapter
|
import com.github.libretube.adapters.WatchHistoryAdapter
|
||||||
import com.github.libretube.databinding.FragmentWatchHistoryBinding
|
import com.github.libretube.databinding.FragmentWatchHistoryBinding
|
||||||
import com.github.libretube.extensions.BaseFragment
|
import com.github.libretube.extensions.BaseFragment
|
||||||
@ -28,18 +30,56 @@ class WatchHistoryFragment : BaseFragment() {
|
|||||||
|
|
||||||
val watchHistory = PreferenceHelper.getWatchHistory()
|
val watchHistory = PreferenceHelper.getWatchHistory()
|
||||||
|
|
||||||
if (watchHistory.isNotEmpty()) {
|
if (watchHistory.isEmpty()) return
|
||||||
val watchHistoryAdapter = WatchHistoryAdapter(watchHistory, childFragmentManager)
|
|
||||||
|
// reversed order
|
||||||
|
binding.watchHistoryRecView.layoutManager = LinearLayoutManager(requireContext()).apply {
|
||||||
|
reverseLayout = true
|
||||||
|
stackFromEnd = true
|
||||||
|
}
|
||||||
|
|
||||||
|
val watchHistoryAdapter = WatchHistoryAdapter(
|
||||||
|
watchHistory,
|
||||||
|
childFragmentManager
|
||||||
|
)
|
||||||
|
|
||||||
|
val itemTouchCallback = object : ItemTouchHelper.SimpleCallback(
|
||||||
|
0,
|
||||||
|
ItemTouchHelper.LEFT
|
||||||
|
) {
|
||||||
|
override fun onMove(
|
||||||
|
recyclerView: RecyclerView,
|
||||||
|
viewHolder: RecyclerView.ViewHolder,
|
||||||
|
target: RecyclerView.ViewHolder
|
||||||
|
): Boolean {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSwiped(
|
||||||
|
viewHolder: RecyclerView.ViewHolder,
|
||||||
|
direction: Int
|
||||||
|
) {
|
||||||
|
val position = viewHolder.absoluteAdapterPosition
|
||||||
|
watchHistoryAdapter.removeFromWatchHistory(position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val itemTouchHelper = ItemTouchHelper(itemTouchCallback)
|
||||||
|
itemTouchHelper.attachToRecyclerView(binding.watchHistoryRecView)
|
||||||
|
|
||||||
|
// observe changes
|
||||||
|
watchHistoryAdapter.registerAdapterDataObserver(object :
|
||||||
|
RecyclerView.AdapterDataObserver() {
|
||||||
|
override fun onChanged() {
|
||||||
|
if (watchHistoryAdapter.itemCount == 0) {
|
||||||
|
binding.watchHistoryRecView.visibility = View.GONE
|
||||||
|
binding.historyEmpty.visibility = View.VISIBLE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
binding.watchHistoryRecView.adapter = watchHistoryAdapter
|
binding.watchHistoryRecView.adapter = watchHistoryAdapter
|
||||||
binding.historyEmpty.visibility = View.GONE
|
binding.historyEmpty.visibility = View.GONE
|
||||||
binding.watchHistoryRecView.visibility = View.VISIBLE
|
binding.watchHistoryRecView.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
|
|
||||||
// reverse order
|
|
||||||
val linearLayoutManager = LinearLayoutManager(view.context)
|
|
||||||
linearLayoutManager.reverseLayout = true
|
|
||||||
linearLayoutManager.stackFromEnd = true
|
|
||||||
|
|
||||||
binding.watchHistoryRecView.layoutManager = linearLayoutManager
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -159,11 +159,18 @@ object PreferenceHelper {
|
|||||||
watchHistory.forEachIndexed { index, item ->
|
watchHistory.forEachIndexed { index, item ->
|
||||||
if (item.videoId == videoId) indexToRemove = index
|
if (item.videoId == videoId) indexToRemove = index
|
||||||
}
|
}
|
||||||
if (indexToRemove != null) {
|
if (indexToRemove == null) return
|
||||||
watchHistory.removeAt(indexToRemove!!)
|
watchHistory.removeAt(indexToRemove!!)
|
||||||
val json = mapper.writeValueAsString(watchHistory)
|
val json = mapper.writeValueAsString(watchHistory)
|
||||||
editor.putString("watch_history", json).commit()
|
editor.putString("watch_history", json).commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun removeFromWatchHistory(position: Int) {
|
||||||
|
val watchHistory = getWatchHistory()
|
||||||
|
watchHistory.removeAt(position)
|
||||||
|
|
||||||
|
val json = mapper.writeValueAsString(watchHistory)
|
||||||
|
editor.putString("watch_history", json).commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getWatchHistory(): ArrayList<WatchHistoryItem> {
|
fun getWatchHistory(): ArrayList<WatchHistoryItem> {
|
||||||
|
Loading…
Reference in New Issue
Block a user