Giter Club home page Giter Club logo

delayactions's Introduction

DelayActions

执行某个操作时需要满足一些前提条件,而这些前提条件需要用户参与才能满足。 比如进入个人资料界面的前提是要先登录,而登录这个操作需要用户的参与才能完成。 平常的做法可能是利用事件通知机制(EventBus)、BroadcastReceiverstartActivityForResult(),把登录成功这个事件告知前面的代码可以跳到个人资料界面了。 但如果前提条件有多个呢,是不是得发送多个事件呀,是不是得监听多个事件呀,是不是很麻烦呀(有没有被恶心过😆)。 那有没有比较优雅的方式来完成呢,嗯嗯,编写这个小框架的目的就在此~

特性

  • 抽象出目标行为前提条件行为,从繁琐就这么简单
  • 前提条件检测异步化,再也不用担心UI Thread卡卡卡了
  • 多个前提条件行为串行执行

预览

DEMO

使用

添加依赖

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(); // 通知框架重新检测

监听CoreActionPremiseAction的状态

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

License

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.

delayactions's People

Stargazers

 avatar TUBB avatar

Watchers

James Cloos avatar TUBB avatar

Forkers

waifeng

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.