|
| 1 | +import { Stack, useLocalSearchParams } from 'expo-router'; |
| 2 | +import { |
| 3 | + Platform, |
| 4 | + Pressable, |
| 5 | + useWindowDimensions, |
| 6 | + StyleSheet, |
| 7 | + View, |
| 8 | + Image, |
| 9 | + Text, |
| 10 | +} from 'react-native'; |
| 11 | +import { useContext, useMemo, useCallback, useRef } from 'react'; |
| 12 | +import { AppContext } from '../../context/AppContext'; |
| 13 | +import { useChatContext, useHandleLiveLocationEvents, useTheme } from 'stream-chat-expo'; |
| 14 | +import { SafeAreaView } from 'react-native-safe-area-context'; |
| 15 | +import MapView, { MapMarker, Marker } from 'react-native-maps'; |
| 16 | +import { SharedLocationResponse, StreamChat } from 'stream-chat'; |
| 17 | + |
| 18 | +export type SharedLiveLocationParamsStringType = SharedLocationResponse & { |
| 19 | + latitude: string; |
| 20 | + longitude: string; |
| 21 | +}; |
| 22 | + |
| 23 | +const MapScreenFooter = ({ |
| 24 | + client, |
| 25 | + shared_location, |
| 26 | + locationResponse, |
| 27 | + isLiveLocationStopped, |
| 28 | +}: { |
| 29 | + client: StreamChat; |
| 30 | + shared_location: SharedLocationResponse; |
| 31 | + locationResponse?: SharedLocationResponse; |
| 32 | + isLiveLocationStopped?: boolean; |
| 33 | +}) => { |
| 34 | + const { channel } = useContext(AppContext); |
| 35 | + const { end_at, user_id } = shared_location; |
| 36 | + const { |
| 37 | + theme: { |
| 38 | + colors: { accent_blue, accent_red, grey }, |
| 39 | + }, |
| 40 | + } = useTheme(); |
| 41 | + const liveLocationActive = isLiveLocationStopped ? false : new Date(end_at) > new Date(); |
| 42 | + const endedAtDate = end_at ? new Date(end_at) : null; |
| 43 | + const formattedEndedAt = endedAtDate ? endedAtDate.toLocaleString() : ''; |
| 44 | + |
| 45 | + const stopSharingLiveLocation = useCallback(async () => { |
| 46 | + await channel.stopLiveLocationSharing(locationResponse); |
| 47 | + }, [channel, locationResponse]); |
| 48 | + |
| 49 | + if (!end_at) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + const isCurrentUser = user_id === client.user.id; |
| 54 | + if (!isCurrentUser) { |
| 55 | + return ( |
| 56 | + <View style={styles.footer}> |
| 57 | + <Text style={[styles.footerText, { color: liveLocationActive ? accent_blue : accent_red }]}> |
| 58 | + {liveLocationActive ? 'Live Location' : 'Live Location ended'} |
| 59 | + </Text> |
| 60 | + <Text style={[styles.footerDescription, { color: grey }]}> |
| 61 | + {liveLocationActive |
| 62 | + ? `Live until: ${formattedEndedAt}` |
| 63 | + : `Location last updated at: ${formattedEndedAt}`} |
| 64 | + </Text> |
| 65 | + </View> |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + if (liveLocationActive) { |
| 70 | + return ( |
| 71 | + <View style={styles.footer}> |
| 72 | + <Pressable |
| 73 | + style={({ pressed }) => [styles.footerButton, { opacity: pressed ? 0.5 : 1 }]} |
| 74 | + onPress={stopSharingLiveLocation} |
| 75 | + hitSlop={10} |
| 76 | + > |
| 77 | + <Text style={[styles.footerText, { color: accent_red }]}>Stop Sharing</Text> |
| 78 | + </Pressable> |
| 79 | + |
| 80 | + <Text style={[styles.footerDescription, { color: grey }]}> |
| 81 | + Live until: {formattedEndedAt} |
| 82 | + </Text> |
| 83 | + </View> |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + return ( |
| 88 | + <View style={styles.footer}> |
| 89 | + <Text style={[styles.footerText, { color: accent_red }]}>Live Location ended</Text> |
| 90 | + <Text style={[styles.footerDescription, { color: grey }]}> |
| 91 | + Location last updated at: {formattedEndedAt} |
| 92 | + </Text> |
| 93 | + </View> |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +export default function MapScreen() { |
| 98 | + const { client } = useChatContext(); |
| 99 | + const shared_location = useLocalSearchParams<SharedLiveLocationParamsStringType>(); |
| 100 | + const { channel } = useContext(AppContext); |
| 101 | + const mapRef = useRef<MapView | null>(null); |
| 102 | + const markerRef = useRef<MapMarker | null>(null); |
| 103 | + const { |
| 104 | + theme: { |
| 105 | + colors: { accent_blue }, |
| 106 | + }, |
| 107 | + } = useTheme(); |
| 108 | + |
| 109 | + const { width, height } = useWindowDimensions(); |
| 110 | + const aspect_ratio = width / height; |
| 111 | + |
| 112 | + const onLocationUpdate = useCallback((location: SharedLocationResponse) => { |
| 113 | + const newPosition = { |
| 114 | + latitude: location.latitude, |
| 115 | + longitude: location.longitude, |
| 116 | + latitudeDelta: 0.1, |
| 117 | + longitudeDelta: 0.1 * aspect_ratio, |
| 118 | + }; |
| 119 | + // Animate the map to the new position |
| 120 | + if (mapRef.current?.animateToRegion) { |
| 121 | + mapRef.current.animateToRegion(newPosition, 500); |
| 122 | + } |
| 123 | + // This is android only |
| 124 | + if (Platform.OS === 'android' && markerRef.current?.animateMarkerToCoordinate) { |
| 125 | + markerRef.current.animateMarkerToCoordinate(newPosition, 500); |
| 126 | + } |
| 127 | + }, []); |
| 128 | + |
| 129 | + const { isLiveLocationStopped, locationResponse } = useHandleLiveLocationEvents({ |
| 130 | + channel, |
| 131 | + messageId: shared_location.message_id, |
| 132 | + onLocationUpdate, |
| 133 | + }); |
| 134 | + |
| 135 | + const initialRegion = useMemo(() => { |
| 136 | + const latitudeDelta = 0.1; |
| 137 | + const longitudeDelta = latitudeDelta * aspect_ratio; |
| 138 | + |
| 139 | + return { |
| 140 | + latitude: parseFloat(shared_location.latitude), |
| 141 | + longitude: parseFloat(shared_location.longitude), |
| 142 | + latitudeDelta, |
| 143 | + longitudeDelta, |
| 144 | + }; |
| 145 | + }, [aspect_ratio]); |
| 146 | + |
| 147 | + const region = useMemo(() => { |
| 148 | + const latitudeDelta = 0.1; |
| 149 | + const longitudeDelta = latitudeDelta * aspect_ratio; |
| 150 | + return { |
| 151 | + latitude: locationResponse?.latitude, |
| 152 | + longitude: locationResponse?.longitude, |
| 153 | + latitudeDelta, |
| 154 | + longitudeDelta, |
| 155 | + }; |
| 156 | + }, [aspect_ratio, locationResponse]); |
| 157 | + |
| 158 | + return ( |
| 159 | + <SafeAreaView style={styles.container} edges={['bottom']}> |
| 160 | + <Stack.Screen options={{ title: 'Map Screen' }} /> |
| 161 | + <MapView |
| 162 | + cameraZoomRange={{ maxCenterCoordinateDistance: 3000 }} |
| 163 | + initialRegion={initialRegion} |
| 164 | + ref={mapRef} |
| 165 | + style={styles.mapView} |
| 166 | + > |
| 167 | + {shared_location.end_at ? ( |
| 168 | + <Marker |
| 169 | + coordinate={ |
| 170 | + !locationResponse |
| 171 | + ? { latitude: initialRegion.latitude, longitude: initialRegion.longitude } |
| 172 | + : { latitude: region.latitude, longitude: region.longitude } |
| 173 | + } |
| 174 | + ref={markerRef} |
| 175 | + > |
| 176 | + <View style={styles.markerWrapper}> |
| 177 | + <Image |
| 178 | + style={[styles.markerImage, { borderColor: accent_blue }]} |
| 179 | + source={{ uri: client.user.image }} |
| 180 | + /> |
| 181 | + </View> |
| 182 | + </Marker> |
| 183 | + ) : ( |
| 184 | + <Marker coordinate={initialRegion} ref={markerRef} pinColor={accent_blue} /> |
| 185 | + )} |
| 186 | + </MapView> |
| 187 | + <MapScreenFooter |
| 188 | + client={client} |
| 189 | + shared_location={shared_location} |
| 190 | + locationResponse={locationResponse} |
| 191 | + isLiveLocationStopped={isLiveLocationStopped} |
| 192 | + /> |
| 193 | + </SafeAreaView> |
| 194 | + ); |
| 195 | +} |
| 196 | + |
| 197 | +const IMAGE_SIZE = 35; |
| 198 | + |
| 199 | +const styles = StyleSheet.create({ |
| 200 | + container: { |
| 201 | + flex: 1, |
| 202 | + }, |
| 203 | + mapView: { |
| 204 | + width: 'auto', |
| 205 | + flex: 3, |
| 206 | + }, |
| 207 | + markerWrapper: { |
| 208 | + overflow: 'hidden', // REQUIRED for rounded corners to show on Android |
| 209 | + }, |
| 210 | + markerImage: { |
| 211 | + width: IMAGE_SIZE, |
| 212 | + height: IMAGE_SIZE, |
| 213 | + borderRadius: IMAGE_SIZE / 2, |
| 214 | + resizeMode: 'cover', // or 'contain' if image is cropped |
| 215 | + borderWidth: 2, |
| 216 | + }, |
| 217 | + footer: { |
| 218 | + marginVertical: 8, |
| 219 | + }, |
| 220 | + footerButton: { |
| 221 | + padding: 4, |
| 222 | + }, |
| 223 | + footerText: { |
| 224 | + textAlign: 'center', |
| 225 | + fontSize: 14, |
| 226 | + }, |
| 227 | + footerDescription: { |
| 228 | + textAlign: 'center', |
| 229 | + fontSize: 12, |
| 230 | + marginTop: 4, |
| 231 | + }, |
| 232 | +}); |
0 commit comments