Apollo-基于编译时注解处理的 RxBus 实现。

2016-08-09 09:57:21 +08:00
 xlsepiphone

Apollo

依赖于 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.2"
  compile "com.github.lsxiao.Apollo:apollo:0.1.2"
  compile 'io.reactivex:rxandroid:1.2.1'//实际操作时请使用最新的 rxandroid 版本,这仅仅是一个示例.
}

使用方法

在你自己的 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());
    }
}

//你可以在 BaseActivity 基类中绑定和解绑 Apollo

public abstract class BaseActivity extends AppCompatActivity {
    private SubscriptionBinder mBinder;

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

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

    protected abstract int getLayoutId();

    protected abstract void afterCreate(Bundle savedInstanceState);
}

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

public class MainActivity extends BaseActivity {
    public static final String EVENT_SHOW_USER = "event_show_user";
    public static final String EVENT_SHOW_BOOK = "event_show_book";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {

    }

    @Receive(tag = EVENT_SHOW_BOOK)
    public void receiveBook(Book book) {
        Log.d("apollo", "MainActivity receive book event" + book.toString());
    }

    //subscribeOn 和 observeOn 支持 main, io, new, computation, trampoline, immediate 这些调度器.
    //subscribeOn 的默认调度器是 io.
    //observeOn 的默认调度器是 main.
    @Receive(tag = EVENT_SHOW_USER,subscribeOn = Receive.Thread.IO, observeOn = Receive.Thread.MAIN)
    public void receiveUser(User user) {
        Log.d("apollo", "MainActivity receive user event" + user.toString());
    }

    //如果你想接收一个 sticky 事件
    //你可以让 type = Receive.Type.STICKY
    //type 的默认值是 Receive.Type.Normal.
    @Receive(tag = EVENT_SHOW_USER,type = Receive.Type.STICKY)
    public void receiveBookSticky(Book book) {
        Log.d("apollo", "MainActivity receive book event" + book.toString());
    }

    public static class User {
        String name;

        public User(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }

    public static class Book {
        String name;

        public Book(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Book{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
}

最后,调用 Apollo 来发送一个事件

 //a normal event
 Apollo.get().send(EVENT_SHOW_USER, new User("lsxiao"));

 //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.
2569 次点击
所在节点    分享创造
1 条回复
twocity
2016-08-09 11:03:30 +08:00
赞!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/298031

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX