From 12c23c793ac111f3fb6735548df8b114bbfddd26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Fazekas?= Date: Wed, 7 Feb 2024 10:34:39 +0100 Subject: [PATCH 1/2] fix(andorid): resumePackDownload should return error to JS instead of throwing a Java exception --- .../rnmbx/modules/RNMBXOfflineModule.kt | 159 ++++++++++-------- 1 file changed, 87 insertions(+), 72 deletions(-) diff --git a/android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXOfflineModule.kt b/android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXOfflineModule.kt index a549fc59c..bbc97f895 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXOfflineModule.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXOfflineModule.kt @@ -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) } @@ -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") } @@ -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 { + 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, promise: Promise) { @@ -641,6 +648,14 @@ class RNMBXOfflineModule(private val mReactContext: ReactApplicationContext) : } } +private fun Result.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()) } From 5394b31ebd4a6e9b064b504b1d00471be57805b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Fazekas?= Date: Wed, 7 Feb 2024 10:54:51 +0100 Subject: [PATCH 2/2] chore: fix ci --- package.json | 2 +- plugin/build/generateCode.d.ts | 2 +- plugin/build/withMapbox.d.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 54682ef16..4c1a0f53e 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/plugin/build/generateCode.d.ts b/plugin/build/generateCode.d.ts index 64583e795..519941ba8 100644 --- a/plugin/build/generateCode.d.ts +++ b/plugin/build/generateCode.d.ts @@ -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; diff --git a/plugin/build/withMapbox.d.ts b/plugin/build/withMapbox.d.ts index 8bea75622..19c21b289 100644 --- a/plugin/build/withMapbox.d.ts +++ b/plugin/build/withMapbox.d.ts @@ -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 */