forked from react-native-maps/react-native-maps
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdecorateMapComponent.ts
183 lines (160 loc) · 4.96 KB
/
decorateMapComponent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {createContext} from 'react';
import {
requireNativeComponent,
NativeModules,
Platform,
UIManager,
HostComponent,
} from 'react-native';
import {PROVIDER_DEFAULT, PROVIDER_GOOGLE} from './ProviderConstants';
import {Provider} from './sharedTypes';
import {MapCallout} from './MapCallout';
import {MapOverlay} from './MapOverlay';
import {MapCalloutSubview} from './MapCalloutSubview';
import {MapCircle} from './MapCircle';
import {MapHeatmap} from './MapHeatmap';
import {MapLocalTile} from './MapLocalTile';
import {MapMarker} from './MapMarker';
import {MapPolygon} from './MapPolygon';
import {MapPolyline} from './MapPolyline';
import {MapCluster} from './MapCluster';
import {MapUrlTile} from './MapUrlTile';
import {MapWMSTile} from './MapWMSTile';
export const SUPPORTED: ImplementationStatus = 'SUPPORTED';
export const USES_DEFAULT_IMPLEMENTATION: ImplementationStatus =
'USES_DEFAULT_IMPLEMENTATION';
export const NOT_SUPPORTED: ImplementationStatus = 'NOT_SUPPORTED';
export const ProviderContext = createContext<Provider>(undefined);
export function getNativeMapName(provider: Provider) {
if (Platform.OS === 'android') {
return 'AIRMap';
}
if (provider === PROVIDER_GOOGLE) {
return 'AIRGoogleMap';
}
return 'AIRMap';
}
function getNativeComponentName(provider: Provider, component: ComponentName) {
return `${getNativeMapName(provider)}${component}`;
}
export const createNotSupportedComponent = (message: string) => () => {
console.error(message);
return null;
};
export const googleMapIsInstalled = !!UIManager.getViewManagerConfig(
getNativeMapName(PROVIDER_GOOGLE),
);
export default function decorateMapComponent<Type extends Component>(
Component: Type,
componentName: ComponentName,
providers: Providers,
): Type {
const components: {
[key: string]: NativeComponent;
} = {};
const getDefaultComponent = () =>
requireNativeComponent(getNativeComponentName(undefined, componentName));
Component.contextType = ProviderContext;
Component.prototype.getNativeComponent =
function getNativeComponent(): NativeComponent {
const provider = this.context;
const key = provider || 'default';
if (components[key]) {
return components[key];
}
if (provider === PROVIDER_DEFAULT) {
components[key] = getDefaultComponent();
return components[key];
}
const providerInfo = providers[provider];
// quick fix. Previous code assumed android | ios
if (Platform.OS !== 'android' && Platform.OS !== 'ios') {
throw new Error(`react-native-maps doesn't support ${Platform.OS}`);
}
const platformSupport = providerInfo[Platform.OS];
const nativeComponentName = getNativeComponentName(
provider,
componentName,
);
if (platformSupport === NOT_SUPPORTED) {
components[key] = createNotSupportedComponent(
`react-native-maps: ${nativeComponentName} is not supported on ${Platform.OS}`,
);
} else if (platformSupport === SUPPORTED) {
if (
provider !== PROVIDER_GOOGLE ||
(Platform.OS === 'ios' && googleMapIsInstalled)
) {
components[key] = requireNativeComponent(nativeComponentName);
}
} else {
// (platformSupport === USES_DEFAULT_IMPLEMENTATION)
if (!components.default) {
components.default = getDefaultComponent();
}
components[key] = components.default;
}
return components[key];
};
Component.prototype.getUIManagerCommand = function getUIManagerCommand(
name: string,
): UIManagerCommand {
const nativeComponentName = getNativeComponentName(
this.context,
componentName,
);
return UIManager.getViewManagerConfig(nativeComponentName).Commands[name];
};
Component.prototype.getMapManagerCommand = function getMapManagerCommand(
name: string,
): MapManagerCommand {
const nativeComponentName = `${getNativeComponentName(
this.context,
componentName,
)}Manager`;
return NativeModules[nativeComponentName][name];
};
return Component;
}
type ImplementationStatus =
| 'SUPPORTED'
| 'USES_DEFAULT_IMPLEMENTATION'
| 'NOT_SUPPORTED';
type Providers = {
google: {
ios: ImplementationStatus;
android: ImplementationStatus;
};
};
export type UIManagerCommand = number;
//todo: narrow down type
export type MapManagerCommand = any;
export type NativeComponent<H = unknown> =
| HostComponent<H>
| ReturnType<typeof createNotSupportedComponent>;
type Component =
| typeof MapCallout
| typeof MapCalloutSubview
| typeof MapCircle
| typeof MapHeatmap
| typeof MapLocalTile
| typeof MapMarker
| typeof MapOverlay
| typeof MapPolygon
| typeof MapPolyline
| typeof MapCluster
| typeof MapUrlTile
| typeof MapWMSTile;
type ComponentName =
| 'Callout'
| 'CalloutSubview'
| 'Circle'
| 'Heatmap'
| 'LocalTile'
| 'Marker'
| 'Overlay'
| 'Polygon'
| 'Polyline'
| 'Cluster'
| 'UrlTile'
| 'WMSTile';