-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathCreateLocationModal.tsx
More file actions
296 lines (278 loc) · 7.82 KB
/
Copy pathCreateLocationModal.tsx
File metadata and controls
296 lines (278 loc) · 7.82 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { useState, useEffect, useMemo, useRef } from 'react';
import {
Alert,
AlertButton,
Modal,
Text,
View,
Pressable,
StyleSheet,
useWindowDimensions,
Image,
Platform,
} from 'react-native';
import MapView, { MapMarker, Marker } from 'react-native-maps';
import * as Location from 'expo-location';
import {
useChatContext,
useMessageComposer,
useTheme,
useTranslationContext,
} from 'stream-chat-expo';
import type { AppTheme } from '@/types/theme';
type LiveLocationCreateModalProps = {
visible: boolean;
onRequestClose: () => void;
};
const endedAtDurations = [60000, 600000, 3600000]; // 1 min, 10 mins, 1 hour
export const LiveLocationCreateModal = ({
visible,
onRequestClose,
}: LiveLocationCreateModalProps) => {
const [location, setLocation] = useState<Location.LocationObjectCoords>();
const messageComposer = useMessageComposer();
const { width, height } = useWindowDimensions();
const { client } = useChatContext();
const {
theme: {
colors: { accent_blue, grey, grey_whisper },
},
} = useTheme() as unknown as { theme: AppTheme };
const { t } = useTranslationContext();
const mapRef = useRef<MapView | null>(null);
const markerRef = useRef<MapMarker | null>(null);
const aspect_ratio = width / height;
const region = useMemo(() => {
const latitudeDelta = 0.1;
const longitudeDelta = latitudeDelta * aspect_ratio;
if (location) {
return {
latitude: location.latitude,
longitude: location.longitude,
latitudeDelta,
longitudeDelta,
};
}
}, [aspect_ratio, location]);
useEffect(() => {
let subscription: Location.LocationSubscription;
const watchLocationHandler = async () => {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
Alert.alert('Permissions not granted!');
return;
}
subscription = await Location.watchPositionAsync(
{
accuracy: Location.Accuracy.High,
distanceInterval: 0,
// Android only: these option are ignored on iOS
timeInterval: 2000,
},
(position) => {
setLocation(position.coords);
const newPosition = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: 0.1,
longitudeDelta: 0.1 * aspect_ratio,
};
if (mapRef.current?.animateToRegion) {
mapRef.current.animateToRegion(newPosition, 500);
}
// This is android only
if (Platform.OS === 'android' && markerRef.current?.animateMarkerToCoordinate) {
markerRef.current.animateMarkerToCoordinate(newPosition, 500);
}
},
(error) => {
console.error('watchPosition', error);
},
);
};
watchLocationHandler();
return () => {
subscription?.remove();
};
// eslint-disable-next-line react-hooks/exhaustive-deps -- example app: mount-only effect, deps captured at first render
}, []);
const buttons = [
{
text: 'Share Live Location',
description: 'Share your location in real-time',
onPress: () => {
const options: AlertButton[] = endedAtDurations.map((offsetMs) => ({
text: t('timestamp/Location end at', { milliseconds: offsetMs }),
onPress: async () => {
if (!location) {
return;
}
await messageComposer.locationComposer.setData({
durationMs: offsetMs,
latitude: location.latitude,
longitude: location.longitude,
});
await messageComposer.sendLocation();
onRequestClose();
},
style: 'default',
}));
options.push({ style: 'destructive', text: 'Cancel' });
Alert.alert(
'Share Live Location',
'Select the duration for which you want to share your live location.',
options,
);
},
},
{
text: 'Share Current Location',
description: 'Share your current location once',
onPress: async () => {
onRequestClose();
const { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
Alert.alert('Permission to access location was denied!');
return;
}
const location = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.High,
distanceInterval: 0,
});
if (location) {
await messageComposer.locationComposer.setData({
latitude: location.coords.latitude,
longitude: location.coords.longitude,
});
await messageComposer.sendLocation();
}
},
},
];
if (!location && client) {
return null;
}
return (
<Modal
animationType='slide'
visible={visible}
onRequestClose={onRequestClose}
presentationStyle='formSheet'
>
<View style={styles.modalHeader}>
<Pressable onPress={onRequestClose} style={styles.leftContent}>
<Text style={[styles.cancelText, { color: accent_blue }]}>Cancel</Text>
</Pressable>
<Text style={styles.headerTitle}>Share Location</Text>
<View style={styles.rightContent} />
</View>
<MapView
cameraZoomRange={{ maxCenterCoordinateDistance: 3000 }}
initialRegion={region}
ref={mapRef}
style={styles.mapView}
>
{location && (
<Marker
coordinate={{
latitude: location.latitude,
longitude: location.longitude,
}}
ref={markerRef}
title='Your Location'
description='This is your current location'
>
<View style={styles.markerWrapper}>
<Image
source={{ uri: client.user?.image || '' }}
style={[styles.markerImage, { borderColor: accent_blue }]}
/>
</View>
</Marker>
)}
</MapView>
<View style={styles.buttons}>
{buttons.map((button, index) => (
<Pressable
key={index}
onPress={button.onPress}
style={({ pressed }) => [
{
borderColor: pressed ? accent_blue : grey_whisper,
},
styles.button,
]}
>
<Text style={[styles.buttonTitle, { color: accent_blue }]}>{button.text}</Text>
<Text style={[styles.buttonDescription, { color: grey }]}>{button.description}</Text>
</Pressable>
))}
</View>
</Modal>
);
};
const IMAGE_SIZE = 35;
const styles = StyleSheet.create({
mapView: {
width: 'auto',
flex: 3,
},
textStyle: {
fontSize: 12,
color: 'gray',
marginHorizontal: 12,
marginVertical: 4,
},
modalHeader: {
flexDirection: 'row',
justifyContent: 'space-between',
paddingHorizontal: 16,
paddingTop: 16,
paddingBottom: 12,
},
cancelText: {
fontSize: 16,
},
headerTitle: {
fontSize: 16,
fontWeight: '600',
textAlign: 'center',
},
rightContent: {
flex: 1,
flexShrink: 1,
},
leftContent: {
flex: 1,
flexShrink: 1,
},
buttons: {
flex: 1,
marginVertical: 16,
},
button: {
borderWidth: 1,
borderRadius: 8,
marginVertical: 4,
marginHorizontal: 16,
padding: 8,
},
buttonTitle: {
fontWeight: '600',
marginVertical: 4,
},
buttonDescription: {
fontSize: 12,
marginVertical: 4,
},
markerWrapper: {
overflow: 'hidden', // REQUIRED for rounded corners to show on Android
},
markerImage: {
width: IMAGE_SIZE,
height: IMAGE_SIZE,
borderRadius: IMAGE_SIZE / 2,
resizeMode: 'cover', // or 'contain' if image is cropped
borderWidth: 2,
},
});