简体   繁体   English

RxJava:如何将复杂的 if 语句转换为响应式流?

[英]RxJava: how to turn a complicated if statement into a reactive stream?

I am implementing an app introduction and waiver that should appear before the user can access the MainActivity of my Android app.我正在实施一个应用程序介绍和豁免,应该在用户可以访问我的 Android 应用程序的MainActivity之前出现。 If the user has not accepted the waiver or gone through the app introduction, then my IntroNavigator kicks them back to those activities.如果用户没有接受豁免或完成应用程序介绍,那么我的IntroNavigator他们踢回这些活动。

How can I rxify my redirectIfNecessary() method in a more functional manner, instead of the imperative approach I implemented below.如何以更实用的方式对我的redirectIfNecessary()方法进行 rxify,而不是我在下面实现的命令式方法。

IntroNavigatorImpl.java IntroNavigatorImpl.java

public class IntroNavigatorImpl implements IntroNavigator {
    WeakReference<Activity> activityWeakReference;
    CloudPrefsRepo cloudPrefsRepo;

    public IntroNavigatorImpl(Activity activity, CloudPrefsRepo cloudPrefsRepo) {
        this.activityWeakReference = new WeakReference<>(activity);
        this.cloudPrefsRepo = cloudPrefsRepo;
    }

    @Override
    public void redirectIfNecessary() {
        final boolean shouldShowAppIntro = cloudPrefsRepo.shouldShowAppIntro()
           .toObservable().toBlocking().first();
        final boolean shouldShowWaiver = cloudPrefsRepo.shouldShowWaiver()
           .toObservable().toBlocking().first();

        if (shouldShowAppIntro) {
            showAppIntro();
            finishActivity();
        } else if (shouldShowWaiver) {
            showWaiver();
            finishActivity();
        } else {
            //do nothing
        }
    }


    @Override
    public void showWaiver() {
        //launch waiver activity
    }

    @Override
    public void showAppIntro() {
        //launch app intro activity
    }

    public void finishActivity() {
        if (activityWeakReference.get() != null) {
            activityWeakReference.get().finish();
        }
    }
}

CloudPrefsRepo.java CloudPrefsRepo.java

public interface 
    /**
     * Whether to show the app intro.
     */
    Single<Boolean> shouldShowAppIntro();

    /**
     * Whether to show the waiver. If the user has already 
     * accepted this waiver, then it shouldn't be shown.
     */
    Single<Boolean> shouldShowWaiver();

Edit Based on comment:根据评论编辑:

It's not the prettiest (I'm leaning towards there not being a good reactive way to do this)这不是最漂亮的(我倾向于没有一个好的反应方式来做到这一点)

final Action0 showInfoAction = new Action0() {
        @Override
        public void call() {
            showAppIntro();
            finishActivity();
        }
    };


    final Action0 showWaiverAction = new Action0() {
        @Override
        public void call() {
            showWaiver();
            finishActivity();
        }
    };

    final Action0 blankAction = new Action0() {
        @Override
        public void call() {

        }
    };

    Observable.zip(shouldShowInfo, shouldShowWaiver, new Func2<Boolean, Boolean,  Action0>() {
        @Override
        public  Action0 call(Boolean shoudlShowInfo, Boolean shouldShowWaiver) {
            if (shoudlShowInfo) {
                return showInfoAction;
            } else if (shouldShowWaiver) {
                return showWaiverAction;
            } else {
                return blankAction;
            }
        }
    }).subscribe(new Action1<Action0>() {
        @Override
        public void call(Action0 action0) {
            action0.call();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM