diff --git a/.gitignore b/.gitignore index f6b286c..81d4b50 100644 --- a/.gitignore +++ b/.gitignore @@ -34,7 +34,7 @@ captures/ # Intellij *.iml -.idea/workspace.xml +.idea/ # Keystore files *.jks diff --git a/README.md b/README.md index 74fa673..5ebef2c 100644 --- a/README.md +++ b/README.md @@ -1 +1,220 @@ -# EventDispatcher \ No newline at end of file +# Universal Event Bus +============================= + +The Universal Event Bus is an event dispatcher architecture which help you to use most common event bus implementation as Otto in a structured mode. + +An events is a bus designed to separate different parts of the application, while still allowing them to communicate efficiently. +The operation of the EventDispatcher is based on the publish-subscribe pattern: the bus asked a series of events that will be collected by those who joined them. + +The publisher is, in this case, called Bus or RxBus and deals with post events using the Observable of RxJava. The event dispatcher contains two RxBuses: one dedicated to the UI thread, and the other for all the other events that have nothing to do with the UI (network calls, CRUD operations with the database etc.). + +The events that are posted by the Event dispatcher are heard by all those who sign up. To register, you must write down the method that the signing will take as an argument the type of object that the public EventDispatcher and will be annotated with the notation @Subscribe or @RxSubscribe + +![alt tag](http://sharing.sysdata.it/fvgmock/ues-diagram01.png) + +Usage +-------- + +1) You've got to initialize the EventDispatcher with your favorite process. +We suggest to do it in the MainApplication's onCreate() and use RxEventProcessor which use RxJava. + +```java + EventDispatcher.useEventProcessor(RxEventProcessor.newInstance()); +``` +We also implemented an EventProcessor which use Otto as Event Bus. + +```java + EventDispatcher.useEventProcessor(EventProcessor.newInstance()); +``` +2) Register the EventDispatcher when the Activity/Fragment/Service is created and unregister it when it is destroyed. + +```java + + @Override + public void onCreate() { + super.onCreate(); + // register the event dispatcher in order to receive + // events that are posted on the Bus. + EventDispatcher.register(this); + } + + @Override + public void onDestroy() { + super.onDestroy(); + // unregister the event dispatcher + EventDispatcher.unregister(this); + } +``` +3) Create events, post and receive them: Once the EventDispatcher is initialized and register in your Android component, you can post and receive events easily. Use `EventDispatcher.post()` to post the event you want to stream in the bus. Create a public method that has the same event type in its signature and annotate it with the `@RxSubscribe` signature in order to receive the Object that has been posted. + +```java + @Override + public void onCreate() { + super.onCreate(); + // post an example event + EventDispatcher.post(new ExampleEvent()); + } + + @RxSubscribe + public void onConsumeExampleEvent(ExampleEvent event) { + // do what you want with the incoming event + } + + /** + * This is an example empty event: remember to add the "Event" annotation!! + */ + @Event(type = Event.Type.UI) + public class ExampleEvent { + + public ExampleEvent() { + // empty constructor + } + + } +``` + +The posting class can be different from the receiving one: both must be registered to the EventDispatcher, though! Remember that each Class that you want to use as an event MUST have the `@Event` annotation. You can choose between 5 type of events based on which is the use of the designed event: GENERIC, DATA, NETWORK, CONTEXT and UI. The difference is that UI events will be posted on the UI thread, meanwhile the others will be posted in a separated Thread. + +Handle configuration changes +-------- + +EventDispatcher allows you to handle configuration changes easily. By using `EventDispatcher.loadPoint()` and `EventDispatcher.savePoint()` you will be able to receive posted events even after a configuration change (i.e. rotation or other lifecycle events). + +```java + + private String mEventDispatcherTag; + + @Override + public void onCreate() { + super.onCreate(); + // retrieve the saved state, if present + if (savedInstanceState != null && savedInstanceState.containsKey("ett")) { + this.mEventDispatcherTag = savedInstanceState.getString("ett"); + } + // loadPoint() is used to handle events' saved state between configuration changes + EventDispatcher.loadPoint(this, this.mEventDispatcherTag); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + // save the EventDispatcher's tag + outState.putString("ett", mEventDispatcherTag); + } + + @Override + public void onDestroy() { + super.onDestroy(); + // save point is used to save the state in order to restore it later, after the configuration change. + this.mEventDispatcherTag = EventDispatcher.savePoint(this); + } +``` + +Create custom EventProcessor +-------- + +You can implement your own EventProcessor if you want, you need to create a class which implements the interface EventProcessor + +```java + public interface EventProcessor { + + /** + * Registers a given Object on both Buses + * + * @param o Object to register on the Buses + */ + void onRegister(Object o); + + /** + * Unregisters a given Object from both Buses + * + * @param o Object to unregister from the Buses + */ + void onUnregister(Object o); + + /** + * After checking whether the object being posted is annotated with the {@link Event} Annotation, + * it will be placed in the corresponding queue and such queue will be then sorted by {@link Event.Priority}. + *

