Skip to content

Commit

Permalink
fix(andorid): resumePackDownload should return error to JS instead of… (
Browse files Browse the repository at this point in the history
#3361)

* fix(andorid): resumePackDownload should return error to JS instead of throwing a Java exception

* chore: fix ci
  • Loading branch information
mfazekas authored Feb 7, 2024
1 parent 3a285c2 commit b512d01
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 76 deletions.
159 changes: 87 additions & 72 deletions android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXOfflineModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,9 @@ class RNMBXOfflineModule(private val mReactContext: ReactApplicationContext) :
metadata = metadata
)
tileRegionPacks[id] = actPack
startLoading(pack = actPack)
promise.resolve(
startLoading(pack = actPack).map {
writableMapOf("bounds" to boundsStr, "metadata" to metadataStr)
)
}.toPromise(promise, "createPack")
} catch (e: Throwable) {
promise.reject("createPack", e)
}
Expand Down Expand Up @@ -196,8 +195,7 @@ class RNMBXOfflineModule(private val mReactContext: ReactApplicationContext) :
fun resumePackDownload(name: String, promise: Promise) {
val pack = tileRegionPacks[name]
if (pack != null) {
startLoading(pack)
promise.resolve(null)
startLoading(pack).map { null }.toPromise(promise,"resumePackDownload")
} else {
promise.reject("resumePackDownload", "Unknown offline pack: $name")
}
Expand Down Expand Up @@ -271,74 +269,83 @@ class RNMBXOfflineModule(private val mReactContext: ReactApplicationContext) :
}
// endregion

fun startLoading(pack: TileRegionPack) {
val id = pack.name
val bounds = pack.bounds
if (bounds == null) {
throw IllegalArgumentException("startLoading failed as there are no bounds in pack")
}
val zoomRange = pack.zoomRange
if (zoomRange == null) {
throw IllegalArgumentException("startLoading failed as there is no zoomRange in pack")
}
val styleURI = pack.styleURI
if (styleURI == null) {
throw IllegalArgumentException("startLoading failed as there is no styleURI in pack")
}
val metadata = pack.metadata
if (metadata == null) {
throw IllegalArgumentException("startLoading failed as there is no metadata in pack")
}
val stylePackOptions = StylePackLoadOptions.Builder()
.glyphsRasterizationMode(GlyphsRasterizationMode.IDEOGRAPHS_RASTERIZED_LOCALLY)
.metadata(metadata.toMapboxValue())
.build()

val descriptorOptions = TilesetDescriptorOptions.Builder()
.styleURI(styleURI)
.minZoom(zoomRange.minZoom)
.maxZoom(zoomRange.maxZoom)
.stylePackOptions(stylePackOptions)
.pixelRatio(2.0f)
.build()
val tilesetDescriptor = offlineManager.createTilesetDescriptor(descriptorOptions)

val loadOptions = TileRegionLoadOptions.Builder()
.geometry(bounds)
.descriptors(arrayListOf(tilesetDescriptor))
.metadata(metadata.toMapboxValue())
.acceptExpired(true)
.networkRestriction(NetworkRestriction.NONE)
.averageBytesPerSecond(null)
.build()

var lastProgress: TileRegionLoadProgress? = null
val task = this.tileStore.loadTileRegion(
id, loadOptions,
{ progress ->
lastProgress = progress
tileRegionPacks[id]!!.progress = progress
tileRegionPacks[id]!!.state = TileRegionPackState.ACTIVE

offlinePackProgressDidChange(progress, metadata, TileRegionPackState.ACTIVE)
}, { expected ->
expected.value?.also {
val progress = lastProgress
if (progress != null) {
offlinePackProgressDidChange(progress, metadata, TileRegionPackState.COMPLETE)
} else {
Logger.w(LOG_TAG, "startLoading: tile region completed, but got no progress information")
}
tileRegionPacks[id]!!.state = TileRegionPackState.COMPLETE
} ?: run {
val error = expected.error ?: TileRegionError(TileRegionErrorType.OTHER, "$LOG_TAG neither value nor error in expected")
fun startLoading(pack: TileRegionPack): Result<Unit> {
try {
val id = pack.name
val bounds = pack.bounds
?: return Result.failure(IllegalArgumentException("startLoading failed as there are no bounds in pack"))
val zoomRange = pack.zoomRange
?: return Result.failure(IllegalArgumentException("startLoading failed as there is no zoomRange in pack"))
val styleURI = pack.styleURI
?: return Result.failure(IllegalArgumentException("startLoading failed as there is no styleURI in pack"))
val metadata = pack.metadata
?: return Result.failure(IllegalArgumentException("startLoading failed as there is no metadata in pack"))

val stylePackOptions = StylePackLoadOptions.Builder()
.glyphsRasterizationMode(GlyphsRasterizationMode.IDEOGRAPHS_RASTERIZED_LOCALLY)
.metadata(metadata.toMapboxValue())
.build()

val descriptorOptions = TilesetDescriptorOptions.Builder()
.styleURI(styleURI)
.minZoom(zoomRange.minZoom)
.maxZoom(zoomRange.maxZoom)
.stylePackOptions(stylePackOptions)
.pixelRatio(2.0f)
.build()
val tilesetDescriptor = offlineManager.createTilesetDescriptor(descriptorOptions)

val loadOptions = TileRegionLoadOptions.Builder()
.geometry(bounds)
.descriptors(arrayListOf(tilesetDescriptor))
.metadata(metadata.toMapboxValue())
.acceptExpired(true)
.networkRestriction(NetworkRestriction.NONE)
.averageBytesPerSecond(null)
.build()

var lastProgress: TileRegionLoadProgress? = null
val task = this.tileStore.loadTileRegion(
id, loadOptions,
{ progress ->
lastProgress = progress
tileRegionPacks[id]!!.progress = progress
tileRegionPacks[id]!!.state = TileRegionPackState.ACTIVE

offlinePackProgressDidChange(progress, metadata, TileRegionPackState.ACTIVE)
},
{ expected ->
expected.value?.also {
val progress = lastProgress
if (progress != null) {
offlinePackProgressDidChange(
progress,
metadata,
TileRegionPackState.COMPLETE
)
} else {
Logger.w(
LOG_TAG,
"startLoading: tile region completed, but got no progress information"
)
}
tileRegionPacks[id]!!.state = TileRegionPackState.COMPLETE
} ?: run {
val error = expected.error ?: TileRegionError(
TileRegionErrorType.OTHER,
"$LOG_TAG neither value nor error in expected"
)

tileRegionPacks[id]!!.state = TileRegionPackState.INACTIVE
offlinePackDidReceiveError(name=id, error=error)
}
},
)
tileRegionPacks[id]!!.cancelable = task
tileRegionPacks[id]!!.state = TileRegionPackState.INACTIVE
offlinePackDidReceiveError(name = id, error = error)
}
},
)
tileRegionPacks[id]!!.cancelable = task
return Result.success(Unit)
} catch (e: Exception) {
return Result.failure(e)
}
}

private fun convertRegionsToJSON(tileRegions: List<TileRegion>, promise: Promise) {
Expand Down Expand Up @@ -641,6 +648,14 @@ class RNMBXOfflineModule(private val mReactContext: ReactApplicationContext) :
}
}

private fun <T> Result<T>.toPromise(promise: Promise, error: String) {
val ok = getOrElse {
promise.reject(error, exceptionOrNull() ?: Exception("Unknown error"))
return@toPromise
}
promise.resolve(ok)
}

fun TileRegionLoadProgress.toPercentage(): Double {
return (completedResourceCount.toDouble() * 100.0) / (requiredResourceCount.toDouble())
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"react-native-builder-bob": "^0.23.1",
"react-test-renderer": "18.2.0",
"ts-node": "10.9.1",
"typescript": "4.8.4",
"typescript": "5.1.3",
"@mdx-js/mdx": "^3.0.0"
},
"codegenConfig": {
Expand Down
2 changes: 1 addition & 1 deletion plugin/build/generateCode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Sourcecode: https://github.com/expo/expo/blob/59ece3cb1d5a7aaea42f4c7fe9d1f4f825b338f8/packages/@expo/config-plugins/src/utils/generateCode.ts
* LICENSE: https://github.com/expo/expo/blob/59ece3cb1d5a7aaea42f4c7fe9d1f4f825b338f8/packages/@expo/config-plugins/LICENSE
*/
export declare type MergeResults = {
export type MergeResults = {
contents: string;
didClear: boolean;
didMerge: boolean;
Expand Down
4 changes: 2 additions & 2 deletions plugin/build/withMapbox.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConfigPlugin } from 'expo/config-plugins';
declare type InstallerBlockName = 'pre' | 'post';
export declare type MapboxPlugProps = {
type InstallerBlockName = 'pre' | 'post';
export type MapboxPlugProps = {
/**
* @deprecated
*/
Expand Down

0 comments on commit b512d01

Please sign in to comment.