Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

RxBinding-kotlin

4,650 views

Published on

RxBinding-kotlin #Kotlin_Sansan

Published in: Technology
  • I need help with toglebuttononSubscribe. Whats your emial address pls
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

RxBinding-kotlin

  1. 1. RxBinding-kotlin @satorufujiwara
  2. 2. about me @satorufujiwara CyberAgent, Inc. AmebaFRESH!
  3. 3. AmebaFRESH! • 85% of code is written in Kotlin • kotlin-android-example • https://github.com/satorufujiwara/kotlin-android-example • 2016年、Kotlinで開発する方へ • http://qiita.com/satorufujiwara/items/871c5b7b66c7691d82a8
  4. 4. RxBiding-kotlin
  5. 5. RxBinding • RxJava binding APIs for Android UI widgets from the platform and support libraries. • by Jake Wharton • https://github.com/JakeWharton/RxBinding • Renamed from NotRxAndroid. • WidgetObservable and ViewObservable have been rolled from RxAndroid into (and improved in) it.
  6. 6. RxBinding • rxbinding • rxbinding-support-v4 • rxbinding-appcompat-v7 • rxbinding-design • rxbinding-recyclerview-v7 • rxbinding-leanback-v17
  7. 7. RxBinding-kotlin • Depends on each RxBinding’s jar. • Kotlin extension methods for each binding method.
  8. 8. RxBinding-kotlin • rxbinding-kotlin • rxbinding-support-v4-kotlin • rxbinding-appcompat-v7-kotlin • rxbinding-design-kotlin • rxbinding-recyclerview-v7-kotlin • rxbinding-leanback-v17-kotlin
  9. 9. RxBinding-kotlin • Work well with Kotlin’s lambda. • Work well with Kotlin’s extensions.
  10. 10. Basic usage in Java RxViewPager.pageSelections(viewPager)
 .subscribe(new Action1<Integer>() {
 @Override
 public void call(Integer integer) {
 // call when page selected
 }
 });
  11. 11. Basic usage in Kotlin viewPager.pageSelections()
 .subscribe {
 // call when page selected
 }
  12. 12. rxbinding-support-v4-kotlin public inline fun ViewPager.pageSelections(): Observable<Int> = RxViewPager.pageSelections(this)
  13. 13. rxbinding-support-v4 public final class RxViewPager { 
 public static Observable<Integer> pageSelections(ViewPager view) {
 checkNotNull(view, "view == null");
 return Observable.create(new ViewPagerPageSelectedOnSubscribe(view));
 }
 }
  14. 14. ViewPagerPageSelectedOnSubscribe final class ViewPagerPageSelectedOnSubscribe implements Observable.OnSubscribe<Integer> {
 final ViewPager view;
 
 ViewPagerPageSelectedOnSubscribe(ViewPager view) {
 this.view = view;
 }
 
 @Override public void call(final Subscriber<? super Integer> subscriber) {
 checkUiThread();
 
 final ViewPager.OnPageChangeListener listener = new ViewPager.SimpleOnPageChangeListener() {
 @Override public void onPageSelected(int position) {
 if (!subscriber.isUnsubscribed()) {
 subscriber.onNext(position);
 }
 }
 };
 view.addOnPageChangeListener(listener);
 
 subscriber.add(new MainThreadSubscription() {
 @Override protected void onUnsubscribe() {
 view.removeOnPageChangeListener(listener);
 }
 });
 
 // Emit initial value.
 subscriber.onNext(view.getCurrentItem());
 }
 }
  15. 15. ViewPagerPageSelectedOnSubscribe final class ViewPagerPageSelectedOnSubscribe implements Observable.OnSubscribe<Integer> {
 final ViewPager view;
 
 ViewPagerPageSelectedOnSubscribe(ViewPager view) {
 this.view = view;
 }
 
 @Override public void call(final Subscriber<? super Integer> subscriber) {
 checkUiThread();
 
 final ViewPager.OnPageChangeListener listener = new ViewPager.SimpleOnPageChangeListener() {
 @Override public void onPageSelected(int position) {
 if (!subscriber.isUnsubscribed()) {
 subscriber.onNext(position);
 }
 }
 };
 view.addOnPageChangeListener(listener);
 
 subscriber.add(new MainThreadSubscription() {
 @Override protected void onUnsubscribe() {
 view.removeOnPageChangeListener(listener);
 }
 });
 
 // Emit initial value.
 subscriber.onNext(view.getCurrentItem());
 }
 }
  16. 16. ViewPagerPageSelectedOnSubscribe final class ViewPagerPageSelectedOnSubscribe implements Observable.OnSubscribe<Integer> {
 final ViewPager view;
 
 ViewPagerPageSelectedOnSubscribe(ViewPager view) {
 this.view = view;
 }
 
 @Override public void call(final Subscriber<? super Integer> subscriber) {
 checkUiThread();
 
 final ViewPager.OnPageChangeListener listener = new ViewPager.SimpleOnPageChangeListener() {
 @Override public void onPageSelected(int position) {
 if (!subscriber.isUnsubscribed()) {
 subscriber.onNext(position);
 }
 }
 };
 view.addOnPageChangeListener(listener);
 
 subscriber.add(new MainThreadSubscription() {
 @Override protected void onUnsubscribe() {
 view.removeOnPageChangeListener(listener);
 }
 });
 
 // Emit initial value.
 subscriber.onNext(view.getCurrentItem());
 }
 }
  17. 17. RxBinding’s Observable • Infinite streams • Emit initial value when subscribe
  18. 18. Basic usage in Kotlin viewPager.pageSelections()
 .subscribe {
 // call when page selected
 }
  19. 19. Use with RxLifecycle viewPager.pageSelections()
 .compose(bindToLifecycle<Int>())
 .subscribe {
 // call when page selected
 }
  20. 20. My usage of RxBiding-kotlin
  21. 21. Basic usage in Kotlin viewPager.pageSelections()
 .compose(bindToLifecycle<Int>())
 .subscribe {
 // call when page selected
 }
  22. 22. Auto loading viewPager.pageSelections()
 .filter { it > 0 && it >= adapter.count - 2 }
 .compose(bindToLifecycle<Int>())
 .subscribe {
 // call when page selected
 }
  23. 23. Auto loading class SkipUntilCompletedAction1<T>(val doSubscribe: (T, () -> Unit) -> Unit) : Action1<T> {
 private var isLoading = false
 override fun call(t: T) {
 if (isLoading) return
 isLoading = true
 doSubscribe(t) { isLoading = false }
 }
 }
 
 fun <T>Observable<T>.subscribeWhenCompleted(doSubscribe: (t: T, completed: () -> Unit) -> Unit): Subscription =
 subscribe(SkipUntilCompletedAction1(doSubscribe))
  24. 24. Auto loading class SkipUntilCompletedAction1<T>(val doSubscribe: (T, () -> Unit) -> Unit) : Action1<T> {
 private var isLoading = false
 override fun call(t: T) {
 if (isLoading) return
 isLoading = true
 doSubscribe(t) { isLoading = false }
 }
 }
 
 fun <T>Observable<T>.subscribeWhenCompleted(doSubscribe: (t: T, completed: () -> Unit) -> Unit): Subscription =
 subscribe(SkipUntilCompletedAction1(doSubscribe))
  25. 25. Auto loading viewPager.pageSelections()
 .filter { it > 0 && it >= adapter.count - 2 }
 .compose(bindToLifecycle<Int>())
 .subscribeWhenCompleted { list, completed ->
 getFromApi().subscribe({
 adapter.addAll(list)
 }, {
 // call when error. completed()
 }, {
 adapter.notifyDataSetChanged()
 completed()
 })
 }
  26. 26. RecyclerView’s paging class RecyclerViewPagingScrollOperator(val startPagingWith: Int = 0)
 : Observable.Operator<RecyclerViewPagingEvent, RecyclerViewScrollEvent> {
 
 override fun call(child: Subscriber<in RecyclerViewPagingEvent>): Subscriber<in RecyclerViewScrollEvent> {
 return object : Subscriber<RecyclerViewScrollEvent>(child) {
 
 override fun onCompleted() {
 child.onCompleted()
 }
 
 override fun onError(e: Throwable) {
 child.onError(e)
 }
 
 override fun onNext(scroll: RecyclerViewScrollEvent) {
 val recyclerView = scroll.view()
 val manager = recyclerView.layoutManager
 if (manager !is LinearLayoutManager) {
 child.onError(IllegalStateException("LayoutManager must be LinearLayoutManager."))
 return
 }
 val visibleCount = recyclerView.childCount
 val totalCount = manager.itemCount
 val firstVisibleCount = manager.findFirstVisibleItemPosition()
 if (visibleCount + firstVisibleCount < totalCount - startPagingWith) {
 request(1)
 return
 }
 child.onNext(RecyclerViewPagingEvent(recyclerView))
 }
 }
 }
 }
 fun Observable<RecyclerViewScrollEvent>.mapToPagingEvents(startPagingWith: Int = 0) =
 lift(RecyclerViewPagingScrollOperator(startPagingWith))
  27. 27. Auto loading with RecyclerView recyclerView.scrollEvents()
 .mapToPagingEvents(startPagingWith = 1)
 .subscribeWhenCompleted { it, completed ->
 getFromApi() .observeOn(AndroidSchedulers.mainThread())
 .compose(bindToLifecycle<Channel>())
 .subscribe({
 adapter.add(it)
 adapter.notifyDataSetChanged()
 }, {
 // call when error. completed()
 }, {
 completed()
 })
 }
  28. 28. Give ToggleButton behaviour class ToggleButtonOnSubscribe(val toggleButton: ToggleButton, val initial:Boolean)
 : Observable.OnSubscribe<Boolean> {
 
 private val subscriptions = CompositeSubscription()
 
 override fun call(subscriber: Subscriber<in Boolean>) {
 toggleButton.isChecked = initial
 subscriptions.add(toggleButton.checkedChanges()
 .skip(1) // Emit initial value in RxBinding, skip it.
 .subscribe { // do something in common doSomething() subscriber.onNext(it)
 })
 subscriber.add(subscriptions)
 }
 
 }
 
 fun ToggleButton.updatedEvents(initial: Boolean) =
 Observable.create(FollowToggleButtonOnSubscribe(this, initial))
  29. 29. Give ToggleButton behaviour toggleButton.updatedEvents(true)
 .compose(bindToLifecycle<Boolean>())
 .subscribe {
 
 }
  30. 30. Benefit of RxBinding-kotlin • Work well with Kotlin’s lambda. • Work well with Kotlin’s extensions.
  31. 31. Benefit of RxBinding-kotlin • Work well with Kotlin’s lambda. • Work well with Kotlin’s extensions. • Method chaining. • Grouping process for the views.
  32. 32. Thank you!

×