ModularServiceManager.kt 4.04 KB
Newer Older
1
package com.ydl.ydlcommon.modular
konghaorui committed
2

3 4 5
import android.content.Context
import android.os.Bundle
import android.os.Parcelable
konghaorui committed
6 7
import com.alibaba.android.arouter.facade.template.IProvider
import com.alibaba.android.arouter.launcher.ARouter
万齐军 committed
8
import com.ydl.ydlcommon.BuildConfig
9
import com.ydl.ydlcommon.utils.LogUtil
konghaorui committed
10 11 12 13 14 15


/**
 * Created by haorui on 2019-09-21 .
 * Des:查找 Arouter 服务封装类
 */
万齐军 committed
16 17 18 19 20 21 22 23
fun <T> findRouteService(clz: Class<T>): T {
    val service = ARouter.getInstance().navigation(clz)
    if (service != null) return service
    if (BuildConfig.DEBUG) {
        throw IllegalStateException("check module dependency by [${clz.simpleName}]")
    }
    return service
}
24 25


万齐军 committed
26
fun route(context: Context?, route: String, vararg params: Pair<String, Any?>) {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    val build = ARouter.getInstance().build(route)
    params.forEach {
        when (val value = it.second) {
            is Boolean -> {
                build.withBoolean(it.first, value)
            }
            is Char -> {
                build.withChar(it.first, value)
            }
            is Int -> {
                build.withInt(it.first, value)
            }
            is Long -> {
                build.withLong(it.first, value)
            }
            is Float -> {
                build.withFloat(it.first, value)
            }
            is Double -> {
                build.withDouble(it.first, value)
            }
            is Short -> {
                build.withShort(it.first, value)
            }
            is Byte -> {
                build.withByte(it.first, value)
            }
            is String -> {
                build.withString(it.first, value)
            }
            is Bundle -> {
                build.withBundle(it.first, value)
            }
            is CharSequence -> {
                build.withCharSequence(it.first, value)
            }
            is Parcelable -> {
                build.withParcelable(it.first, value)
            }
        }
    }
万齐军 committed
68 69 70 71 72
    if (context == null) {
        build.navigation()
    } else {
        build.navigation(context)
    }
73 74
}

75
object ModularServiceManager {
konghaorui committed
76 77 78

    private val routerMap: HashMap<String, IProvider> = HashMap()

79
    fun getPlatformUserService(): IPlatformUserModuleService? {
konghaorui committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        return provide(IPlatformUserModuleService::class.java)
    }

    fun <T : IProvider> provide(clz: Class<T>, path: String): T {

        var provider: IProvider? = null
        val simpleName = clz.simpleName;

        if (routerMap[simpleName]!=null){
            return routerMap[simpleName] as T
        }

        try {
            val navigation = ARouter.getInstance()
                .build(path)
                .navigation()

            provider = navigation as IProvider

            routerMap[simpleName] = provider

        } catch (e: Exception) {
            LogUtil.e(e.message)
103
            return throw ServiceNotFoundException("请检查您是否依赖于包含以下内容的模块: $simpleName, path is $path")
konghaorui committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        }

        return provider as T
    }

    fun <T : IProvider> provide(clz: Class<T>): T {

        var provider: IProvider? = null
        val simpleName = clz.simpleName;
        if (routerMap[simpleName]!=null){
            return routerMap[simpleName] as T
        }
        try {
            provider = ARouter.getInstance().navigation(clz)
            if (provider == null){
119
                LogUtil.e("provide : Service Not Found ")
konghaorui committed
120 121 122
            }
            routerMap[simpleName] = provider
        } catch (e: Exception) {
123 124
            LogUtil.e(e.toString())
            return throw ServiceNotFoundException("请检查您是否依赖于包含以下内容的模块: $simpleName")
konghaorui committed
125 126 127 128
        }

        return provider as T
    }
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

    /**
     * 用以判断是否接入依赖某业务模块
     */
    fun <T : IProvider> isDependByClz(clz: Class<T>): Boolean {
        var provider: IProvider? = null

        try {
            provider = ARouter.getInstance().navigation(clz)
            return provider != null
        } catch (e: Exception) {
            LogUtil.e(e.toString())
        }

        return provider != null
    }
konghaorui committed
145
}