Skip to content

Commit

Permalink
update library
Browse files Browse the repository at this point in the history
  • Loading branch information
JahidHasanCO committed Mar 16, 2023
1 parent 8c15e70 commit e8b0a7f
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 50 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: Test, Review and Generate APK
on:
push:
branches: [ "master" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "master" ]
# push:
# branches: [ "master" ]
# pull_request:
# # The branches below must be a subset of the branches above
# branches: [ "master" ]
workflow_dispatch:

permissions:
contents: read
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class ChildToDraw
private val swipeView: SwipeView
) {
var bg: ColorDrawable? = null
var icon: Drawable? = null
var paintText: Paint? = null
var text: String? = null
var icon: Drawable? = null
var paintText: Paint? = null
var text: String? = null

operator fun invoke(): ChildToDraw {
var iconMargin = 0
Expand All @@ -29,7 +29,7 @@ class ChildToDraw
context,
if (mSide == LEFT) swipeView.leftIcon else swipeView.rightIcon
)
iconMargin = (v.height - icon!!.intrinsicHeight + (text?.length?:0))/ 2
iconMargin = (v.height - icon!!.intrinsicHeight + (text?.length ?: 0)) / 2
iconTop = v.top + (v.height - icon!!.intrinsicHeight) / 2
iconBottom = iconTop + icon!!.intrinsicHeight
} catch (e: Exception) {
Expand All @@ -48,7 +48,7 @@ class ChildToDraw
}
bg = ColorDrawable(
context.resources
.getColor(swipeView.leftBg,context.theme)
.getColor(swipeView.leftBg, context.theme)
)
bg!!.setBounds(
v.left, v.top, v.left +
Expand All @@ -64,7 +64,7 @@ class ChildToDraw
}
bg = ColorDrawable(
context.resources
.getColor(swipeView.rightBg,context.theme)
.getColor(swipeView.rightBg, context.theme)
)
bg!!.setBounds(
v.right + dX, v.top,
Expand All @@ -83,8 +83,8 @@ class ChildToDraw
return this
}

