Skip to content

Commit 6271367

Browse files
committed
implemented actions builder.
1 parent f53f0c5 commit 6271367

2 files changed

Lines changed: 84 additions & 19 deletions

File tree

  • app/src/main/java/github/com/st235/swipetoactionlayout
  • lib-swipetoactionlayout/src/main/java/github/com/st235/lib_swipetoactionlayout

app/src/main/java/github/com/st235/swipetoactionlayout/ShowcaseActivity.kt

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,29 @@ class ShowcaseActivity : AppCompatActivity() {
3737

3838
swipeToActionLayout.actions =
3939
listOf(
40-
SwipeAction.withBackgroundColor(
40+
SwipeAction.Builder(
4141
actionId = R.id.action_call,
42-
iconId = R.drawable.ic_call_black_18dp,
43-
text = "Call",
44-
backgroundColor = 0xFF455A64.toInt()
45-
),
46-
SwipeAction.withBackgroundColor(
42+
iconId = R.drawable.ic_call_black_18dp
43+
)
44+
.text("Call")
45+
.backgroundColor(0xFF455A64.toInt())
46+
.build(),
47+
SwipeAction.Builder(
4748
actionId = R.id.action_email,
48-
iconId = R.drawable.ic_email_black_18dp,
49-
text = "Email",
50-
backgroundColor = 0xFF37474F.toInt()
51-
),
52-
SwipeAction.withBackgroundColor(
49+
iconId = R.drawable.ic_email_black_18dp
50+
)
51+
.text("Email")
52+
.backgroundColor(0xFF37474F.toInt())
53+
.build(),
54+
SwipeAction.Builder(
5355
actionId = R.id.action_delete,
54-
iconId = R.drawable.ic_delete_black_18dp,
55-
text = "Remove",
56-
iconTint = 0xFFEEEEEE.toInt(),
57-
textColor = 0xFFEEEEEE.toInt(),
58-
backgroundColor = 0xFF263238.toInt()
56+
iconId = R.drawable.ic_delete_black_18dp
5957
)
58+
.text("Remove")
59+
.backgroundColor(0xFF263238.toInt())
60+
.iconTint(0xFFEEEEEE.toInt())
61+
.textColor(0xFFEEEEEE.toInt())
62+
.build()
6063
)
6164

6265
vibrateCheckBox.setOnCheckedChangeListener { _, isChecked ->

lib-swipetoactionlayout/src/main/java/github/com/st235/lib_swipetoactionlayout/SwipeAction.kt

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,80 @@ import android.content.Context
44
import android.graphics.Color
55
import android.graphics.drawable.ColorDrawable
66
import android.graphics.drawable.Drawable
7-
import androidx.annotation.*
7+
import androidx.annotation.ColorInt
8+
import androidx.annotation.ColorRes
9+
import androidx.annotation.DrawableRes
10+
import androidx.annotation.IdRes
811
import androidx.core.content.ContextCompat
12+
import androidx.core.view.ViewCompat
913

1014
data class SwipeAction internal constructor(
1115
@IdRes val actionId: Int,
1216
val background: Drawable?,
1317
@DrawableRes val iconId: Int,
1418
val text: CharSequence?,
15-
@ColorInt val iconTint: Int = Color.WHITE,
16-
@ColorInt val textColor: Int = Color.WHITE
19+
@ColorInt val iconTint: Int,
20+
@ColorInt val textColor: Int
1721
) {
1822

23+
constructor(builder: Builder):
24+
this(
25+
builder.actionId,
26+
builder.background,
27+
builder.iconId,
28+
builder.text,
29+
builder.iconTint,
30+
builder.textColor
31+
)
32+
33+
class Builder(
34+
@IdRes internal val actionId: Int = ViewCompat.generateViewId(),
35+
@DrawableRes internal val iconId: Int
36+
) {
37+
38+
internal var background: Drawable? = null
39+
internal var text: CharSequence? = null
40+
@ColorInt
41+
internal var iconTint: Int = Color.WHITE
42+
@ColorInt
43+
internal var textColor: Int = Color.WHITE
44+
45+
fun background(background: Drawable): Builder {
46+
this.background = background
47+
return this
48+
}
49+
50+
fun backgroundRes(context: Context, @DrawableRes backgroundRes: Int): Builder {
51+
this.background = ContextCompat.getDrawable(context, backgroundRes)
52+
return this
53+
}
54+
55+
fun backgroundColor(@ColorInt color: Int): Builder {
56+
this.background = ColorDrawable(color)
57+
return this
58+
}
59+
60+
fun text(text: CharSequence): Builder {
61+
this.text = text
62+
return this
63+
}
64+
65+
fun iconTint(@ColorInt iconTint: Int): Builder {
66+
this.iconTint = iconTint
67+
return this
68+
}
69+
70+
fun textColor(@ColorInt textColor: Int): Builder {
71+
this.textColor = textColor
72+
return this
73+
}
74+
75+
fun build(): SwipeAction {
76+
return SwipeAction(this)
77+
}
78+
79+
}
80+
1981
companion object {
2082

2183
fun withBackgroundColor(

0 commit comments

Comments
 (0)