Skip to content

Commit e8b0a7f

Browse files
committed
update library
1 parent 8c15e70 commit e8b0a7f

File tree

11 files changed

+66
-50
lines changed

11 files changed

+66
-50
lines changed

.github/workflows/testing.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
name: Test, Review and Generate APK
22
on:
3-
push:
4-
branches: [ "master" ]
5-
pull_request:
6-
# The branches below must be a subset of the branches above
7-
branches: [ "master" ]
3+
# push:
4+
# branches: [ "master" ]
5+
# pull_request:
6+
# # The branches below must be a subset of the branches above
7+
# branches: [ "master" ]
8+
workflow_dispatch:
89

910
permissions:
1011
contents: read

RecyclerViewSwipeExt/src/main/java/dev/jahidhasanco/recyclerviewswipeext/ChildToDraw.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class ChildToDraw
1616
private val swipeView: SwipeView
1717
) {
1818
var bg: ColorDrawable? = null
19-
var icon: Drawable? = null
20-
var paintText: Paint? = null
21-
var text: String? = null
19+
var icon: Drawable? = null
20+
var paintText: Paint? = null
21+
var text: String? = null
2222

2323
operator fun invoke(): ChildToDraw {
2424
var iconMargin = 0
@@ -29,7 +29,7 @@ class ChildToDraw
2929
context,
3030
if (mSide == LEFT) swipeView.leftIcon else swipeView.rightIcon
3131
)
32-
iconMargin = (v.height - icon!!.intrinsicHeight + (text?.length?:0))/ 2
32+
iconMargin = (v.height - icon!!.intrinsicHeight + (text?.length ?: 0)) / 2
3333
iconTop = v.top + (v.height - icon!!.intrinsicHeight) / 2
3434
iconBottom = iconTop + icon!!.intrinsicHeight
3535
} catch (e: Exception) {
@@ -48,7 +48,7 @@ class ChildToDraw
4848
}
4949
bg = ColorDrawable(
5050
context.resources
51-
.getColor(swipeView.leftBg,context.theme)
51+
.getColor(swipeView.leftBg, context.theme)
5252
)
5353
bg!!.setBounds(
5454
v.left, v.top, v.left +
@@ -64,7 +64,7 @@ class ChildToDraw
6464
}
6565
bg = ColorDrawable(
6666
context.resources
67-
.getColor(swipeView.rightBg,context.theme)
67+
.getColor(swipeView.rightBg, context.theme)
6868
)
6969
bg!!.setBounds(
7070
v.right + dX, v.top,
@@ -83,8 +83,8 @@ class ChildToDraw
8383
return this
8484
}
8585

86-
companion object {
87-
const val LEFT = 0
88-
const val RIGHT = 1
89-
}
86+
companion object {
87+
const val LEFT = 0
88+
const val RIGHT = 1
89+
}
9090
}

RecyclerViewSwipeExt/src/main/java/dev/jahidhasanco/recyclerviewswipeext/RecyclerViewSwipeExt.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import android.util.AttributeSet
66
import androidx.recyclerview.widget.ItemTouchHelper
77
import androidx.recyclerview.widget.RecyclerView
88

