Skip to content

Commit 2ba1e74

Browse files
committed
Allowing initialise multiple instances of location component.
1 parent a453f5a commit 2ba1e74

File tree

5 files changed

+185
-34
lines changed

5 files changed

+185
-34
lines changed

plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LayerSourceProvider.kt

+7-10
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,35 @@ package com.mapbox.maps.plugin.locationcomponent
22

33
import com.mapbox.maps.plugin.LocationPuck2D
44
import com.mapbox.maps.plugin.LocationPuck3D
5-
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER
6-
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER
7-
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_SOURCE
85

9-
internal class LayerSourceProvider {
6+
internal class LayerSourceProvider(private val locationComponentInitOptions: LocationComponentInitOptions) {
107

118
fun getModelSource(locationModelLayerOptions: LocationPuck3D): ModelSourceWrapper {
129
if (locationModelLayerOptions.modelUri.isEmpty()) {
1310
throw IllegalArgumentException("Model Url must not be empty!")
1411
}
1512
return ModelSourceWrapper(
16-
MODEL_SOURCE,
13+
locationComponentInitOptions.puck3DSourceId,
1714
locationModelLayerOptions.modelUri,
1815
locationModelLayerOptions.position.map { it.toDouble() }
1916
)
2017
}
2118

2219
fun getModelLayer(locationModelLayerOptions: LocationPuck3D) =
2320
ModelLayerWrapper(
24-
MODEL_LAYER,
25-
MODEL_SOURCE,
21+
locationComponentInitOptions.puck3DLayerId,
22+
locationComponentInitOptions.puck3DSourceId,
2623
locationModelLayerOptions.modelScale.map { it.toDouble() },
2724
locationModelLayerOptions.modelRotation.map { it.toDouble() },
2825
locationModelLayerOptions.modelTranslation.map { it.toDouble() },
2926
locationModelLayerOptions.modelOpacity.toDouble()
3027
)
3128

32-
fun getLocationIndicatorLayer() = LocationIndicatorLayerWrapper(LOCATION_INDICATOR_LAYER)
29+
fun getLocationIndicatorLayer() = LocationIndicatorLayerWrapper(locationComponentInitOptions.puck2DLayerId)
3330

3431
fun getLocationIndicatorLayerRenderer(puckOptions: LocationPuck2D) =
35-
LocationIndicatorLayerRenderer(puckOptions, this)
32+
LocationIndicatorLayerRenderer(locationComponentInitOptions, puckOptions, this)
3633

3734
fun getModelLayerRenderer(locationModelLayerOptions: LocationPuck3D) =
38-
ModelLayerRenderer(this, locationModelLayerOptions)
35+
ModelLayerRenderer(locationComponentInitOptions, this, locationModelLayerOptions)
3936
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package com.mapbox.maps.plugin.locationcomponent
2+
3+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.BEARING_ICON
4+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER
5+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER
6+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_SOURCE
7+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.SHADOW_ICON
8+
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.TOP_ICON
9+
import java.util.Objects
10+
import kotlin.Any
11+
import kotlin.Boolean
12+
import kotlin.Int
13+
import kotlin.String
14+
import kotlin.Unit
15+
import kotlin.jvm.JvmSynthetic
16+
17+
/**
18+
* Initialisation options for location component to allow multiple instances
19+
* of LocationComponent.
20+
*/
21+
public class LocationComponentInitOptions private constructor(
22+
public val puck2DLayerId: String,
23+
public val puck3DLayerId: String,
24+
public val puck3DSourceId: String,
25+
public val topIconImageId: String,
26+
public val shadowIconImageId: String,
27+
public val bearingIconImageId: String
28+
) {
29+
public override fun toString() = "LocationComponentInitOptions(puck2DLayerId=$puck2DLayerId,puck3DLayerId=$puck3DLayerId, puck3DSourceId=$puck3DSourceId, topIconImageId=$topIconImageId,shadowIconImageId=$shadowIconImageId, bearingIconImageId=$bearingIconImageId)"
30+
31+
public override fun equals(other: Any?): Boolean = other is LocationComponentInitOptions
32+
&& puck2DLayerId == other.puck2DLayerId
33+
&& puck3DLayerId == other.puck3DLayerId
34+
&& puck3DSourceId == other.puck3DSourceId
35+
&& topIconImageId == other.topIconImageId
36+
&& shadowIconImageId == other.shadowIconImageId
37+
&& bearingIconImageId == other.bearingIconImageId
38+
39+
public override fun hashCode(): Int = Objects.hash(puck2DLayerId, puck3DLayerId, puck3DSourceId,
40+
topIconImageId, shadowIconImageId, bearingIconImageId)
41+
42+
/**
43+
* Composes and builds a [LocationComponentInitOptions] object.
44+
*
45+
* This is a concrete implementation of the builder design pattern.
46+
*
47+
* @property
48+
*/
49+
public class Builder {
50+
@set:JvmSynthetic
51+
public var puck2DLayerId: String = LOCATION_INDICATOR_LAYER
52+
53+
@set:JvmSynthetic
54+
public var puck3DLayerId: String = MODEL_LAYER
55+
56+
@set:JvmSynthetic
57+
public var puck3DSourceId: String = MODEL_SOURCE
58+
59+
@set:JvmSynthetic
60+
public var topIconImageId: String = TOP_ICON
61+
62+
@set:JvmSynthetic
63+
public var shadowIconImageId: String = SHADOW_ICON
64+
65+
@set:JvmSynthetic
66+
public var bearingIconImageId: String = BEARING_ICON
67+
68+
/**
69+
* Set puck2DLayerId
70+
*
71+
* @param puck2DLayerId puck2DLayerId
72+
* @return Builder
73+
*/
74+
public fun setPuck2DLayerId(puck2DLayerId: String): Builder {
75+
this.puck2DLayerId = puck2DLayerId
76+
return this
77+
}
78+
79+
/**
80+
* Set puck3DLayerId
81+
*
82+
* @param puck3DLayerId puck3DLayerId
83+
* @return Builder
84+
*/
85+
public fun setPuck3DLayerId(puck3DLayerId: String): Builder {
86+
this.puck3DLayerId = puck3DLayerId
87+
return this
88+
}
89+
90+
/**
91+
* Set puck3DSourceId
92+
*
93+
* @param puck3DSourceId puck3DSourceId
94+
* @return Builder
95+
*/
96+
public fun setPuck3DSourceId(puck3DSourceId: String): Builder {
97+
this.puck3DSourceId = puck3DSourceId
98+
return this
99+
}
100+
101+
/**
102+
* Set topIconImageId
103+
*
104+
* @param topIconImageId topIconImageId
105+
* @return Builder
106+
*/
107+
public fun setTopIconImageId(topIconImageId: String): Builder {
108+
this.topIconImageId = topIconImageId
109+
return this
110+
}
111+
112+
/**
113+
* Set shadowIconImageId
114+
*
115+
* @param shadowIconImageId shadowIconImageId
116+
* @return Builder
117+
*/
118+
public fun setShadowIconImageId(shadowIconImageId: String): Builder {
119+
this.shadowIconImageId = shadowIconImageId
120+
return this
121+
}
122+
123+
/**
124+
* Set bearingIconImageId
125+
*
126+
* @param bearingIconImageId bearingIconImageId
127+
* @return Builder
128+
*/
129+
public fun setBearingIconImageId(bearingIconImageId: String): Builder {
130+
this.bearingIconImageId = bearingIconImageId
131+
return this
132+
}
133+
134+
/**
135+
* Returns a [LocationComponentInitOptions] reference to the object being constructed by the
136+
* builder.
137+
*
138+
* Throws an [IllegalArgumentException] when a non-null property wasn't initialised.
139+
*
140+
* @return LocationComponentInitOptions
141+
*/
142+
public fun build(): LocationComponentInitOptions {
143+
return LocationComponentInitOptions(puck2DLayerId, puck3DLayerId, puck3DSourceId,
144+
topIconImageId, shadowIconImageId, bearingIconImageId)
145+
}
146+
}
147+
}
148+
149+
/**
150+
* Creates a [LocationComponentInitOptions] through a DSL-style builder.
151+
*
152+
* @param initializer the intialisation block
153+
* @return LocationComponentInitOptions
154+
*/
155+
@JvmSynthetic
156+
public fun LocationComponentInitOptions(initializer: LocationComponentInitOptions.Builder.() -> Unit):
157+
LocationComponentInitOptions = LocationComponentInitOptions.Builder().apply(initializer).build()

plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationComponentPluginImpl.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import com.mapbox.maps.RenderedQueryGeometry
1111
import com.mapbox.maps.RenderedQueryOptions
1212
import com.mapbox.maps.extension.style.StyleInterface
1313
import com.mapbox.maps.plugin.delegates.MapDelegateProvider
14-
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER
15-
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER
1614
import com.mapbox.maps.plugin.locationcomponent.animators.PuckAnimatorManager
1715
import com.mapbox.maps.plugin.locationcomponent.generated.*
1816
import com.mapbox.maps.plugin.locationcomponent.generated.LocationComponentAttributeParser
@@ -23,7 +21,10 @@ import java.util.concurrent.CopyOnWriteArraySet
2321
* Default implementation of the LocationComponentPlugin, it renders the configured location puck
2422
* to the user's current location.
2523
*/
26-
class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2,
24+
class LocationComponentPluginImpl(
25+
val locationComponentInitOptions: LocationComponentInitOptions = LocationComponentInitOptions.Builder()
26+
.build()
27+
) : LocationComponentPlugin2, LocationConsumer2,
2728
LocationComponentSettingsBase2() {
2829
private lateinit var delegateProvider: MapDelegateProvider
2930

@@ -119,8 +120,8 @@ class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2,
119120
RenderedQueryGeometry(delegateProvider.mapCameraManagerDelegate.pixelForCoordinate(point)),
120121
RenderedQueryOptions(
121122
listOf(
122-
LOCATION_INDICATOR_LAYER,
123-
MODEL_LAYER
123+
locationComponentInitOptions.puck2DLayerId,
124+
locationComponentInitOptions.puck3DLayerId
124125
),
125126
null
126127
)
@@ -195,7 +196,7 @@ class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2,
195196
internalSettings.layerAbove,
196197
internalSettings.layerBelow
197198
),
198-
layerSourceProvider = LayerSourceProvider(),
199+
layerSourceProvider = LayerSourceProvider(locationComponentInitOptions),
199200
animationManager = PuckAnimatorManager(
200201
indicatorPositionChangedListener,
201202
indicatorBearingChangedListener,

plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/LocationIndicatorLayerRenderer.kt

+11-14
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ import com.mapbox.bindgen.Value
55
import com.mapbox.geojson.Point
66
import com.mapbox.maps.extension.style.StyleInterface
77
import com.mapbox.maps.plugin.LocationPuck2D
8-
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.BEARING_ICON
9-
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER
10-
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.SHADOW_ICON
11-
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.TOP_ICON
128
import com.mapbox.maps.plugin.locationcomponent.utils.BitmapUtils
139
import java.text.DecimalFormat
1410
import java.text.NumberFormat
1511
import java.util.Locale
1612

1713
internal class LocationIndicatorLayerRenderer(
14+
private val locationComponentInitOptions: LocationComponentInitOptions,
1815
private val puckOptions: LocationPuck2D,
1916
layerSourceProvider: LayerSourceProvider
2017
) : LocationLayerRenderer {
@@ -28,7 +25,7 @@ internal class LocationIndicatorLayerRenderer(
2825
}
2926

3027
override fun isRendererInitialised(): Boolean {
31-
return style?.styleLayerExists(LOCATION_INDICATOR_LAYER) ?: false
28+
return style?.styleLayerExists(locationComponentInitOptions.puck2DLayerId) ?: false
3229
}
3330

3431
override fun addLayers(positionManager: LocationComponentPositionManager) {
@@ -76,22 +73,22 @@ internal class LocationIndicatorLayerRenderer(
7673

7774
private fun setupBitmaps() {
7875
puckOptions.topImage?.let { BitmapUtils.getBitmapFromDrawable(it) }
79-
?.let { style?.addImage(TOP_ICON, it) }
76+
?.let { style?.addImage(locationComponentInitOptions.topIconImageId, it) }
8077
puckOptions.bearingImage?.let { BitmapUtils.getBitmapFromDrawable(it) }
81-
?.let { style?.addImage(BEARING_ICON, it) }
78+
?.let { style?.addImage(locationComponentInitOptions.bearingIconImageId, it) }
8279

8380
puckOptions.shadowImage?.let { BitmapUtils.getBitmapFromDrawable(it) }
84-
?.let { style?.addImage(SHADOW_ICON, it) }
85-
layer.topImage(TOP_ICON)
86-
layer.bearingImage(BEARING_ICON)
87-
layer.shadowImage(SHADOW_ICON)
81+
?.let { style?.addImage(locationComponentInitOptions.shadowIconImageId, it) }
82+
layer.topImage(locationComponentInitOptions.topIconImageId)
83+
layer.bearingImage(locationComponentInitOptions.bearingIconImageId)
84+
layer.shadowImage(locationComponentInitOptions.shadowIconImageId)
8885
layer.opacity(puckOptions.opacity.toDouble())
8986
}
9087

9188
override fun clearBitmaps() {
92-
style?.removeStyleImage(TOP_ICON)
93-
style?.removeStyleImage(BEARING_ICON)
94-
style?.removeStyleImage(SHADOW_ICON)
89+
style?.removeStyleImage(locationComponentInitOptions.topIconImageId)
90+
style?.removeStyleImage(locationComponentInitOptions.bearingIconImageId)
91+
style?.removeStyleImage(locationComponentInitOptions.shadowIconImageId)
9592
}
9693

9794
override fun updateStyle(style: StyleInterface) {

plugin-locationcomponent/src/main/java/com/mapbox/maps/plugin/locationcomponent/ModelLayerRenderer.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import com.mapbox.bindgen.Value
66
import com.mapbox.geojson.Point
77
import com.mapbox.maps.extension.style.StyleInterface
88
import com.mapbox.maps.plugin.LocationPuck3D
9-
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER
10-
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_SOURCE
119

1210
internal class ModelLayerRenderer(
11+
private val locationComponentInitOptions: LocationComponentInitOptions,
1312
layerSourceProvider: LayerSourceProvider,
1413
private val locationModelLayerOptions: LocationPuck3D
1514
) : LocationLayerRenderer {
@@ -32,11 +31,11 @@ internal class ModelLayerRenderer(
3231
}
3332

3433
private fun isLayerInitialised(): Boolean {
35-
return style?.styleLayerExists(MODEL_LAYER) ?: false
34+
return style?.styleLayerExists(locationComponentInitOptions.puck3DLayerId) ?: false
3635
}
3736

3837
private fun isSourceInitialised(): Boolean {
39-
return style?.styleSourceExists(MODEL_SOURCE) ?: false
38+
return style?.styleSourceExists(locationComponentInitOptions.puck3DSourceId) ?: false
4039
}
4140

4241
override fun addLayers(positionManager: LocationComponentPositionManager) {

0 commit comments

Comments
 (0)