-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatScreen.js
More file actions
196 lines (180 loc) · 5.69 KB
/
ChatScreen.js
File metadata and controls
196 lines (180 loc) · 5.69 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
import React, { useState, useEffect } from 'react';
import { View, TextInput, Button, FlatList, Text, TouchableOpacity, Image, ImageBackground, Alert } from 'react-native';
import { db, auth } from './firebase';
import { collection, addDoc, query, onSnapshot, orderBy, where } from "firebase/firestore";
import { launchImageLibrary } from 'react-native-image-picker';
import Video from 'react-native-video';
import { useNavigation, useRoute } from '@react-navigation/native';
const backgroundGif = { uri: "https://media1.giphy.com/media/v1.Y2lkPTc5MGI3NjExZXZjZXo3Ym02bXloa25rMWQ2NWx6NHE5MDM5ZmNmNWJxeWN0ZHNiMiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/w4E7xK8UM9ZeY1ksDa/giphy.webp" };
export default function ChatScreen() {
const [message, setMessage] = useState('');
const [messages, setMessages] = useState([]);
const [media, setMedia] = useState(null);
const navigation = useNavigation();
const route = useRoute();
const roomName = route.params?.roomName;
useEffect(() => {
if (!roomName) {
Alert.alert("Error", "No room selected. Please go back and select a room.");
navigation.goBack();
return;
}
const q = query(
collection(db, "messages"),
where("roomName", "==", roomName),
orderBy("createdAt", "desc")
);
const unsubscribe = onSnapshot(q, (snapshot) => {
setMessages(snapshot.docs.map(doc => ({
id: doc.id,
data: doc.data()
})));
}, (error) => {
console.error("Error fetching messages: ", error.message);
Alert.alert("Error", "There was a problem loading the messages. Please try again later.");
});
return unsubscribe;
}, [roomName]);
const sendMessage = async () => {
try {
if (message.trim() || media) {
await addDoc(collection(db, "messages"), {
text: message,
media,
createdAt: new Date(),
user: auth.currentUser.email,
displayName: auth.currentUser.displayName || auth.currentUser.email,
photoURL: auth.currentUser.photoURL || 'https://media2.giphy.com/media/v1.Y2lkPTc5MGI3NjExa3I5ODY0aTZhanl6MWNzMXVxemhnbmVxZnhheXJ3OHJ5cTlsdjZwZyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/xaZCqV4weJwHu/giphy.webp',
roomName: roomName
});
setMessage('');
setMedia(null);
}
} catch (error) {
console.error("Error sending message: ", error.message);
Alert.alert("Error", "There was a problem sending your message. Please try again.");
}
};
const pickMedia = () => {
launchImageLibrary({ mediaType: 'mixed' }, (response) => {
if (response.assets && response.assets.length > 0) {
const selectedMedia = response.assets[0];
setMedia({
uri: selectedMedia.uri,
type: selectedMedia.type,
fileName: selectedMedia.fileName,
});
}
});
};
const renderMessage = ({ item }) => {
const isImage = item.data.media && item.data.media.type.startsWith('image/');
const isVideo = item.data.media && item.data.media.type.startsWith('video/');
const formattedDate = new Date(item.data.createdAt.seconds * 1000).toLocaleString();
return (
<View style={styles.messageContainer}>
<Image source={{ uri: item.data.photoURL }} style={styles.profileImage} />
<View style={styles.messageContent}>
<Text style={styles.displayName}>{item.data.displayName}</Text>
<Text style={styles.messageText}>{item.data.text}</Text>
{isImage && (
<Image source={{ uri: item.data.media.uri }} style={styles.media} resizeMode="cover" />
)}
{isVideo && (
<Video source={{ uri: item.data.media.uri }} style={styles.media} resizeMode="cover" controls={true} />
)}
<Text style={styles.messageDate}>{formattedDate}</Text>
</View>
</View>
);
};
return (
<ImageBackground source={backgroundGif} style={{ flex: 1 }} resizeMode="cover">
<FlatList
data={messages}
renderItem={renderMessage}
keyExtractor={item => item.id}
inverted
/>
<TextInput
placeholder="Type a message"
placeholderTextColor="#ccc"
value={message}
onChangeText={setMessage}
style={styles.input}
/>
{media && (
<View style={styles.mediaInfoContainer}>
<Text style={styles.mediaInfoText}>{media.fileName}</Text>
</View>
)}
<View style={styles.actionsContainer}>
<TouchableOpacity onPress={pickMedia}>
<Text style={styles.pickMediaText}>Pick Media</Text>
</TouchableOpacity>
<Button title="Send" onPress={sendMessage} />
</View>
</ImageBackground>
);
}
const styles = {
messageContainer: {
flexDirection: 'row',
margin: 10,
backgroundColor: 'rgba(0, 0, 0, 0.6)',
borderRadius: 10,
padding: 10,
},
profileImage: {
width: 40,
height: 40,
borderRadius: 20,
marginRight: 10,
},
messageContent: {
flex: 1,
},
displayName: {
color: 'white',
fontWeight: 'bold',
marginBottom: 5,
},
messageText: {
color: 'white',
marginBottom: 5,
},
media: {
width: 200,
height: 200,
borderRadius: 10,
marginTop: 5,
},
messageDate: {
color: 'gray',
fontSize: 12,
marginTop: 5,
},
input: {
backgroundColor: 'rgba(255, 255, 255, 0.2)',
color: 'white',
padding: 10,
borderRadius: 5,
marginHorizontal: 10,
marginBottom: 5,
},
mediaInfoContainer: {
marginHorizontal: 10,
marginBottom: 5,
},
mediaInfoText: {
color: 'white',
},
actionsContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
padding: 10,
},
pickMediaText: {
color: 'grey',
},
};