Skip to content

Commit 2d0f7ea

Browse files
committed
feat(offlineLegacyManager) add observers
1 parent 591e248 commit 2d0f7ea

10 files changed

Lines changed: 1254 additions & 194 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,4 @@ class RNMBXPackage : TurboReactPackage() {
321321
moduleInfos
322322
}
323323
}
324-
}
324+
}

android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXOfflineModuleLegacy.kt

Lines changed: 406 additions & 96 deletions
Large diffs are not rendered by default.

docs/examples.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,4 +936,4 @@
936936
}
937937
]
938938
}
939-
]
939+
]

example/src/examples/CacheOffline/OfflineExample.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ const OfflineExample = () => {
7676
}
7777
}}
7878
/>
79+
<Button
80+
title="Pause pack"
81+
onPress={async () => {
82+
const pack = await offlineManager.getPack(packName);
83+
if (pack) {
84+
await pack.pause();
85+
}
86+
}}
87+
/>
7988
<Button
8089
title="Resume pack"
8190
onPress={async () => {
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import geoViewport from '@mapbox/geo-viewport';
2+
import Mapbox, {
3+
Camera,
4+
MapView,
5+
offlineManagerLegacy,
6+
StyleURL,
7+
} from '@rnmapbox/maps';
8+
import React, { useRef, useState } from 'react';
9+
import { Button, Dimensions, TextInput } from 'react-native';
10+
11+
import { ExampleWithMetadata } from '../common/ExampleMetadata'; // exclude-from-doc
12+
13+
const CENTER_COORD: [number, number] = [-73.970895, 40.723279];
14+
const MAPBOX_VECTOR_TILE_SIZE = 512;
15+
console.log('=> Mapbox[0]:', Mapbox);
16+
console.log('=> Mapbox.StyleURL[1]:', Mapbox.StyleURL);
17+
console.log('=> StyleURL[2]:', StyleURL);
18+
const STYLE_URL = Mapbox.StyleURL.Satellite;
19+
20+
const OfflineLegacyExample = () => {
21+
const [packName, setPackName] = useState('pack-1');
22+
const [showEditTitle, setShowEditTitle] = useState(false);
23+
const [tileLimit, setTileLimit] = useState(6000);
24+
const [timeout, setTimeout] = useState(60);
25+
26+
const map = useRef();
27+
28+
return (
29+
<>
30+
<Button
31+
title={`Pack name: ${packName}`}
32+
onPress={() => {
33+
setShowEditTitle(!showEditTitle);
34+
}}
35+
/>
36+
{showEditTitle && (
37+
<TextInput
38+
value={packName}
39+
autoFocus={true}
40+
onChangeText={(text) => setPackName(text)}
41+
onBlur={() => setShowEditTitle(false)}
42+
/>
43+
)}
44+
<Button
45+
title={`Toggle tile limit (${tileLimit})`}
46+
onPress={() => {
47+
setTileLimit(tileLimit === 6000 ? 10 : 6000);
48+
offlineManagerLegacy.setTileCountLimit(
49+
tileLimit === 6000 ? 10 : 6000,
50+
);
51+
}}
52+
/>
53+
<Button
54+
title={`Toggle timeout (${timeout})`}
55+
onPress={() => {
56+
setTimeout(timeout === 60 ? 10 : 60);
57+
offlineManagerLegacy.setTimeout(timeout === 60 ? 10 : 60);
58+
}}
59+
/>
60+
<Button
61+
title="Get all packs"
62+
onPress={async () => {
63+
const packs = await offlineManagerLegacy.getPacks();
64+
console.log('=> packs:', packs);
65+
packs.forEach((pack) => {
66+
console.log(
67+
'pack:',
68+
pack,
69+
'name:',
70+
pack.name,
71+
'bounds:',
72+
pack?.bounds,
73+
'metadata',
74+
pack?.metadata,
75+
);
76+
});
77+
}}
78+
/>
79+
<Button
80+
title="Get pack"
81+
onPress={async () => {
82+
const pack = await offlineManagerLegacy.getPack(packName);
83+
if (pack) {
84+
console.log(
85+
'pack:',
86+
pack,
87+
'name:',
88+
pack.name,
89+
'bounds:',
90+
pack?.bounds,
91+
'metadata',
92+
pack?.metadata,
93+
);
94+
95+
console.log('=> status', await pack?.status());
96+
}
97+
}}
98+
/>
99+
<Button
100+
title="Pause pack"
101+
onPress={async () => {
102+
const pack = await offlineManagerLegacy.getPack(packName);
103+
if (pack) {
104+
await pack.pause();
105+
}
106+
}}
107+
/>
108+
<Button
109+
title="Resume pack"
110+
onPress={async () => {
111+
const pack = await offlineManagerLegacy.getPack(packName);
112+
if (pack) {
113+
await pack.resume();
114+
}
115+
}}
116+
/>
117+
<Button
118+
title="Remove packs"
119+
onPress={async () => {
120+
const result = await offlineManagerLegacy.resetDatabase();
121+
console.log('Reset DB done:', result);
122+
}}
123+
/>
124+
<Button
125+
title="Create Pack"
126+
onPress={async () => {
127+
if (map.current) {
128+
const { width, height } = Dimensions.get('window');
129+
const mapCenter = await map.current.getCenter();
130+
131+
const bounds: [number, number, number, number] = geoViewport.bounds(
132+
mapCenter,
133+
13,
134+
[width, height],
135+
MAPBOX_VECTOR_TILE_SIZE,
136+
);
137+
138+
const options = {
139+
name: packName,
140+
styleURL: STYLE_URL,
141+
bounds: [
142+
[bounds[0], bounds[1]],
143+
[bounds[2], bounds[3]],
144+
] as [[number, number], [number, number]],
145+
minZoom: 10,
146+
maxZoom: 14,
147+
metadata: {
148+
whatIsThat: 'foo',
149+
},
150+
};
151+
152+
const pack = await offlineManagerLegacy.createPack(
153+
options,
154+
(region, status) =>
155+
console.log('=> progress callback region:', 'status: ', status),
156+
(region, error) =>
157+
console.error('=> error callback region:', 'error:', error),
158+
);
159+
160+
console.log('Create pack', pack);
161+
}
162+
}}
163+
/>
164+
<MapView ref={map} style={{ flex: 1 }} styleURL={STYLE_URL}>
165+
<Camera zoomLevel={10} centerCoordinate={CENTER_COORD} />
166+
</MapView>
167+
</>
168+
);
169+
};
170+
171+
export default OfflineLegacyExample;
172+
173+
/* end-example-doc */
174+
175+
const metadata: ExampleWithMetadata['metadata'] = {
176+
title: 'Offline Legacy Example',
177+
tags: [
178+
'offlineManagerLegacy#createPack',
179+
'offlineManagerLegacy#getPack',
180+
'offlineManagerLegacy#getPacks',
181+
],
182+
docs: `
183+
Demonstates basic use of offlineManagerLegacy api.
184+
`,
185+
};
186+
OfflineLegacyExample.metadata = metadata;

example/src/examples/CacheOffline/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { default as CacheManagement } from './CacheManagement';
22
export { default as OfflineTilesets } from './OfflineTilesets';
33
export { default as CreateOfflineRegion } from './CreateOfflineRegion';
44
export { default as OfflineExample } from './OfflineExample';
5+
export { default as OfflineLegacyExample } from './OfflineLegacyExample';
56

67
export const metadata = {
78
title: 'Cache / Offline',

ios/RNMBX/Offline/RNMBXOfflineModuleLegacy.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ @interface RCT_EXTERN_MODULE(RNMBXOfflineModuleLegacy, RCTEventEmitter<RCTBridge
1616

1717
RCT_EXTERN_METHOD(setTileCountLimit:(nonnull NSNumber *)limit)
1818

19+
RCT_EXTERN_METHOD(setProgressEventThrottle:(nonnull NSNumber *)throttleValue)
20+
21+
RCT_EXTERN_METHOD(setTimeout:(nonnull NSNumber *)seconds)
22+
1923
RCT_EXTERN_METHOD(getPackStatus:(NSString *)name
2024
resolver:(RCTPromiseResolveBlock)resolve
2125
rejecter:(RCTPromiseRejectBlock)reject)

0 commit comments

Comments
 (0)