package cn.dankal.basiclib.base.lifecycle;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleRegistry;

import cn.dankal.basiclib.util.Logger;
import io.reactivex.Observable;
import io.reactivex.functions.Predicate;

public class LifeCycleUtil<T> {


    public static <T, R> LifecycleTransformer<T> bindLifeUntilEvent(MyLifeCycleObserver lifecycleRegistry, Lifecycle.Event event) {
        return bindLifeCycle(takeUntilEvent(lifecycleRegistry, event));
    }

    public static <R> Observable takeUntilEvent(MyLifeCycleObserver lifecycleRegistry, Lifecycle.Event event) {

        return lifecycleRegistry.getObservable().filter(new Predicate<Lifecycle.Event>() {
            @Override
            public boolean test(Lifecycle.Event lifeEvent) throws Exception {
                Logger.e("test\t" + lifeEvent.name());
                return lifeEvent == event;
            }
        });
    }


    public static <T, R> LifecycleTransformer<T> bindLifeCycle(Observable lifecycleRegistry) {
        return new LifecycleTransformer(lifecycleRegistry);
    }

}

