package com.convenient.android.common.utils.eventbus

import org.greenrobot.eventbus.EventBus

/**
 *    @author : hubowen
 *    @date   : 2020/9/14  4:59 PM
 *    @description：
 */
object EventBusUtils {
    
    
    fun register(obj : Any) {
        if (!EventBus.getDefault().isRegistered(obj)) EventBus.getDefault().register(obj)
    }

    fun unRegister(obj: Any) {
        if (EventBus.getDefault().isRegistered(obj)) EventBus.getDefault().unregister(obj)
    }

    fun post(Publisher: Any) {
        EventBus.getDefault().post(Publisher)
    }

    fun <T> post(tag: String, t: T) {
        EventBus.getDefault().post(MessageEvent(tag, t))
    }

    fun postSticky(Publisher: Any) {
        EventBus.getDefault().postSticky(Publisher)
    }

    /**
     * @methodName：cancelEventDelivery created by luozhipeng on 14/8/18 15:06.
     * @description： 取消事件传送 事件取消仅限于ThreadMode.PostThread下才可以使用,不取消事件就会一直存在
     */
    fun cancelEventDelivery(event: Any) {
        EventBus.getDefault().cancelEventDelivery(event)
    }

    /**
     * @param ：[eventType]
     * @return : void
     * @methodName ：removeStickyEvent created by luozhipeng on 14/8/18 15:08.
     * @description ：移除指定的粘性订阅事件
     */
    fun <T> removeStickyEvent(eventType: Class<T>?) {
        EventBus.getDefault().getStickyEvent(eventType)?.apply {
            EventBus.getDefault().removeStickyEvent(this)
        }
    }

    /**
     * @methodName：removeAllStickyEvents created by luozhipeng on 14/8/18 15:06.
     * @description： 移除所有的粘性订阅事件
     */
    fun removeAllStickyEvents() {
        EventBus.getDefault().removeAllStickyEvents()
    }


}