+ * NOTE: if an object being posted has not been annotated with the {@link Event} Annotation it will be disregarded!!! + *

+ * + * @param o the Object we want to post as an event + */ + void onPost(Object o); + + /** + * This method return a string used by {@link EventProcessor} to save the state of the object in configuration changes.
+ * You should use the string returned by this method with {@code EventDispatcher.loadPoint()}
+ * This method must be called before {@code EventDispatcher.unregister(...)} (in {@code OnPause(...)} method for example) + *

+ * NOTE: this string should be saved on instance state in Activity and retrieved when it restart on OnCreate(...) method + *

+ * + * @param object + * @return + */ + String onSavePoint(Object object); + + /** + * This method load the configuration variables used by {@link EventDispatcher} to handle the configuration changes.
+ * You should use this method in pair with {@code EventDispatcher.savePoint(...)}.
+ * This method must be called before {@code EventDispatcher.register(...)} (in {@code OnResume(...)} method for example)
+ * + * @param object + * + */ + void onLoadPoint(Object object, String key); + } +``` + + +Download +-------- + +Downloadable .jars can be found on the [Bintray download page][2]. + +You can also depend on the .jar through Maven: +```xml + + com.baseandroid + baseandroid-rxeventdispatcher + 0.0.14 + +``` +or Gradle: +```groovy +compile "com.baseandroid:baseandroid-rxeventdispatcher:0.0.14" +// rxJava +compile 'io.reactivex:rxandroid:1.2.1' +// Because RxAndroid releases are few and far between, it is recommended you also +// explicitly depend on RxJava's latest version for bug fixes and new features. +compile 'io.reactivex:rxjava:1.2.6' +``` + +Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap]. + + + +License +------- + + Copyright (C) 2020 Sysdata, S.p.a. + + 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. + + + + [1]: http://square.github.com/otto/ + [2]: https://dl.bintray.com/sysdata/maven/com/baseandroid/baseandroid-eventdispatcher/ + [snap]: https://oss.sonatype.org/content/repositories/snapshots/ diff --git a/app/build.gradle b/app/build.gradle index eff4b58..6643b24 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 23 - buildToolsVersion "23.0.3" + compileSdkVersion 28 defaultConfig { applicationId "it.sysdata.eventdispatcher" - minSdkVersion 9 - targetSdkVersion 23 + minSdkVersion 14 + targetSdkVersion 28 versionCode 1 versionName "1.0" } @@ -26,21 +25,21 @@ dependencies { def adapterVersionName = project.ext.libVersionName - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:23.3.0' - compile 'com.android.support:design:23.3.0' + implementation fileTree(dir: 'libs', include: ['*.jar']) + testImplementation 'junit:junit:4.13' + implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'com.google.android.material:material:1.0.0' // WITH THESE ONES YOU CAN TEST REMOTE LIBS // REMEMBER TO USE THESE AFTER TEST CAUSE JENKINS TROUBLES - compile "com.baseandroid:baseandroid-busadapter:$adapterVersionName" - compile "com.baseandroid:baseandroid-eventdispatcher:$adapterVersionName" - // compile "com.baseandroid:baseandroid-rxeventdispatcher:$adapterVersionName' + implementation "com.baseandroid:baseandroid-busadapter:$adapterVersionName" + // implementation "com.baseandroid:baseandroid-eventdispatcher:$adapterVersionName" + implementation "com.baseandroid:baseandroid-rxeventdispatcher:$adapterVersionName" // WITH THESE ONES YOU CAN USE LOCAL PROJECTS // REMEMBER: COMMENT THESE ONES BEFOR PUSH ON DEVELOP CAUSE JENKINS TROUBLES - //compile project(path: ':baseandroid-busadapter') - //compile project(path: ':baseandroid-eventdispatcher') - //compile project(path: ':baseandroid-rxeventdispatcher') + // implementation project(path: ':baseandroid-busadapter') + // implementation project(path: ':baseandroid-eventdispatcher') + // implementation project(path: ':baseandroid-rxeventdispatcher2') } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 64ccc47..5af0c3d 100755 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,17 +1,22 @@ + xmlns:tools="http://schemas.android.com/tools" + package="it.sysdata.eventdispatcher"> + android:theme="@style/AppTheme" + tools:ignore="GoogleAppIndexingWarning" + tools:replace="android:allowBackup"> + android:theme="@style/AppTheme.NoActionBar"> diff --git a/app/src/main/java/it/sysdata/eventdispatcher/SampleApplication.java b/app/src/main/java/it/sysdata/eventdispatcher/SampleApplication.java new file mode 100644 index 0000000..d784df8 --- /dev/null +++ b/app/src/main/java/it/sysdata/eventdispatcher/SampleApplication.java @@ -0,0 +1,21 @@ +package it.sysdata.eventdispatcher; + +import android.app.Application; + +import com.baseandroid.events.EventDispatcher; +import com.baseandroid.events.rx.RxEventProcessor; + +public class SampleApplication extends Application { + + @Override + public void onCreate() { + super.onCreate(); + // init event bus ASAP + initEventBus(); + } + + private void initEventBus() { + // choose the processor you want to use + EventDispatcher.useEventProcessor(RxEventProcessor.newInstance()); + } +} diff --git a/app/src/main/java/it/sysdata/eventdispatcher/UIEvent.java b/app/src/main/java/it/sysdata/eventdispatcher/events/UIEvent.java old mode 100755 new mode 100644 similarity index 88% rename from app/src/main/java/it/sysdata/eventdispatcher/UIEvent.java rename to app/src/main/java/it/sysdata/eventdispatcher/events/UIEvent.java index cbf03af..4c1aafc --- a/app/src/main/java/it/sysdata/eventdispatcher/UIEvent.java +++ b/app/src/main/java/it/sysdata/eventdispatcher/events/UIEvent.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Sysdata Digital, S.r.l. + * Copyright (C) 2020 Sysdata Digital, S.r.l. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package it.sysdata.eventdispatcher; +package it.sysdata.eventdispatcher.events; import com.baseandroid.events.Event; diff --git a/app/src/main/java/it/sysdata/eventdispatcher/MainActivity.java b/app/src/main/java/it/sysdata/eventdispatcher/ui/MainActivity.java old mode 100755 new mode 100644 similarity index 79% rename from app/src/main/java/it/sysdata/eventdispatcher/MainActivity.java rename to app/src/main/java/it/sysdata/eventdispatcher/ui/MainActivity.java index f60cb9a..cb56a9d --- a/app/src/main/java/it/sysdata/eventdispatcher/MainActivity.java +++ b/app/src/main/java/it/sysdata/eventdispatcher/ui/MainActivity.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Sysdata Digital, S.r.l. + * Copyright (C) 2020 Sysdata Digital, S.r.l. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,16 +14,20 @@ * limitations under the License. */ -package it.sysdata.eventdispatcher; +package it.sysdata.eventdispatcher.ui; import android.os.Bundle; -import android.support.design.widget.FloatingActionButton; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; + import com.baseandroid.events.EventDispatcher; +import com.google.android.material.floatingactionbutton.FloatingActionButton; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import it.sysdata.eventdispatcher.R; +import it.sysdata.eventdispatcher.events.UIEvent; public class MainActivity extends AppCompatActivity { @@ -31,10 +35,10 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + + Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - EventDispatcher.register(this); - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); + FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { diff --git a/app/src/main/java/it/sysdata/eventdispatcher/MainActivityFragment.java b/app/src/main/java/it/sysdata/eventdispatcher/ui/MainFragment.java old mode 100755 new mode 100644 similarity index 62% rename from app/src/main/java/it/sysdata/eventdispatcher/MainActivityFragment.java rename to app/src/main/java/it/sysdata/eventdispatcher/ui/MainFragment.java index 548251c..ccad307 --- a/app/src/main/java/it/sysdata/eventdispatcher/MainActivityFragment.java +++ b/app/src/main/java/it/sysdata/eventdispatcher/ui/MainFragment.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Sysdata Digital, S.r.l. + * Copyright (C) 2020 Sysdata Digital, S.r.l. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,35 +14,50 @@ * limitations under the License. */ -package it.sysdata.eventdispatcher; +package it.sysdata.eventdispatcher.ui; import android.os.Bundle; -import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.baseandroid.events.EventDispatcher; -import com.squareup.otto.Subscribe; +import com.baseandroid.events.rx.annotations.RxSubscribe; + +import androidx.fragment.app.Fragment; +import it.sysdata.eventdispatcher.R; +import it.sysdata.eventdispatcher.events.UIEvent; /** * A placeholder fragment containing a simple view. */ -public class MainActivityFragment extends Fragment { +public class MainFragment extends Fragment { - public MainActivityFragment() { + public MainFragment() { } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - EventDispatcher.register(this); + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_main, container, false); } - @Subscribe - public void onConsumePippo(final UIEvent UIEvent) { + @Override + public void onResume() { + super.onResume(); + // register to events + EventDispatcher.register(this); + } + + @Override + public void onPause() { + super.onPause(); + // unregister from incoming events + EventDispatcher.unregister(this); + } + + @RxSubscribe + public void onConsumeUIEvent(final UIEvent uiEvent) { Toast.makeText(getActivity(), "received UIEvent", Toast.LENGTH_SHORT).show(); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 800bcfc..0a348c0 100755 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,6 +1,6 @@ - + tools:context=".ui.MainActivity"> - - - + - - + diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 21b1aad..16f8beb 100755 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -1,5 +1,5 @@