RxLifecycleUtils.kt 3.69 KB
Newer Older
1
package com.ydl.ydlcommon.utils
konghaorui committed
2 3 4 5 6 7

import com.trello.rxlifecycle2.LifecycleTransformer
import com.trello.rxlifecycle2.RxLifecycle
import com.trello.rxlifecycle2.android.ActivityEvent
import com.trello.rxlifecycle2.android.FragmentEvent
import com.trello.rxlifecycle2.android.RxLifecycleAndroid
8 9 10 11
import com.ydl.ydlcommon.base.lifecycle.IActivityLifecycleable
import com.ydl.ydlcommon.base.lifecycle.IFragmentLifecycleable
import com.ydl.ydlcommon.base.lifecycle.ILifecycleable
import com.ydl.ydlcommon.mvp.base.IView
konghaorui committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import io.reactivex.annotations.NonNull

/**
 * Created by haorui on 2019-08-22 .
 * Des: Activity/Fragment RxLifecycle 特性支持类
 */
class RxLifecycleUtils private constructor() {

    init {
        throw IllegalStateException("you can't instantiate me!")
    }

    companion object {

        /**
         * 绑定 Activity 的指定生命周期
         *
         * @param view
         * @param event
         * @param <T>
         * @return
        </T> */
        fun <T> bindUntilEvent(
            @NonNull view: IView,
            event: ActivityEvent
        ): LifecycleTransformer<T> {
            YDLPreconditions.checkNotNull(view, "view == null")
            return if (view is IActivityLifecycleable) {
40 41 42 43
                bindUntilEvent(
                    view as IActivityLifecycleable,
                    event
                )
konghaorui committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
            } else {
                throw IllegalArgumentException("view isn't IActivityLifecycleable")
            }
        }

        /**
         * 绑定 Fragment 的指定生命周期
         *
         * @param view
         * @param event
         * @param <T>
         * @return
        </T> */
        fun <T> bindUntilEvent(
            @NonNull view: IView,
            event: FragmentEvent
        ): LifecycleTransformer<T> {
            YDLPreconditions.checkNotNull(view, "view == null")
            return if (view is IFragmentLifecycleable) {
63 64 65 66
                bindUntilEvent(
                    view as IFragmentLifecycleable,
                    event
                )
konghaorui committed
67 68 69 70 71 72 73 74 75
            } else {
                throw IllegalArgumentException("view isn't IFragmentLifecycleable")
            }
        }

        fun <T, R> bindUntilEvent(
            @NonNull lifecycleable: ILifecycleable<R>,
            event: R
        ): LifecycleTransformer<T> {
76 77 78 79
            YDLPreconditions.checkNotNull(
                lifecycleable,
                "lifecycleable == null"
            )
konghaorui committed
80 81 82 83 84 85
            return RxLifecycle.bindUntilEvent(lifecycleable.provideLifecycleSubject(), event)
        }

        /**
         * 绑定 Activity/Fragment 的生命周期
         *
86
         * @param activity
konghaorui committed
87 88 89 90 91 92 93 94 95 96 97 98 99
         * @param <T>
         * @return
        </T> */
        fun <T> bindToLifecycle(@NonNull view: IView): LifecycleTransformer<T> {
            YDLPreconditions.checkNotNull(view, "view == null")
            return if (view is ILifecycleable<*>) {
                bindToLifecycle(view as ILifecycleable<*>)
            } else {
                throw IllegalArgumentException("view isn't ILifecycleable")
            }
        }

        fun <T> bindToLifecycle(@NonNull lifecycleable: ILifecycleable<*>): LifecycleTransformer<T> {
100 101 102 103
            YDLPreconditions.checkNotNull(
                lifecycleable,
                "lifecycleable == null"
            )
konghaorui committed
104 105 106 107 108 109 110 111 112 113
            return if (lifecycleable is IActivityLifecycleable) {
                RxLifecycleAndroid.bindActivity(lifecycleable.provideLifecycleSubject())
            } else if (lifecycleable is IFragmentLifecycleable) {
                RxLifecycleAndroid.bindFragment(lifecycleable.provideLifecycleSubject())
            } else {
                throw IllegalArgumentException("ILifecycleable not match")
            }
        }
    }
}