Giter Site home page Giter Site logo

apollo's Introduction

Apollo

Compile-time android event bus depended on RxJava ,which support sticky event and multiple schedulers.

中文文档

Demo Preview

TODO

  • life circle bind by annotation(@Receive(tag="some event",bindUntil=ActivityEvent.DESTROY)).
  • unit test.

Including in your project

We need to include the apt plugin in our classpath to enable Annotation Processing:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        //android annotation process tool
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

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

Add the library to the project-level build.gradle, using the apt plugin to enable Annotation Processing:

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

dependencies {
  apt "com.github.lsxiao.Apollo:processor:0.1.4-alpha.2"
  compile "com.github.lsxiao.Apollo:apollo:0.1.4-alpha.2"
  compile 'io.reactivex:rxandroid:1.2.1'//use the latest version,this just a simple.
}

Usage

Init

init the Apollo in your custom application.

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

        //note!the SubscriberBinderImplement is generated code.
        //because Apollo is a java library and it can't depend on a android library(RxAndroid),
        //so you must provide a AndroidSchedulers.mainThread() to init.
        Apollo.get().init(SubscriberBinderImplement.instance(), AndroidSchedulers.mainThread());
    }
}

Bind/Unbind

you can bind and unbind Apollo in BaseActivity.

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 Event

write a method where you want to receive events

  • default
    @Receive(tag = TAG)
    public void receiveEvent(Event event) {
       //do something.
    }
  • non-parameter
    @Receive(tag = TAG)
    public void showDialog(){
        //show dialog.
    }
  • multiple tag
    @Receive(tag = {TAG1,TAG2})
    public void showDialog(){
        //show dialog.
    }
  • receive normal event only once.
    //the event will be received only once.
    @Receive(tag = TAG,type = Receive.Type.NORMAL_ONCE)
    public void showDialog(){
        //show dialog.
    }
  • schedulers
    //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.
    }
  • receive sticky event
    @Receive(tag = TAG,type = Receive.Type.STICKY)
    public void receiveEvent(Event event) {
        //do something.
    }
  • receive sticky event and remove that sticky event.
    @Receive(tag = TAG,type = Receive.Type.STICKY_REMOVE)
    public void receiveEvent(Event event) {
        //do something.
    }
  • receive sticky event and remove all sticky events.
    @Receive(tag = TAG,type = Receive.Type.STICKY_REMOVE_ALL)
    public void receiveEvent(Event event) {
        //do something.
    }

Send Event

finally send a event where your want.

 //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"));

Release Note

  • 0.1.4-alpha.2 (2016-8-23)

    • update demo.
    • support send and receive primitive type event.(int,boolean,float,etc...)
  • 0.1.4-alpha.1 (2016-8-12)

    • support receive a normal event only once.(NORAML_ONCE)
    • support receive sticky event and remove that sticky event.(STICKY_REMOVE)
    • support receive sticky event and remove all sticky events.(STICKY_REMOVE_ALL)
  • 0.1.4-alpha (2016-8-11)

    • support multiple tags.
    • support non-parameter method.
    • fixed a bug in processor which may causing compile fail.
  • 0.1.3 (2016-8-10)

    • avoid multiple bind the same object.
    • fixed a bug may cause unsubscribe.
  • 0.1.2 (2016-8-8)

    • compile-time RxBus
    • support sticky event
    • support multiple scheduler.
    • support annotation.

Maintained By

知乎 : @面条

Github : @lsxiao

License

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.

apollo's People

Contributors

sloaix avatar

Watchers

 avatar  avatar

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.