Files
MoGoEagleEye/libraries/mogo-map-api/src/main/java/com/mogo/map/MogoMap.kt

60 lines
1.7 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.mogo.map
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger
import com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant.Companion.M_MAP
import java.util.concurrent.ConcurrentHashMap
class MogoMap private constructor() {
companion object {
private const val TAG = "MogoMap"
const val DEFAULT = "Default"
@JvmStatic
val mapInstance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
MogoMap()
}
}
private val mapCache = ConcurrentHashMap<String, IMogoMap>()
/**
* 缓存多实例对象instanceTag未指定默认给予DEFAULT
*/
fun initInstance(map: IMogoMap, instanceTag: String = DEFAULT) {
if (mapCache.contains(instanceTag)) {
CallerLogger.e(
"$M_MAP$TAG",
" already has map instance with tag :$instanceTag , please check"
)
return
}
mapCache[instanceTag] = map
}
/**
* 业务使用时根据地图是否加载来判定 是否获取IMogoMap实例
*/
fun getMogoMap(instance: String = DEFAULT): IMogoMap? {
return if (mapCache[instance] == null) {
CallerLogger.e("$M_MAP$TAG", "getMogoMap func has error with no instance in mapCache")
mapCache[DEFAULT]
} else {
mapCache[instance]
}
}
fun clear(instance: String = DEFAULT) {
if (mapCache.containsKey(instance)) {
mapCache[instance]?.uiController?.destroy()
mapCache.remove(instance)
} else {
CallerLogger.e(
"$M_MAP$TAG",
"Map instance invoke destroy in clear func has no instance key"
)
}
}
}