Skip to content

Commit

Permalink
added swipe to perform
Browse files Browse the repository at this point in the history
  • Loading branch information
pknotfound committed Apr 1, 2021
1 parent 3666041 commit b19edd7
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.graphics.Canvas
import androidx.annotation.ColorInt
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.kedia.swipetodelete.utils.DIRECTION
import java.lang.Exception


Expand Down Expand Up @@ -83,9 +84,3 @@ interface OnSwiped {
fun swipeToDelete(adapterPosition: Int)
}

enum class DIRECTION(val direction: Int) {
UP (ItemTouchHelper.UP),
DOWN(ItemTouchHelper.DOWN),
RIGHT(ItemTouchHelper.RIGHT),
LEFT(ItemTouchHelper.LEFT)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.kedia.swipetodelete

import android.graphics.Canvas
import androidx.annotation.ColorInt
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.kedia.swipetodelete.utils.DIRECTION
import java.lang.Exception

fun RecyclerView.addSwipeToPerform(
list: List<DIRECTION>? = emptyList(),
listener: OnSwipeToPerform? = null,
@ColorInt colorOneInt: Int? = null,
@ColorInt colorTwoInt: Int? = null
) {

var swipeDirs = ItemTouchHelper.RIGHT
list?.apply {
for (element in list) {
if (element != DIRECTION.RIGHT) {
swipeDirs = swipeDirs or DIRECTION.valueOf(element.name).direction
}
}
}

val simpleCallback = object : ItemTouchHelper.SimpleCallback(0 , swipeDirs) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return false
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
listener?.swipeToPerform(adapterPosition = viewHolder.adapterPosition)
}

override fun onChildDraw(
c: Canvas,
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
dX: Float,
dY: Float,
actionState: Int,
isCurrentlyActive: Boolean
) {
c.clipRect(0f, viewHolder.itemView.top.toFloat(),
dX, viewHolder.itemView.bottom.toFloat())

if (colorTwoInt != null && colorOneInt == null)
throw Exception("Color One cannot be null if Color Two is non null")

if (colorTwoInt == null) {
colorOneInt?.apply { c.drawColor(this) }
} else {
if(dX < width / 2)
colorOneInt?.apply { c.drawColor(this) }
else
colorTwoInt?.apply { c.drawColor(this) }
}

super.onChildDraw(
c,
recyclerView,
viewHolder,
dX,
dY,
actionState,
isCurrentlyActive
)
}
}
ItemTouchHelper(simpleCallback).attachToRecyclerView(this)
}

interface OnSwipeToPerform {
fun swipeToPerform(adapterPosition: Int)
}
10 changes: 10 additions & 0 deletions SwipeToDelete/src/main/java/com/kedia/swipetodelete/utils/utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.kedia.swipetodelete.utils

import androidx.recyclerview.widget.ItemTouchHelper

enum class DIRECTION(val direction: Int) {
UP (ItemTouchHelper.UP),
DOWN(ItemTouchHelper.DOWN),
RIGHT(ItemTouchHelper.RIGHT),
LEFT(ItemTouchHelper.LEFT)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.kedia.customswipelibrary

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.kedia.swipetodelete.*
import com.kedia.swipetodelete.utils.DIRECTION
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), OnSwiped, OnDragged {
class MainActivity : AppCompatActivity(), OnSwiped, OnDragged, OnSwipeToPerform {

private val list = mutableListOf<String>()
private lateinit var adapter: Adapter
Expand All @@ -30,6 +32,7 @@ class MainActivity : AppCompatActivity(), OnSwiped, OnDragged {
val list = listOf(
DIRECTION.LEFT,
DIRECTION.RIGHT)
recycler.addSwipeToPerform(list, this, ContextCompat.getColor(this, R.color.colorPrimaryDark))
recycler.addSwipeToDelete(list, this, ContextCompat.getColor(this, R.color.colorPrimaryDark))
// try {
// SwipeToDelete.javaClass.getDeclaredMethod("some").invoke(SwipeToDelete)
Expand All @@ -46,4 +49,8 @@ class MainActivity : AppCompatActivity(), OnSwiped, OnDragged {
adapter.removeItem(adapterPosition)
}

override fun swipeToPerform(adapterPosition: Int) {
Log.d("TAG!!!!", "swipeToPerform: swiped")
}

}

0 comments on commit b19edd7

Please sign in to comment.