companion object {
const val LEFT = 0
const val RIGHT = 1
}
companion object {
const val LEFT = 0
const val RIGHT = 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import android.util.AttributeSet
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

class RecyclerViewSwipeExt(context: Context, attrs: AttributeSet?, private val recyclerView: RecyclerView): SwipeListener {
class RecyclerViewSwipeExt(
context: Context,
attrs: AttributeSet?,
private val recyclerView: RecyclerView
) : SwipeListener {

private var mListener: SwipeListener? = null
private lateinit var mSwipedView: SwipeView

init {
createSwipedView(context,attrs);
recyclerView.setWillNotDraw(false);
createSwipedView(context, attrs)
recyclerView.setWillNotDraw(false)
}

fun setRightBg(bg: Int): RecyclerViewSwipeExt {
Expand Down Expand Up @@ -62,11 +66,11 @@ class RecyclerViewSwipeExt(context: Context, attrs: AttributeSet?, private val r
}

override fun onSwipedLeft(position: Int) {
mListener?.onSwipedLeft(position);
mListener?.onSwipedLeft(position)
}

override fun onSwipedRight(position: Int) {
mListener?.onSwipedRight(position);
mListener?.onSwipedRight(position)
}

private fun createSwipedView(context: Context, attrs: AttributeSet?) {
Expand Down Expand Up @@ -95,6 +99,9 @@ class RecyclerViewSwipeExt(context: Context, attrs: AttributeSet?, private val r

}

fun RecyclerView.makeLeftRightSwipeAble(context: Context,attrs: AttributeSet? = null): RecyclerViewSwipeExt {
return RecyclerViewSwipeExt(context,attrs,this)
fun RecyclerView.makeLeftRightSwipeAble(
context: Context,
attrs: AttributeSet? = null
): RecyclerViewSwipeExt {
return RecyclerViewSwipeExt(context, attrs, this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class SwipeCallback constructor(
bg?.draw(c)
icon?.draw(c)
c.drawText(
toDraw.text?:"", icon?.bounds?.centerX()?.toFloat()?:0f,
(icon?.bounds?.centerY()?.toFloat() ?: 0f) + (icon?.bounds?.height()?:0) +
toDraw.text ?: "", icon?.bounds?.centerX()?.toFloat() ?: 0f,
(icon?.bounds?.centerY()?.toFloat() ?: 0f) + (icon?.bounds?.height() ?: 0) +
convertDpToPixel(5f, context), (paint!!)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package dev.jahidhasanco.recyclerviewswipeext

class SwipeView constructor(icons: IntArray, backgrounds: IntArray, texts: Array<String>){
class SwipeView constructor(icons: IntArray, backgrounds: IntArray, texts: Array<String>) {

private var mIcons: IntArray
private var mBackgrounds: IntArray
private var mTexts: Array<String>
var textColor = R.color.white
var textSize = 15
var leftText: String = "Delete"
var rightText: String ="Read"
var leftText: String = ""
var rightText: String = ""

var leftIcon: Int = R.drawable.baseline_delete_24
var rightIcon: Int = R.drawable.baseline_markunread_24
Expand All @@ -17,17 +17,17 @@ class SwipeView constructor(icons: IntArray, backgrounds: IntArray, texts: Array

init {
mIcons = icons
if(mIcons.size == 2){
if (mIcons.size == 2) {
leftIcon = mIcons[0]
rightIcon = mIcons[1]
}
mBackgrounds = backgrounds
if(backgrounds.size == 2){
if (backgrounds.size == 2) {
leftBg = mBackgrounds[0]
rightBg = mBackgrounds[1]
}
mTexts = texts
if(mTexts.size == 2){
if (mTexts.size == 2) {
leftText = mTexts[0]
rightText = mTexts[1]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<vector android:height="24dp" android:tint="#000000"
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<vector android:height="24dp" android:tint="#000000"
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,45 @@ class MainActivity : AppCompatActivity() {

val data = ArrayList<ItemsViewModel>()
for (i in 1..20) {
data.add(ItemsViewModel(R.drawable.baseline_markunread_24, "Item $i"))
data.add(ItemsViewModel(R.drawable.baseline_outlet_24, "Item $i"))
}

val adapter = CustomAdapter(data)
recyclerview.adapter = adapter

recyclerview.makeLeftRightSwipeAble(this)
.setLeftText("Delete")
.setRightText("Archive")
.setTextColor(R.color.white)
.setListener(object : SwipeListener {

override fun onSwipedLeft(position: Int) {
val deletedItem = data[position]
data.removeAt(position)
adapter.notifyDataSetChanged();
adapter.notifyItemRemoved(position)
Snackbar.make(
recyclerview,
"Item $position Left",
"Item $position Archive",
Snackbar.LENGTH_LONG
).show();
).setAction(
"Undo"
) {
data.add(position, deletedItem)
adapter.notifyItemInserted(position)
}.show()
}


override fun onSwipedRight(position: Int) {
// data.removeAt(position)
adapter.notifyDataSetChanged();
val deletedItem = data[position]
data.removeAt(position)
adapter.notifyItemRemoved(position)
Snackbar.make(
recyclerview,
"Item $position Right",
"Item $position Deleted",
Snackbar.LENGTH_LONG
).show();
).setAction(
"Undo"
) {
data.add(position, deletedItem)
adapter.notifyItemInserted(position)
}.show()
}

})
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/res/drawable/baseline_markunread_24.xml

This file was deleted.

5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_outlet_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.48,2 2,6.48 2,12c0,5.52 4.48,10 10,10s10,-4.48 10,-10C22,6.48 17.52,2 12,2zM9,12c-0.55,0 -1,-0.45 -1,-1V8c0,-0.55 0.45,-1 1,-1s1,0.45 1,1v3C10,11.55 9.55,12 9,12zM14,18h-4v-2c0,-1.1 0.9,-2 2,-2c1.1,0 2,0.9 2,2V18zM16,11c0,0.55 -0.45,1 -1,1c-0.55,0 -1,-0.45 -1,-1V8c0,-0.55 0.45,-1 1,-1c0.55,0 1,0.45 1,1V11z"/>
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<resources>
<string name="app_name">RecyclerViewSwipeExt</string>
<string name="app_name">Demo App</string>
</resources>

0 comments on commit e8b0a7f

Please sign in to comment.