-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50b6463
commit c029f7d
Showing
3 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |