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>() {
private val TAG = "WatchHistoryAdapter"
fun removeFromWatchHistory(position: Int) {
PreferenceHelper.removeFromWatchHistory(position)
watchHistory.removeAt(position)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WatchHistoryViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = WatchHistoryRowBinding.inflate(layoutInflater, parent, false)
@ -41,9 +47,7 @@ class WatchHistoryAdapter(
}
deleteBTN.setOnClickListener {
PreferenceHelper.removeFromWatchHistory(video.videoId!!)
watchHistory.removeAt(position)
notifyDataSetChanged()
removeFromWatchHistory(position)
}
root.setOnClickListener {

View File

@ -4,7 +4,9 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.adapters.WatchHistoryAdapter
import com.github.libretube.databinding.FragmentWatchHistoryBinding
import com.github.libretube.extensions.BaseFragment
@ -28,18 +30,56 @@ class WatchHistoryFragment : BaseFragment() {
val watchHistory = PreferenceHelper.getWatchHistory()
if (watchHistory.isNotEmpty()) {
val watchHistoryAdapter = WatchHistoryAdapter(watchHistory, childFragmentManager)
binding.watchHistoryRecView.adapter = watchHistoryAdapter
binding.historyEmpty.visibility = View.GONE
binding.watchHistoryRecView.visibility = View.VISIBLE
if (watchHistory.isEmpty()) return
// reversed order
binding.watchHistoryRecView.layoutManager = LinearLayoutManager(requireContext()).apply {
reverseLayout = true
stackFromEnd = true
}
// reverse order
val linearLayoutManager = LinearLayoutManager(view.context)
linearLayoutManager.reverseLayout = true
linearLayoutManager.stackFromEnd = true
val watchHistoryAdapter = WatchHistoryAdapter(
watchHistory,
childFragmentManager
)
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 ->
if (item.videoId == videoId) indexToRemove = index
}
if (indexToRemove != null) {
watchHistory.removeAt(indexToRemove!!)
val json = mapper.writeValueAsString(watchHistory)
editor.putString("watch_history", json).commit()
}
if (indexToRemove == null) return
watchHistory.removeAt(indexToRemove!!)
val json = mapper.writeValueAsString(watchHistory)
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> {