eventex

Project Url: uchitel/eventex
Introduction: Android Express Events Library
More: Author   ReportBugs   
Tags:
Android library to exchange messages between Fragments, ViewGroups, Activity. No need to create interfaces and pass listeners to multiple classes. There is also no need to subscribe/unsubscribe for events!

Simple

To post message

new UIEvent("button.ok.click").post(view); // yes, this is it

To receive message. In any class that extends Fragment, ViewGroup, or Activity

public class CustomFragment extends Fragment implements UIEventListener {
//  .....
    @Override
    public boolean onMessage(@NonNull UIEvent uiEvent) {
        switch (uiEvent.what) {
            case "button.ok.click":
                Log.d(uiEvent.toString());
                return true; // to stop message propagation
        }
        return false;   // to let other objects to process message
    }
}

No need to setOnItemClickListener in the RecyclerView.Adapter! Much less boilerplate code compare to classic solution Communicate with other fragments! Class CustomFragment extends Android class Fragment. It will also work well if the class extends Activity, ViewGroup, or any layout derived from ViewGroup (LinearLayout, FrameLayout, etc..)

Features

  • Delivers messages between UI components of an Activity.
  • Supports synchronous and asynchronous communication.
  • No need to subscribe/unsubscribe to receive messages.
  • Completely decouples components.
  • No reflection and no ProGuard rules.
  • Tiny code size.

Requirements

  • Android 4.1.0(API 16) or above.
  • Java 8

Prerequisites

Make sure Java 8 (1.8) support is enabled in the gradle file

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

Add EventEx to the project gradle file (Androidx based projects)

implementation 'dev.uchitel:eventex:1.0.0'

Or for Android Support Library projects

implementation 'dev.uchitel:eventex-support:1.0.0'

More Details

Message can be sent synchronously

new UIEvent(12345).send(viewGroup);

Message can carry additional integer and string value:

new UIEvent(12345)
    .setText("some text to pass with message")
    .setNumber(9876) // some integer to pass with message
    .post(viewGroup);

Next code will properly receive this message:

public class FragmentReceiver extends Fragment implements UIEventListener {
//  .....
    @Override
    public boolean onMessage(@NonNull UIEvent uiEvent) {
        switch (uiEvent.code) {
            case 12345:
                Log.d("FragmentReceiver", "text="+uiEvent.getText());
                Log.d("FragmentReceiver", "number="+uiEvent.getNumber());
                return true; // to stop message propagation
        }
        return false;   // to let other components to process the message
    }
}

Message can use integer ID, string ID, or both for more complex control scenarios:

new UIEvent(12345, "button.ok.click"))
    .post(view);

The 'onMessage' for the above code:

public class FragmentReceiver extends Fragment implements UIEventListener {
//  .....
    @Override
    public boolean onMessage(@NonNull UIEvent uiEvent) {
        switch (uiEvent.code) {
            case 12345:
                if(uiEvent.what.equals("button.ok.click")){
                    // ...
                    return true; // to stop message propagation
                }
        }
        return false;   // to let other components to process the message
    }
}

When writing android library make sure to use 'namespace' to prevent collisions. Sending message inside library can look like:

new UIEvent("button.ok.click")
    .setNamespace("lib_name.company_name.com")
    .post(view);

Namespace "lib_name.company_name.com" is going to prevent ID collisions when the library is distributed to third party developers.

And to receive this message inside library module

public class FragmentReceiver extends Fragment implements UIEventListener {
//  .....
    @Override
    public boolean onMessage(@NonNull UIEvent uiEvent) {
        // return false if this is not library message
        if (!uiEvent.getNamespace().equals("libname.company.com")) return false;

        switch (uiEvent.what){
            case "button.ok.click":
                Log.d(uiEvent.getText());
                return true; // to stop message propagation
        }
        return false;   // to let other objects to process message
    }
}

R8 / ProGuard

No special requirements for R8 or ProGuard

Do you think it might be useful? Help devs to find it.

Alternative libraries

License

Copyright 2019 Alexander Uchitel

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.
Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea