loadTileAsync refactor
loadTileAsync consumes all tileIds
This commit is contained in:
parent
fc0f223766
commit
5d3db81c4f
@ -2,7 +2,6 @@ package centre.sciprog.maps.compose
|
|||||||
|
|
||||||
import androidx.compose.ui.graphics.ImageBitmap
|
import androidx.compose.ui.graphics.ImageBitmap
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Deferred
|
|
||||||
import kotlin.math.floor
|
import kotlin.math.floor
|
||||||
|
|
||||||
data class TileId(
|
data class TileId(
|
||||||
@ -17,7 +16,7 @@ data class MapTile(
|
|||||||
)
|
)
|
||||||
|
|
||||||
interface MapTileProvider {
|
interface MapTileProvider {
|
||||||
suspend fun loadTileAsync(id: TileId, scope: CoroutineScope): Deferred<MapTile>
|
suspend fun loadTileAsync(tileIds: List<TileId>, scope: CoroutineScope, onTileLoad: (mapTile: MapTile) -> Unit)
|
||||||
|
|
||||||
val tileSize: Int get() = DEFAULT_TILE_SIZE
|
val tileSize: Int get() = DEFAULT_TILE_SIZE
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ import androidx.compose.ui.graphics.toArgb
|
|||||||
import androidx.compose.ui.input.pointer.*
|
import androidx.compose.ui.input.pointer.*
|
||||||
import androidx.compose.ui.unit.*
|
import androidx.compose.ui.unit.*
|
||||||
import centre.sciprog.maps.*
|
import centre.sciprog.maps.*
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import mu.KotlinLogging
|
import mu.KotlinLogging
|
||||||
import org.jetbrains.skia.Font
|
import org.jetbrains.skia.Font
|
||||||
import org.jetbrains.skia.Paint
|
import org.jetbrains.skia.Paint
|
||||||
@ -46,7 +45,8 @@ actual fun MapView(
|
|||||||
) {
|
) {
|
||||||
var canvasSize by remember { mutableStateOf(DpSize(512.dp, 512.dp)) }
|
var canvasSize by remember { mutableStateOf(DpSize(512.dp, 512.dp)) }
|
||||||
|
|
||||||
var viewPointOverride by remember { mutableStateOf<MapViewPoint?>(
|
var viewPointOverride by remember {
|
||||||
|
mutableStateOf<MapViewPoint?>(
|
||||||
if (config.inferViewBoxFromFeatures) {
|
if (config.inferViewBoxFromFeatures) {
|
||||||
features.values.computeBoundingBox(1)?.let { box ->
|
features.values.computeBoundingBox(1)?.let { box ->
|
||||||
val zoom = log2(
|
val zoom = log2(
|
||||||
@ -60,7 +60,8 @@ actual fun MapView(
|
|||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
) }
|
)
|
||||||
|
}
|
||||||
|
|
||||||
val viewPoint by derivedStateOf { viewPointOverride ?: computeViewPoint(canvasSize) }
|
val viewPoint by derivedStateOf { viewPointOverride ?: computeViewPoint(canvasSize) }
|
||||||
|
|
||||||
@ -163,21 +164,17 @@ actual fun MapView(
|
|||||||
|
|
||||||
mapTiles.clear()
|
mapTiles.clear()
|
||||||
|
|
||||||
verticalIndices
|
val tileIds = verticalIndices
|
||||||
.flatMap { j ->
|
.flatMap { j ->
|
||||||
horizontalIndices
|
horizontalIndices
|
||||||
.asSequence()
|
.asSequence()
|
||||||
.map { TileId(zoom, it, j) }
|
.map { TileId(zoom, it, j) }
|
||||||
}
|
}
|
||||||
.forEach {
|
|
||||||
try {
|
mapTileProvider.loadTileAsync(
|
||||||
launch {
|
tileIds = tileIds,
|
||||||
mapTiles += mapTileProvider.loadTileAsync(it, this).await()
|
scope = this
|
||||||
}
|
) { mapTiles += it }
|
||||||
} catch (ex: Exception) {
|
|
||||||
logger.error(ex) { "Failed to load tile $it" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,10 +6,7 @@ import centre.sciprog.maps.LruCache
|
|||||||
import io.ktor.client.*
|
import io.ktor.client.*
|
||||||
import io.ktor.client.request.*
|
import io.ktor.client.request.*
|
||||||
import io.ktor.client.statement.*
|
import io.ktor.client.statement.*
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.Deferred
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.async
|
|
||||||
import kotlinx.coroutines.sync.Semaphore
|
import kotlinx.coroutines.sync.Semaphore
|
||||||
import mu.KotlinLogging
|
import mu.KotlinLogging
|
||||||
import org.jetbrains.skia.Image
|
import org.jetbrains.skia.Image
|
||||||
@ -63,11 +60,20 @@ class OpenStreetMapTileProvider(
|
|||||||
Image.makeFromEncoded(byteArray).toComposeImageBitmap()
|
Image.makeFromEncoded(byteArray).toComposeImageBitmap()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun loadTileAsync(id: TileId, scope: CoroutineScope) = scope.async {
|
override suspend fun loadTileAsync(
|
||||||
|
tileIds: List<TileId>,
|
||||||
|
scope: CoroutineScope,
|
||||||
|
onTileLoad: (mapTile: MapTile) -> Unit,
|
||||||
|
) {
|
||||||
|
tileIds
|
||||||
|
.forEach { id ->
|
||||||
|
try {
|
||||||
|
scope.launch {
|
||||||
semaphore.acquire()
|
semaphore.acquire()
|
||||||
try {
|
try {
|
||||||
val image = cache.getOrPut(id) { downloadImageAsync(id) }
|
val image = cache.getOrPut(id) { downloadImageAsync(id) }
|
||||||
MapTile(id, image.await())
|
val result = MapTile(id, image.await())
|
||||||
|
onTileLoad(result)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
cache.remove(id)
|
cache.remove(id)
|
||||||
throw e
|
throw e
|
||||||
@ -75,6 +81,11 @@ class OpenStreetMapTileProvider(
|
|||||||
semaphore.release()
|
semaphore.release()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (ex: Exception) {
|
||||||
|
logger.error(ex) { "Failed to load tile $id" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val logger = KotlinLogging.logger("OpenStreetMapCache")
|
private val logger = KotlinLogging.logger("OpenStreetMapCache")
|
||||||
|
Loading…
Reference in New Issue
Block a user