Skip to content

Annotation processor #4

@MFlisar

Description

@MFlisar

Idea

  1. Define subcriptions via annotations:

     @RxBusSubscribe
     public void onEvent(Event event) {
         // handle bus event here
     }
    
  2. Annotation processor should generate functions in this class:

     public final void subscribeRxBusEvents() {
          // generated code
          // disposables must be managed by the class so those are bound to this class
     }
    
    public final void unsubscribeRxBusEvents() {
        RxDisposableManager.unsubscribe(this);
    }
    
  3. user calls subscribeRxBusEvents() wherever he wants to subscribe all annotated functions in this class to bus - manually

  4. user also calls unsubscribeRxBusEvents() wherever he wants to unsubscribe all annotated functions in this class from bus - manually

Settings

@RxBusSubscribe has following annotation fields:

  • queuing... defines the RxBusBuilders withQueuing value
  • mode... defines the RxBusBuilders withMode value
  • key/keys... defines the RxBusBuilders withKey value

RxBusBuilder.create(<CLASS>.class) is derived from the annotated functions parameter class. public void onEvent(Event event) will generate a RxBusBuilder.create(Event.class) code line

Examples

    @RxBusSubscribe(queuing = false, mode = RxBusMode.Main)
    public void onEvent(Event event) {
        // handle bus event here
    }        

    @RxBusSubscribe(queuing = false, mode = RxBusMode.Main, key = R.integer.key1)
    public void onEvent(Event event) {
        // handle bus event here
    }

    @RxBusSubscribe(queuing = true, mode = RxBusMode.Background, keys = {R.integer.key1, R.integer.key2})
    public void onEvent(Event event) {
        // handle bus event here
    }

    @RxBusSubscribe(queuing = true, mode = RxBusMode.Background, keys = {"key1", "key2"})
    public void onEvent(Event event) {
        // handle bus event here
    }

TODO

  • just a idea so far
  • rethink annotation names
  • maybe enable auto subscribtion/unsubscription for classes with lifecycle via an additional annotation for the class itself (for Activity/Fragment/...). This could generate code that subscribes to the bus in the constructor/onCreate/onResume and unsubscribes them in the appropriate counter function

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions