Post about this Gist is on Medium: https://medium.com/@uludagcan/observable-livedata-rx-bind-extensions-4f2188a53ad1
import android.graphics.Bitmap | |
import android.graphics.drawable.Drawable | |
import android.view.View | |
import android.widget.ImageView | |
import android.widget.TextView | |
import androidx.lifecycle.LifecycleOwner | |
import androidx.lifecycle.LiveData | |
import androidx.lifecycle.Observer | |
import com.jakewharton.rxrelay2.BehaviorRelay | |
import io.reactivex.disposables.CompositeDisposable | |
import io.reactivex.rxkotlin.addTo | |
fun <T> LiveData<T>.bindText(lifecycleOwner: LifecycleOwner, textView: TextView?) { | |
this.observe(lifecycleOwner, Observer { textView?.text = it.toString() }) | |
} | |
fun LiveData<Bitmap>.bindBitmap(lifecycleOwner: LifecycleOwner, imageView: ImageView?) { | |
this.observe(lifecycleOwner, Observer { imageView?.setImageBitmap(it) }) | |
} | |
fun LiveData<Drawable>.bindDrawable(lifecycleOwner: LifecycleOwner, imageView: ImageView?) { | |
this.observe(lifecycleOwner, Observer { imageView?.setImageDrawable(value) }) | |
} | |
fun LiveData<Boolean>.bindVisibility(lifecycleOwner: LifecycleOwner, view: View?) { | |
this.observe( | |
lifecycleOwner, | |
Observer { value -> | |
view?.visibility = if (value) View.VISIBLE else View.GONE | |
}) | |
} | |
fun <T> BehaviorRelay<T>.bindText(textView: TextView?, bag: CompositeDisposable) { | |
this.subscribe { textView?.text = it.toString() }?.addTo(bag) | |
} | |
fun BehaviorRelay<Boolean>.bindVisibility(view: View?, bag: CompositeDisposable) { | |
this.subscribe { view?.isSelected = it }?.addTo(bag) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment