Skip to content

Commit 3e89463

Browse files
committed
feat: introduced withAdapterItem<SomeItem> { } to replace error prone and cumbersome (getAdapterItem() as? SomeItem)?.apply { }
1 parent 74c1a82 commit 3e89463

File tree

5 files changed

+19
-10
lines changed

5 files changed

+19
-10
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,13 @@ open class ExampleItem(private val context: Context, private val text: String)
394394

395395
init {
396396
titleField.setOnClickListener {
397-
(getAdapterItem() as? ExampleItem)?.apply {
397+
withAdapterItem<ExampleItem> {
398398
onTitleClicked(adapterPosition)
399399
}
400400
}
401401

402402
subtitleField.setOnClickListener {
403-
(getAdapterItem() as? ExampleItem)?.apply {
403+
withAdapterItem<ExampleItem> {
404404
onSubTitleClicked(adapterPosition)
405405
}
406406
}
@@ -412,15 +412,15 @@ open class ExampleItem(private val context: Context, private val text: String)
412412
As you can see, to handle click events on a view, you have to create a click listener in the ViewHolder and propagate an event to the `AdapterItem`:
413413
```kotlin
414414
titleField.setOnClickListener {
415-
(getAdapterItem() as? ExampleItem)?.apply {
415+
withAdapterItem<ExampleItem> {
416416
onTitleClicked(adapterPosition)
417417
}
418418
}
419419
```
420420
You can call any method defined in your `AdapterItem` and pass whatever parameters you want. It's important that you honor nullability, as each ViewHolder has a weak reference to its `AdapterItem`, so to prevent crashes at runtime always use the form:
421421

422422
```kotlin
423-
(getAdapterItem() as? YourAdapterItem)?.apply {
423+
withAdapterItem<ExampleItem> {
424424
// methods to call on the adapter item
425425
}
426426
```
@@ -482,7 +482,7 @@ class TextWithButtonItem(private val text: String) : AdapterItem<TextWithButtonI
482482

483483
init {
484484
buttonField.setOnClickListener {
485-
(getAdapterItem() as? TextWithButtonItem)?.apply {
485+
withAdapterItem<TextWithButtonItem> {
486486
pressed = buttonField.isChecked
487487
notifyItemChanged()
488488
}
@@ -496,7 +496,7 @@ In the `Holder` we have added a click listener to the `ToggleButton`. When the u
496496
So, to recap, the <a name="eventLifecycle"></a>event lifecycle is:
497497
```kotlin
498498
// gets ViewHolder's AdapterItem
499-
(getAdapterItem() as? YourAdapterItem)?.apply {
499+
withAdapterItem<YourAdapterItem> {
500500
// methods to call on the adapter item
501501

502502
// (optional)

app/demo/src/main/java/net/gotev/recycleradapterdemo/adapteritems/SelectableItem.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ open class SelectableItem(val label: String, val onClick: ((item: SelectableItem
2828

2929
init {
3030
toggleField.setOnClickListener {
31-
(getAdapterItem() as? SelectableItem)?.apply {
31+
withAdapterItem<SelectableItem> {
3232
selected = !selected
3333
onClick?.invoke(this)
3434
}

app/demo/src/main/java/net/gotev/recycleradapterdemo/adapteritems/TextWithToggleItem.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class TextWithToggleItem(private val text: String) : AdapterItem<TextWithToggleI
2727

2828
init {
2929
itemView.setOnClickListener {
30-
(getAdapterItem() as? TextWithToggleItem)?.apply {
30+
withAdapterItem<TextWithToggleItem> {
3131
pressed = !buttonField.isChecked
3232
notifyItemChanged()
3333
}

app/demo/src/main/java/net/gotev/recycleradapterdemo/adapteritems/TitleSubtitleItem.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ open class TitleSubtitleItem(private val title: String, private val subtitle: St
3939

4040
init {
4141
titleField.setOnClickListener {
42-
(getAdapterItem() as? TitleSubtitleItem)?.apply {
42+
withAdapterItem<TitleSubtitleItem> {
4343
onTitleClicked(itemView.context, adapterPosition)
4444
}
4545
}
4646

4747
subtitleField.setOnClickListener {
48-
(getAdapterItem() as? TitleSubtitleItem)?.apply {
48+
withAdapterItem<TitleSubtitleItem> {
4949
onSubTitleClicked(itemView.context, adapterPosition)
5050
}
5151
}

recycleradapter/src/main/java/net/gotev/recycleradapter/RecyclerAdapterViewHolder.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@ abstract class RecyclerAdapterViewHolder(itemView: View) : RecyclerView.ViewHold
1616
adapter = WeakReference(recyclerAdapter)
1717
}
1818

19+
@Deprecated(
20+
message = "use withAdapterItem<Type> { } instead",
21+
level = DeprecationLevel.WARNING
22+
)
1923
protected fun getAdapterItem() = adapter?.get()?.getAdapterItem(this)
2024

25+
@Suppress("UNCHECKED_CAST")
26+
protected fun <T: AdapterItem<*>> withAdapterItem(action: T.() -> Unit) {
27+
(adapter?.get()?.getAdapterItem(this) as? T)?.apply(action)
28+
}
29+
2130
/**
2231
* Notifies that the model associated to this ViewHolder has been changed.
2332
*/

0 commit comments

Comments
 (0)