From 1321c26dbd7aee3773ceeb53aeb3b843a8ae5b2b Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 14 Jun 2019 15:24:34 -0300 Subject: [PATCH] Adicionado funcionalidade de infinity scroll --- src/Components/Chat/chatContainer.js | 5 +- src/Screens/Chat/chat.js | 74 ++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/Components/Chat/chatContainer.js b/src/Components/Chat/chatContainer.js index c84473d..3edddb0 100644 --- a/src/Components/Chat/chatContainer.js +++ b/src/Components/Chat/chatContainer.js @@ -1,3 +1,4 @@ +/* eslint-disable react-native/no-inline-styles */ import React from "react" import { View, FlatList } from "react-native" @@ -5,10 +6,12 @@ import getTime from "~/functions/getTime" import Message from "../mensagem" const Chat = props => { - const { messages, destUserUid } = props + const { messages, destUserUid, onLoadMore } = props return ( item.key} diff --git a/src/Screens/Chat/chat.js b/src/Screens/Chat/chat.js index 0a65e47..a70a9a1 100644 --- a/src/Screens/Chat/chat.js +++ b/src/Screens/Chat/chat.js @@ -1,7 +1,14 @@ +/* eslint-disable react-native/no-inline-styles */ /* eslint-disable camelcase */ import React, { Component } from "react" -import { View, StyleSheet, StatusBar, BackHandler } from "react-native" +import { + View, + StyleSheet, + StatusBar, + BackHandler, + ActivityIndicator +} from "react-native" import { ProviderTypes, TranslatorConfiguration, @@ -29,7 +36,10 @@ export default class Conversas extends Component { userData: null, isValueNull: true, destUser: navigation.getParam("item"), - status: "" + status: "", + numMsgsRender: 20, + isRefreshing: false, + load: true } const { user, destUser } = this.state this.ref = firebase @@ -69,6 +79,7 @@ export default class Conversas extends Component { } componentDidMount() { + const { numMsgsRender } = this.state BackHandler.addEventListener("hardwareBackPress", this.handleBackPress) this.unsubscribeDest = this.userDest.onSnapshot(() => { this.getTime() @@ -76,6 +87,7 @@ export default class Conversas extends Component { this.unsubscribe = this.ref .collection("messages") .orderBy("date", "desc") + .limit(numMsgsRender) .onSnapshot(querySnapshot => { const messages = [] querySnapshot.forEach(doc => { @@ -103,6 +115,44 @@ export default class Conversas extends Component { this.unsubscribe() } + handleLoadMore = () => { + const { numMsgsRender, load } = this.state + + this.ref + .collection("messages") + .get() + .then(doc => { + if (numMsgsRender >= doc.size) { + this.setState({ load: false }) + } else { + this.setState({ load: true }) + } + }) + if (load) { + this.setState({ isRefreshing: true }) + this.setState({ numMsgsRender: numMsgsRender + 30 }) + this.ref + .collection("messages") + .orderBy("date", "desc") + .limit(numMsgsRender) + .get() + .then(doc => { + const messages = [] + doc.forEach(msgs => { + const { content, contentTranslated, date, source } = msgs.data() + messages.push({ + key: msgs.id, + content, + contentTranslated, + date: date.toDate(), + source + }) + }) + this.setState({ isRefreshing: false, messages }) + }) + } + } + handleBackPress = () => { const { navigation } = this.props navigation.goBack() @@ -255,7 +305,14 @@ export default class Conversas extends Component { } render() { - const { messages, messageText, isValueNull, destUser, status } = this.state + const { + messages, + messageText, + isValueNull, + destUser, + status, + isRefreshing + } = this.state const { navigation } = this.props // firebase.auth().signOut() return ( @@ -268,7 +325,16 @@ export default class Conversas extends Component { status={status} /> - + {isRefreshing && ( + + + + )} +