RoomsListView re-render (#304)

<!-- INSTRUCTION: Keep the line below to notify all core developers about this new PR -->
@RocketChat/ReactNative

<!-- INSTRUCTION: Inform the issue number that this PR closes, or remove the line below -->

<!-- INSTRUCTION: Tell us more about your PR with screen shots if you can -->
- [x] Removed unnecessary re-renders on RoomsListView
This commit is contained in:
Diego Mello 2018-05-24 17:17:00 -03:00 committed by Guilherme Gazzo
parent 182ab69d6f
commit 8f90565e55
5 changed files with 41 additions and 17 deletions

View File

@ -12,6 +12,6 @@ if (__DEV__) {
.connect(); .connect();
// Running on android device // Running on android device
// $ adb reverse tcp:9090 tcp:9090 // $ adb reverse tcp:9090 tcp:9090
// Reactotron.clear(); Reactotron.clear();
// console.warn = Reactotron.log; console.warn = Reactotron.log;
} }

View File

@ -4,9 +4,13 @@ export default fn => (params) => {
try { try {
fn(params); fn(params);
} catch (e) { } catch (e) {
Answers.logCustom('error', e); let error = e;
if (typeof error !== 'object') {
error = { error };
}
Answers.logCustom('error', error);
if (__DEV__) { if (__DEV__) {
alert(e); alert(error);
} }
} }
}; };

View File

@ -149,7 +149,7 @@ const renderNumber = (unread, userMentions) => {
); );
}; };
const attrs = ['name', 'unread', 'userMentions', 'alert', 'showLastMessage', 'type', '_updatedAt']; const attrs = ['name', 'unread', 'userMentions', 'alert', 'showLastMessage', 'type'];
@connect(state => ({ @connect(state => ({
user: state.login && state.login.user, user: state.login && state.login.user,
StoreLastMessage: state.settings.Store_Last_Message StoreLastMessage: state.settings.Store_Last_Message
@ -183,7 +183,10 @@ export default class RoomItem extends React.Component {
const oldlastMessage = this.props.lastMessage; const oldlastMessage = this.props.lastMessage;
const newLastmessage = nextProps.lastMessage; const newLastmessage = nextProps.lastMessage;
if (oldlastMessage && newLastmessage && oldlastMessage.ts !== newLastmessage.ts) { if (oldlastMessage && newLastmessage && oldlastMessage.ts.toGMTString() !== newLastmessage.ts.toGMTString()) {
return true;
}
if (this.props._updatedAt && nextProps._updatedAt && nextProps._updatedAt.toGMTString() !== this.props._updatedAt.toGMTString()) {
return true; return true;
} }
return attrs.some(key => nextProps[key] !== this.props[key]); return attrs.some(key => nextProps[key] !== this.props[key]);

View File

@ -1,6 +1,9 @@
import { Answers } from 'react-native-fabric'; import { Answers } from 'react-native-fabric';
export default (event, error) => { export default (event, error) => {
if (typeof error !== 'object') {
error = { error };
}
Answers.logCustom(event, error); Answers.logCustom(event, error);
if (__DEV__) { if (__DEV__) {
console.warn(event, error); console.warn(event, error);

View File

@ -8,11 +8,10 @@ import { connect } from 'react-redux';
import database from '../../lib/realm'; import database from '../../lib/realm';
import RocketChat from '../../lib/rocketchat'; import RocketChat from '../../lib/rocketchat';
import RoomItem from '../../presentation/RoomItem'; import RoomItem from '../../presentation/RoomItem';
import { goRoom } from '../../containers/routes/NavigationService';
import Header from '../../containers/Header'; import Header from '../../containers/Header';
import RoomsListHeader from './Header'; import RoomsListHeader from './Header';
import styles from './styles'; import styles from './styles';
import throttle from '../../utils/throttle'; import debounce from '../../utils/debounce';
import LoggedView from '../View'; import LoggedView from '../View';
import log from '../../utils/log'; import log from '../../utils/log';
@ -39,7 +38,8 @@ export default class RoomsListView extends LoggedView {
super('RoomsListView', props); super('RoomsListView', props);
this.state = { this.state = {
search: [] search: [],
rooms: []
}; };
this._keyExtractor = this._keyExtractor.bind(this); this._keyExtractor = this._keyExtractor.bind(this);
this.data = database.objects('subscriptions').filtered('archived != true && open == true').sorted('roomUpdatedAt', true); this.data = database.objects('subscriptions').filtered('archived != true && open == true').sorted('roomUpdatedAt', true);
@ -68,10 +68,10 @@ export default class RoomsListView extends LoggedView {
this.search(text); this.search(text);
} }
updateState = throttle(() => { updateState = debounce(() => {
LayoutAnimation.easeInEaseOut(); LayoutAnimation.easeInEaseOut();
this.forceUpdate(); this.setState({ rooms: this.data.slice() });
}, 1500); })
async search(text) { async search(text) {
const searchText = text.trim(); const searchText = text.trim();
@ -118,20 +118,33 @@ export default class RoomsListView extends LoggedView {
} }
} }
goRoom = (rid, name) => {
this.props.navigation.navigate({
key: `Room-${ rid }`,
routeName: 'Room',
params: { room: { rid, name }, rid, name }
});
}
_onPressItem = async(item = {}) => { _onPressItem = async(item = {}) => {
if (!item.search) { if (!item.search) {
return goRoom({ rid: item.rid, name: item.name }); const { rid, name } = item;
return this.goRoom(rid, name);
} }
if (item.t === 'd') { if (item.t === 'd') {
// if user is using the search we need first to join/create room // if user is using the search we need first to join/create room
try { try {
const sub = await RocketChat.createDirectMessage(item.username); const { username } = item;
return goRoom(sub); const sub = await RocketChat.createDirectMessage(username);
const { rid } = sub;
return this.goRoom(rid, username);
} catch (e) { } catch (e) {
log('RoomsListView._onPressItem', e); log('RoomsListView._onPressItem', e);
} }
} else {
const { rid, name } = item;
return this.goRoom(rid, name);
} }
return goRoom(item);
} }
createChannel() { createChannel() {
@ -184,7 +197,8 @@ export default class RoomsListView extends LoggedView {
renderList = () => ( renderList = () => (
<FlatList <FlatList
data={this.state.search.length > 0 ? this.state.search : this.data} data={this.state.search.length > 0 ? this.state.search : this.state.rooms}
extraData={this.state.search.length > 0 ? this.state.search : this.state.rooms}
keyExtractor={this._keyExtractor} keyExtractor={this._keyExtractor}
style={styles.list} style={styles.list}
renderItem={this.renderItem} renderItem={this.renderItem}