Only on phone and emulator and a few fixes

This commit is contained in:
Luna712 2024-06-17 13:03:20 -06:00 committed by GitHub
parent baaf9320e9
commit 5cfd98e4a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 33 deletions

View file

@ -33,6 +33,8 @@ import com.lagradost.cloudstream3.ui.player.GeneratorPlayer
import com.lagradost.cloudstream3.ui.player.LinkGenerator import com.lagradost.cloudstream3.ui.player.LinkGenerator
import com.lagradost.cloudstream3.ui.result.FOCUS_SELF import com.lagradost.cloudstream3.ui.result.FOCUS_SELF
import com.lagradost.cloudstream3.ui.result.setLinearListLayout import com.lagradost.cloudstream3.ui.result.setLinearListLayout
import com.lagradost.cloudstream3.ui.settings.Globals.EMULATOR
import com.lagradost.cloudstream3.ui.settings.Globals.PHONE
import com.lagradost.cloudstream3.ui.settings.Globals.TV import com.lagradost.cloudstream3.ui.settings.Globals.TV
import com.lagradost.cloudstream3.ui.settings.Globals.isLayout import com.lagradost.cloudstream3.ui.settings.Globals.isLayout
import com.lagradost.cloudstream3.utils.AppUtils.loadResult import com.lagradost.cloudstream3.utils.AppUtils.loadResult
@ -199,6 +201,7 @@ class DownloadFragment : Fragment() {
nextDown = FOCUS_SELF nextDown = FOCUS_SELF
) )
if (isLayout(PHONE or EMULATOR)) {
val itemTouchHelper = ItemTouchHelper( val itemTouchHelper = ItemTouchHelper(
DownloadSwipeDeleteCallback( DownloadSwipeDeleteCallback(
this.adapter as DownloadHeaderAdapter, this.adapter as DownloadHeaderAdapter,
@ -207,6 +210,7 @@ class DownloadFragment : Fragment() {
) )
itemTouchHelper.attachToRecyclerView(binding?.downloadList) itemTouchHelper.attachToRecyclerView(binding?.downloadList)
} }
}
// Should be visible in emulator layout // Should be visible in emulator layout
binding?.downloadStreamButton?.isGone = isLayout(TV) binding?.downloadStreamButton?.isGone = isLayout(TV)

View file

@ -82,6 +82,15 @@ class DownloadSwipeDeleteCallback(
val swipeDistance = minOf(minSwipeDistance, maxSwipeDistance) val swipeDistance = minOf(minSwipeDistance, maxSwipeDistance)
val limitedDX = if (dX < -swipeDistance) -swipeDistance else if (dX >= 0) 0f else dX val limitedDX = if (dX < -swipeDistance) -swipeDistance else if (dX >= 0) 0f else dX
val position = viewHolder.bindingAdapterPosition
if (swipeOpenItems.contains(position)) {
// If the item is already swiped we need to restore that
// state so that you can delete items without the state
// resetting, making it easier to quickly delete multiple items.
super.onChildDraw(c, recyclerView, viewHolder, limitedDX, dY, actionState, isCurrentlyActive)
}
if (limitedDX < 0) { // Swiping to the left if (limitedDX < 0) { // Swiping to the left
val icon = deleteIcon ?: return val icon = deleteIcon ?: return
@ -126,11 +135,11 @@ class DownloadSwipeDeleteCallback(
icon.draw(c) icon.draw(c)
} else background.setBounds(0, 0, 0, 0) } else background.setBounds(0, 0, 0, 0)
if (dX <= -swipeDistance && !isCurrentlyActive) { if (dX <= -swipeDistance && !isCurrentlyActive && adapter.cardList.getOrNull(position) != null) {
swipeOpenItems.add(viewHolder.bindingAdapterPosition) swipeOpenItems.add(position)
setRecyclerViewTouchListener(recyclerView, swipeDistance) setRecyclerViewTouchListener(recyclerView, swipeDistance)
} else { } else {
swipeOpenItems.remove(viewHolder.bindingAdapterPosition) swipeOpenItems.remove(position)
if (swipeOpenItems.isEmpty()) removeRecyclerViewTouchListener(recyclerView) if (swipeOpenItems.isEmpty()) removeRecyclerViewTouchListener(recyclerView)
super.onChildDraw(c, recyclerView, viewHolder, limitedDX, dY, actionState, isCurrentlyActive) super.onChildDraw(c, recyclerView, viewHolder, limitedDX, dY, actionState, isCurrentlyActive)
} }
@ -153,9 +162,7 @@ class DownloadSwipeDeleteCallback(
if (event.action == MotionEvent.ACTION_UP) { if (event.action == MotionEvent.ACTION_UP) {
val x = event.x.toInt() val x = event.x.toInt()
val y = event.y.toInt() val y = event.y.toInt()
// We use toList() to avoid a very rare edge case swipeOpenItems.forEach { pos ->
// where it gives concurrent modification errors
swipeOpenItems.toList().forEach { pos ->
val vh = recyclerView.findViewHolderForAdapterPosition(pos) val vh = recyclerView.findViewHolderForAdapterPosition(pos)
vh?.itemView?.let { swipeItemView -> vh?.itemView?.let { swipeItemView ->
val backgroundLeft: Int = swipeItemView.right - swipeDistance.toInt() val backgroundLeft: Int = swipeItemView.right - swipeDistance.toInt()
@ -163,7 +170,7 @@ class DownloadSwipeDeleteCallback(
val backgroundYRange: IntRange = swipeItemView.top..swipeItemView.bottom val backgroundYRange: IntRange = swipeItemView.top..swipeItemView.bottom
if (x in backgroundXRange && y in backgroundYRange) { if (x in backgroundXRange && y in backgroundYRange) {
handleDeleteAction(pos) handleDeleteAction(pos)
addDownloadDeleteEvent(pos, recyclerView) addDownloadDeleteEvent(pos)
return@setOnTouchListener true return@setOnTouchListener true
} }
} }
@ -200,10 +207,7 @@ class DownloadSwipeDeleteCallback(
} }
} }
private fun addDownloadDeleteEvent( private fun addDownloadDeleteEvent(position: Int) {
position: Int,
recyclerView: RecyclerView
) {
// Clear any old events as we don't want to get // Clear any old events as we don't want to get
// concurrent modification errors // concurrent modification errors
clearDownloadDeleteEvent() clearDownloadDeleteEvent()
@ -221,29 +225,30 @@ class DownloadSwipeDeleteCallback(
* and performant solution to it since we do have access to * and performant solution to it since we do have access to
* the position we need to target here. * the position we need to target here.
*/ */
if (list.getOrNull(position) != null) {
adapter.cardList.removeAt(position) adapter.cardList.removeAt(position)
}
adapter.notifyItemRemoved(position) adapter.notifyItemRemoved(position)
adapter.notifyItemRangeChanged(
position,
adapter.cardList.size
) // rebind to new positions
/**
* we need to clear the listener now since nothing should be open
* and it was closed outside of on onChildDraw so we don't
* want to have random unexpected touch events.
*/
removeRecyclerViewTouchListener(recyclerView)
} }
} }
// We use synchronized to ensure we are thread-safe and
// to avoid potential race conditions that may cause
// concurrent modification errors
synchronized(this) {
downloadDeleteEventListener?.let { downloadDeleteEvent += it } downloadDeleteEventListener?.let { downloadDeleteEvent += it }
} }
}
private fun clearDownloadDeleteEvent() { private fun clearDownloadDeleteEvent() {
// We use synchronized to ensure we are thread-safe and
// to avoid potential race conditions that may cause
// concurrent modification errors
synchronized(this) {
if (downloadDeleteEventListener != null) { if (downloadDeleteEventListener != null) {
downloadDeleteEvent -= downloadDeleteEventListener!! downloadDeleteEvent -= downloadDeleteEventListener!!
downloadDeleteEventListener = null downloadDeleteEventListener = null
} }
} }
} }
}