执行某个操作时需要满足一些前提条件,而这些前提条件需要用户参与
才能满足。
比如进入个人资料界面
的前提是要先登录
,而登录这个操作需要用户的参与才能完成。
平常的做法可能是利用事件通知机制(EventBus)、BroadcastReceiver和startActivityForResult(),把登录成功
这个事件告知前面的代码可以跳到个人资料界面
了。
但如果前提条件有多个呢,是不是得发送多个事件呀,是不是得监听多个事件呀,是不是很麻烦呀(有没有被恶心过😆
)。
那有没有比较优雅的方式来完成呢,嗯嗯,编写这个小框架的目的就在此~
- 抽象出
目标行为
和前提条件行为
,从繁琐
到爽
就这么简单 前提条件
检测异步化,再也不用担心UI Thread
卡卡卡了- 多个
前提条件行为
串行执行
dependencies {
api 'com.tubb.delayactions:delayactions:0.0.6'
}
目标行为
被抽象为CoreAction接口,实现该接口就可以
class OrderDetailCoreAction implements CoreAction {
private Activity mActivity;
OrderDetailCoreAction(Activity activity) {
mActivity = activity;
}
@Override
public void execute() {
mActivity.startActivity(new Intent(mActivity, OrderDetailActivity.class));
}
}
前提条件行为
被抽象为PremiseAction接口,实现该接口就可以
public class LoginPremiseAction implements PremiseAction {
private Activity mContext;
public LoginPremiseAction(Activity context) {
mContext = context;
}
@Override
public boolean isPremiseCheckAsync() {
return true;
}
@NonNull
@Override
public Observable<Boolean> onFinish() {
return Observable.create(new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(ObservableEmitter<Boolean> e) throws Exception {
e.onNext(UserInfoCache.isLogin(mContext));
}
});
}
@Override
public void execute() {
mContext.startActivity(new Intent(mContext, LoginActivity.class));
}
}
PremiseAction接口一共定义了三个接口方法,PremiseAction.onFinish()
方法用来检测前提条件是否满足了,返回值为RxJava中的Observable<Boolean>
。
PremiseAction.isPremiseCheckAsync()
方法用来判定PremiseAction.onFinish()
方法的调用是否为异步。
PremiseAction.execute()
方法定义前提条件行为
具体的操作(需要用户参与),比如登录。
目标行为
的执行需要添加一些前提条件行为
,多个前提条件行为
将被以队列的方式串行执行
CoreAction coreAction = new OrderDetailCoreAction(this);
ActionUnit unit = DelayActions.instance().createActionUnit(coreAction)
.addPremiseAction(new LoginPremiseAction(this))
.addPremiseAction(new DiscountPremiseAction(this));
DelayActions.instance().post(unit);
当用户完成了前提条件行为
时,需要通知框架,框架会去重新检测是否所有的前提条件行为
都完成了,如果都完成,目标行为
将被执行
UserInfoCache.setLogin(this, true);
DelayActions.instance().notifyLoop(); // 通知框架重新检测
监听CoreAction和PremiseAction的状态
mListener = new ActionUnitListener(){
@Override
public void onStart() {
Log.d(TAG, "ActionUnit onStart");
}
@Override
public void onFinish() {
Log.d(TAG, "ActionUnit onFinish");
}
};
DelayActions.instance().registerActionUnitListener(OrderDetailCoreAction.class, mListener);
mListener = new PremiseActionListener() {
@Override
public void onFinish() {
finish();
}
};
DelayActions.instance().registerPremiseActionFinishedListener(LoginPremiseAction.class, mListener);
详细使用请参照Demo工程,强烈建议clone下来看一看!
Inspired by jinyb09017/delayActionDemo
Copyright 2017 TUBB
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.