小试牛刀RxJava2之首页检查
2017-01-14 19:12
260 查看
前言
当我第一次听说RxJava是在2015年的11月,掐指一算距今过去了1年多了,现在RxJava2都发布了。现在公司项目的的业务需要,有一块逻辑涉及到子线程和主线程的来回切换。如果使用传统的Thread+Handler的写法,写出来的代码就跳来跳去,还有可能出现多次嵌套。很利于阅读理解里面的逻辑和后期的维护开发。早就听说RxJava的大名,通过响应式编程很好的处理异步问题。这次正好有这个机会,自己就动手实践一下。![](http://upload-images.jianshu.io/upload_images/631252-a44e183d67c213fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
产品流程
只有前一个执行完成(不管是否成功),才能执行后面一个。1. 新手引导
2. 检查升级
3. 弹窗视图
一张图你就懂了
![](http://upload-images.jianshu.io/upload_images/631252-0471f6b30e8ad17d.gif?imageMogr2/auto-orient/strip)
开发流程
检查sp里的isFirst-子线程
显示带动画的引导界面-
主线程
监听动画结束-
主线程
监听引导界面的点击事件-
主线程
隐藏带动画的引导界-
主线程
监听动画结束-
主线程
将sp里的isFirst置为false-
子线程
发起检查版本请求-
子线程
显示版本更新的dialog-
主线程
监听对话框消失-
主线程
发起获取弹窗数据的请求-
子线程
显示弹窗视图-
主线程
Disposable disposable = Observable .create(new ObservableOnSubscribe<Boolean>() { @Override public void subscribe(ObservableEmitter<Boolean> e) throws Exception { e.onNext(PreferenceManager .getDefaultSharedPreferences(MainActivity.this) .getBoolean("isFirst", true)); } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .flatMap(new Function<Boolean, ObservableSource<Boolean>>() { @Override public ObservableSource<Boolean> apply(Boolean aBoolean) throws Exception { if (aBoolean) { return Observable.create(new ObservableOnSubscribe<Boolean>() { @Override public void subscribe(final ObservableEmitter<Boolean> e) throws Exception { AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f); alphaAnimation.setDuration(2000); alphaAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { if (!e.isDisposed()) { e.onNext(true); } } @Override public void onAnimationRepeat(Animation animation) { } }); fragment_home_guide.setVisibility(View.VISIBLE); fragment_home_guide.startAnimation(alphaAnimation); } }); } else { return Observable.create(new ObservableOnSubscribe<Boolean>() { @Override public void subscribe(ObservableEmitter<Boolean> e) throws Exception { e.onNext(false); } }); } } }) .flatMap(new Function<Boolean, ObservableSource<Boolean>>() { @Override public ObservableSource<Boolean> apply(Boolean aBoolean) throws Exception { if (aBoolean) { return Observable.create(new ObservableOnSubscribe<Boolean>() { @Override public void subscribe(final ObservableEmitter<Boolean> e) throws Exception { fragment_home_guide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (!e.isDisposed()) { e.onNext(true); } } }); e.setDisposable(new Disposable() { @Override public void dispose() { fragment_home_guide.setOnClickListener(null); } @Override public boolean isDisposed() { return true; } }); // e.add(new MainThreadSubscription() { // @Override // protected void onUnsubscribe() { // fragment_home_guide.setOnClickListener(null); // } // }); } }); } else { return Observable.create(new ObservableOnSubscribe<Boolean>() { @Override public void subscribe(ObservableEmitter<Boolean> e) throws Exception { e.onNext(false); } }); } } }) .flatMap(new Function<Boolean, ObservableSource<Boolean>>() { @Override public ObservableSource<Boolean> apply(Boolean aBoolean) throws Exception { if (aBoolean) { return Observable.create(new ObservableOnSubscribe<Boolean>() { @Override public void subscribe(final ObservableEmitter<Boolean> e) throws Exception { AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.1f); alphaAnimation.setDuration(2000); alphaAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { if (!e.isDisposed()) { e.onNext(true); } } @Override public void onAnimationRepeat(Animation animation) { } }); fragment_home_guide.setVisibility(View.GONE); fragment_home_guide.startAnimation(alphaAnimation); } }); } else { return Observable.create(new ObservableOnSubscribe<Boolean>() { @Override public void subscribe(ObservableEmitter<Boolean> e) throws Exception { e.onNext(false); } }); } } }) .observeOn(Schedulers.io()) .map(new Function<Boolean, Boolean>() { @Override public Boolean apply(Boolean aBoolean) throws Exception { if (aBoolean) { PreferenceManager .getDefaultSharedPreferences(MainActivity.this) .edit() .putBoolean("isFirst", false) .apply(); } return aBoolean; } }) .flatMap(new Function<Boolean, ObservableSource<Version>>() { @Override public ObservableSource<Version> apply(Boolean aBoolean) throws Exception { Thread.sleep(300); return new Observable<Version>() { @Override protected void subscribeActual(Observer<? super Version> observer) { observer.onNext(new Version()); } }; } }) .observeOn(AndroidSchedulers.mainThread()) .flatMap(new Function<Version, ObservableSource<Boolean>>() { @Override public ObservableSource<Boolean> apply(Version version) throws Exception { if (version == null) { return Observable.create(new ObservableOnSubscribe<Boolean>() { @Override public void subscribe(ObservableEmitter<Boolean> e) throws Exception { e.onNext(false); } }); } else { return Observable.create(new ObservableOnSubscribe<Boolean>() { @Override public void subscribe(final ObservableEmitter<Boolean> e) throws Exception { new AlertDialog.Builder(MainActivity.this) .setTitle("发现新版本") .setMessage("更新内容") .setPositiveButton("升级", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialogInterface) { e.onNext(true); } }) .create().show(); } }); } } }) .observeOn(Schedulers.io()) .flatMap(new Function<Boolean, ObservableSource<String>>() { @Override public ObservableSource<String> apply(Boolean aBoolean) throws Exception { Thread.sleep(300); return Observable.create(new ObservableOnSubscribe<String>() { @Override public void subscribe(ObservableEmitter<String> e) throws Exception { e.onNext("ok"); } }); } }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<String>() { @Override public void accept(String s) throws Exception { Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); } }); disposables.add(disposable);
小结
在熟悉了RxJava的基本概念之后,写Rx代码确实比较顺手,有一种行云流水的感觉。值得注意的是,使用RxJava进行开发并不会减少原有代码量,反而有一定的增加。遇到的主要问题就是,要理解并区分subscribeOn和observeOn的线程控制,以及map和flatMap的变换。我的粗浅理解是:subscribeOn控制的是最前面的create和最后的subscribe,而observeOn控制的是后面的变化操作。map是Observable对象没有改变,执行形式变了,比如有File变成Bitmap。而flatMap是Observable发生了改变,比如以前被观察的对象是sp里的isFirst,现在变成了引导界面的动画结束。
最后附上demo地址:https://github.com/ihrthk/RxHomeCheck
相关文章推荐
- C#检查指定对象是否存在于ArrayList集合中的方法
- Android检查手机有没有安装某应用的方法
- Bootstrap模仿起筷首页效果
- Flow之一个新的Javascript静态类型检查器
- 检查并修复mysql数据库表的具体方法
- 也谈截取首页新闻 - 范例
- mysql 性能的检查和优化方法
- JavaScript对象属性检查、增加、删除、访问操作实例
- js检查是否关闭浏览器的方法
- python检查字符串是否是正确ISBN的方法
- php检查字符串中是否包含7位GSM字符的方法
- Bootstrap项目实战之首页内容介绍(全)
- Java中静态类型检查是如何进行的实例思路详解
- JavaWeb开发之模仿知乎首页完整代码
- python检查指定文件是否存在的方法
- python友情链接检查方法
- 检查表单元素的值是否为空的实例代码
- php检查日期函数checkdate用法实例
- 聊一聊百度移动端首页前端速度那些事儿
- 淘宝13年前的首页你见过吗?(据说只有0.2%的人见过)