From 5f531c3b63c38ed0a68972945e204a79273dff39 Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Tue, 25 Oct 2022 16:40:10 +0300 Subject: [PATCH] Allowing initialise multiple instances of location component. --- .../locationcomponent/LayerSourceProvider.kt | 17 +- .../LocationComponentInitOptions.kt | 157 ++++++++++++++++++ .../LocationComponentPluginImpl.kt | 13 +- .../LocationIndicatorLayerRenderer.kt | 25 ++- .../locationcomponent/ModelLayerRenderer.kt | 7 +- 5 files changed, 185 insertions(+), 34 deletions(-) create mode 100644 plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationComponentInitOptions.kt diff --git a/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LayerSourceProvider.kt b/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LayerSourceProvider.kt index 9ac4ba077b..757333dbf0 100644 --- a/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LayerSourceProvider.kt +++ b/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LayerSourceProvider.kt @@ -2,18 +2,15 @@ package com.mapbox.maps.plugin.locationcomponent import com.mapbox.maps.plugin.LocationPuck2D import com.mapbox.maps.plugin.LocationPuck3D -import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER -import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER -import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_SOURCE -internal class LayerSourceProvider { +internal class LayerSourceProvider(private val locationComponentInitOptions: LocationComponentInitOptions) { fun getModelSource(locationModelLayerOptions: LocationPuck3D): ModelSourceWrapper { if (locationModelLayerOptions.modelUri.isEmpty()) { throw IllegalArgumentException("Model Url must not be empty!") } return ModelSourceWrapper( - MODEL_SOURCE, + locationComponentInitOptions.puck3DSourceId, locationModelLayerOptions.modelUri, locationModelLayerOptions.position.map { it.toDouble() } ) @@ -21,19 +18,19 @@ internal class LayerSourceProvider { fun getModelLayer(locationModelLayerOptions: LocationPuck3D) = ModelLayerWrapper( - MODEL_LAYER, - MODEL_SOURCE, + locationComponentInitOptions.puck3DLayerId, + locationComponentInitOptions.puck3DSourceId, locationModelLayerOptions.modelScale.map { it.toDouble() }, locationModelLayerOptions.modelRotation.map { it.toDouble() }, locationModelLayerOptions.modelTranslation.map { it.toDouble() }, locationModelLayerOptions.modelOpacity.toDouble() ) - fun getLocationIndicatorLayer() = LocationIndicatorLayerWrapper(LOCATION_INDICATOR_LAYER) + fun getLocationIndicatorLayer() = LocationIndicatorLayerWrapper(locationComponentInitOptions.puck2DLayerId) fun getLocationIndicatorLayerRenderer(puckOptions: LocationPuck2D) = - LocationIndicatorLayerRenderer(puckOptions, this) + LocationIndicatorLayerRenderer(locationComponentInitOptions, puckOptions, this) fun getModelLayerRenderer(locationModelLayerOptions: LocationPuck3D) = - ModelLayerRenderer(this, locationModelLayerOptions) + ModelLayerRenderer(locationComponentInitOptions, this, locationModelLayerOptions) } \ No newline at end of file diff --git a/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationComponentInitOptions.kt b/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationComponentInitOptions.kt new file mode 100644 index 0000000000..db4ee0adb1 --- /dev/null +++ b/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationComponentInitOptions.kt @@ -0,0 +1,157 @@ +package com.mapbox.maps.plugin.locationcomponent + +import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.BEARING_ICON +import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER +import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER +import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_SOURCE +import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.SHADOW_ICON +import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.TOP_ICON +import java.util.Objects +import kotlin.Any +import kotlin.Boolean +import kotlin.Int +import kotlin.String +import kotlin.Unit +import kotlin.jvm.JvmSynthetic + +/** + * Initialisation options for location component to allow multiple instances + * of LocationComponent. + */ +public class LocationComponentInitOptions private constructor( + public val puck2DLayerId: String, + public val puck3DLayerId: String, + public val puck3DSourceId: String, + public val topIconImageId: String, + public val shadowIconImageId: String, + public val bearingIconImageId: String +) { + public override fun toString() = "LocationComponentInitOptions(puck2DLayerId=$puck2DLayerId,puck3DLayerId=$puck3DLayerId, puck3DSourceId=$puck3DSourceId, topIconImageId=$topIconImageId,shadowIconImageId=$shadowIconImageId, bearingIconImageId=$bearingIconImageId)" + + public override fun equals(other: Any?): Boolean = other is LocationComponentInitOptions + && puck2DLayerId == other.puck2DLayerId + && puck3DLayerId == other.puck3DLayerId + && puck3DSourceId == other.puck3DSourceId + && topIconImageId == other.topIconImageId + && shadowIconImageId == other.shadowIconImageId + && bearingIconImageId == other.bearingIconImageId + + public override fun hashCode(): Int = Objects.hash(puck2DLayerId, puck3DLayerId, puck3DSourceId, + topIconImageId, shadowIconImageId, bearingIconImageId) + + /** + * Composes and builds a [LocationComponentInitOptions] object. + * + * This is a concrete implementation of the builder design pattern. + * + * @property + */ + public class Builder { + @set:JvmSynthetic + public var puck2DLayerId: String = LOCATION_INDICATOR_LAYER + + @set:JvmSynthetic + public var puck3DLayerId: String = MODEL_LAYER + + @set:JvmSynthetic + public var puck3DSourceId: String = MODEL_SOURCE + + @set:JvmSynthetic + public var topIconImageId: String = TOP_ICON + + @set:JvmSynthetic + public var shadowIconImageId: String = SHADOW_ICON + + @set:JvmSynthetic + public var bearingIconImageId: String = BEARING_ICON + + /** + * Set puck2DLayerId + * + * @param puck2DLayerId puck2DLayerId + * @return Builder + */ + public fun setPuck2DLayerId(puck2DLayerId: String): Builder { + this.puck2DLayerId = puck2DLayerId + return this + } + + /** + * Set puck3DLayerId + * + * @param puck3DLayerId puck3DLayerId + * @return Builder + */ + public fun setPuck3DLayerId(puck3DLayerId: String): Builder { + this.puck3DLayerId = puck3DLayerId + return this + } + + /** + * Set puck3DSourceId + * + * @param puck3DSourceId puck3DSourceId + * @return Builder + */ + public fun setPuck3DSourceId(puck3DSourceId: String): Builder { + this.puck3DSourceId = puck3DSourceId + return this + } + + /** + * Set topIconImageId + * + * @param topIconImageId topIconImageId + * @return Builder + */ + public fun setTopIconImageId(topIconImageId: String): Builder { + this.topIconImageId = topIconImageId + return this + } + + /** + * Set shadowIconImageId + * + * @param shadowIconImageId shadowIconImageId + * @return Builder + */ + public fun setShadowIconImageId(shadowIconImageId: String): Builder { + this.shadowIconImageId = shadowIconImageId + return this + } + + /** + * Set bearingIconImageId + * + * @param bearingIconImageId bearingIconImageId + * @return Builder + */ + public fun setBearingIconImageId(bearingIconImageId: String): Builder { + this.bearingIconImageId = bearingIconImageId + return this + } + + /** + * Returns a [LocationComponentInitOptions] reference to the object being constructed by the + * builder. + * + * Throws an [IllegalArgumentException] when a non-null property wasn't initialised. + * + * @return LocationComponentInitOptions + */ + public fun build(): LocationComponentInitOptions { + return LocationComponentInitOptions(puck2DLayerId, puck3DLayerId, puck3DSourceId, + topIconImageId, shadowIconImageId, bearingIconImageId) + } + } +} + +/** + * Creates a [LocationComponentInitOptions] through a DSL-style builder. + * + * @param initializer the intialisation block + * @return LocationComponentInitOptions + */ +@JvmSynthetic +public fun LocationComponentInitOptions(initializer: LocationComponentInitOptions.Builder.() -> Unit): + LocationComponentInitOptions = LocationComponentInitOptions.Builder().apply(initializer).build() diff --git a/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationComponentPluginImpl.kt b/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationComponentPluginImpl.kt index f7a928b72e..5f061b45fa 100644 --- a/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationComponentPluginImpl.kt +++ b/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationComponentPluginImpl.kt @@ -11,8 +11,6 @@ import com.mapbox.maps.RenderedQueryGeometry import com.mapbox.maps.RenderedQueryOptions import com.mapbox.maps.extension.style.StyleInterface import com.mapbox.maps.plugin.delegates.MapDelegateProvider -import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER -import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER import com.mapbox.maps.plugin.locationcomponent.animators.PuckAnimatorManager import com.mapbox.maps.plugin.locationcomponent.generated.* import com.mapbox.maps.plugin.locationcomponent.generated.LocationComponentAttributeParser @@ -23,7 +21,10 @@ import java.util.concurrent.CopyOnWriteArraySet * Default implementation of the LocationComponentPlugin, it renders the configured location puck * to the user's current location. */ -class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2, +class LocationComponentPluginImpl( + val locationComponentInitOptions: LocationComponentInitOptions = LocationComponentInitOptions.Builder() + .build() +) : LocationComponentPlugin2, LocationConsumer2, LocationComponentSettingsBase2() { private lateinit var delegateProvider: MapDelegateProvider @@ -135,8 +136,8 @@ class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2, RenderedQueryGeometry(delegateProvider.mapCameraManagerDelegate.pixelForCoordinate(point)), RenderedQueryOptions( listOf( - LOCATION_INDICATOR_LAYER, - MODEL_LAYER + locationComponentInitOptions.puck2DLayerId, + locationComponentInitOptions.puck3DLayerId ), null ) @@ -213,7 +214,7 @@ class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2, internalSettings.layerAbove, internalSettings.layerBelow ), - layerSourceProvider = LayerSourceProvider(), + layerSourceProvider = LayerSourceProvider(locationComponentInitOptions), animationManager = PuckAnimatorManager( indicatorPositionChangedListener, indicatorBearingChangedListener, diff --git a/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationIndicatorLayerRenderer.kt b/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationIndicatorLayerRenderer.kt index 27322a2efc..a83d126be4 100644 --- a/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationIndicatorLayerRenderer.kt +++ b/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationIndicatorLayerRenderer.kt @@ -5,16 +5,13 @@ import com.mapbox.bindgen.Value import com.mapbox.geojson.Point import com.mapbox.maps.extension.style.StyleInterface import com.mapbox.maps.plugin.LocationPuck2D -import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.BEARING_ICON -import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER -import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.SHADOW_ICON -import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.TOP_ICON import com.mapbox.maps.plugin.locationcomponent.utils.BitmapUtils import java.text.DecimalFormat import java.text.NumberFormat import java.util.Locale internal class LocationIndicatorLayerRenderer( + private val locationComponentInitOptions: LocationComponentInitOptions, private val puckOptions: LocationPuck2D, layerSourceProvider: LayerSourceProvider ) : LocationLayerRenderer { @@ -28,7 +25,7 @@ internal class LocationIndicatorLayerRenderer( } override fun isRendererInitialised(): Boolean { - return style?.styleLayerExists(LOCATION_INDICATOR_LAYER) ?: false + return style?.styleLayerExists(locationComponentInitOptions.puck2DLayerId) ?: false } override fun addLayers(positionManager: LocationComponentPositionManager) { @@ -76,22 +73,22 @@ internal class LocationIndicatorLayerRenderer( private fun setupBitmaps() { puckOptions.topImage?.let { BitmapUtils.getBitmapFromDrawable(it) } - ?.let { style?.addImage(TOP_ICON, it) } + ?.let { style?.addImage(locationComponentInitOptions.topIconImageId, it) } puckOptions.bearingImage?.let { BitmapUtils.getBitmapFromDrawable(it) } - ?.let { style?.addImage(BEARING_ICON, it) } + ?.let { style?.addImage(locationComponentInitOptions.bearingIconImageId, it) } puckOptions.shadowImage?.let { BitmapUtils.getBitmapFromDrawable(it) } - ?.let { style?.addImage(SHADOW_ICON, it) } - layer.topImage(TOP_ICON) - layer.bearingImage(BEARING_ICON) - layer.shadowImage(SHADOW_ICON) + ?.let { style?.addImage(locationComponentInitOptions.shadowIconImageId, it) } + layer.topImage(locationComponentInitOptions.topIconImageId) + layer.bearingImage(locationComponentInitOptions.bearingIconImageId) + layer.shadowImage(locationComponentInitOptions.shadowIconImageId) layer.opacity(puckOptions.opacity.toDouble()) } override fun clearBitmaps() { - style?.removeStyleImage(TOP_ICON) - style?.removeStyleImage(BEARING_ICON) - style?.removeStyleImage(SHADOW_ICON) + style?.removeStyleImage(locationComponentInitOptions.topIconImageId) + style?.removeStyleImage(locationComponentInitOptions.bearingIconImageId) + style?.removeStyleImage(locationComponentInitOptions.shadowIconImageId) } override fun updateStyle(style: StyleInterface) { diff --git a/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/ModelLayerRenderer.kt b/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/ModelLayerRenderer.kt index 5c2618abe7..2cfe3358d9 100644 --- a/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/ModelLayerRenderer.kt +++ b/plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/ModelLayerRenderer.kt @@ -6,10 +6,9 @@ import com.mapbox.bindgen.Value import com.mapbox.geojson.Point import com.mapbox.maps.extension.style.StyleInterface import com.mapbox.maps.plugin.LocationPuck3D -import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER -import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_SOURCE internal class ModelLayerRenderer( + private val locationComponentInitOptions: LocationComponentInitOptions, layerSourceProvider: LayerSourceProvider, private val locationModelLayerOptions: LocationPuck3D ) : LocationLayerRenderer { @@ -32,11 +31,11 @@ internal class ModelLayerRenderer( } private fun isLayerInitialised(): Boolean { - return style?.styleLayerExists(MODEL_LAYER) ?: false + return style?.styleLayerExists(locationComponentInitOptions.puck3DLayerId) ?: false } private fun isSourceInitialised(): Boolean { - return style?.styleSourceExists(MODEL_SOURCE) ?: false + return style?.styleSourceExists(locationComponentInitOptions.puck3DSourceId) ?: false } override fun addLayers(positionManager: LocationComponentPositionManager) {