Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a delegate property for single component items #81

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.appcompat.app.AppCompatActivity
import com.yelp.android.bento.componentcontrollers.RecyclerViewComponentController
import com.yelp.android.bento.core.Component
import com.yelp.android.bento.core.ComponentViewHolder
import com.yelp.android.bento.core.notifyingItem
import com.yelp.android.bento.utils.inflate
import com.yelp.android.bentosampleapp.components.LabeledComponent
import kotlinx.android.synthetic.main.activity_recycler_view.*
Expand All @@ -33,9 +34,10 @@ class ComponentReplacementActivity : AppCompatActivity() {
}

class ReplaceComponentButton(
private var replacementId: Int,
replacementId: Int,
private val onComponentClicked: (replacenentId: Int) -> Unit
) : Component() {
private var replacementId by notifyingItem(replacementId)
override fun getPresenter(position: Int) = this

override fun getItem(position: Int) = replacementId
Expand All @@ -47,7 +49,6 @@ class ReplaceComponentButton(
fun onClick() {
onComponentClicked(replacementId)
replacementId++
notifyDataChanged()
}
}

Expand Down
18 changes: 18 additions & 0 deletions bento/src/main/java/com/yelp/android/bento/core/ComponentX.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.yelp.android.bento.core

import kotlin.properties.ObservableProperty
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
* Returns the data items of the [Component] as a sequence.
*/
Expand All @@ -20,3 +24,17 @@ fun Component.asItemSequence(): Sequence<Any?> {
ComponentIterator(this)
}
}

/**
* Helper delegate that can be used in a single item component, that will take care of notifying for
* change when its value gets updated.
*/
fun <T> Component.notifyingItem(initialValue: T): ReadWriteProperty<Any?, T> {
return object : ObservableProperty<T>(initialValue) {
override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) {
if (oldValue != newValue) {
notifyDataChanged()
}
}
}
}