From 379dca68e9b144ea6b98535aa37a957379a5e8dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Fazekas?= Date: Tue, 5 Dec 2023 14:32:32 +0100 Subject: [PATCH] feat: clearData on Mapbox module (#3238) --- README.md | 2 +- __tests__/interface.test.js | 1 + .../com/rnmapbox/rnmbx/modules/RNMBXModule.kt | 19 ++++++++++++++- .../com/rnmapbox/rnmbx/v11compat/MapboxMap.kt | 11 +++++++++ .../com/rnmapbox/rnmbx/v11compat/MapboxMap.kt | 5 ++++ docs/{MapboxGL.md => Mapbox.md} | 13 ++++++++-- ios/RNMBX/RNMBXModule.m | 2 +- ios/RNMBX/RNMBXModule.swift | 24 +++++++++++++++++++ setup-jest.js | 1 + src/RNMBXModule.ts | 2 ++ 10 files changed, 75 insertions(+), 5 deletions(-) rename docs/{MapboxGL.md => Mapbox.md} (87%) diff --git a/README.md b/README.md index 159754f33..559e952f6 100644 --- a/README.md +++ b/README.md @@ -220,7 +220,7 @@ const styles = StyleSheet.create({ ### Misc -- [Mapbox](/docs/MapboxGL.md) +- [Mapbox](/docs/Mapbox.md) - [CustomHttpHeaders](/docs/CustomHttpHeaders.md) - [Logger](/docs/Logger.md) diff --git a/__tests__/interface.test.js b/__tests__/interface.test.js index 3c62c4d82..2d0939874 100644 --- a/__tests__/interface.test.js +++ b/__tests__/interface.test.js @@ -86,6 +86,7 @@ describe('Public Interface', () => { // methods 'setWellKnownTileServer', + 'clearData', 'setAccessToken', 'getAccessToken', 'setTelemetryEnabled', diff --git a/android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXModule.kt b/android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXModule.kt index 6245a2810..8f74eb01d 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXModule.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXModule.kt @@ -14,12 +14,15 @@ import com.facebook.react.bridge.ReactMethod import com.facebook.react.common.MapBuilder import com.mapbox.common.* import com.mapbox.maps.MapView +import com.mapbox.maps.MapboxMap import com.mapbox.maps.Style import com.mapbox.maps.plugin.attribution.attribution import com.rnmapbox.rnmbx.components.camera.constants.CameraMode +import com.rnmapbox.rnmbx.utils.Logger import java.util.HashMap import com.rnmapbox.rnmbx.v11compat.resourceoption.* +import com.rnmapbox.rnmbx.v11compat.mapboxmap.* @ReactModule(name = RNMBXModule.REACT_CLASS) class RNMBXModule(private val mReactContext: ReactApplicationContext) : ReactContextBaseJavaModule( @@ -128,6 +131,19 @@ class RNMBXModule(private val mReactContext: ReactApplicationContext) : ReactCon } } + @ReactMethod + fun clearData(promise: Promise) { + mReactContext.runOnUiQueueThread { + MapboxMap.clearData(mReactContext) { + if (it.isValue) { + promise.resolve(it.value) + } else { + promise.reject("error", "RNMBXModule.clearError ${it.error}") + } + } + } + } + @ReactMethod fun setAccessToken(accessToken: String?, promise: Promise) { mReactContext.runOnUiQueueThread(Runnable { @@ -138,7 +154,7 @@ class RNMBXModule(private val mReactContext: ReactApplicationContext) : ReactCon @ReactMethod fun setWellKnownTileServer(tileServer: String?) { - // NO-OP + Logger.e(LOG_TAG, "setWellKnownTileServer is deprecated and will be removed") } @ReactMethod @@ -153,6 +169,7 @@ class RNMBXModule(private val mReactContext: ReactApplicationContext) : ReactCon companion object { const val REACT_CLASS = "RNMBXModule" + const val LOG_TAG = "RNMBXModule" private val customHeaderInterceptorAdded = false @JvmStatic fun getAccessToken(reactContext: ReactApplicationContext?): String { diff --git a/android/src/main/mapbox-v11-compat/v10/com/rnmapbox/rnmbx/v11compat/MapboxMap.kt b/android/src/main/mapbox-v11-compat/v10/com/rnmapbox/rnmbx/v11compat/MapboxMap.kt index 4ac50a5fd..9f935d0e4 100644 --- a/android/src/main/mapbox-v11-compat/v10/com/rnmapbox/rnmbx/v11compat/MapboxMap.kt +++ b/android/src/main/mapbox-v11-compat/v10/com/rnmapbox/rnmbx/v11compat/MapboxMap.kt @@ -1,7 +1,14 @@ package com.rnmapbox.rnmbx.v11compat.mapboxmap; import android.animation.Animator +import android.app.Activity +import android.content.Context +import android.content.ContextWrapper +import androidx.activity.result.contract.ActivityResultContracts +import com.mapbox.bindgen.Expected +import com.mapbox.bindgen.None import com.mapbox.maps.CameraOptions +import com.mapbox.maps.MapView import com.mapbox.maps.MapboxMap import com.mapbox.maps.plugin.animation.MapAnimationOptions import com.mapbox.maps.plugin.animation.easeTo @@ -29,3 +36,7 @@ fun MapboxMap.easeToV11( ) } + +fun MapboxMap.Companion.clearData(context: Context, callback: (result: Expected) -> Unit) { + MapView(context).getMapboxMap().clearData(callback) +} diff --git a/android/src/main/mapbox-v11-compat/v11/com/rnmapbox/rnmbx/v11compat/MapboxMap.kt b/android/src/main/mapbox-v11-compat/v11/com/rnmapbox/rnmbx/v11compat/MapboxMap.kt index 50adcc84c..beb6d9dc9 100644 --- a/android/src/main/mapbox-v11-compat/v11/com/rnmapbox/rnmbx/v11compat/MapboxMap.kt +++ b/android/src/main/mapbox-v11-compat/v11/com/rnmapbox/rnmbx/v11compat/MapboxMap.kt @@ -1,6 +1,7 @@ package com.rnmapbox.rnmbx.v11compat.mapboxmap import android.animation.Animator +import android.content.Context import com.mapbox.maps.AsyncOperationResultCallback import com.mapbox.maps.CameraOptions import com.mapbox.maps.MapboxMap @@ -34,4 +35,8 @@ fun MapboxMap.easeToV11( fun MapboxMap.clearData(callback: AsyncOperationResultCallback) { return MapboxMap.clearData(callback) +} + +fun MapboxMap.Companion.clearData(context: Context, callback: AsyncOperationResultCallback) { + this.clearData(callback) } \ No newline at end of file diff --git a/docs/MapboxGL.md b/docs/Mapbox.md similarity index 87% rename from docs/MapboxGL.md rename to docs/Mapbox.md index d2a2061ce..79c3d1cae 100644 --- a/docs/MapboxGL.md +++ b/docs/Mapbox.md @@ -1,4 +1,4 @@ -# MapboxGL +# Mapbox ## methods ### setAccessToken(accessToken) @@ -14,13 +14,15 @@ not required when using other tiles ### setWellKnownTileServer(tileServer) + + #### arguments | Name | Type | Required | Description | | ---- | :--: | :------: | :----------: | | `tileServer` | `String` | `Yes` | tile server | #### Description -No-op on non MapLibre implemntations. Use MapboxGL.TileServers.Mapbox to consume mapbox tiles with maplibre. *Note*: Consuming mapbox with MapLibre has different pricing than with the official SDK. Other values: MapboxGL.TileServers.MapLibre, MapboxGL.TileServers.MapTiler +Deprecard will be removed on next version ### getAccessToken() @@ -81,3 +83,10 @@ Either permission was granted or denied. ### Description If you want to fully block online map - maybe to force offline maps + +### clearData + +Clears temporary map data from the data path defined in the given resource options. Useful to reduce the disk usage or in case the disk cache contains invalid data. + +### Description +If you want to fully block online map - maybe to force offline maps \ No newline at end of file diff --git a/ios/RNMBX/RNMBXModule.m b/ios/RNMBX/RNMBXModule.m index a228efa1d..3b0af79ad 100644 --- a/ios/RNMBX/RNMBXModule.m +++ b/ios/RNMBX/RNMBXModule.m @@ -9,6 +9,6 @@ @interface RCT_EXTERN_MODULE(RNMBXModule, NSObject) RCT_EXTERN_METHOD(setTelemetryEnabled:(BOOL)telemetryEnabled) RCT_EXTERN_METHOD(setWellKnownTileServer:(NSString *)tileServer) - +RCT_EXTERN_METHOD(clearData:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) @end diff --git a/ios/RNMBX/RNMBXModule.swift b/ios/RNMBX/RNMBXModule.swift index cc327969d..c3071af10 100644 --- a/ios/RNMBX/RNMBXModule.swift +++ b/ios/RNMBX/RNMBXModule.swift @@ -113,4 +113,28 @@ class RNMBXModule : NSObject { Logger.error("setWellKnownTileServer: \(tileServer) should be mapbox") } } + + @objc func clearData(_ resolver: @escaping RCTPromiseResolveBlock, + rejecter: @escaping RCTPromiseRejectBlock) { + + DispatchQueue.main.async { + #if RNMBX_11 + MapboxMap.clearData { error in + if let error = error { + rejecter("error", error.localizedDescription, error) + } else { + resolver(nil) + } + } + #else + MapboxMap.clearData(for: ResourceOptions(accessToken: RNMBXModule.accessToken ?? "")) { error in + if let error = error { + rejecter("error", error.localizedDescription, error) + } else { + resolver(nil) + } + } + #endif + } + } } diff --git a/setup-jest.js b/setup-jest.js index a93e86aa3..bdd921cfd 100644 --- a/setup-jest.js +++ b/setup-jest.js @@ -98,6 +98,7 @@ NativeModules.RNMBXModule = { getAccessToken: () => Promise.resolve('test-token'), setTelemetryEnabled: jest.fn(), setConnected: jest.fn(), + clearData: jest.fn(), MapboxV10: true, }; diff --git a/src/RNMBXModule.ts b/src/RNMBXModule.ts index af839e966..5901144aa 100644 --- a/src/RNMBXModule.ts +++ b/src/RNMBXModule.ts @@ -31,6 +31,7 @@ interface RNMBXModule { addCustomHeader(headerName: string, headerValue: string): void; setAccessToken(accessToken: string | null): Promise; setWellKnownTileServer(tileServer: string): void; + clearData(): Promise; getAccessToken(): Promise; setTelemetryEnabled(telemetryEnabled: boolean): void; setConnected(connected: boolean): void; @@ -48,6 +49,7 @@ export const { addCustomHeader, setAccessToken, setWellKnownTileServer, + clearData, getAccessToken, setTelemetryEnabled, setConnected,