From dc6a70e8f7632fa2f834982d85fce8cd535dc45b Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 10 May 2019 22:41:06 -0300 Subject: [PATCH 01/16] =?UTF-8?q?Inicio=20da=20implementa=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Screens/Chat/chat.js | 14 ++++- src/Screens/Conversas/conversas.js | 83 ++++++++++++++++++++++-------- 2 files changed, 74 insertions(+), 23 deletions(-) diff --git a/src/Screens/Chat/chat.js b/src/Screens/Chat/chat.js index fb902b8..39352f8 100644 --- a/src/Screens/Chat/chat.js +++ b/src/Screens/Chat/chat.js @@ -59,6 +59,7 @@ export default class Conversas extends Component { const messages = [] querySnapshot.forEach(doc => { const { content, contentTranslated, date, source } = doc.data() + const msgRef = this.ref.collection("messages").doc(doc.id) messages.push({ key: doc.id, content, @@ -66,6 +67,13 @@ export default class Conversas extends Component { date: date.toDate(), source }) + if (source === "2") { + firebase.firestore().runTransaction(t => { + return t.get(msgRef).then(() => { + t.update(msgRef, { unread: false }) + }) + }) + } }) this.setState({ messages }) }) @@ -107,7 +115,8 @@ export default class Conversas extends Component { .add({ content: newMessage.content, date: newMessage.date, - source: newMessage.source + source: newMessage.source, + unread: true }) .then(() => true) .catch(error => error) @@ -120,7 +129,8 @@ export default class Conversas extends Component { content: newMessage.content, date: newMessage.date, contentTranslated: translated, - source: "2" + source: "2", + unread: true }) .then(() => true) .catch(error => error) diff --git a/src/Screens/Conversas/conversas.js b/src/Screens/Conversas/conversas.js index ba5953d..5a9bd68 100644 --- a/src/Screens/Conversas/conversas.js +++ b/src/Screens/Conversas/conversas.js @@ -58,12 +58,43 @@ export default class Conversas extends Component { querySnapshot.forEach(doc => { contacts.forEach(contact => { if (contact.key === doc.id) { - conversas.push({ - contact, - key: doc.id, - profileImage: contact.profile_img_url, - contactName: contact.contactName - }) + let numUnreadMsgs = 0 + this.ref + .collection("conversas") + .doc(doc.id) + .collection("messages") + .onSnapshot(snapshot => { + snapshot.forEach(msg => { + const { source, unread } = msg.data() + if (source === "2") { + if (unread) { + Alert.alert("Numero de msgs", numUnreadMsgs.toString()) + numUnreadMsgs += 1 + Alert.alert("Numero de msgs", numUnreadMsgs.toString()) + } + } + }) + }) + if (numUnreadMsgs > 0) { + Alert.alert("Check", "Tem msgs não lidas") + conversas.push({ + contact, + key: doc.id, + profileImage: contact.profile_img_url, + contactName: contact.contactName, + unreadMsgs: true, + numUnreadMsgs + }) + } else { + // Alert.alert("Check", "Não tem msgs não lidas") + conversas.push({ + contact, + key: doc.id, + profileImage: contact.profile_img_url, + contactName: contact.contactName, + unreadMsgs: false + }) + } } }) }) @@ -94,17 +125,23 @@ export default class Conversas extends Component { deleteChat = item => { const { conversas } = this.state - + conversas.map(conversa => { - if(conversa.key === item.key) { - this.ref.collection("conversas").doc(item.key).collection("messages") - .get() - .then(snapshot => { - snapshot.docs.forEach(docs => { - docs.ref.delete() + if (conversa.key === item.key) { + this.ref + .collection("conversas") + .doc(item.key) + .collection("messages") + .get() + .then(snapshot => { + snapshot.docs.forEach(docs => { + docs.ref.delete() + }) }) - }) - this.ref.collection("conversas").doc(item.key).delete() + this.ref + .collection("conversas") + .doc(item.key) + .delete() } return true }) @@ -154,12 +191,16 @@ export default class Conversas extends Component { {item.lastMessage} {item.lastMessage} - - {item.unread} - + {item.unreadMsgs && ( + + + {item.numUnreadMsgs} + + + )} } From 87f43cff5e0fad452c6f77f2157a642aa1ac2744 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 11 May 2019 11:12:24 -0300 Subject: [PATCH 02/16] Adicionada efeito de click nas conversas com TouchableOpacity --- src/Screens/Conversas/conversas.js | 136 ++++++++++++++++------------- 1 file changed, 73 insertions(+), 63 deletions(-) diff --git a/src/Screens/Conversas/conversas.js b/src/Screens/Conversas/conversas.js index 5a9bd68..49401fa 100644 --- a/src/Screens/Conversas/conversas.js +++ b/src/Screens/Conversas/conversas.js @@ -39,7 +39,12 @@ export default class Conversas extends Component { myPicture: doc.data().profile_img_url }) }) - this.getData() + this.unsubscribe = firebase + .firestore() + .collection("users") + .onSnapshot(() => { + this.getData() + }) } componentWillUnmount() { @@ -50,6 +55,36 @@ export default class Conversas extends Component { return true } + getNumMsgsNaoLidas = () => { + const { conversas } = this.state + // Alert.alert("Check", "1") + conversas.map((conversa, i) => { + // Alert.alert("Check", "2") + this.ref + .collection("conversas") + .doc(conversa.key) + .collection("messages") + .onSnapshot(snapshot => { + snapshot.forEach(msg => { + const { source, unread } = msg.data() + if (source === "2") { + if (unread) { + conversas[i].numUnreadMsgs += 1 + } + } + }) + if (conversas[i].numUnreadMsgs > 0) { + conversas[i].unreadMsgs = true + } + Alert.alert( + "Num msgs na conversa", + conversas[i].numUnreadMsgs.toString() + ) + }) + return true + }) + } + getData = async () => { AsyncStorage.getItem("@contacts").then(contactsResponse => { const contacts = JSON.parse(contactsResponse) @@ -58,47 +93,19 @@ export default class Conversas extends Component { querySnapshot.forEach(doc => { contacts.forEach(contact => { if (contact.key === doc.id) { - let numUnreadMsgs = 0 - this.ref - .collection("conversas") - .doc(doc.id) - .collection("messages") - .onSnapshot(snapshot => { - snapshot.forEach(msg => { - const { source, unread } = msg.data() - if (source === "2") { - if (unread) { - Alert.alert("Numero de msgs", numUnreadMsgs.toString()) - numUnreadMsgs += 1 - Alert.alert("Numero de msgs", numUnreadMsgs.toString()) - } - } - }) - }) - if (numUnreadMsgs > 0) { - Alert.alert("Check", "Tem msgs não lidas") - conversas.push({ - contact, - key: doc.id, - profileImage: contact.profile_img_url, - contactName: contact.contactName, - unreadMsgs: true, - numUnreadMsgs - }) - } else { - // Alert.alert("Check", "Não tem msgs não lidas") - conversas.push({ - contact, - key: doc.id, - profileImage: contact.profile_img_url, - contactName: contact.contactName, - unreadMsgs: false - }) - } + conversas.push({ + contact, + key: doc.id, + profileImage: contact.profile_img_url, + contactName: contact.contactName, + unreadMsgs: false, + numUnreadMsgs: 0 + }) } }) }) this.setState({ conversas }) + this.getNumMsgsNaoLidas() }) }) } @@ -177,38 +184,41 @@ export default class Conversas extends Component { data={conversas} renderItem={({ item }) => { return ( - { this.goToChat(item.contact) }} onLongPress={() => { this.confirmDelete(item.contact) }} - style={styles.conversa} - subtitle={ - - {item.contactName} - {item.lastMessage} - - {item.lastMessage} - {item.unreadMsgs && ( - - - {item.numUnreadMsgs} - - - )} + > + + {item.contactName} + {item.lastMessage} + + {item.lastMessage} + {item.unreadMsgs && ( + + + {item.numUnreadMsgs} + + + )} + - - } - leftAvatar={{ - source: { uri: item.profileImage }, - size: "medium" - }} - /> + } + leftAvatar={{ + source: { uri: item.profileImage }, + size: "medium" + }} + /> + ) }} keyExtractor={i => i.key} From 694f4cc85d093a5f505b736cd35449f4948e1e79 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 11 May 2019 13:42:18 -0300 Subject: [PATCH 03/16] =?UTF-8?q?Adicionado=20adapta=C3=A7=C3=A3o=20para?= =?UTF-8?q?=20pegar=20o=20numero=20de=20msgs=20n=C3=A3o=20lidas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Screens/Conversas/conversas.js | 89 ++++++++++-------------------- 1 file changed, 28 insertions(+), 61 deletions(-) diff --git a/src/Screens/Conversas/conversas.js b/src/Screens/Conversas/conversas.js index 49401fa..22a1e31 100644 --- a/src/Screens/Conversas/conversas.js +++ b/src/Screens/Conversas/conversas.js @@ -55,36 +55,6 @@ export default class Conversas extends Component { return true } - getNumMsgsNaoLidas = () => { - const { conversas } = this.state - // Alert.alert("Check", "1") - conversas.map((conversa, i) => { - // Alert.alert("Check", "2") - this.ref - .collection("conversas") - .doc(conversa.key) - .collection("messages") - .onSnapshot(snapshot => { - snapshot.forEach(msg => { - const { source, unread } = msg.data() - if (source === "2") { - if (unread) { - conversas[i].numUnreadMsgs += 1 - } - } - }) - if (conversas[i].numUnreadMsgs > 0) { - conversas[i].unreadMsgs = true - } - Alert.alert( - "Num msgs na conversa", - conversas[i].numUnreadMsgs.toString() - ) - }) - return true - }) - } - getData = async () => { AsyncStorage.getItem("@contacts").then(contactsResponse => { const contacts = JSON.parse(contactsResponse) @@ -93,19 +63,19 @@ export default class Conversas extends Component { querySnapshot.forEach(doc => { contacts.forEach(contact => { if (contact.key === doc.id) { + const { numUnreadMsgs, unreadMsgs } = doc.data() conversas.push({ contact, key: doc.id, profileImage: contact.profile_img_url, contactName: contact.contactName, - unreadMsgs: false, - numUnreadMsgs: 0 + unreadMsgs, + numUnreadMsgs }) } }) }) this.setState({ conversas }) - this.getNumMsgsNaoLidas() }) }) } @@ -184,41 +154,38 @@ export default class Conversas extends Component { data={conversas} renderItem={({ item }) => { return ( - { this.goToChat(item.contact) }} onLongPress={() => { this.confirmDelete(item.contact) }} - > - - {item.contactName} - {item.lastMessage} - - {item.lastMessage} - {item.unreadMsgs && ( - - - {item.numUnreadMsgs} - - - )} - + style={styles.conversa} + subtitle={ + + {item.contactName} + {item.lastMessage} + + {item.lastMessage} + {item.unreadMsgs && ( + + + {item.numUnreadMsgs} + + + )} - } - leftAvatar={{ - source: { uri: item.profileImage }, - size: "medium" - }} - /> - + + } + leftAvatar={{ + source: { uri: item.profileImage }, + size: "medium" + }} + /> ) }} keyExtractor={i => i.key} From 987b85b8be7b91f14b35ea614e6a4eb5f57e4ea2 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 11 May 2019 13:42:50 -0300 Subject: [PATCH 04/16] =?UTF-8?q?Adicionado=20adapta=C3=A7=C3=A3o=20para?= =?UTF-8?q?=20pegar=20a=20ultima=20msg=20na=20conversa?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Screens/Conversas/conversas.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Screens/Conversas/conversas.js b/src/Screens/Conversas/conversas.js index 22a1e31..6f605f2 100644 --- a/src/Screens/Conversas/conversas.js +++ b/src/Screens/Conversas/conversas.js @@ -63,14 +63,21 @@ export default class Conversas extends Component { querySnapshot.forEach(doc => { contacts.forEach(contact => { if (contact.key === doc.id) { - const { numUnreadMsgs, unreadMsgs } = doc.data() + const { + numUnreadMsgs, + unreadMsgs, + lastMessage, + dateLastMessage + } = doc.data() conversas.push({ contact, key: doc.id, profileImage: contact.profile_img_url, contactName: contact.contactName, unreadMsgs, - numUnreadMsgs + numUnreadMsgs, + lastMessage, + dateLastMessage }) } }) @@ -167,7 +174,9 @@ export default class Conversas extends Component { {item.contactName} {item.lastMessage} - {item.lastMessage} + + {item.dateLastMessage.toDate().toString()} + {item.unreadMsgs && ( Date: Sat, 11 May 2019 14:01:58 -0300 Subject: [PATCH 05/16] =?UTF-8?q?Adicionada=20fun=C3=A7=C3=A3o=20para=20ca?= =?UTF-8?q?lcular=20a=20data=20mostrada=20na=20ultima=20msg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Screens/Conversas/conversas.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Screens/Conversas/conversas.js b/src/Screens/Conversas/conversas.js index 6f605f2..8cecc1a 100644 --- a/src/Screens/Conversas/conversas.js +++ b/src/Screens/Conversas/conversas.js @@ -14,6 +14,7 @@ import { ListItem, Icon } from "react-native-elements" import LinearGradient from "react-native-linear-gradient" import firebase from "react-native-firebase" import AsyncStorage from "@react-native-community/async-storage" +import getTime from "~/functions/getTime" export default class Conversas extends Component { constructor() { @@ -138,6 +139,24 @@ export default class Conversas extends Component { search = () => {} + parseTime = dateNanoScds => { + const date = dateNanoScds.toDate() + const atualDate = firebase.database().getServerTime() + let textDate = "" + if (atualDate.getDate() - date.getDate() === 0) { + textDate = getTime(date) + } else if (atualDate.getDate() - date.getDate() === 1) { + textDate = "Ontem" + } else if (atualDate.getDate() - date.getDate() >= 7) { + textDate = `${date + .getDate() + .toString()}/${date + .getMonth() + .toString()}/${date.getFullYear().toString()}` + } + return textDate + } + render() { const { conversas, myName, myPicture } = this.state return ( @@ -175,7 +194,7 @@ export default class Conversas extends Component { {item.lastMessage} - {item.dateLastMessage.toDate().toString()} + {this.parseTime(item.dateLastMessage)} {item.unreadMsgs && ( Date: Sat, 11 May 2019 14:04:35 -0300 Subject: [PATCH 06/16] =?UTF-8?q?Adicionada=20funcionalidade=20para=20most?= =?UTF-8?q?rar=20o=20numero=20de=20msgs=20n=C3=A3o=20lidas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Screens/Chat/chat.js | 49 ++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/src/Screens/Chat/chat.js b/src/Screens/Chat/chat.js index 39352f8..b09c0c4 100644 --- a/src/Screens/Chat/chat.js +++ b/src/Screens/Chat/chat.js @@ -59,7 +59,6 @@ export default class Conversas extends Component { const messages = [] querySnapshot.forEach(doc => { const { content, contentTranslated, date, source } = doc.data() - const msgRef = this.ref.collection("messages").doc(doc.id) messages.push({ key: doc.id, content, @@ -67,13 +66,12 @@ export default class Conversas extends Component { date: date.toDate(), source }) - if (source === "2") { - firebase.firestore().runTransaction(t => { - return t.get(msgRef).then(() => { - t.update(msgRef, { unread: false }) - }) + firebase.firestore().runTransaction(t => { + return t.get(this.ref).then(() => { + t.update(this.ref, { unreadMsgs: false }) + t.update(this.ref, { numUnreadMsgs: 0 }) }) - } + }) }) this.setState({ messages }) }) @@ -95,11 +93,34 @@ export default class Conversas extends Component { sendMessage = () => { const { destUser, user } = this.state - this.ref.set({ - userKey: destUser.key + this.ref.get().then(doc => { + if (!doc.exists) { + this.ref.set({ + userKey: destUser.key, + unreadMsgs: false, + numUnreadMsgs: 0 + }) + } }) - this.refDest.set({ - userKey: user + + this.refDest.get().then(doc => { + if (!doc.exists) { + this.refDest.set({ + userKey: user, + unreadMsgs: false, + numUnreadMsgs: 0 + }) + } else { + this.refDest.get().then(conversa => { + const { numUnreadMsgs } = conversa.data() + firebase.firestore().runTransaction(t => { + return t.get(this.refDest).then(() => { + t.update(this.refDest, { numUnreadMsgs: numUnreadMsgs + 1 }) + t.update(this.refDest, { unreadMsgs: true }) + }) + }) + }) + } }) const { messageText } = this.state @@ -115,8 +136,7 @@ export default class Conversas extends Component { .add({ content: newMessage.content, date: newMessage.date, - source: newMessage.source, - unread: true + source: newMessage.source }) .then(() => true) .catch(error => error) @@ -129,8 +149,7 @@ export default class Conversas extends Component { content: newMessage.content, date: newMessage.date, contentTranslated: translated, - source: "2", - unread: true + source: "2" }) .then(() => true) .catch(error => error) From a5de6e2dd489ccbab2340e8cd198ad1eda5c7354 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 11 May 2019 14:04:54 -0300 Subject: [PATCH 07/16] Adicionada funcionalidade para mostrar a ultima msg enviada na conversa --- src/Screens/Chat/chat.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/Screens/Chat/chat.js b/src/Screens/Chat/chat.js index b09c0c4..fc458bd 100644 --- a/src/Screens/Chat/chat.js +++ b/src/Screens/Chat/chat.js @@ -66,11 +66,11 @@ export default class Conversas extends Component { date: date.toDate(), source }) - firebase.firestore().runTransaction(t => { - return t.get(this.ref).then(() => { - t.update(this.ref, { unreadMsgs: false }) - t.update(this.ref, { numUnreadMsgs: 0 }) - }) + }) + firebase.firestore().runTransaction(t => { + return t.get(this.ref).then(() => { + t.update(this.ref, { unreadMsgs: false }) + t.update(this.ref, { numUnreadMsgs: 0 }) }) }) this.setState({ messages }) @@ -92,13 +92,15 @@ export default class Conversas extends Component { } sendMessage = () => { - const { destUser, user } = this.state + const { destUser, user, messageText } = this.state this.ref.get().then(doc => { if (!doc.exists) { this.ref.set({ userKey: destUser.key, unreadMsgs: false, - numUnreadMsgs: 0 + numUnreadMsgs: 0, + lastMessage: "", + dateLastMessage: "" }) } }) @@ -108,7 +110,9 @@ export default class Conversas extends Component { this.refDest.set({ userKey: user, unreadMsgs: false, - numUnreadMsgs: 0 + numUnreadMsgs: 0, + lastMessage: "", + dateLastMessage: "" }) } else { this.refDest.get().then(conversa => { @@ -122,8 +126,6 @@ export default class Conversas extends Component { }) } }) - - const { messageText } = this.state if (messageText === "") this.setState({ isValueNull: true }) const newMessage = { content: messageText, @@ -143,6 +145,19 @@ export default class Conversas extends Component { const translator = TranslatorFactory.createTranslator() translator.translate(messageText, "en").then(translated => { + firebase.firestore().runTransaction(t => { + return t.get(this.refDest).then(() => { + t.update(this.refDest, { lastMessage: translated }) + t.update(this.refDest, { dateLastMessage: newMessage.date }) + }) + }) + firebase.firestore().runTransaction(t => { + return t.get(this.ref).then(() => { + t.update(this.ref, { lastMessage: newMessage.content }) + t.update(this.ref, { dateLastMessage: newMessage.date }) + }) + }) + this.refDest .collection("messages") .add({ From 79013a36e68a327f5642bab319454157c3ae2a63 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 11 May 2019 17:58:40 -0300 Subject: [PATCH 08/16] Bug que fechava o app ao enviar mensagem corrigido --- src/Screens/Chat/chat.js | 141 +++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 71 deletions(-) diff --git a/src/Screens/Chat/chat.js b/src/Screens/Chat/chat.js index fc458bd..f8bc452 100644 --- a/src/Screens/Chat/chat.js +++ b/src/Screens/Chat/chat.js @@ -67,12 +67,7 @@ export default class Conversas extends Component { source }) }) - firebase.firestore().runTransaction(t => { - return t.get(this.ref).then(() => { - t.update(this.ref, { unreadMsgs: false }) - t.update(this.ref, { numUnreadMsgs: 0 }) - }) - }) + this.ref.update({ unreadMsgs: false, numUnreadMsgs: 0 }) this.setState({ messages }) }) } @@ -93,84 +88,88 @@ export default class Conversas extends Component { sendMessage = () => { const { destUser, user, messageText } = this.state - this.ref.get().then(doc => { - if (!doc.exists) { - this.ref.set({ - userKey: destUser.key, - unreadMsgs: false, - numUnreadMsgs: 0, - lastMessage: "", - dateLastMessage: "" - }) + if (messageText === "") { + this.setState({ isValueNull: true }) + } else { + const newMessage = { + content: messageText, + date: firebase.database().getServerTime(), + source: "1" } - }) - - this.refDest.get().then(doc => { - if (!doc.exists) { - this.refDest.set({ - userKey: user, - unreadMsgs: false, - numUnreadMsgs: 0, - lastMessage: "", - dateLastMessage: "" - }) - } else { - this.refDest.get().then(conversa => { - const { numUnreadMsgs } = conversa.data() - firebase.firestore().runTransaction(t => { - return t.get(this.refDest).then(() => { - t.update(this.refDest, { numUnreadMsgs: numUnreadMsgs + 1 }) - t.update(this.refDest, { unreadMsgs: true }) - }) + this.ref.get().then(doc => { + if (!doc.exists) { + this.ref.set({ + userKey: destUser.key, + unreadMsgs: false, + numUnreadMsgs: 0, + lastMessage: newMessage.content, + dateLastMessage: newMessage.date }) - }) - } - }) - if (messageText === "") this.setState({ isValueNull: true }) - const newMessage = { - content: messageText, - date: firebase.database().getServerTime(), - source: "1" - } - - this.ref - .collection("messages") - .add({ - content: newMessage.content, - date: newMessage.date, - source: newMessage.source - }) - .then(() => true) - .catch(error => error) - - const translator = TranslatorFactory.createTranslator() - translator.translate(messageText, "en").then(translated => { - firebase.firestore().runTransaction(t => { - return t.get(this.refDest).then(() => { - t.update(this.refDest, { lastMessage: translated }) - t.update(this.refDest, { dateLastMessage: newMessage.date }) - }) + } else { + this.ref.update({ + lastMessage: newMessage.content, + dateLastMessage: newMessage.date + }) + } }) - firebase.firestore().runTransaction(t => { - return t.get(this.ref).then(() => { - t.update(this.ref, { lastMessage: newMessage.content }) - t.update(this.ref, { dateLastMessage: newMessage.date }) - }) + this.refDest.get().then(doc => { + if (!doc.exists) { + this.refDest.set({ + userKey: user, + unreadMsgs: false, + numUnreadMsgs: 0, + lastMessage: "", + dateLastMessage: "" + }) + this.refDest.get().then(conversa => { + const { numUnreadMsgs } = conversa.data() + this.refDest.update({ + numUnreadMsgs: numUnreadMsgs + 1, + unreadMsgs: true + }) + }) + } else { + this.refDest.get().then(conversa => { + const { numUnreadMsgs } = conversa.data() + this.refDest.update({ + numUnreadMsgs: numUnreadMsgs + 1, + unreadMsgs: true + }) + }) + } }) - this.refDest + this.ref .collection("messages") .add({ content: newMessage.content, date: newMessage.date, - contentTranslated: translated, - source: "2" + source: newMessage.source }) .then(() => true) .catch(error => error) - }) - this.setState({ messageText: "", isValueNull: true }) + const translator = TranslatorFactory.createTranslator() + translator.translate(messageText, "en").then(translated => { + this.refDest.update({ + lastMessage: translated, + dateLastMessage: newMessage.date + }) + + this.refDest + .collection("messages") + .add({ + content: newMessage.content, + date: newMessage.date, + contentTranslated: translated, + source: "2" + }) + .then(() => true) + .catch(error => error) + }) + + this.setState({ messageText: "", isValueNull: true }) + } } render() { From fb1cdaa49bba282a318de18f588f5a34d5c430f3 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 11 May 2019 18:48:44 -0300 Subject: [PATCH 09/16] =?UTF-8?q?Adicionada=20reestrutura=C3=A7=C3=A3o=20d?= =?UTF-8?q?a=20string=20no=20last=20msg=20da=20conversa?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Screens/Chat/chat.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Screens/Chat/chat.js b/src/Screens/Chat/chat.js index f8bc452..78dd077 100644 --- a/src/Screens/Chat/chat.js +++ b/src/Screens/Chat/chat.js @@ -86,6 +86,16 @@ export default class Conversas extends Component { this.setState({ messageText: text, isValueNull: false }) } + proccessLastMsg = string => { + let strProcs = "" + if (string.length >= 25) { + strProcs = `${string.substr(0, 25)}...` + } else { + strProcs = string + } + return strProcs + } + sendMessage = () => { const { destUser, user, messageText } = this.state if (messageText === "") { @@ -102,12 +112,12 @@ export default class Conversas extends Component { userKey: destUser.key, unreadMsgs: false, numUnreadMsgs: 0, - lastMessage: newMessage.content, + lastMessage: this.proccessLastMsg(newMessage.content), dateLastMessage: newMessage.date }) } else { this.ref.update({ - lastMessage: newMessage.content, + lastMessage: this.proccessLastMsg(newMessage.content), dateLastMessage: newMessage.date }) } @@ -152,7 +162,7 @@ export default class Conversas extends Component { const translator = TranslatorFactory.createTranslator() translator.translate(messageText, "en").then(translated => { this.refDest.update({ - lastMessage: translated, + lastMessage: this.proccessLastMsg(translated), dateLastMessage: newMessage.date }) From bb780bdc61b13587cc051c76541b85d147608dfc Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 13 May 2019 19:27:38 -0300 Subject: [PATCH 10/16] Corrigido bug da warning repetida e ocasional erro relacionado ao toDate() --- src/Screens/Chat/chat.js | 47 +++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/src/Screens/Chat/chat.js b/src/Screens/Chat/chat.js index 78dd077..168cddf 100644 --- a/src/Screens/Chat/chat.js +++ b/src/Screens/Chat/chat.js @@ -66,8 +66,8 @@ export default class Conversas extends Component { date: date.toDate(), source }) + this.ref.update({ unreadMsgs: false, numUnreadMsgs: 0 }) }) - this.ref.update({ unreadMsgs: false, numUnreadMsgs: 0 }) this.setState({ messages }) }) } @@ -106,6 +106,7 @@ export default class Conversas extends Component { date: firebase.database().getServerTime(), source: "1" } + this.ref.get().then(doc => { if (!doc.exists) { this.ref.set({ @@ -122,32 +123,6 @@ export default class Conversas extends Component { }) } }) - this.refDest.get().then(doc => { - if (!doc.exists) { - this.refDest.set({ - userKey: user, - unreadMsgs: false, - numUnreadMsgs: 0, - lastMessage: "", - dateLastMessage: "" - }) - this.refDest.get().then(conversa => { - const { numUnreadMsgs } = conversa.data() - this.refDest.update({ - numUnreadMsgs: numUnreadMsgs + 1, - unreadMsgs: true - }) - }) - } else { - this.refDest.get().then(conversa => { - const { numUnreadMsgs } = conversa.data() - this.refDest.update({ - numUnreadMsgs: numUnreadMsgs + 1, - unreadMsgs: true - }) - }) - } - }) this.ref .collection("messages") @@ -176,6 +151,24 @@ export default class Conversas extends Component { }) .then(() => true) .catch(error => error) + + this.refDest.get().then(doc => { + if (!doc.exists) { + this.refDest.set({ + userKey: user, + unreadMsgs: true, + numUnreadMsgs: 1, + lastMessage: translated, + dateLastMessage: newMessage.date + }) + } else { + const { numUnreadMsgs } = doc.data() + this.refDest.update({ + numUnreadMsgs: numUnreadMsgs + 1, + unreadMsgs: true + }) + } + }) }) this.setState({ messageText: "", isValueNull: true }) From ae48bb58c0a93d12974c530a714184c292f799d1 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 13 May 2019 19:42:32 -0300 Subject: [PATCH 11/16] =?UTF-8?q?Corre=C3=A7=C3=A3o=20da=20corre=C3=A7?= =?UTF-8?q?=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Screens/Chat/chat.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Screens/Chat/chat.js b/src/Screens/Chat/chat.js index 168cddf..1eae60f 100644 --- a/src/Screens/Chat/chat.js +++ b/src/Screens/Chat/chat.js @@ -136,39 +136,35 @@ export default class Conversas extends Component { const translator = TranslatorFactory.createTranslator() translator.translate(messageText, "en").then(translated => { - this.refDest.update({ - lastMessage: this.proccessLastMsg(translated), - dateLastMessage: newMessage.date - }) - - this.refDest - .collection("messages") - .add({ - content: newMessage.content, - date: newMessage.date, - contentTranslated: translated, - source: "2" - }) - .then(() => true) - .catch(error => error) - this.refDest.get().then(doc => { if (!doc.exists) { this.refDest.set({ userKey: user, unreadMsgs: true, numUnreadMsgs: 1, - lastMessage: translated, + lastMessage: this.proccessLastMsg(translated), dateLastMessage: newMessage.date }) } else { const { numUnreadMsgs } = doc.data() this.refDest.update({ numUnreadMsgs: numUnreadMsgs + 1, - unreadMsgs: true + unreadMsgs: true, + lastMessage: this.proccessLastMsg(translated), + dateLastMessage: newMessage.date }) } }) + this.refDest + .collection("messages") + .add({ + content: newMessage.content, + date: newMessage.date, + contentTranslated: translated, + source: "2" + }) + .then(() => true) + .catch(error => error) }) this.setState({ messageText: "", isValueNull: true }) From 45513799ef35234deba4d2dda03e1b3215ef546a Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 13 May 2019 20:40:34 -0300 Subject: [PATCH 12/16] bug fix warnings --- src/Screens/Chat/chat.js | 22 ++++++++++++++-------- src/Screens/Conversas/conversas.js | 8 +------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Screens/Chat/chat.js b/src/Screens/Chat/chat.js index 1eae60f..c9fbfb2 100644 --- a/src/Screens/Chat/chat.js +++ b/src/Screens/Chat/chat.js @@ -58,15 +58,21 @@ export default class Conversas extends Component { .onSnapshot(querySnapshot => { const messages = [] querySnapshot.forEach(doc => { - const { content, contentTranslated, date, source } = doc.data() - messages.push({ - key: doc.id, - content, - contentTranslated, - date: date.toDate(), - source + if (doc.exists) { + const { content, contentTranslated, date, source } = doc.data() + messages.push({ + key: doc.id, + content, + contentTranslated, + date: date.toDate(), + source + }) + } + this.ref.get().then(conversa => { + if (conversa.exists) { + this.ref.update({ unreadMsgs: false, numUnreadMsgs: 0 }) + } }) - this.ref.update({ unreadMsgs: false, numUnreadMsgs: 0 }) }) this.setState({ messages }) }) diff --git a/src/Screens/Conversas/conversas.js b/src/Screens/Conversas/conversas.js index 8cecc1a..178961a 100644 --- a/src/Screens/Conversas/conversas.js +++ b/src/Screens/Conversas/conversas.js @@ -24,7 +24,6 @@ export default class Conversas extends Component { myName: "", myPicture: null } - this.lastMessage = null this.ref = firebase .firestore() @@ -40,12 +39,7 @@ export default class Conversas extends Component { myPicture: doc.data().profile_img_url }) }) - this.unsubscribe = firebase - .firestore() - .collection("users") - .onSnapshot(() => { - this.getData() - }) + this.getData() } componentWillUnmount() { From a2227a790a4e5353ddcffede164a7fd6470a5b0d Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 13 May 2019 21:38:36 -0300 Subject: [PATCH 13/16] =?UTF-8?q?Corrigido=20forma=20de=20navega=C3=A7?= =?UTF-8?q?=C3=A3o=20entre=20telas=20com=20StackActions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Components/Chat/chatHeader.js | 19 ++++++++++++---- src/Screens/Chat/chat.js | 35 ++++++++++++++++++++++-------- src/Screens/Conversas/conversas.js | 15 ++++++++++++- 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/src/Components/Chat/chatHeader.js b/src/Components/Chat/chatHeader.js index bc95917..0e265df 100644 --- a/src/Components/Chat/chatHeader.js +++ b/src/Components/Chat/chatHeader.js @@ -2,16 +2,27 @@ import React from "react" import { View, StyleSheet, Text, TouchableOpacity } from "react-native" import { Avatar, Icon } from "react-native-elements" +import { StackActions, NavigationActions } from "react-navigation" const chatHeader = props => { const { userName, userPhoto, navigation } = props + const goBack = () => { + const resetAction = StackActions.reset({ + index: 0, + key: null, + actions: [ + NavigationActions.navigate({ + routeName: "Conversas" + }) + ] + }) + navigation.dispatch(resetAction) + } + return ( - navigation.goBack()} - > + goBack()}> { - if (conversa.exists) { - this.ref.update({ unreadMsgs: false, numUnreadMsgs: 0 }) - } - }) + if (isMounted) { + this.ref.get().then(conversa => { + if (conversa.exists) { + this.ref.update({ unreadMsgs: false, numUnreadMsgs: 0 }) + } + }) + } }) this.setState({ messages }) }) @@ -84,7 +91,17 @@ export default class Conversas extends Component { handleBackPress = () => { const { navigation } = this.props - navigation.goBack() + const resetAction = StackActions.reset({ + index: 0, + key: null, + actions: [ + NavigationActions.navigate({ + routeName: "Conversas" + }) + ] + }) + this.setState({ isMounted: false }) + navigation.dispatch(resetAction) return true } diff --git a/src/Screens/Conversas/conversas.js b/src/Screens/Conversas/conversas.js index 178961a..ebd595f 100644 --- a/src/Screens/Conversas/conversas.js +++ b/src/Screens/Conversas/conversas.js @@ -10,6 +10,7 @@ import { BackHandler, Alert } from "react-native" +import { StackActions, NavigationActions } from "react-navigation" import { ListItem, Icon } from "react-native-elements" import LinearGradient from "react-native-linear-gradient" import firebase from "react-native-firebase" @@ -32,6 +33,8 @@ export default class Conversas extends Component { } componentDidMount() { + const { navigation } = this.props + Alert.alert("Testando apenas", navigation.state.routeName) BackHandler.addEventListener("hardwareBackPress", this.handleBackPress) this.ref.get().then(doc => { this.setState({ @@ -84,7 +87,17 @@ export default class Conversas extends Component { goToChat = item => { const { navigation } = this.props - navigation.navigate("ChatScreen", { item }) + const resetAction = StackActions.reset({ + index: 0, + key: null, + actions: [ + NavigationActions.navigate({ + routeName: "ChatScreen", + params: { item } + }) + ] + }) + navigation.dispatch(resetAction) } confirmDelete = item => { From dfb80df8a9a539cd6e1f7fbd3a5e91727aa1c69e Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 14 May 2019 10:59:17 -0300 Subject: [PATCH 14/16] Todas as warnings e problemas foram corrigidos --- src/Components/Chat/chatHeader.js | 27 +++++++++++----------- src/Screens/Chat/chat.js | 36 +++++++++++------------------- src/Screens/Conversas/conversas.js | 29 ++++++++++++------------ 3 files changed, 40 insertions(+), 52 deletions(-) diff --git a/src/Components/Chat/chatHeader.js b/src/Components/Chat/chatHeader.js index 0e265df..e56992a 100644 --- a/src/Components/Chat/chatHeader.js +++ b/src/Components/Chat/chatHeader.js @@ -2,27 +2,26 @@ import React from "react" import { View, StyleSheet, Text, TouchableOpacity } from "react-native" import { Avatar, Icon } from "react-native-elements" -import { StackActions, NavigationActions } from "react-navigation" const chatHeader = props => { const { userName, userPhoto, navigation } = props - const goBack = () => { - const resetAction = StackActions.reset({ - index: 0, - key: null, - actions: [ - NavigationActions.navigate({ - routeName: "Conversas" - }) - ] - }) - navigation.dispatch(resetAction) - } + // const goBack = () => { + // const resetAction = StackActions.reset({ + // index: 0, + // key: null, + // actions: [ + // NavigationActions.navigate({ + // routeName: "Conversas" + // }) + // ] + // }) + // navigation.dispatch(resetAction) + // } return ( - goBack()}> + navigation.goBack()}> { const messages = [] querySnapshot.forEach(doc => { - if (doc.exists) { const { content, contentTranslated, date, source } = doc.data() messages.push({ key: doc.id, @@ -72,14 +65,11 @@ export default class Conversas extends Component { date: date.toDate(), source }) - } - if (isMounted) { this.ref.get().then(conversa => { if (conversa.exists) { this.ref.update({ unreadMsgs: false, numUnreadMsgs: 0 }) } }) - } }) this.setState({ messages }) }) @@ -87,21 +77,21 @@ export default class Conversas extends Component { componentWillUnmount() { BackHandler.removeEventListener("hardwareBackPress", this.handleBackPress) + this.unsubscribe() } handleBackPress = () => { const { navigation } = this.props - const resetAction = StackActions.reset({ - index: 0, - key: null, - actions: [ - NavigationActions.navigate({ - routeName: "Conversas" - }) - ] - }) - this.setState({ isMounted: false }) - navigation.dispatch(resetAction) + // const resetAction = StackActions.reset({ + // index: 0, + // key: null, + // actions: [ + // NavigationActions.navigate({ + // routeName: "Conversas" + // }) + // ] + // }) + navigation.goBack() return true } diff --git a/src/Screens/Conversas/conversas.js b/src/Screens/Conversas/conversas.js index ebd595f..a360010 100644 --- a/src/Screens/Conversas/conversas.js +++ b/src/Screens/Conversas/conversas.js @@ -10,7 +10,6 @@ import { BackHandler, Alert } from "react-native" -import { StackActions, NavigationActions } from "react-navigation" import { ListItem, Icon } from "react-native-elements" import LinearGradient from "react-native-linear-gradient" import firebase from "react-native-firebase" @@ -33,8 +32,6 @@ export default class Conversas extends Component { } componentDidMount() { - const { navigation } = this.props - Alert.alert("Testando apenas", navigation.state.routeName) BackHandler.addEventListener("hardwareBackPress", this.handleBackPress) this.ref.get().then(doc => { this.setState({ @@ -47,6 +44,7 @@ export default class Conversas extends Component { componentWillUnmount() { BackHandler.removeEventListener("hardwareBackPress", this.handleBackPress) + this.unsubscribe() } handleBackPress = () => { @@ -56,7 +54,7 @@ export default class Conversas extends Component { getData = async () => { AsyncStorage.getItem("@contacts").then(contactsResponse => { const contacts = JSON.parse(contactsResponse) - this.ref.collection("conversas").onSnapshot(querySnapshot => { + this.unsubscribe = this.ref.collection("conversas").onSnapshot(querySnapshot => { const conversas = [] querySnapshot.forEach(doc => { contacts.forEach(contact => { @@ -87,17 +85,18 @@ export default class Conversas extends Component { goToChat = item => { const { navigation } = this.props - const resetAction = StackActions.reset({ - index: 0, - key: null, - actions: [ - NavigationActions.navigate({ - routeName: "ChatScreen", - params: { item } - }) - ] - }) - navigation.dispatch(resetAction) + // const resetAction = StackActions.reset({ + // index: 0, + // key: null, + // actions: [ + // NavigationActions.navigate({ + // routeName: "ChatScreen", + // params: { item } + // }) + // ] + // }) + // navigation.dispatch(resetAction) + navigation.navigate("ChatScreen", { item }) } confirmDelete = item => { From d8685ce13170aa973eb4ab0efbd28cbdb9f34996 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 14 May 2019 11:15:36 -0300 Subject: [PATCH 15/16] Removidos comentarios --- src/Components/Chat/chatHeader.js | 13 ------------- src/Screens/Chat/chat.js | 9 --------- src/Screens/Conversas/conversas.js | 11 ----------- 3 files changed, 33 deletions(-) diff --git a/src/Components/Chat/chatHeader.js b/src/Components/Chat/chatHeader.js index e56992a..d994939 100644 --- a/src/Components/Chat/chatHeader.js +++ b/src/Components/Chat/chatHeader.js @@ -5,19 +5,6 @@ import { Avatar, Icon } from "react-native-elements" const chatHeader = props => { const { userName, userPhoto, navigation } = props - // const goBack = () => { - // const resetAction = StackActions.reset({ - // index: 0, - // key: null, - // actions: [ - // NavigationActions.navigate({ - // routeName: "Conversas" - // }) - // ] - // }) - // navigation.dispatch(resetAction) - // } - return ( diff --git a/src/Screens/Chat/chat.js b/src/Screens/Chat/chat.js index 631be36..c3d746b 100644 --- a/src/Screens/Chat/chat.js +++ b/src/Screens/Chat/chat.js @@ -82,15 +82,6 @@ export default class Conversas extends Component { handleBackPress = () => { const { navigation } = this.props - // const resetAction = StackActions.reset({ - // index: 0, - // key: null, - // actions: [ - // NavigationActions.navigate({ - // routeName: "Conversas" - // }) - // ] - // }) navigation.goBack() return true } diff --git a/src/Screens/Conversas/conversas.js b/src/Screens/Conversas/conversas.js index a360010..e53f78c 100644 --- a/src/Screens/Conversas/conversas.js +++ b/src/Screens/Conversas/conversas.js @@ -85,17 +85,6 @@ export default class Conversas extends Component { goToChat = item => { const { navigation } = this.props - // const resetAction = StackActions.reset({ - // index: 0, - // key: null, - // actions: [ - // NavigationActions.navigate({ - // routeName: "ChatScreen", - // params: { item } - // }) - // ] - // }) - // navigation.dispatch(resetAction) navigation.navigate("ChatScreen", { item }) } From 21c7afc779d0f72fb4d092ee7d6c37bce6198eec Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 14 May 2019 14:53:24 -0300 Subject: [PATCH 16/16] =?UTF-8?q?Corre=C3=A7=C3=A3o=20prettier-formater=20?= =?UTF-8?q?e=20data=20da=20ultima=20mensagem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Components/Chat/chatHeader.js | 5 ++- src/Screens/Conversas/conversas.js | 52 ++++++++++++++++-------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/Components/Chat/chatHeader.js b/src/Components/Chat/chatHeader.js index d994939..bc95917 100644 --- a/src/Components/Chat/chatHeader.js +++ b/src/Components/Chat/chatHeader.js @@ -8,7 +8,10 @@ const chatHeader = props => { return ( - navigation.goBack()}> + navigation.goBack()} + > { AsyncStorage.getItem("@contacts").then(contactsResponse => { const contacts = JSON.parse(contactsResponse) - this.unsubscribe = this.ref.collection("conversas").onSnapshot(querySnapshot => { - const conversas = [] - querySnapshot.forEach(doc => { - contacts.forEach(contact => { - if (contact.key === doc.id) { - const { - numUnreadMsgs, - unreadMsgs, - lastMessage, - dateLastMessage - } = doc.data() - conversas.push({ - contact, - key: doc.id, - profileImage: contact.profile_img_url, - contactName: contact.contactName, - unreadMsgs, - numUnreadMsgs, - lastMessage, - dateLastMessage - }) - } + this.unsubscribe = this.ref + .collection("conversas") + .onSnapshot(querySnapshot => { + const conversas = [] + querySnapshot.forEach(doc => { + contacts.forEach(contact => { + if (contact.key === doc.id) { + const { + numUnreadMsgs, + unreadMsgs, + lastMessage, + dateLastMessage + } = doc.data() + conversas.push({ + contact, + key: doc.id, + profileImage: contact.profile_img_url, + contactName: contact.contactName, + unreadMsgs, + numUnreadMsgs, + lastMessage, + dateLastMessage + }) + } + }) }) + this.setState({ conversas }) }) - this.setState({ conversas }) - }) }) } @@ -142,7 +144,7 @@ export default class Conversas extends Component { textDate = getTime(date) } else if (atualDate.getDate() - date.getDate() === 1) { textDate = "Ontem" - } else if (atualDate.getDate() - date.getDate() >= 7) { + } else if (atualDate.getDate() - date.getDate() >= 2) { textDate = `${date .getDate() .toString()}/${date