Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Provide common LifecycleObservers #630

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion EventBus/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
buildscript {
repositories {
maven { url "https://plugins.gradle.org/m2/" }
}

dependencies {
classpath "net.ltgt.gradle:gradle-apt-plugin:0.13"
}
}

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'signing'
apply plugin: 'idea'
apply plugin: 'net.ltgt.apt-idea'

archivesBaseName = 'eventbus'
group = 'org.greenrobot'
version = '3.1.1'
version = '3.2.0-SNAPSHOT'
sourceCompatibility = 1.7

def isSnapshot = version.endsWith('-SNAPSHOT')
Expand All @@ -18,6 +29,7 @@ if(isSnapshot) {

repositories {
mavenCentral()
google()
}

// Still unsupported, see http://issues.gradle.org/browse/GRADLE-784
Expand All @@ -32,6 +44,8 @@ dependencies {
provided 'com.google.android:android-test:4.1.1.4'
provided 'com.google.android:annotations:4.1.1.4'
provided 'com.google.android:support-v4:r7'
provided "android.arch.lifecycle:runtime:1.0.3"
apt "android.arch.lifecycle:compiler:1.0.0"
// deployerJars 'org.apache.maven.wagon:wagon-webdav-jackrabbit:2.4'
deployerJars 'org.apache.maven.wagon:wagon-webdav:1.0-beta-2'
}
Expand Down
42 changes: 42 additions & 0 deletions EventBus/src/org/greenrobot/eventbus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package org.greenrobot.eventbus;

import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.LifecycleOwner;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -127,6 +130,45 @@ public EventBus() {
executorService = builder.executorService;
}

/**
* Adds an observer to the given {@link LifecycleOwner}s lifecycle to automatically register
* EventBus during an onCreated event and unregister during an onDestroy event.
* <p>
* Requires dependency on Android Architecture Components Lifecycle package.
*
* @see #register(Object)
* @see #unregister(Object)
*/
public void observeCreateDestroy(LifecycleOwner lifecycleOwner) {
createAndAddObserver(lifecycleOwner, "org.greenrobot.eventbus.android.lifecycle.EventBusCreateDestroyObserver");
}

/**
* Adds an observer to the given {@link LifecycleOwner}s lifecycle to automatically register
* EventBus during an onStart event and unregister during an onStop event.
* <p>
* Requires dependency on Android Architecture Components Lifecycle package.
*
* @see #register(Object)
* @see #unregister(Object)
*/
public void observeStartStop(LifecycleOwner lifecycleOwner) {
createAndAddObserver(lifecycleOwner, "org.greenrobot.eventbus.android.lifecycle.EventBusStartStopObserver");
}

private void createAndAddObserver(LifecycleOwner lifecycleOwner, String className) {
try {
Class<?> observer = Class.forName(className);
LifecycleObserver instance = (LifecycleObserver) observer
.getDeclaredConstructor(LifecycleOwner.class, EventBus.class)
.newInstance(lifecycleOwner, this);
lifecycleOwner.getLifecycle().addObserver(instance);
} catch (Exception e) {
throw new EventBusException("Failed to create LifecycleObserver. " +
"Do you have a dependency on Android Architecture Components Lifecycles?", e);
}
}

/**
* Registers the given subscriber to receive events. Subscribers must call {@link #unregister(Object)} once they
* are no longer interested in receiving events.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.greenrobot.eventbus.android.lifecycle;

import android.arch.lifecycle.Lifecycle.Event;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.OnLifecycleEvent;

import org.greenrobot.eventbus.EventBus;


public class EventBusCreateDestroyObserver implements LifecycleObserver {

private final LifecycleOwner lifecycleOwner;
private final EventBus eventBus;

public EventBusCreateDestroyObserver(LifecycleOwner lifecycleOwner, EventBus eventBus) {
this.lifecycleOwner = lifecycleOwner;
this.eventBus = eventBus;
}

@OnLifecycleEvent(Event.ON_CREATE)
void register() {
eventBus.register(lifecycleOwner);
}

@OnLifecycleEvent(Event.ON_DESTROY)
void unregister() {
eventBus.unregister(lifecycleOwner);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.greenrobot.eventbus.android.lifecycle;

import android.arch.lifecycle.Lifecycle.Event;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.OnLifecycleEvent;

import org.greenrobot.eventbus.EventBus;


public class EventBusStartStopObserver implements LifecycleObserver {

private final LifecycleOwner lifecycleOwner;
private final EventBus eventBus;

public EventBusStartStopObserver(LifecycleOwner lifecycleOwner, EventBus eventBus) {
this.lifecycleOwner = lifecycleOwner;
this.eventBus = eventBus;
}

@OnLifecycleEvent(Event.ON_START)
void register() {
eventBus.register(lifecycleOwner);
}

@OnLifecycleEvent(Event.ON_STOP)
void unregister() {
eventBus.unregister(lifecycleOwner);
}

}