Skip to content

Commit

Permalink
[annotation] Initialise annotations and location component immediatel…
Browse files Browse the repository at this point in the history
…y instead of waiting for style load. (#2331)
  • Loading branch information
pengdev committed Mar 27, 2024
1 parent 70be826 commit 6160749
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 174 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Mapbox welcomes participation and contributions from everyone.
## Bug fixes 🐞
* Return `ViewAnnotationOptions.Builder` when calling `ViewAnnotationOptions.Builder.annotationAnchor` extension function.
* [compose] Fix the `IndexOutOfBoundsException` because of `RootNode` of `MapboxMap` node tree being shared across multiple maps.
* Immediately add annotations and location component to the map instead of waiting for style load events.

# 11.3.0-beta.1 March 14, 2024
## Breaking changes ⚠️
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import com.mapbox.geojson.Feature
import com.mapbox.geojson.FeatureCollection
import com.mapbox.geojson.Geometry
import com.mapbox.geojson.Point
import com.mapbox.maps.*
import com.mapbox.maps.LayerPosition
import com.mapbox.maps.MapboxStyleManager
import com.mapbox.maps.RenderedQueryGeometry
import com.mapbox.maps.RenderedQueryOptions
import com.mapbox.maps.ScreenCoordinate
import com.mapbox.maps.extension.style.expressions.dsl.generated.literal
import com.mapbox.maps.extension.style.expressions.generated.Expression
import com.mapbox.maps.extension.style.expressions.generated.Expression.Companion.all
Expand All @@ -27,18 +31,22 @@ import com.mapbox.maps.extension.style.layers.generated.symbolLayer
import com.mapbox.maps.extension.style.sources.addSource
import com.mapbox.maps.extension.style.sources.generated.GeoJsonSource
import com.mapbox.maps.extension.style.sources.generated.geoJsonSource
import com.mapbox.maps.logE
import com.mapbox.maps.logW
import com.mapbox.maps.plugin.InvalidPluginConfigurationException
import com.mapbox.maps.plugin.Plugin.Companion.MAPBOX_GESTURES_PLUGIN_ID
import com.mapbox.maps.plugin.annotation.generated.PointAnnotation
import com.mapbox.maps.plugin.delegates.*
import com.mapbox.maps.plugin.delegates.MapCameraManagerDelegate
import com.mapbox.maps.plugin.delegates.MapDelegateProvider
import com.mapbox.maps.plugin.delegates.MapFeatureQueryDelegate
import com.mapbox.maps.plugin.delegates.MapListenerDelegate
import com.mapbox.maps.plugin.gestures.GesturesPlugin
import com.mapbox.maps.plugin.gestures.OnMapClickListener
import com.mapbox.maps.plugin.gestures.OnMapLongClickListener
import com.mapbox.maps.plugin.gestures.OnMoveListener
import java.util.*
import java.util.UUID
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import kotlin.collections.LinkedHashMap

/**
* Base class for annotation managers
Expand Down Expand Up @@ -387,20 +395,19 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
}

private fun updateDragSource() {
delegateProvider.getStyle { style ->
dragSource?.let { geoJsonSource ->
dragLayer?.let { layer ->
if (!style.styleSourceExists(geoJsonSource.sourceId) || !style.styleLayerExists(layer.layerId)) {
logE(
TAG,
"Can't update dragSource: drag source or layer has not been added to style."
)
return@getStyle
}
addIconToStyle(style, dragAnnotationMap.values)
val features = convertAnnotationsToFeatures(dragAnnotationMap.values)
geoJsonSource.featureCollection(FeatureCollection.fromFeatures(features))
val style = delegateProvider.mapStyleManagerDelegate
dragSource?.let { geoJsonSource ->
dragLayer?.let { layer ->
if (!style.styleSourceExists(geoJsonSource.sourceId) || !style.styleLayerExists(layer.layerId)) {
logE(
TAG,
"Can't update dragSource: drag source or layer has not been added to style."
)
return
}
addIconToStyle(style, dragAnnotationMap.values)
val features = convertAnnotationsToFeatures(dragAnnotationMap.values)
geoJsonSource.featureCollection(FeatureCollection.fromFeatures(features))
}
}
}
Expand All @@ -409,20 +416,19 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
* Trigger an update to the underlying source
*/
private fun updateSource() {
delegateProvider.getStyle { style ->
if (source == null || layer == null) {
initLayerAndSource(style)
}
source?.let { geoJsonSource ->
layer?.let { layer ->
if (!style.styleSourceExists(geoJsonSource.sourceId) || !style.styleLayerExists(layer.layerId)) {
logE(TAG, "Can't update source: source or layer has not been added to style.")
return@getStyle
}
addIconToStyle(style, annotationMap.values)
val features = convertAnnotationsToFeatures(annotationMap.values)
geoJsonSource.featureCollection(FeatureCollection.fromFeatures(features))
val style = delegateProvider.mapStyleManagerDelegate
if (source == null || layer == null) {
initLayerAndSource(style)
}
source?.let { geoJsonSource ->
layer?.let { layer ->
if (!style.styleSourceExists(geoJsonSource.sourceId) || !style.styleLayerExists(layer.layerId)) {
logE(TAG, "Can't update source: source or layer has not been added to style.")
return
}
addIconToStyle(style, annotationMap.values)
val features = convertAnnotationsToFeatures(annotationMap.values)
geoJsonSource.featureCollection(FeatureCollection.fromFeatures(features))
}
}
}
Expand Down Expand Up @@ -508,26 +514,25 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
* Invoked when Mapview or Annotation manager is destroyed.
*/
override fun onDestroy() {
delegateProvider.getStyle { style ->
layer?.let {
if (style.styleLayerExists(it.layerId)) {
style.removeStyleLayer(it.layerId)
}
val style = delegateProvider.mapStyleManagerDelegate
layer?.let {
if (style.styleLayerExists(it.layerId)) {
style.removeStyleLayer(it.layerId)
}
source?.let {
if (style.styleSourceExists(it.sourceId)) {
style.removeStyleSource(it.sourceId)
}
}
source?.let {
if (style.styleSourceExists(it.sourceId)) {
style.removeStyleSource(it.sourceId)
}
dragLayer?.let {
if (style.styleLayerExists(it.layerId)) {
style.removeStyleLayer(it.layerId)
}
}
dragLayer?.let {
if (style.styleLayerExists(it.layerId)) {
style.removeStyleLayer(it.layerId)
}
dragSource?.let {
if (style.styleSourceExists(it.sourceId)) {
style.removeStyleSource(it.sourceId)
}
}
dragSource?.let {
if (style.styleSourceExists(it.sourceId)) {
style.removeStyleSource(it.sourceId)
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,32 @@ import com.mapbox.bindgen.ExpectedFactory
import com.mapbox.maps.MapboxStyleManager
import com.mapbox.maps.extension.style.layers.addLayer
import com.mapbox.maps.extension.style.sources.addSource
import com.mapbox.maps.logE
import com.mapbox.maps.logW
import com.mapbox.maps.plugin.Plugin
import com.mapbox.maps.plugin.annotation.generated.CircleAnnotationManager
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager
import com.mapbox.maps.plugin.annotation.generated.PolygonAnnotationManager
import com.mapbox.maps.plugin.annotation.generated.PolylineAnnotationManager
import com.mapbox.maps.plugin.delegates.MapDelegateProvider
import com.mapbox.maps.plugin.gestures.GesturesPlugin
import io.mockk.*
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.unmockkStatic
import junit.framework.Assert.assertEquals
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class AnnotationPluginImplTest {
private val delegateProvider: MapDelegateProvider = mockk(relaxed = true)
private val delegateProvider: MapDelegateProvider = mockk()
private val style: MapboxStyleManager = mockk(relaxed = true)
private val gesturesPlugin: GesturesPlugin = mockk(relaxed = true)
private lateinit var annotationPluginImpl: AnnotationPluginImpl
Expand All @@ -29,18 +38,34 @@ class AnnotationPluginImplTest {
fun setUp() {
mockkStatic("com.mapbox.maps.extension.style.layers.LayerUtils")
mockkStatic("com.mapbox.maps.extension.style.sources.SourceUtils")
mockkStatic("com.mapbox.maps.MapboxLogger")
every { logW(any(), any()) } just Runs
every { logE(any(), any()) } just Runs

every { style.addSource(any()) } just Runs
every { style.addLayer(any()) } just Runs
every { style.addPersistentStyleLayer(any(), any()) } returns ExpectedFactory.createNone()
every { style.styleSourceExists(any()) } returns false
every { style.styleLayerExists(any()) } returns false
every { style.setStyleLayerProperty(any(), any(), any()) } returns ExpectedFactory.createNone()
every { delegateProvider.mapPluginProviderDelegate.getPlugin<GesturesPlugin>(Plugin.MAPBOX_GESTURES_PLUGIN_ID) } returns gesturesPlugin
every { delegateProvider.mapStyleManagerDelegate } returns style
every { delegateProvider.mapCameraManagerDelegate } returns mockk()
every { delegateProvider.mapFeatureQueryDelegate } returns mockk()
every { delegateProvider.mapListenerDelegate } returns mockk()

annotationPluginImpl = AnnotationPluginImpl()
annotationPluginImpl.onDelegateProvider(delegateProvider)
}

@After
fun cleanUp() {
unmockkStatic("com.mapbox.maps.extension.style.layers.LayerUtils")
unmockkStatic("com.mapbox.maps.extension.style.sources.SourceUtils")
unmockkStatic("com.mapbox.maps.MapboxLogger")
unmockkAll()
}

@Test
fun createAndRemove() {
val circleAnnotationManager =
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6160749

Please sign in to comment.