Git Product home page Git Product logo

rxandroideventssample's Introduction

RxAndroidEventsSample

BusEvents implementation base RxJava/RxAndroid/AndroidInject.

博客《基于RxJava、RxAndroid的EventBus实现》:
http://www.cnblogs.com/tiantianbyconan/p/4578699.html

Dependencies:

AndroidBucket:My base library
AndroidInject:My Inject library
RxJava
RxAndroid
retrolambda

How to use with Annotation(recommended)

It's realy simple to use with annotation

1. You must be config presents to support annotations(For example: BaseActivity which extends AIAppCompatActivity in the AndroidInject library)

private RxBusAnnotationManager rxBusAnnotationManager;
@Override
public void parserMethodAnnotations(Method method) throws Exception {
    if (method.isAnnotationPresent(Accept.class)) {
        if (null == rxBusAnnotationManager) {
            rxBusAnnotationManager = new RxBusAnnotationManager(this);
        }
        rxBusAnnotationManager.parserObservableEventAnnotations(method);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (null != rxBusAnnotationManager) {
        rxBusAnnotationManager.clear();
    }
}

2. Write a method in your present(eg. your activity), and use an @Accept annotation

@Accept
public void onPostAccept(Object tag, AddFeedsEvent event) {
    // todo: Accept event and process here (in main thread)
}

or

@Accept(
            acceptScheduler = AcceptScheduler.NEW_THREAD,
            value = {
                    @AcceptType(tag = ActionEvent.CLOSE, clazz = String.class),
                    @AcceptType(tag = ActionEvent.BACK, clazz = String.class),
                    @AcceptType(tag = ActionEvent.EDIT, clazz = String.class),
                    @AcceptType(tag = ActionEvent.REFRESH, clazz = String.class)
            }
    )
    public void onPostAccept(Object tag, Object actionEvent) {
        Logger.d(TAG, "[ActionEvent]onPostAccept action event name: " + actionEvent);
        // todo: Accept event and process here (in new thread)
    }

@Accept:

acceptScheduler: Perform this method on a specified scheduler;
value[]: AcceptType annotation array that specify the types this method can be accept. 

@AcceptType:

tag: The tag of the event.
clazz: The Class of the event.

3. Post a event any where:

AddFeedsEvent addFeedsEvent = new AddFeedsEvent();
// Construct a AddFeedsEvent Object...
RxBus.get().post(addFeedsEvent);

4. Specify an Executor or Handler(Optional)

public class MyApplication extends Application {
    private Executor acceptExecutor = Executors.newCachedThreadPool();
    private Handler handler = new Handler(Looper.getMainLooper());

    @Override
    public void onCreate() {
        super.onCreate();
        DefaultAcceptConfiguration.getInstance().registerAcceptConfiguration(new DefaultAcceptConfiguration.OnDefaultAcceptConfiguration() {
            @Override
            public Executor applyAcceptExecutor() {
                return acceptExecutor;
            }

            @Override
            public Handler applyAcceptHandler() {
                return handler;
            }
        });
    }
}




How to use without Annotation(Not recommended)

Register an Observer

Observable<String> addOb = RxBus.get()
                .register("addFeedTag", String.class);
                
addOb.observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    // todo: Accept event and process here
                });

Post a event any where

RxBus.get().post("addFeedTag", "hello RxBus!");

unregister the Observer

RxBus.get().unregister("addFeedTag", addOb);

License

Copyright 2015 Wang Jie

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.

rxandroideventssample's People

Contributors

wangjiegulu avatar

Watchers

James Cloos 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.