9-
class RecyclerViewSwipeExt(context: Context, attrs: AttributeSet?, private val recyclerView: RecyclerView): SwipeListener {
9+
class RecyclerViewSwipeExt(
10+
context: Context,
11+
attrs: AttributeSet?,
12+
private val recyclerView: RecyclerView
13+
) : SwipeListener {
1014

1115
private var mListener: SwipeListener? = null
1216
private lateinit var mSwipedView: SwipeView
1317

1418
init {
15-
createSwipedView(context,attrs);
16-
recyclerView.setWillNotDraw(false);
19+
createSwipedView(context, attrs)
20+
recyclerView.setWillNotDraw(false)
1721
}
1822

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

6468
override fun onSwipedLeft(position: Int) {
65-
mListener?.onSwipedLeft(position);
69+
mListener?.onSwipedLeft(position)
6670
}
6771

6872
override fun onSwipedRight(position: Int) {
69-
mListener?.onSwipedRight(position);
73+
mListener?.onSwipedRight(position)
7074
}
7175

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

96100
}
97101

98-
fun RecyclerView.makeLeftRightSwipeAble(context: Context,attrs: AttributeSet? = null): RecyclerViewSwipeExt {
99-
return RecyclerViewSwipeExt(context,attrs,this)
102+
fun RecyclerView.makeLeftRightSwipeAble(
103+
context: Context,
104+
attrs: AttributeSet? = null
105+
): RecyclerViewSwipeExt {
106+
return RecyclerViewSwipeExt(context, attrs, this)
100107
}

RecyclerViewSwipeExt/src/main/java/dev/jahidhasanco/recyclerviewswipeext/SwipeCallback.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ class SwipeCallback constructor(
7272
bg?.draw(c)
7373
icon?.draw(c)
7474
c.drawText(
75-
toDraw.text?:"", icon?.bounds?.centerX()?.toFloat()?:0f,
76-
(icon?.bounds?.centerY()?.toFloat() ?: 0f) + (icon?.bounds?.height()?:0) +
75+
toDraw.text ?: "", icon?.bounds?.centerX()?.toFloat() ?: 0f,
76+
(icon?.bounds?.centerY()?.toFloat() ?: 0f) + (icon?.bounds?.height() ?: 0) +
7777
convertDpToPixel(5f, context), (paint!!)
7878
)
7979
}

RecyclerViewSwipeExt/src/main/java/dev/jahidhasanco/recyclerviewswipeext/SwipeView.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package dev.jahidhasanco.recyclerviewswipeext
22

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

55
private var mIcons: IntArray
66
private var mBackgrounds: IntArray
77
private var mTexts: Array<String>
88
var textColor = R.color.white
99
var textSize = 15
10-
var leftText: String = "Delete"
11-
var rightText: String ="Read"
10+
var leftText: String = ""
11+
var rightText: String = ""
1212

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

1818
init {
1919
mIcons = icons
20-
if(mIcons.size == 2){
20+
if (mIcons.size == 2) {
2121
leftIcon = mIcons[0]
2222
rightIcon = mIcons[1]
2323
}
2424
mBackgrounds = backgrounds
25-
if(backgrounds.size == 2){
25+
if (backgrounds.size == 2) {
2626
leftBg = mBackgrounds[0]
2727
rightBg = mBackgrounds[1]
2828
}
2929
mTexts = texts
30-
if(mTexts.size == 2){
30+
if (mTexts.size == 2) {
3131
leftText = mTexts[0]
3232
rightText = mTexts[1]
3333
}

RecyclerViewSwipeExt/src/main/res/drawable/baseline_delete_24.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<vector android:height="24dp" android:tint="#000000"
1+
<vector android:height="24dp" android:tint="#FFFFFF"
22
android:viewportHeight="24" android:viewportWidth="24"
33
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
44
<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"/>

RecyclerViewSwipeExt/src/main/res/drawable/baseline_markunread_24.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<vector android:height="24dp" android:tint="#000000"
1+
<vector android:height="24dp" android:tint="#FFFFFF"
22
android:viewportHeight="24" android:viewportWidth="24"
33
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
44
<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"/>

app/src/main/java/dev/jahidhasanco/recyclerviewswipeextdemo/MainActivity.kt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,45 @@ class MainActivity : AppCompatActivity() {
2020

2121
val data = ArrayList<ItemsViewModel>()
2222
for (i in 1..20) {
23-
data.add(ItemsViewModel(R.drawable.baseline_markunread_24, "Item $i"))
23+
data.add(ItemsViewModel(R.drawable.baseline_outlet_24, "Item $i"))
2424
}
2525

2626
val adapter = CustomAdapter(data)
2727
recyclerview.adapter = adapter
2828

2929
recyclerview.makeLeftRightSwipeAble(this)
30-
.setLeftText("Delete")
31-
.setRightText("Archive")
32-
.setTextColor(R.color.white)
3330
.setListener(object : SwipeListener {
34-
3531
override fun onSwipedLeft(position: Int) {
32+
val deletedItem = data[position]
3633
data.removeAt(position)
37-
adapter.notifyDataSetChanged();
34+
adapter.notifyItemRemoved(position)
3835
Snackbar.make(
3936
recyclerview,
40-
"Item $position Left",
37+
"Item $position Archive",
4138
Snackbar.LENGTH_LONG
42-
).show();
39+
).setAction(
40+
"Undo"
41+
) {
42+
data.add(position, deletedItem)
43+
adapter.notifyItemInserted(position)
44+
}.show()
4345
}
4446

4547

4648
override fun onSwipedRight(position: Int) {
47-
// data.removeAt(position)
48-
adapter.notifyDataSetChanged();
49+
val deletedItem = data[position]
50+
data.removeAt(position)
51+
adapter.notifyItemRemoved(position)
4952
Snackbar.make(
5053
recyclerview,
51-
"Item $position Right",
54+
"Item $position Deleted",
5255
Snackbar.LENGTH_LONG
53-
).show();
56+
).setAction(
57+
"Undo"
58+
) {
59+
data.add(position, deletedItem)
60+
adapter.notifyItemInserted(position)
61+
}.show()
5462
}
5563

5664
})

app/src/main/res/drawable/baseline_markunread_24.xml

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#000000"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<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"/>
5+
</vector>

0 commit comments

Comments
 (0)