一個編譯時註解的 RxBus 庫 - Apollo

依賴於RxJava的編譯時Android事件總線,而且支持Sticky(粘連)事件,以及多個Rx調度器.java

示例預覽

引入Apollo到項目中

咱們須要引入一個apt插件到咱們的classpath來開啓註解處理功能.react

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插件來開啓註解處理功能.android

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.git

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.github

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);
}

接收事件

在你喜歡的地方來接收事件.express

  • 默認使用apache

@Receive(tag = TAG)
    public void receiveEvent(Event event) {
       //do something.
    }
  • 無參使用app

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

@Receive(tag = {TAG1,TAG2})
    public void showDialog(){
        //show dialog.
    }
  • 只接受一次普通事件maven

//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.
相關文章
相關標籤/搜索