Skip to content

Commit

Permalink
feat: clearData on Mapbox module (#3238)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfazekas authored Dec 5, 2023
1 parent 7f0e577 commit 379dca6
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions __tests__/interface.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('Public Interface', () => {

// methods
'setWellKnownTileServer',
'clearData',
'setAccessToken',
'getAccessToken',
'setTelemetryEnabled',
Expand Down
19 changes: 18 additions & 1 deletion android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -29,3 +36,7 @@ fun MapboxMap.easeToV11(

)
}

fun MapboxMap.Companion.clearData(context: Context, callback: (result: Expected<String, None>) -> Unit) {
MapView(context).getMapboxMap().clearData(callback)
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
13 changes: 11 additions & 2 deletions docs/MapboxGL.md → docs/Mapbox.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# MapboxGL
# Mapbox

## methods
### setAccessToken(accessToken)
Expand All @@ -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()

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion ios/RNMBX/RNMBXModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions ios/RNMBX/RNMBXModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
1 change: 1 addition & 0 deletions setup-jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ NativeModules.RNMBXModule = {
getAccessToken: () => Promise.resolve('test-token'),
setTelemetryEnabled: jest.fn(),
setConnected: jest.fn(),
clearData: jest.fn(),

MapboxV10: true,
};
Expand Down
2 changes: 2 additions & 0 deletions src/RNMBXModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface RNMBXModule {
addCustomHeader(headerName: string, headerValue: string): void;
setAccessToken(accessToken: string | null): Promise<string | null>;
setWellKnownTileServer(tileServer: string): void;
clearData(): Promise<void>;
getAccessToken(): Promise<string>;
setTelemetryEnabled(telemetryEnabled: boolean): void;
setConnected(connected: boolean): void;
Expand All @@ -48,6 +49,7 @@ export const {
addCustomHeader,
setAccessToken,
setWellKnownTileServer,
clearData,
getAccessToken,
setTelemetryEnabled,
setConnected,
Expand Down

0 comments on commit 379dca6

Please sign in to comment.