WdRouterRule.java 6.3 KB

package com.wd.capability.router;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.wd.capability.router.data.ActionBean;
import com.wd.capability.router.data.RouterParameter;

/**
 * 路由管理类
 *
 * @author shishuangxi_wd
 * @date 2022/7/6 10:39
 */
public class WdRouterRule {
    private static final int REQUEST_CODE = 20231112;

    private static String TAG = "WdRouterRule";

    private static WdRouterRule mInstance;

    private Postcard postcard;

    public Postcard getPostcard() {
        return postcard;
    }

    private WdRouterRule() {
    }

    public static WdRouterRule getInstance() {
        if (mInstance == null) {
            mInstance = new WdRouterRule();
        }
        return mInstance;
    }

    /**
     * 无传参时使用
     *
     * @param context
     * @param path
     */
    public void processAction(Context context, String path) {
        ActionBean actionBean = new ActionBean();
        actionBean.paramBean.pageID = path;
        processAction(context, actionBean);
    }

    /**
     * 有传参时 自定义 ActionBean
     *
     * @param context
     * @param actionBean
     */
    public void processAction(Context context, ActionBean actionBean) {
        processAction(context, actionBean, -1);
    }

    /**
     * 有传参时 自定义 ActionBean
     *
     * @param context
     * @param actionBean
     */
    public void processAction(Context context, ActionBean actionBean, int flag) {

        processAction(context, actionBean, flag, REQUEST_CODE);

    }

    /**
     * 有传参时 自定义 ActionBean
     *
     * @param context
     * @param actionBean
     */
    public void processAction(Context context, ActionBean actionBean, int flag, int requestCode) {

        if (checkNull(actionBean)) {
            Log.e(TAG, "传参 为空");
            return;
        }

        try {
            postcard = ARouter.getInstance().build(actionBean.paramBean.pageID);
            if (postcard == null) {
                Log.e(TAG, "postcard 为空");
                return;
            }
            if (-1 != flag) {
                postcard.withFlags(flag);
            }
            if (actionBean.enterAnim != -1 && actionBean.exitAnim != -1) {
                postcard.withTransition(actionBean.enterAnim, actionBean.exitAnim);
            }

            postcard.withSerializable(RouterParameter.ACTION_KEY, actionBean);
            if (actionBean.requestCode != 0) {
                if (context instanceof Activity) {
                    postcard.navigation((Activity) context, actionBean.requestCode);
                } else {
                    Log.e(TAG, "context is not activity");
                }

            } else {
                if (context instanceof Activity) {
                    postcard.navigation((Activity) context, requestCode);
                } else {
                    postcard.navigation();
                }

            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }

    }

    /**
     * 传参为bundle 类型
     */
    public void processAction(Context context, String path, Bundle bundle) {
        ActionBean actionBean = new ActionBean();
        actionBean.paramBean.pageID = path;
        processAction(context, actionBean, bundle);
    }

    /**
     * 传参为json 和bundle 类型
     */
    public void processAction(Context context, ActionBean actionBean, Bundle bundle) {
        if (checkNull(actionBean)) {
            Log.e(TAG, "传参 为空");
            return;
        }

        try {
            postcard = ARouter.getInstance().build(actionBean.paramBean.pageID);
            if (postcard == null) {
                Log.e(TAG, "postcard 为空");
                return;
            }
            if (actionBean.enterAnim != -1 && actionBean.exitAnim != -1) {
                postcard.withTransition(actionBean.enterAnim, actionBean.exitAnim);
            }

            postcard.withSerializable(RouterParameter.ACTION_KEY, actionBean);
            if (bundle != null) {
                postcard.withBundle(RouterParameter.ACTION_BUNDLE, bundle);
            }
            if (actionBean.requestCode != 0) {
                if (context instanceof Activity) {
                    postcard.navigation((Activity) context, actionBean.requestCode);
                } else {
                    Log.e(TAG, "context is not activity");
                }

            } else {
                if (context instanceof Activity) {
                    postcard.navigation((Activity) context);
                } else {
                    postcard.navigation();
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }

    /**
     * 获取fragment实例
     */
    public Fragment getFragment(String path) {
        ActionBean actionBean = new ActionBean();
        actionBean.paramBean.pageID = path;
        return getFragment(actionBean);
    }

    /**
     * 获取fragment实例
     */
    public Fragment getFragment(ActionBean actionBean) {
        Fragment navigation = null;
        if (checkNull(actionBean)) {
            return navigation;
        }
        try {
            postcard = ARouter.getInstance().build(actionBean.paramBean.pageID);
            if (postcard == null) {
                return navigation;
            }
            postcard.withSerializable(RouterParameter.ACTION_KEY, actionBean);
            navigation = (Fragment) postcard.navigation();
            return navigation;
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
        return navigation;
    }

    /**
     * 获取provider实例
     */
    public Object getProvider(Class clazz) {
        return ARouter.getInstance().navigation(clazz);
    }

    /**
     * 检测数据是否为空
     */
    public boolean checkNull(ActionBean actionBean) {
        if (actionBean == null) {
            return true;
        }
        if (actionBean.paramBean == null) {
            return true;
        }
        if (TextUtils.isEmpty(actionBean.paramBean.pageID)) {
            return true;
        }
        return false;
    }
}