Skip to content

Commit

Permalink
添加eventbus的封装
Browse files Browse the repository at this point in the history
  • Loading branch information
albert-lii committed Aug 4, 2017
1 parent 50b6463 commit c029f7d
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sutil/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdkVersion 11
targetSdkVersion 25
versionCode 1
versionName "1.0.0"
versionName "1.0.1"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand All @@ -32,4 +32,5 @@ dependencies {
implementation 'com.android.support:appcompat-v7:25.3.1'
testImplementation 'junit:junit:4.12'
implementation 'com.google.code.gson:gson:2.8.1'
implementation 'org.greenrobot:eventbus:3.0.0'
}
11 changes: 11 additions & 0 deletions sutil/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

#eventbus3.0
-keepattributes *Annotation*
-keepclassmembers class ** {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
98 changes: 98 additions & 0 deletions sutil/src/main/java/com/liyi/sutil/utils/SEventUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.liyi.sutil.utils;


import com.liyi.sutil.utils.prompt.SLogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.meta.SubscriberInfoIndex;

public class SEventUtil {
private static final String TAG = SEventUtil.class.getSimpleName();

/**
* Use index acceleration
* <p>
* Recommended for use in application
*
* @param index
*/
public static void installIndex(SubscriberInfoIndex index) {
EventBus.builder().addIndex(index).installDefaultEventBus();
}

/**
* Register eventbus
*
* @param subscriber
*/
public static void register(Object subscriber) {
if (!EventBus.getDefault().isRegistered(subscriber)) {
EventBus.getDefault().register(subscriber);
} else {
SLogUtil.e(TAG, "Failed to register eventbus");
}
}

/**
* Unregister the eventbus
*
* @param subscriber
*/
public static void unregister(Object subscriber) {
EventBus.getDefault().unregister(subscriber);
}

/**
* Publish a subscription event
* <p>
* you must register first, then publish the event to receive;
* It's kind of like the startActivityForResult method.
*/
public static void post(Object event) {
EventBus.getDefault().post(event);
}

/**
* Publish sticky subscription events (you can publish events first, after registration, and then receive)
* <p>
* StickyEvent keeps the latest information in memory, cancels the original message, performs the latest news,
* only after registration, and if it is not registered, the message will remain in memory
*/
public static void postSticky(Object event) {
EventBus.getDefault().postSticky(event);
}

/**
* Removes the specified sticky subscription event
*
* @param eventType
*/
public static <T> void removeStickyEvent(Class<T> eventType) {
T stickyEvent = EventBus.getDefault().getStickyEvent(eventType);
if (stickyEvent != null) {
EventBus.getDefault().removeStickyEvent(stickyEvent);
}
}

/**
* Remove all sticky subscription events
*/
public static void removeAllStickyEvents() {
EventBus.getDefault().removeAllStickyEvents();
}

/**
* Abort event passing, and subsequent events are not called (
* <p>
* only invoked when the event is passed
*
* @param event
*/
public static void cancelEventDelivery(Object event) {
EventBus.getDefault().cancelEventDelivery(event);
}

public EventBus getEventBus() {
return EventBus.getDefault();
}
}

0 comments on commit c029f7d

Please sign in to comment.