资讯专栏INFORMATION COLUMN

一个编译时注解的 RxBus 库 - Apollo

sugarmo / 2390人阅读

摘要:依赖于的编译时事件总线并且支持粘连事件以及多个调度器示例预览引入到项目中我们需要引入一个插件到我们的来开启注解处理功能注解处理工具增加插件到项目的配置文件中使用插件来开启注解处理功能实际操作时请使用最新的版本这仅仅是一个示例使用方法

依赖于RxJava的编译时Android事件总线,并且支持Sticky(粘连)事件,以及多个Rx调度器.

示例预览

引入Apollo到项目中

我们需要引入一个apt插件到我们的classpath来开启注解处理功能.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        //Android注解处理工具
        classpath "com.neenbedankt.gradle.plugins:android-apt:1.8"
    }
}

allProjects {
  repositories {
    maven { url "https://www.jitpack.io" }
  }
}

增加apt插件到项目的build.gradle配置文件中,使用apt插件来开启注解处理功能.

apply plugin: "com.neenbedankt.android-apt"

dependencies {
  apt "com.github.lsxiao.Apollo:processor:0.1.4-alpha.1"
  compile "com.github.lsxiao.Apollo:apollo:0.1.4-alpha.1"
  compile "io.reactivex:rxandroid:1.2.1"//实际操作时请使用最新的rxandroid版本,这仅仅是一个示例.
}
使用方法 Init

在Application中初始化Apollo.

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        //注意!SubscriberBinderImplement 是由Apollo在编译时生成的代码.
        //因为Apollo是java库,所以无法依赖于Android库(RxAndroid).
        //所以你必须提供一个AndroidSchedulers.mainThread()调度器来初始化Apollo.
        Apollo.get().init(SubscriberBinderImplement.instance(), AndroidSchedulers.mainThread());
    }
}
绑定/解绑

你可以在基类中来绑定和解绑Apollo.

public abstract class BaseActivity extends AppCompatActivity {
    private SubscriptionBinder mBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        mBinder = Apollo.get().bind(this);
        afterCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBinder.unbind();
    }

    protected abstract int getLayoutId();

    protected abstract void afterCreate(Bundle savedInstanceState);
}
接收事件

在你喜欢的地方来接收事件.

默认使用

    @Receive(tag = TAG)
    public void receiveEvent(Event event) {
       //do something.
    }

无参使用

    @Receive(tag = TAG)
    public void showDialog(){
        //show dialog.
    }

多个Tag

    @Receive(tag = {TAG1,TAG2})
    public void showDialog(){
        //show dialog.
    }

只接受一次普通事件

    //the event will be received only once.
    @Receive(tag = TAG,type = Receive.Type.NORMAL_ONCE)
    public void showDialog(){
        //show dialog.
    }

调度器

    //the subscribeOn and observeOn support  main, io, new, computation, trampoline, immediate schedulers.
    //subscribeOn default scheduler is io.
    //observeOn default scheduler is main.
    @Receive(tag = TAG,subscribeOn = Receive.Thread.IO, observeOn = Receive.Thread.MAIN)
    public void receiveUser() {
        //do something.
    }

接收sticky事件

    @Receive(tag = TAG,type = Receive.Type.STICKY)
    public void receiveEvent(Event event) {
        //do something.
    }

接收后清除该sticky事件

    @Receive(tag = TAG,type = Receive.Type.STICKY_REMOVE)
    public void receiveEvent(Event event) {
        //do something.
    }

接收后清除所有的sticky事件

    @Receive(tag = TAG,type = Receive.Type.STICKY_REMOVE_ALL)
    public void receiveEvent(Event event) {
        //do something.
    }
发送事件
 //a normal event
 Apollo.get().send(EVENT_SHOW_USER, new User("lsxiao"));

 //a non-parameter event
 Apollo.get().send(EVENT_SHOW_USER);

 //a sticky event
 Apollo.get().sendSticky(EVENT_SHOW_BOOK, new Book("A Song of Ice and Fire"));
Pull Requests(请求代码合并)

欢迎所有的 pull requests.

维护人

知乎 : @面条

Github : @lsxiao

开源许可
Copyright 2016 lsxiao, Inc.

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.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65056.html

相关文章

  • Apollo源码分析(二): Apollo代码层次

    摘要:不同与其它中间件框架,中有大量的业务代码,它向我们展示了大神是如何写业务代码的依赖的层次结构,如何进行基础包配置,以及工具类编写,可以称之为之最佳实践。代码参考视图解析器,这里的配置指的是不检查头,而且默认请求为格式。 不同与其它中间件框架,Apollo中有大量的业务代码,它向我们展示了大神是如何写业务代码的:maven依赖的层次结构,如何进行基础包配置,以及工具类编写,可以称之为sp...

    cyqian 评论0 收藏0
  • 阿里开源缓存框架JetCache

    摘要:是一个基于的缓存系统封装,提供统一的和注解来简化缓存的使用。提供了比更加强大的注解,可以原生的支持两级缓存分布式自动刷新,还提供了接口用于手工缓存操作。缓存失效时间缓存的类型,包括。 之前一直在用Spring Cache进行接口数据的缓存,主要是Spring Cache在对具体key缓存失效时间的设置不是很方法,还要自己去扩展,无意中发现了阿里的JetCache。大部分的需求都能满足,...

    MageekChiu 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<