RxJava2 for andorid jar/library source parsing

Posted by qteks200 on Wed, 06 May 2020 00:37:28 +0200

Catalog: andorid jar/library source code parsing 

RxJava2:

Role:

Optimize logical processes by providing an architecture that provides a model for observers and subscribers.Suitable for complex and multi-data conversion and long processes.

Chestnuts:

Define three object classes

public class ResultInfo {
    public int code;
    public String msg;
    public String data;
}

public class UserInfo {
    public int status;
    public String name;
    public String head;
    public List<SkillInfo> skillInfoList;
}

public class SkillInfo {
    public String name;
    public int level;

    public SkillInfo(String name, int level){
        this.name = name;
        this.level = level;
    }
}

A piece of logic test code:

    private ResultInfo login_http(String name, String pwd){
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.code = 0;
        resultInfo.msg = "";

        Gson gson = new Gson();
        UserInfo userInfo = new UserInfo();
        userInfo.status = 1;
        userInfo.name = "";
        userInfo.head = "";
        userInfo.skillInfoList = new ArrayList<>();
        userInfo.skillInfoList.add(new SkillInfo("Music", 10));
        userInfo.skillInfoList.add(new SkillInfo("Fine Arts", 6));
        userInfo.skillInfoList.add(new SkillInfo("Sports", 9));
        resultInfo.data = gson.toJson(userInfo);

        return resultInfo;
    }

Version 1: (Define an observed object, and subscribers, set the threads they use, and finally associate them by calling subscribe.And execute)

Observable<ResultInfo> observable = Observable.create(new ObservableOnSubscribe<ResultInfo>() {
            @Override
            public void subscribe(ObservableEmitter<ResultInfo> e) throws Exception {
                e.onNext(login_http(name, pwd));
            }
        });

        Consumer<ResultInfo> consumer = new Consumer<ResultInfo>() {
            @Override
            public void accept(ResultInfo resultInfo) throws Exception {
                if(resultInfo.code == 0){
                    Gson gson = new Gson();
                    UserInfo userInfo = gson.fromJson(resultInfo.data, UserInfo.class);
                    if(userInfo.status == 0){
                        // register
                        Toast.makeText(getBaseContext(), "register", Toast.LENGTH_LONG).show();
                    } else if(userInfo.status == 1){
                        // Sign in
                        Toast.makeText(getBaseContext(), "Sign in", Toast.LENGTH_LONG).show();
                    }
                }
            }
        };

        // subscribeOn() Specifies the thread that sent the event, observeOn() Specifies the thread that receives the event.
        observable.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(consumer);

Version 2: (On the basis of version 1, by calling map processing on the observed data, the observed data only returns the results, while the subscriber also judges the results, and the type of data used here has changed).

Observable<Integer> observable = Observable.create(new ObservableOnSubscribe<ResultInfo>() {
            @Override
            public void subscribe(ObservableEmitter<ResultInfo> e) throws Exception {
                e.onNext(login_http(name, pwd));
            }
        }).map(new Function<ResultInfo, Integer>() {
            @Override
            public Integer apply(ResultInfo resultInfo) throws Exception {
                return resultInfo.code;
            }
        });

        Consumer<Integer> consumer = new Consumer<Integer>() {
            @Override
            public void accept(Integer code) throws Exception {
                if(code == 0){
                    Toast.makeText(getBaseContext(), "Success", Toast.LENGTH_LONG).show();
                } else{
                    Toast.makeText(getBaseContext(), "fail", Toast.LENGTH_LONG).show();
                }
            }
        };

        // subscribeOn() Specifies the thread that sent the event, observeOn() Specifies the thread that receives the event.
        observable.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(consumer);

Version 3: (Strengths of using RxJava, chain calls to complete logic)

Observable.create(new ObservableOnSubscribe<ResultInfo>() {
            @Override
            public void subscribe(ObservableEmitter<ResultInfo> e) throws Exception {
                e.onNext(login_http(name, pwd));
            }
        }).map(new Function<ResultInfo, Integer>() {
            @Override
            public Integer apply(ResultInfo resultInfo) throws Exception {
                return resultInfo.code;
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer code) throws Exception {
                        if(code == 0){
                            Toast.makeText(getBaseContext(), "Success", Toast.LENGTH_LONG).show();
                        } else{
                            Toast.makeText(getBaseContext(), "fail", Toast.LENGTH_LONG).show();
                        }
                    }
                });

Version 4: (flatMap is also introduced to process the collection data and return the collection, each data can respond to the subscriber's accept method, and a filter is introduced to filter the data, similar to the linq usage in C#.)

Observable.create(new ObservableOnSubscribe<ResultInfo>() {
            @Override
            public void subscribe(ObservableEmitter<ResultInfo> e) throws Exception {
                e.onNext(login_http(name, pwd));
            }
        }).map(new Function<ResultInfo, UserInfo>() {
            @Override
            public UserInfo apply(ResultInfo resultInfo) throws Exception {
                UserInfo userInfo = new Gson().fromJson(resultInfo.data, UserInfo.class);

                return userInfo;
            }
        }).flatMap(new Function<UserInfo, Observable<SkillInfo>>() {
            @Override
            public Observable<SkillInfo> apply(UserInfo userInfo) throws Exception {
                return Observable.fromArray(userInfo.skillInfoList.toArray(new SkillInfo[userInfo.skillInfoList.size()]));
            }
        }).filter(new Predicate<SkillInfo>() {
            @Override
            public boolean test(SkillInfo skillInfo) throws Exception {
                return skillInfo.level > 8;
            }
        })
        .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<SkillInfo>() {
                    @Override
                    public void accept(SkillInfo skillInfo) throws Exception {
                        Toast.makeText(getBaseContext(), skillInfo.name + " " + skillInfo.level, Toast.LENGTH_SHORT).show();
                    }
                });

        // Be similar to linq , take, skip deng

Version 5: Of course, the use of RxJava isn't just that. Take, take a few members, skip a few members.Wait, these usages are similar to linq in C#and are well understood in detail

//Code, omitted

Source interpretation:

//Source interpretation, followed by a supplement.

Source: https://github.com/ReactiveX/RxJava

Introduction:

implementation "io.reactivex.rxjava2:rxjava:2.0.7"
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"

Topics: Android github