diff --git a/app/src/main/java/com/github/libretube/adapters/WatchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/adapters/WatchHistoryAdapter.kt index 5ff8604b4..f6a63f981 100644 --- a/app/src/main/java/com/github/libretube/adapters/WatchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/WatchHistoryAdapter.kt @@ -20,6 +20,12 @@ class WatchHistoryAdapter( RecyclerView.Adapter() { 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 { diff --git a/app/src/main/java/com/github/libretube/fragments/WatchHistoryFragment.kt b/app/src/main/java/com/github/libretube/fragments/WatchHistoryFragment.kt index df8fec274..2aa4ad123 100644 --- a/app/src/main/java/com/github/libretube/fragments/WatchHistoryFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/WatchHistoryFragment.kt @@ -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 } } diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt index 3b7c69c27..e53f18863 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt @@ -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 {