Skip to content

Commit 14a7396

Browse files
Seth BourgetSeth Bourget
authored andcommitted
Modified the initialize layers behavior to always apply the current options even if it means removing the existing layers.
1 parent 09b5248 commit 14a7396

File tree

5 files changed

+378
-3
lines changed

5 files changed

+378
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Updated behavior of `MapboxRouteLineView::initializeLayers` to always reset the route line related layers with the options provided even if it means removing existing layers in order to apply the options. Also new instances of `MapboxRouteLineView` will result in an initialization of the layers upon the first render call.

libnavui-maps/src/main/java/com/mapbox/navigation/ui/maps/internal/route/line/MapboxRouteLineUtils.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,4 +1751,31 @@ internal object MapboxRouteLineUtils {
17511751
.iconIgnorePlacement(true)
17521752
.iconKeepUpright(true)
17531753
}
1754+
1755+
internal fun removeLayersAndSources(style: Style) {
1756+
style.removeStyleSource(RouteLayerConstants.LAYER_GROUP_1_SOURCE_ID)
1757+
style.removeStyleSource(RouteLayerConstants.LAYER_GROUP_2_SOURCE_ID)
1758+
style.removeStyleSource(RouteLayerConstants.LAYER_GROUP_3_SOURCE_ID)
1759+
style.removeStyleSource(RouteLayerConstants.WAYPOINT_SOURCE_ID)
1760+
style.removeStyleLayer(RouteLayerConstants.TOP_LEVEL_ROUTE_LINE_LAYER_ID)
1761+
style.removeStyleLayer(RouteLayerConstants.BOTTOM_LEVEL_ROUTE_LINE_LAYER_ID)
1762+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_1_TRAIL_CASING)
1763+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_1_TRAIL)
1764+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_1_CASING)
1765+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_1_MAIN)
1766+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_1_TRAFFIC)
1767+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_1_RESTRICTED)
1768+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_2_TRAIL_CASING)
1769+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_2_TRAIL)
1770+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_2_CASING)
1771+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_2_MAIN)
1772+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_2_TRAFFIC)
1773+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_2_RESTRICTED)
1774+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_3_TRAIL_CASING)
1775+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_3_TRAIL)
1776+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_3_CASING)
1777+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_3_MAIN)
1778+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_3_TRAFFIC)
1779+
style.removeStyleLayer(RouteLayerConstants.LAYER_GROUP_3_RESTRICTED)
1780+
}
17541781
}

