package com.joker.fourfun.utils;

import org.reactivestreams.Publisher;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by joker on 2016/11/28.
 */

public class RxUtil {
    /**
     * rxjava 线程封装
     *
     * @param <T>
     * @return
     */
    public static <T> FlowableTransformer<T, T> rxSchedulerTransformer() {
        return new FlowableTransformer<T, T>() {
            @Override
            public Publisher<T> apply(Flowable<T> upstream) {
                return upstream
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    /**
     * rxjava 超时抛出异常
     * @param <T>
     * @return
     */
    public static <T> FlowableTransformer<T, T> rxTimeoutTransformer() {
        return new FlowableTransformer<T, T>() {
            @Override
            public Publisher<T> apply(Flowable<T> upstream) {
                return upstream.timeout(3, TimeUnit.SECONDS);
            }
        };
    }
}

@@ -27,16 +27,140 @@
 utils;%0A%0A
+import com.joker.fourfun.model.LoginInfo;%0Aimport com.joker.fourfun.net.LoginException;%0Aimport com.orhanobut.logger.Logger;%0A%0A
 import o
@@ -186,24 +186,47 @@
 Publisher;%0A%0A
+import java.util.List;%0A
 import java.
@@ -381,16 +381,56 @@
 dulers;%0A
+import io.reactivex.functions.Function;%0A
 import i
@@ -1029,16 +1029,23 @@
  %E8%B6%85%E6%97%B6%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8%0A
+     *%0A
      * @
@@ -1375,11 +1375,993 @@
 %7D;%0A    %7D
+%0A%0A    /**%0A     * %E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E7%8A%B6%E6%80%81%E6%A0%A1%E9%AA%8C%0A     *%0A     * @param code%0A     * @return%0A     */%0A    public static FlowableTransformer%3CList%3CLoginInfo%3E, LoginInfo%3E rxStateCheck(final int code) %7B%0A        return new FlowableTransformer%3CList%3CLoginInfo%3E, LoginInfo%3E() %7B%0A            @Override%0A            public Publisher%3CLoginInfo%3E apply(Flowable%3CList%3CLoginInfo%3E%3E upstream) %7B%0A                return upstream%0A                        .map(new Function%3CList%3CLoginInfo%3E, LoginInfo%3E() %7B%0A                            @Override%0A                            public LoginInfo apply(List%3CLoginInfo%3E loginInfos) throws Exception %7B%0A                                int serverCode = loginInfos.get(0).getCode();%0A                                if (serverCode != code) %7B%0A                                    throw new LoginException(serverCode);%0A                                %7D%0A%0A                                return loginInfos.get(0);%0A                            %7D%0A                        %7D);%0A            %7D%0A        %7D;%0A    %7D
 %0A%7D%0A

add login and register check