-
-
Notifications
You must be signed in to change notification settings - Fork 853
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: TileStore with TileStore.setOption (#3278)
- Loading branch information
Showing
21 changed files
with
392 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXTileStoreModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.rnmapbox.rnmbx.modules | ||
|
||
import com.facebook.react.bridge.Promise | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule | ||
import com.facebook.react.bridge.ReactMethod | ||
import com.facebook.react.bridge.ReadableMap | ||
import com.facebook.react.module.annotations.ReactModule | ||
import com.mapbox.common.TileDataDomain | ||
import com.mapbox.common.TileStore | ||
import com.rnmapbox.rnmbx.utils.extensions.toValue | ||
import com.rnmapbox.rnmbx.utils.writableMapOf | ||
|
||
typealias Tag = Int | ||
|
||
@ReactModule(name = RNMBXTileStoreModule.REACT_CLASS) | ||
class RNMBXTileStoreModule(private val mReactContext: ReactApplicationContext) : | ||
ReactContextBaseJavaModule( | ||
mReactContext | ||
) { | ||
|
||
fun shared(path: String?): TileStore { | ||
return if (path != null) { | ||
TileStore.create(path) | ||
} else { | ||
TileStore.create() | ||
} | ||
} | ||
|
||
@ReactMethod | ||
fun shared(path: String?, promise: Promise) { | ||
val tag = RNMBXTileStoreModule.tileStorePathTags.get(path) | ||
if (tag != null) { | ||
promise.resolve(tag) | ||
} else { | ||
val tileStore = shared(path) | ||
RNMBXTileStoreModule.lastTag += 1 | ||
val tag = RNMBXTileStoreModule.lastTag | ||
RNMBXTileStoreModule.tileStores.put(tag, tileStore) | ||
RNMBXTileStoreModule.tileStorePathTags.set(path, tag) | ||
promise.resolve(tag) | ||
} | ||
} | ||
|
||
@ReactMethod | ||
fun setOption(tag: Double, key:String, domain: String, value: ReadableMap, promise: Promise) { | ||
val tileStore = RNMBXTileStoreModule.tileStores[tag.toInt()] | ||
if (tileStore == null) { | ||
promise.reject(REACT_CLASS, "No tile store found for tag") | ||
return | ||
} | ||
|
||
tileStore.setOption(key, TileDataDomain.valueOf(domain.uppercase()), value.getDynamic("value").toValue()); | ||
promise.resolve(null) | ||
} | ||
|
||
override fun getName(): String { | ||
return REACT_CLASS | ||
} | ||
|
||
companion object { | ||
const val REACT_CLASS = "RNMBXTileStoreModule" | ||
|
||
var tileStores = mutableMapOf<Tag, TileStore>() | ||
var tileStorePathTags = mutableMapOf<String?, Tag>() | ||
var lastTag = REACT_CLASS.hashCode() % 1096 | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
android/src/main/old-arch/com/rnmapbox/rnmbx/NativeRNMBXTileStoreModuleSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
|
||
/** | ||
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). | ||
* | ||
* Do not edit this file as changes may cause incorrect behavior and will be lost | ||
* once the code is regenerated. | ||
* | ||
* @generated by codegen project: GenerateModuleJavaSpec.js | ||
* | ||
* @nolint | ||
*/ | ||
|
||
package com.rnmapbox.rnmbx; | ||
|
||
import com.facebook.proguard.annotations.DoNotStrip; | ||
import com.facebook.react.bridge.Promise; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.ReactModuleWithSpec; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.turbomodule.core.interfaces.TurboModule; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
public abstract class NativeRNMBXTileStoreModuleSpec extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule { | ||
public static final String NAME = "RNMBXTileStoreModule"; | ||
|
||
public NativeRNMBXTileStoreModuleSpec(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
} | ||
|
||
@Override | ||
public @Nonnull String getName() { | ||
return NAME; | ||
} | ||
|
||
@ReactMethod | ||
@DoNotStrip | ||
public abstract void shared(@Nullable String path, Promise promise); | ||
|
||
@ReactMethod | ||
@DoNotStrip | ||
public abstract void setOption(double tag, String key, String domain, ReadableMap value, Promise promise); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!-- This file was autogenerated from TileStore.ts do not modify --> | ||
|
||
|
||
|
||
```tsx | ||
import { tileStore } from '@rnmapbox/maps'; | ||
|
||
tileStore | ||
|
||
``` | ||
TileStore manages downloads and storage for requests to tile-related API endpoints, | ||
enforcing a disk usage quota: tiles available on disk may be deleted to make room for a new download. | ||
This interface can be used by an app developer to set the disk quota. | ||
|
||
|
||
|
||
## methods | ||
### setOption(key, domain, value) | ||
|
||
Sets additional options for this instance that are specific to a data type.<br/>Params:<br/>key – The configuration option that should be changed. Valid keys are listed in \c TileStoreOptions. domain – The data type this setting should be applied for. value – The value for the configuration option, or null if it should be reset. | ||
|
||
#### arguments | ||
| Name | Type | Required | Description | | ||
| ---- | :--: | :------: | :----------: | | ||
| `key` | `string` | `Yes` | | | ||
| `domain` | `TileDataDomain` | `Yes` | | | ||
| `value` | `TileDataValue` | `Yes` | | | ||
|
||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#import "React/RCTBridgeModule.h" | ||
#import <React/RCTEventEmitter.h> | ||
|
||
@interface RCT_EXTERN_MODULE(RNMBXTileStoreModule, NSObject) | ||
|
||
RCT_EXTERN_METHOD(shared:(NSString *) path resolver:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject) | ||
RCT_EXTERN_METHOD(setOption:(nonnull NSNumber *) tag key:(NSString*) key domain:(NSString*) domain value:(NSDictionary*) value resolver:(RCTPromiseResolveBlock) resolve rejecter:(RCTPromiseRejectBlock) reject) | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import Foundation | ||
import MapboxMaps | ||
|
||
typealias Tag = Int | ||
|
||
@objc(RNMBXTileStoreModule) | ||
class RNMBXTileStoreModule: NSObject { | ||
|
||
static var tileStores : [Tag: TileStore] = [:] | ||
static var tileStorePathTags: [String?:Tag] = [:] | ||
|
||
static var lastTag: Tag = ("RNMBXOfflineModule".hashValue % 1096) | ||
|
||
func shared(path: String?) -> TileStore { | ||
if let path = path { | ||
let url = URL(fileURLWithPath: path) | ||
return TileStore.shared(for: url) | ||
} else { | ||
return TileStore.default | ||
} | ||
} | ||
|
||
@objc | ||
public func shared(_ path: String?, resolver: RCTPromiseResolveBlock, rejecter: RCTPromiseRejectBlock) { | ||
if let tag = RNMBXTileStoreModule.tileStorePathTags[path] { | ||
resolver(NSNumber(value: tag)) | ||
} else { | ||
let tileStore = shared(path: path) | ||
RNMBXTileStoreModule.lastTag += 1; | ||
let tag = RNMBXTileStoreModule.lastTag | ||
RNMBXTileStoreModule.tileStores[tag] = tileStore | ||
RNMBXTileStoreModule.tileStorePathTags[path] = tag | ||
resolver(NSNumber(value: tag)) | ||
} | ||
} | ||
|
||
private func tileDataDomain(name: String) -> TileDataDomain? { | ||
switch name { | ||
case "Maps": return .maps | ||
case "Navigation": return .navigation | ||
case "Search": return .search | ||
case "ADAS": return .adas | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
@objc | ||
func setOption(_ tag: NSNumber, key:String, domain: String, value: NSDictionary, resolver: RCTPromiseResolveBlock, rejecter: RCTPromiseRejectBlock) { | ||
guard let tileStore = RNMBXTileStoreModule.tileStores[tag.intValue] else { | ||
rejecter("invalidArgument","No tile store found for tag \(tag)", nil) | ||
return | ||
} | ||
|
||
|
||
guard let domain = tileDataDomain(name: domain) else { | ||
rejecter("invalidArgument","No domain found for \(domain)", nil) | ||
return | ||
} | ||
|
||
tileStore.setOptionForKey(key, domain: domain, value: value.object(forKey: "value")) | ||
resolver(nil) | ||
} | ||
|
||
@objc | ||
public static func requiresMainQueueSetup() -> Bool { | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.