swipe to left to delete from history

This commit is contained in:
Bnyro 2022-08-12 12:05:12 +02:00
parent 21a5f17a19
commit 47ed98ffc3
3 changed files with 69 additions and 18 deletions

View File

@ -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 {

View File

@ -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)
binding.watchHistoryRecView.adapter = watchHistoryAdapter // reversed order
binding.historyEmpty.visibility = View.GONE binding.watchHistoryRecView.layoutManager = LinearLayoutManager(requireContext()).apply {
binding.watchHistoryRecView.visibility = View.VISIBLE reverseLayout = true
stackFromEnd = true
} }
// reverse order val watchHistoryAdapter = WatchHistoryAdapter(
val linearLayoutManager = LinearLayoutManager(view.context) watchHistory,
linearLayoutManager.reverseLayout = true childFragmentManager
linearLayoutManager.stackFromEnd = true )
binding.watchHistoryRecView.layoutManager = linearLayoutManager 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.historyEmpty.visibility = View.GONE
binding.watchHistoryRecView.visibility = View.VISIBLE
} }
} }

View File

@ -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> {