libnavui-maps/src/main/java/com/mapbox/navigation/ui/maps/route/line/api/MapboxRouteLineView.kt

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.ge
1818
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layerGroup1SourceLayerIds
1919
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layerGroup2SourceLayerIds
2020
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layerGroup3SourceLayerIds
21+
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.layersAreInitialized
2122
import com.mapbox.navigation.ui.maps.internal.route.line.MapboxRouteLineUtils.sourceLayerMap
2223
import com.mapbox.navigation.ui.maps.route.RouteLayerConstants
2324
import com.mapbox.navigation.ui.maps.route.RouteLayerConstants.LAYER_GROUP_1_CASING
@@ -78,6 +79,8 @@ import org.jetbrains.annotations.TestOnly
7879
@UiThread
7980
class MapboxRouteLineView(options: MapboxRouteLineOptions) {
8081

82+
private var rebuildLayersOnFirstRender: Boolean = true
83+
8184
private companion object {
8285
private const val TAG = "MbxRouteLineView"
8386
}
@@ -142,14 +145,32 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
142145
* the layers if they have not yet been initialized. If you have a use case for initializing
143146
* the layers in advance of any API calls this method may be used.
144147
*
148+
* If the layers already exist they will be removed and re-initialized with the options provided.
149+
*
145150
* Each [Layer] added to the map by this class is a persistent layer - it will survive style changes.
146151
* This means that if the data has not changed, it does not have to be manually redrawn after a style change.
147152
* See [Style.addPersistentStyleLayer].
148153
*
149154
* @param style a valid [Style] instance
150155
*/
151156
fun initializeLayers(style: Style) {
152-
MapboxRouteLineUtils.initializeLayers(style, options)
157+
resetLayers(style)
158+
}
159+
160+
/**
161+
* Updates the [MapboxRouteLineOptions] used for the route line related layers and initializes the layers.
162+
* If the layers already exist they will be removed and re-initialized with the options provided.
163+
*
164+
* Each [Layer] added to the map by this class is a persistent layer - it will survive style changes.
165+
* This means that if the data has not changed, it does not have to be manually redrawn after a style change.
166+
* See [Style.addPersistentStyleLayer].
167+
*
168+
* @param style a valid [Style] instance
169+
* @param options used for the route line related layers.
170+
*/
171+
fun initializeLayers(style: Style, options: MapboxRouteLineOptions) {
172+
this.options = options
173+
resetLayers(style)
153174
}
154175

155176
/**
@@ -159,7 +180,7 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
159180
* @param routeDrawData a [Expected<RouteLineError, RouteSetValue>]
160181
*/
161182
fun renderRouteDrawData(style: Style, routeDrawData: Expected<RouteLineError, RouteSetValue>) {
162-
MapboxRouteLineUtils.initializeLayers(style, options)
183+
rebuildSourcesAndLayersIfNeeded(style)
163184
jobControl.scope.launch(Dispatchers.Main) {
164185
mutex.withLock {
165186
val primaryRouteTrafficVisibility = getTrafficVisibility(style)
@@ -332,7 +353,7 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
332353
style: Style,
333354
clearRouteLineValue: Expected<RouteLineError, RouteLineClearValue>
334355
) {
335-
MapboxRouteLineUtils.initializeLayers(style, options)
356+
rebuildSourcesAndLayersIfNeeded(style)
336357
jobControl.scope.launch(Dispatchers.Main) {
337358
mutex.withLock {
338359
clearRouteLineValue.onValue { value ->
@@ -862,4 +883,33 @@ class MapboxRouteLineView(options: MapboxRouteLineOptions) {
862883
}
863884
}
864885
}
886+
887+
private fun rebuildSourcesAndLayersIfNeeded(style: Style) {
888+
if (rebuildLayersOnFirstRender || !layersAreInitialized(style, options)) {
889+
rebuildLayersOnFirstRender = false
890+
resetLayers(style)
891+
}
892+
}
893+
894+
private fun resetLayers(style: Style) {
895+
sourceToFeatureMap.clear()
896+
sourceToFeatureMap[MapboxRouteLineUtils.layerGroup1SourceKey] = RouteLineFeatureId(null)
897+
sourceToFeatureMap[MapboxRouteLineUtils.layerGroup2SourceKey] = RouteLineFeatureId(null)
898+
sourceToFeatureMap[MapboxRouteLineUtils.layerGroup3SourceKey] = RouteLineFeatureId(null)
899+
primaryRouteLineLayerGroup = setOf()
900+
listOf(
901+
RouteLayerConstants.LAYER_GROUP_1_SOURCE_ID,
902+
RouteLayerConstants.LAYER_GROUP_2_SOURCE_ID,
903+
RouteLayerConstants.LAYER_GROUP_3_SOURCE_ID,
904+
RouteLayerConstants.WAYPOINT_SOURCE_ID
905+
).forEach {
906+
updateSource(
907+
style,
908+
it,
909+
FeatureCollection.fromFeatures(listOf())
910+
)
911+
}
912+
MapboxRouteLineUtils.removeLayersAndSources(style)
913+
MapboxRouteLineUtils.initializeLayers(style, options)
914+
}
865915
}

libnavui-maps/src/test/java/com/mapbox/navigation/ui/maps/internal/route/line/MapboxRouteLineUtilsTest.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,41 @@ class MapboxRouteLineUtilsTest {
20282028
assertFalse(result)
20292029
}
20302030

2031+
@Test
2032+
fun removeLayersAndSources() {
2033+
val style = mockk<Style> {
2034+
every { removeStyleLayer(any()) } returns ExpectedFactory.createNone()
2035+
every { removeStyleSource(any()) } returns ExpectedFactory.createNone()
2036+
}
2037+
2038+
MapboxRouteLineUtils.removeLayersAndSources(style)
2039+
2040+
verify { style.removeStyleSource(LAYER_GROUP_1_SOURCE_ID) }
2041+
verify { style.removeStyleSource(LAYER_GROUP_2_SOURCE_ID) }
2042+
verify { style.removeStyleSource(LAYER_GROUP_3_SOURCE_ID) }
2043+
verify { style.removeStyleSource(WAYPOINT_SOURCE_ID) }
2044+
verify { style.removeStyleLayer(TOP_LEVEL_ROUTE_LINE_LAYER_ID) }
2045+
verify { style.removeStyleLayer(BOTTOM_LEVEL_ROUTE_LINE_LAYER_ID) }
2046+
verify { style.removeStyleLayer(LAYER_GROUP_1_TRAIL_CASING) }
2047+
verify { style.removeStyleLayer(LAYER_GROUP_1_TRAIL) }
2048+
verify { style.removeStyleLayer(LAYER_GROUP_1_CASING) }
2049+
verify { style.removeStyleLayer(LAYER_GROUP_1_MAIN) }
2050+
verify { style.removeStyleLayer(LAYER_GROUP_1_TRAFFIC) }
2051+
verify { style.removeStyleLayer(LAYER_GROUP_1_RESTRICTED) }
2052+
verify { style.removeStyleLayer(LAYER_GROUP_2_TRAIL_CASING) }
2053+
verify { style.removeStyleLayer(LAYER_GROUP_2_TRAIL) }
2054+
verify { style.removeStyleLayer(LAYER_GROUP_2_CASING) }
2055+
verify { style.removeStyleLayer(LAYER_GROUP_2_MAIN) }
2056+
verify { style.removeStyleLayer(LAYER_GROUP_2_TRAFFIC) }
2057+
verify { style.removeStyleLayer(LAYER_GROUP_2_RESTRICTED) }
2058+
verify { style.removeStyleLayer(LAYER_GROUP_3_TRAIL_CASING) }
2059+
verify { style.removeStyleLayer(LAYER_GROUP_3_TRAIL) }
2060+
verify { style.removeStyleLayer(LAYER_GROUP_3_CASING) }
2061+
verify { style.removeStyleLayer(LAYER_GROUP_3_MAIN) }
2062+
verify { style.removeStyleLayer(LAYER_GROUP_3_TRAFFIC) }
2063+
verify { style.removeStyleLayer(LAYER_GROUP_3_RESTRICTED) }
2064+
}
2065+
20312066
private fun <T> listElementsAreEqual(
20322067
first: List<T>,
20332068
second: List<T>,

0 commit comments

Comments
 (0)