@@ -348,6 +348,9 @@ class MapboxRouteLineView @VisibleForTesting internal constructor(
348348 val featuresNotOnMap = routeLineDatas.map {
349349 RouteLineFeatureId (it.featureCollection.features()?.firstOrNull()?.id())
350350 }.filter { it !in sourceToFeatureMap.values }
351+ // If the incoming feature IDs are already in the map. That is, they're in the
352+ // sourceToFeatureMap we don't need to update them. `sourcesToUpdate` will contain
353+ // the sources that are available to be replaced for the new features.
351354 val sourcesToUpdate = sourceToFeatureMap.filter {
352355 it.value.id() !in incomingFeatureIds
353356 }
@@ -361,17 +364,24 @@ class MapboxRouteLineView @VisibleForTesting internal constructor(
361364 it.featureCollection.features()?.firstOrNull()?.id() ==
362365 nextFeatureId?.id()
363366 }
364- val fc : FeatureCollection =
365- nextRouteLineData?.featureCollection
366- ? : FeatureCollection .fromFeatures(listOf ())
367+ // Either we get the next route line data or empty if there are no more.
368+ val fc : FeatureCollection = nextRouteLineData?.featureCollection
369+ ? : FeatureCollection .fromFeatures(listOf ())
367370 val sourceId = sourceToUpdate.key.sourceId
368371 val routeId = nextFeatureId?.id()
369372 val dataId = dataIdHolder.incrementDataId(sourceId)
373+
374+ // We don't really clear the source but rather replace its data with the new
375+ // feature collection `fc` (which can be an empty).
376+ // We add it to the cleared route list if the previous feature collection
377+ // (`sourceToFeatureMap[sourceToUpdate.key]?.id()`) is not null.
370378 expectedRoutesData.addClearedRoute(
371379 sourceId,
372380 dataId,
373381 sourceToFeatureMap[sourceToUpdate.key]?.id(),
374382 )
383+ // Finally add the new route to the rendered route list and create the command
384+ // to update the source (which can be empty geometry).
375385 expectedRoutesData.addRenderedRoute(sourceId, dataId, routeId)
376386 updateSourceCommands.add {
377387 updateSource(style, sourceToUpdate.key.sourceId, fc, dataId)
@@ -740,24 +750,25 @@ class MapboxRouteLineView @VisibleForTesting internal constructor(
740750 logE(TAG , error.message)
741751 null
742752 },
743- )?.also {
744- val newDataId = dataIdHolder.incrementDataId(it.sourceId)
753+ )?.also { primaryRouteLineSourceKey ->
754+ val primarySourceId = primaryRouteLineSourceKey.sourceId
755+ val newDataId = dataIdHolder.incrementDataId(primarySourceId)
745756 val routeId = value.primaryRouteSource.features()?.firstOrNull()?.id()
746757 expectedRoutesData.addClearedRoute(
747- it.sourceId ,
758+ primarySourceId ,
748759 newDataId,
749- sourceToFeatureMap[it ]?.id(),
760+ sourceToFeatureMap[primaryRouteLineSourceKey ]?.id(),
750761 )
751- expectedRoutesData.addRenderedRoute(it.sourceId , newDataId, routeId)
762+ expectedRoutesData.addRenderedRoute(primarySourceId , newDataId, routeId)
752763 updateSourceCommands.add {
753764 updateSource(
754765 style,
755- it.sourceId ,
766+ primarySourceId ,
756767 value.primaryRouteSource,
757768 newDataId,
758769 )
759770 }
760- sourceToFeatureMap[it ] = RouteLineFeatureId (routeId)
771+ sourceToFeatureMap[primaryRouteLineSourceKey ] = RouteLineFeatureId (routeId)
761772 }
762773 sourceLayerMap.keys.filter { it != primarySourceKey }
763774 .forEachIndexed { index, routeLineSourceKey ->
0 commit comments