Skip to content

Commit e30ffba

Browse files
authored
feat: TileStore with TileStore.setOption (#3278)
1 parent 5bd099b commit e30ffba

21 files changed

+392
-8
lines changed

__tests__/interface.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('Public Interface', () => {
2222

2323
// modules
2424
'offlineManager',
25+
'TileStore',
2526
'offlineManagerLegacy',
2627
'OfflineCreatePackOptions',
2728
'snapshotManager',

android/src/main/java/com/rnmapbox/rnmbx/RNMBXPackage.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import com.rnmapbox.rnmbx.modules.RNMBXModule
5050
import com.rnmapbox.rnmbx.modules.RNMBXOfflineModule
5151
import com.rnmapbox.rnmbx.modules.RNMBXOfflineModuleLegacy
5252
import com.rnmapbox.rnmbx.modules.RNMBXSnapshotModule
53+
import com.rnmapbox.rnmbx.modules.RNMBXTileStoreModule
5354
import com.rnmapbox.rnmbx.shape_animators.RNMBXMovePointShapeAnimatorModule
5455
import com.rnmapbox.rnmbx.shape_animators.ShapeAnimatorManager
5556
import com.rnmapbox.rnmbx.utils.ViewTagResolver
@@ -90,6 +91,7 @@ class RNMBXPackage : TurboReactPackage() {
9091
RNMBXModule.REACT_CLASS -> return RNMBXModule(reactApplicationContext)
9192
RNMBXLocationModule.REACT_CLASS -> return RNMBXLocationModule(reactApplicationContext)
9293
RNMBXOfflineModule.REACT_CLASS -> return RNMBXOfflineModule(reactApplicationContext)
94+
RNMBXTileStoreModule.REACT_CLASS -> return RNMBXTileStoreModule(reactApplicationContext)
9395
RNMBXOfflineModuleLegacy.REACT_CLASS -> return RNMBXOfflineModuleLegacy(reactApplicationContext)
9496
RNMBXSnapshotModule.REACT_CLASS -> return RNMBXSnapshotModule(reactApplicationContext)
9597
RNMBXLogging.REACT_CLASS -> return RNMBXLogging(reactApplicationContext)
@@ -189,6 +191,15 @@ class RNMBXPackage : TurboReactPackage() {
189191
false, // isCxxModule
190192
false // isTurboModule
191193
)
194+
moduleInfos[RNMBXTileStoreModule.REACT_CLASS] = ReactModuleInfo(
195+
RNMBXTileStoreModule.REACT_CLASS,
196+
RNMBXTileStoreModule.REACT_CLASS,
197+
false, // canOverrideExistingModule
198+
false, // needsEagerInit
199+
true, // hasConstants
200+
false, // isCxxModule
201+
false // isTurboModule
202+
)
192203
moduleInfos[RNMBXOfflineModuleLegacy.REACT_CLASS] = ReactModuleInfo(
193204
RNMBXOfflineModuleLegacy.REACT_CLASS,
194205
RNMBXOfflineModuleLegacy.REACT_CLASS,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.rnmapbox.rnmbx.modules
2+
3+
import com.facebook.react.bridge.Promise
4+
import com.facebook.react.bridge.ReactApplicationContext
5+
import com.facebook.react.bridge.ReactContextBaseJavaModule
6+
import com.facebook.react.bridge.ReactMethod
7+
import com.facebook.react.bridge.ReadableMap
8+
import com.facebook.react.module.annotations.ReactModule
9+
import com.mapbox.common.TileDataDomain
10+
import com.mapbox.common.TileStore
11+
import com.rnmapbox.rnmbx.utils.extensions.toValue
12+
import com.rnmapbox.rnmbx.utils.writableMapOf
13+
14+
typealias Tag = Int
15+
16+
@ReactModule(name = RNMBXTileStoreModule.REACT_CLASS)
17+
class RNMBXTileStoreModule(private val mReactContext: ReactApplicationContext) :
18+
ReactContextBaseJavaModule(
19+
mReactContext
20+
) {
21+
22+
fun shared(path: String?): TileStore {
23+
return if (path != null) {
24+
TileStore.create(path)
25+
} else {
26+
TileStore.create()
27+
}
28+
}
29+
30+
@ReactMethod
31+
fun shared(path: String?, promise: Promise) {
32+
val tag = RNMBXTileStoreModule.tileStorePathTags.get(path)
33+
if (tag != null) {
34+
promise.resolve(tag)
35+
} else {
36+
val tileStore = shared(path)
37+
RNMBXTileStoreModule.lastTag += 1
38+
val tag = RNMBXTileStoreModule.lastTag
39+
RNMBXTileStoreModule.tileStores.put(tag, tileStore)
40+
RNMBXTileStoreModule.tileStorePathTags.set(path, tag)
41+
promise.resolve(tag)
42+
}
43+
}
44+
45+
@ReactMethod
46+
fun setOption(tag: Double, key:String, domain: String, value: ReadableMap, promise: Promise) {
47+
val tileStore = RNMBXTileStoreModule.tileStores[tag.toInt()]
48+
if (tileStore == null) {
49+
promise.reject(REACT_CLASS, "No tile store found for tag")
50+
return
51+
}
52+
53+
tileStore.setOption(key, TileDataDomain.valueOf(domain.uppercase()), value.getDynamic("value").toValue());
54+
promise.resolve(null)
55+
}
56+
57+
override fun getName(): String {
58+
return REACT_CLASS
59+
}
60+
61+
companion object {
62+
const val REACT_CLASS = "RNMBXTileStoreModule"
63+
64+
var tileStores = mutableMapOf<Tag, TileStore>()
65+
var tileStorePathTags = mutableMapOf<String?, Tag>()
66+
var lastTag = REACT_CLASS.hashCode() % 1096
67+
}
68+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
/**
3+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
4+
*
5+
* Do not edit this file as changes may cause incorrect behavior and will be lost
6+
* once the code is regenerated.
7+
*
8+
* @generated by codegen project: GenerateModuleJavaSpec.js
9+
*
10+
* @nolint
11+
*/
12+
13+
package com.rnmapbox.rnmbx;
14+
15+
import com.facebook.proguard.annotations.DoNotStrip;
16+
import com.facebook.react.bridge.Promise;
17+
import com.facebook.react.bridge.ReactApplicationContext;
18+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
19+
import com.facebook.react.bridge.ReactMethod;
20+
import com.facebook.react.bridge.ReactModuleWithSpec;
21+
import com.facebook.react.bridge.ReadableMap;
22+
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
23+
import javax.annotation.Nonnull;
24+
import javax.annotation.Nullable;
25+
26+
public abstract class NativeRNMBXTileStoreModuleSpec extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule {
27+
public static final String NAME = "RNMBXTileStoreModule";
28+
29+
public NativeRNMBXTileStoreModuleSpec(ReactApplicationContext reactContext) {
30+
super(reactContext);
31+
}
32+
33+
@Override
34+
public @Nonnull String getName() {
35+
return NAME;
36+
}
37+
38+
@ReactMethod
39+
@DoNotStrip
40+
public abstract void shared(@Nullable String path, Promise promise);
41+
42+
@ReactMethod
43+
@DoNotStrip
44+
public abstract void setOption(double tag, String key, String domain, ReadableMap value, Promise promise);
45+
}

docs/docs.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8390,5 +8390,52 @@
83908390
}
83918391
}
83928392
]
8393+
},
8394+
"tileStore": {
8395+
"name": "tileStore",
8396+
"fileNameWithExt": "TileStore.ts",
8397+
"relPath": "src/modules/offline/TileStore.ts",
8398+
"description": "TileStore manages downloads and storage for requests to tile-related API endpoints,\nenforcing a disk usage quota: tiles available on disk may be deleted to make room for a new download.\nThis interface can be used by an app developer to set the disk quota.",
8399+
"props": [],
8400+
"styles": [],
8401+
"methods": [
8402+
{
8403+
"name": "setOption",
8404+
"description": "Sets additional options for this instance that are specific to a data type.\nParams:\nkey – 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.",
8405+
"params": [
8406+
{
8407+
"name": "key",
8408+
"description": "",
8409+
"type": {
8410+
"name": "string"
8411+
},
8412+
"optional": false
8413+
},
8414+
{
8415+
"name": "domain",
8416+
"description": "",
8417+
"type": {
8418+
"name": "TileDataDomain"
8419+
},
8420+
"optional": false
8421+
},
8422+
{
8423+
"name": "value",
8424+
"description": "",
8425+
"type": {
8426+
"name": "TileDataValue"
8427+
},
8428+
"optional": false
8429+
}
8430+
],
8431+
"examples": [],
8432+
"returns": {
8433+
"description": "",
8434+
"type": {
8435+
"name": "Promise"
8436+
}
8437+
}
8438+
}
8439+
]
83938440
}
83948441
}

docs/tileStore.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!-- This file was autogenerated from TileStore.ts do not modify -->
2+
3+
4+
5+
```tsx
6+
import { tileStore } from '@rnmapbox/maps';
7+
8+
tileStore
9+
10+
```
11+
TileStore manages downloads and storage for requests to tile-related API endpoints,
12+
enforcing a disk usage quota: tiles available on disk may be deleted to make room for a new download.
13+
This interface can be used by an app developer to set the disk quota.
14+
15+
16+
17+
## methods
18+
### setOption(key, domain, value)
19+
20+
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.
21+
22+
#### arguments
23+
| Name | Type | Required | Description |
24+
| ---- | :--: | :------: | :----------: |
25+
| `key` | `string` | `Yes` | |
26+
| `domain` | `TileDataDomain` | `Yes` | |
27+
| `value` | `TileDataValue` | `Yes` | |
28+
29+
30+

ios/RNMBX/RNMBXOfflineModule.swift renamed to ios/RNMBX/Offline/RNMBXOfflineModule.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,32 +127,32 @@ class RNMBXOfflineModule: RCTEventEmitter {
127127
)
128128

129129
@objc override
130-
func startObserving() {
130+
public func startObserving() {
131131
super.startObserving()
132132
hasListeners = true
133133
}
134134

135135
@objc override
136-
func stopObserving() {
136+
public func stopObserving() {
137137
super.stopObserving()
138138
hasListeners = false
139139
}
140140

141141
@objc
142142
override
143-
static func requiresMainQueueSetup() -> Bool {
143+
static public func requiresMainQueueSetup() -> Bool {
144144
return true
145145
}
146146

147147
@objc
148148
override
149-
func constantsToExport() -> [AnyHashable: Any]! {
149+
public func constantsToExport() -> [AnyHashable: Any]! {
150150
return [:]
151151
}
152152

153153
@objc
154154
override
155-
func supportedEvents() -> [String] {
155+
public func supportedEvents() -> [String] {
156156
return [Callbacks.error.rawValue, Callbacks.progress.rawValue]
157157
}
158158

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)