|
| 1 | +/** |
| 2 | + * Copyright (c) React Native Community |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +import React from 'react'; |
| 13 | +import { |
| 14 | + StyleSheet, |
| 15 | + View, |
| 16 | + Text, |
| 17 | + Switch, |
| 18 | + TextInput, |
| 19 | + Platform, |
| 20 | +} from 'react-native'; |
| 21 | +import type { GeolocationOptions } from '@react-native-community/geolocation'; |
| 22 | + |
| 23 | +export const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000; // 10 minutes |
| 24 | +export const DEFAULT_DISTANCE_FILTER_M = 100; |
| 25 | + |
| 26 | +export type WatchOptionFormValues = { |
| 27 | + enableHighAccuracy: boolean; |
| 28 | + timeout: string; |
| 29 | + maximumAge: string; |
| 30 | + distanceFilter: string; |
| 31 | + useSignificantChanges: boolean; |
| 32 | + interval: string; |
| 33 | + fastestInterval: string; |
| 34 | +}; |
| 35 | + |
| 36 | +export const initialWatchOptionValues: WatchOptionFormValues = { |
| 37 | + enableHighAccuracy: false, |
| 38 | + timeout: '', |
| 39 | + maximumAge: '', |
| 40 | + distanceFilter: '', |
| 41 | + useSignificantChanges: false, |
| 42 | + interval: '', |
| 43 | + fastestInterval: '', |
| 44 | +}; |
| 45 | + |
| 46 | +const parseNumber = (value: string) => { |
| 47 | + if (value.trim().length === 0) { |
| 48 | + return undefined; |
| 49 | + } |
| 50 | + |
| 51 | + const parsed = Number(value); |
| 52 | + return Number.isFinite(parsed) ? parsed : undefined; |
| 53 | +}; |
| 54 | + |
| 55 | +export const buildWatchOptions = ( |
| 56 | + values: WatchOptionFormValues |
| 57 | +): GeolocationOptions => { |
| 58 | + const options: GeolocationOptions = {}; |
| 59 | + |
| 60 | + if (values.enableHighAccuracy) { |
| 61 | + options.enableHighAccuracy = true; |
| 62 | + } |
| 63 | + |
| 64 | + const timeoutValue = parseNumber(values.timeout); |
| 65 | + if (timeoutValue !== undefined) { |
| 66 | + options.timeout = timeoutValue; |
| 67 | + } |
| 68 | + |
| 69 | + const maximumAgeValue = parseNumber(values.maximumAge); |
| 70 | + if (maximumAgeValue !== undefined) { |
| 71 | + options.maximumAge = maximumAgeValue; |
| 72 | + } |
| 73 | + |
| 74 | + const distanceFilterValue = parseNumber(values.distanceFilter); |
| 75 | + if (distanceFilterValue !== undefined) { |
| 76 | + options.distanceFilter = distanceFilterValue; |
| 77 | + } |
| 78 | + |
| 79 | + if (Platform.OS === 'ios') { |
| 80 | + options.useSignificantChanges = values.useSignificantChanges; |
| 81 | + } |
| 82 | + |
| 83 | + if (Platform.OS === 'android') { |
| 84 | + const intervalValue = parseNumber(values.interval); |
| 85 | + if (intervalValue !== undefined) { |
| 86 | + options.interval = intervalValue; |
| 87 | + } |
| 88 | + |
| 89 | + const fastestIntervalValue = parseNumber(values.fastestInterval); |
| 90 | + if (fastestIntervalValue !== undefined) { |
| 91 | + options.fastestInterval = fastestIntervalValue; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return options; |
| 96 | +}; |
| 97 | + |
| 98 | +export const buildCurrentPositionOptions = ( |
| 99 | + values: WatchOptionFormValues |
| 100 | +): GeolocationOptions => { |
| 101 | + const options: GeolocationOptions = {}; |
| 102 | + |
| 103 | + if (values.enableHighAccuracy) { |
| 104 | + options.enableHighAccuracy = true; |
| 105 | + } |
| 106 | + |
| 107 | + const timeoutValue = parseNumber(values.timeout); |
| 108 | + if (timeoutValue !== undefined) { |
| 109 | + options.timeout = timeoutValue; |
| 110 | + } |
| 111 | + |
| 112 | + const maximumAgeValue = parseNumber(values.maximumAge); |
| 113 | + if (maximumAgeValue !== undefined) { |
| 114 | + options.maximumAge = maximumAgeValue; |
| 115 | + } |
| 116 | + |
| 117 | + return options; |
| 118 | +}; |
| 119 | + |
| 120 | +type Props = { |
| 121 | + values: WatchOptionFormValues; |
| 122 | + onChange: <T extends keyof WatchOptionFormValues>( |
| 123 | + field: T, |
| 124 | + value: WatchOptionFormValues[T] |
| 125 | + ) => void; |
| 126 | +}; |
| 127 | + |
| 128 | +export function WatchOptionsForm({ values, onChange }: Props) { |
| 129 | + return ( |
| 130 | + <> |
| 131 | + <View style={styles.row}> |
| 132 | + <Text style={styles.label}>High accuracy (default: off)</Text> |
| 133 | + <Switch |
| 134 | + value={values.enableHighAccuracy} |
| 135 | + onValueChange={(next) => onChange('enableHighAccuracy', next)} |
| 136 | + /> |
| 137 | + </View> |
| 138 | + <View style={styles.row}> |
| 139 | + <Text style={styles.label}> |
| 140 | + Timeout (ms · default: {DEFAULT_TIMEOUT_MS}) |
| 141 | + </Text> |
| 142 | + <TextInput |
| 143 | + style={styles.input} |
| 144 | + keyboardType="numeric" |
| 145 | + value={values.timeout} |
| 146 | + onChangeText={(next) => onChange('timeout', next)} |
| 147 | + placeholder={`${DEFAULT_TIMEOUT_MS}`} |
| 148 | + /> |
| 149 | + </View> |
| 150 | + <View style={styles.row}> |
| 151 | + <Text style={styles.label}>Maximum age (ms · default: Infinity)</Text> |
| 152 | + <TextInput |
| 153 | + style={styles.input} |
| 154 | + keyboardType="numeric" |
| 155 | + value={values.maximumAge} |
| 156 | + onChangeText={(next) => onChange('maximumAge', next)} |
| 157 | + placeholder="Infinity" |
| 158 | + /> |
| 159 | + </View> |
| 160 | + <View style={styles.row}> |
| 161 | + <Text style={styles.label}> |
| 162 | + Distance filter (m · default: {DEFAULT_DISTANCE_FILTER_M}) |
| 163 | + </Text> |
| 164 | + <TextInput |
| 165 | + style={styles.input} |
| 166 | + keyboardType="numeric" |
| 167 | + value={values.distanceFilter} |
| 168 | + onChangeText={(next) => onChange('distanceFilter', next)} |
| 169 | + placeholder={`${DEFAULT_DISTANCE_FILTER_M}`} |
| 170 | + /> |
| 171 | + </View> |
| 172 | + {Platform.OS === 'ios' && ( |
| 173 | + <View style={styles.row}> |
| 174 | + <Text style={styles.label}>Use significant changes (default: false)</Text> |
| 175 | + <Switch |
| 176 | + value={values.useSignificantChanges} |
| 177 | + onValueChange={(next) => onChange('useSignificantChanges', next)} |
| 178 | + /> |
| 179 | + </View> |
| 180 | + )} |
| 181 | + {Platform.OS === 'android' && ( |
| 182 | + <> |
| 183 | + <View style={styles.row}> |
| 184 | + <Text style={styles.label}>Interval (ms · default: system)</Text> |
| 185 | + <TextInput |
| 186 | + style={styles.input} |
| 187 | + keyboardType="numeric" |
| 188 | + value={values.interval} |
| 189 | + onChangeText={(next) => onChange('interval', next)} |
| 190 | + placeholder="System" |
| 191 | + /> |
| 192 | + </View> |
| 193 | + <View style={styles.row}> |
| 194 | + <Text style={styles.label}>Fastest interval (ms · default: system)</Text> |
| 195 | + <TextInput |
| 196 | + style={styles.input} |
| 197 | + keyboardType="numeric" |
| 198 | + value={values.fastestInterval} |
| 199 | + onChangeText={(next) => onChange('fastestInterval', next)} |
| 200 | + placeholder="System" |
| 201 | + /> |
| 202 | + </View> |
| 203 | + </> |
| 204 | + )} |
| 205 | + </> |
| 206 | + ); |
| 207 | +} |
| 208 | + |
| 209 | +const styles = StyleSheet.create({ |
| 210 | + row: { |
| 211 | + marginBottom: 12, |
| 212 | + flexDirection: 'row', |
| 213 | + alignItems: 'center', |
| 214 | + justifyContent: 'space-between', |
| 215 | + }, |
| 216 | + label: { |
| 217 | + flex: 1, |
| 218 | + }, |
| 219 | + input: { |
| 220 | + borderWidth: 1, |
| 221 | + borderColor: '#ccc', |
| 222 | + paddingHorizontal: 8, |
| 223 | + paddingVertical: 4, |
| 224 | + borderRadius: 4, |
| 225 | + minWidth: 100, |
| 226 | + textAlign: 'right', |
| 227 | + }, |
| 228 | +}); |
0 commit comments