Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shields loading optimization #6826

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
package com.mapbox.navigation.ui.shield

import com.mapbox.navigation.ui.shield.internal.RoadShieldDownloader
import com.mapbox.navigation.ui.shield.internal.loader.CachedLoader
import com.mapbox.navigation.ui.shield.internal.loader.RoadShieldLoader
import com.mapbox.navigation.ui.shield.internal.loader.ShieldSpritesDownloader
import com.mapbox.navigation.ui.shield.internal.model.RouteShieldToDownload

/**
* Container for [RoadShieldContentManager] implementation which holds it in a static context,
* so that the same cache can be reused through the app processes life.
*/
internal object RoadShieldContentManagerContainer : RoadShieldContentManager {
private const val SPRITES_CACHE_SIZE = 8 // entries
private const val IMAGES_CACHE_SIZE = 40 // entries

private val contentManager: RoadShieldContentManager by lazy {
RoadShieldContentManagerImpl()
RoadShieldContentManagerImpl(
shieldLoader = CachedLoader(
IMAGES_CACHE_SIZE,
RoadShieldLoader(
spritesLoader = CachedLoader(
SPRITES_CACHE_SIZE,
ShieldSpritesDownloader()
),
imageLoader = { url ->
RoadShieldDownloader.download(url)
}
)
)
)
}

override suspend fun getShields(shieldsToDownload: List<RouteShieldToDownload>) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package com.mapbox.navigation.ui.shield

import com.mapbox.api.directions.v5.models.BannerComponents
import com.mapbox.bindgen.Expected
import com.mapbox.bindgen.ExpectedFactory
import com.mapbox.bindgen.ExpectedFactory.createError
import com.mapbox.bindgen.ExpectedFactory.createValue
import com.mapbox.navigation.ui.shield.internal.loader.Loader
import com.mapbox.navigation.ui.shield.internal.model.RouteShieldToDownload
import com.mapbox.navigation.ui.shield.model.RouteShield
import com.mapbox.navigation.ui.shield.model.RouteShieldError
import com.mapbox.navigation.ui.shield.model.RouteShieldOrigin
import com.mapbox.navigation.ui.shield.model.RouteShieldResult
import com.mapbox.navigation.utils.internal.InternalJobControlFactory
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import java.util.UUID
import java.util.*
import kotlin.coroutines.resume

/**
Expand Down Expand Up @@ -67,7 +70,7 @@ import kotlin.coroutines.resume
* - If request fails: repeat step 1.
*/
internal class RoadShieldContentManagerImpl(
private val shieldResultCache: ShieldResultCache = ShieldResultCache()
private val shieldLoader: Loader<RouteShieldToDownload, RouteShield>
) : RoadShieldContentManager {
internal companion object {
internal const val CANCELED_MESSAGE = "canceled"
Expand Down Expand Up @@ -95,7 +98,7 @@ internal class RoadShieldContentManagerImpl(
}
returnList.addAll(
missingResults.map {
ExpectedFactory.createError(
createError(
RouteShieldError(it.toDownload.url, CANCELED_MESSAGE)
)
}
Expand All @@ -116,90 +119,103 @@ internal class RoadShieldContentManagerImpl(
mainJob.scope.launch {
when (toDownload) {
is RouteShieldToDownload.MapboxDesign -> {
val mapboxDesignShieldResult = shieldResultCache.getOrRequest(toDownload)
resultMap[request] = if (mapboxDesignShieldResult.isError) {
val legacyFallback = toDownload.legacyFallback
if (legacyFallback != null) {
shieldResultCache.getOrRequest(legacyFallback).fold(
{ error ->
ExpectedFactory.createError(
RouteShieldError(
url = toDownload.url,
errorMessage = """
|original request failed with:
|url: ${toDownload.url}
|error: ${mapboxDesignShieldResult.error}
|
|fallback request failed with:
|url: ${legacyFallback.url}
|error: $error
""".trimMargin()
)
)
},
{ legacyShield ->
ExpectedFactory.createValue(
RouteShieldResult(
legacyShield,
RouteShieldOrigin(
isFallback = true,
originalUrl = toDownload.url,
mapboxDesignShieldResult.error!!
)
)
)
}
)
} else {
ExpectedFactory.createError(
RouteShieldError(
url = toDownload.url,
errorMessage = mapboxDesignShieldResult.error!!
)
resultMap[request] = loadDesignShield(toDownload)
}
is RouteShieldToDownload.MapboxLegacy -> {
resultMap[request] = loadLegacyShield(toDownload)
}
}
invalidate()
}
request
}.toSet()
}

private suspend fun loadDesignShield(
toDownload: RouteShieldToDownload.MapboxDesign
): Expected<RouteShieldError, RouteShieldResult> {
val mapboxDesignShieldResult = shieldLoader.load(toDownload)
return if (mapboxDesignShieldResult.isError) {
val legacyFallback = toDownload.legacyFallback
if (legacyFallback != null) {
shieldLoader.load(legacyFallback)
.fold(
{ error ->
createError(
RouteShieldError(
toDownload.url,
"""
|original request failed with:
|url: ${toDownload.url}
|error: ${mapboxDesignShieldResult.error?.localizedMessage}
|
|fallback request failed with:
|url: ${legacyFallback.url}
|error: ${error.localizedMessage}
""".trimMargin()
)
}
} else {
ExpectedFactory.createValue(
)
},
{ legacyShield ->
createValue(
RouteShieldResult(
mapboxDesignShieldResult.value!!,
legacyShield,
RouteShieldOrigin(
isFallback = false,
mapboxDesignShieldResult.value!!.url,
""
true,
toDownload.url,
mapboxDesignShieldResult.error?.message ?: ""
)
)
)
}
}
is RouteShieldToDownload.MapboxLegacy -> {
resultMap[request] = shieldResultCache.getOrRequest(toDownload).fold(
{ error ->
ExpectedFactory.createError(
RouteShieldError(
url = toDownload.url,
errorMessage = error
)
)
},
{ legacyShield ->
ExpectedFactory.createValue(
RouteShieldResult(
legacyShield,
RouteShieldOrigin(
isFallback = false,
originalUrl = toDownload.url,
originalErrorMessage = ""
)
)
)
}
)
} else {
createError(
RouteShieldError(
toDownload.url,
mapboxDesignShieldResult.error?.message ?: ""
)
)
}
} else {
createValue(
RouteShieldResult(
mapboxDesignShieldResult.value!!,
RouteShieldOrigin(
false,
mapboxDesignShieldResult.value!!.url,
""
)
)
)
}
}

private suspend fun loadLegacyShield(
toDownload: RouteShieldToDownload.MapboxLegacy
): Expected<RouteShieldError, RouteShieldResult> {
return shieldLoader.load(toDownload).fold(
{ error ->
createError(
RouteShieldError(
toDownload.url,
error.message ?: ""
)
)
},
{ legacyShield ->
createValue(
RouteShieldResult(
legacyShield,
RouteShieldOrigin(
false,
toDownload.url,
""
)
}
}
invalidate()
)
)
}
request
}.toSet()
)
}

private fun invalidate() {
Expand Down
Loading