V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
xlsepiphone
V2EX  ›  分享创造

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

  •  
  •   xlsepiphone · 2016-08-09 09:57:21 +08:00 · 2543 次点击
    这是一个创建于 3030 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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.
    
    1 条回复    2016-08-09 11:03:30 +08:00
    twocity
        1
    twocity  
       2016-08-09 11:03:30 +08:00
    赞!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   6080 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 02:43 · PVG 10:43 · LAX 18:43 · JFK 21:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.