Better reactivity

This commit is contained in:
Rodrigo Nascimento 2017-08-11 14:05:30 -03:00
parent 0b702dc4ca
commit 0e1cb6e5ed
4 changed files with 47 additions and 24 deletions

View File

@ -8,14 +8,10 @@ import RoomsListView from './views/roomsList';
import RoomView from './views/room'; import RoomView from './views/room';
import CreateChannel from './views/CreateChannel'; import CreateChannel from './views/CreateChannel';
const position = Platform.OS === 'ios' ? 'headerLeft' : 'headerRight';
const MainCardNavigator = StackNavigator({ const MainCardNavigator = StackNavigator({
Rooms: { Rooms: {
screen: RoomsListView, screen: RoomsListView
navigationOptions: ({ navigation }) => ({
[position]: <Button title='Servers' onPress={() => navigation.navigate('ListServerModal')} />
})
}, },
Room: { Room: {
screen: RoomView screen: RoomView

View File

@ -51,7 +51,7 @@ export default class RoomView extends React.Component {
// this.rid = 'GENERAL'; // this.rid = 'GENERAL';
this.state = { this.state = {
dataSource: this.getMessages(), dataSource: [],
loaded: true, loaded: true,
joined: typeof props.navigation.state.params.rid === 'undefined' joined: typeof props.navigation.state.params.rid === 'undefined'
}; };
@ -69,11 +69,17 @@ export default class RoomView extends React.Component {
loaded: true loaded: true
}); });
}); });
realm.addListener('change', this.updateState);
this.data = realm.objects('messages').filtered('_server.id = $0 AND rid = $1', RocketChat.currentServer, this.rid).sorted('ts', true);
this.setState({
dataSource: this.data
});
this.data.addListener(this.updateState);
} }
componentWillUnmount() { componentWillUnmount() {
realm.removeListener('change', this.updateState); this.data.removeListener(this.updateState);
} }
onEndReached = () => { onEndReached = () => {
@ -92,12 +98,9 @@ export default class RoomView extends React.Component {
} }
} }
getMessages = () => realm.objects('messages').filtered('_server.id = $0 AND rid = $1', RocketChat.currentServer, this.rid).sorted('ts', true) updateState = (data) => {
updateState = () => {
this.setState({ this.setState({
...this.state, dataSource: data
dataSource: this.getMessages()
}); });
}; };

View File

@ -2,7 +2,7 @@ import ActionButton from 'react-native-action-button';
import Icon from 'react-native-vector-icons/Ionicons'; import Icon from 'react-native-vector-icons/Ionicons';
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { Text, View, FlatList, StyleSheet, TouchableOpacity, Platform, TextInput } from 'react-native'; import { Button, Text, View, FlatList, StyleSheet, TouchableOpacity, Platform, TextInput } from 'react-native';
import Meteor from 'react-native-meteor'; import Meteor from 'react-native-meteor';
import realm from '../lib/realm'; import realm from '../lib/realm';
import RocketChat from '../lib/rocketchat'; import RocketChat from '../lib/rocketchat';
@ -57,6 +57,7 @@ const styles = StyleSheet.create({
}); });
let navigation; let navigation;
let setInitialData;
Meteor.getData().on('loggingIn', () => { Meteor.getData().on('loggingIn', () => {
setTimeout(() => { setTimeout(() => {
@ -76,16 +77,19 @@ export default class RoomsListView extends React.Component {
navigation: PropTypes.object.isRequired navigation: PropTypes.object.isRequired
} }
static navigationOptions = () => { static navigationOptions = (props) => {
const server = RocketChat.currentServer ? RocketChat.currentServer.replace(/^https?:\/\//, '') : ''; const server = RocketChat.currentServer ? RocketChat.currentServer.replace(/^https?:\/\//, '') : '';
const textAlign = Platform.OS === 'ios' ? 'center' : 'left'; const textAlign = Platform.OS === 'ios' ? 'center' : 'left';
const marginLeft = Platform.OS === 'ios' ? 0 : 20; const marginLeft = Platform.OS === 'ios' ? 0 : 20;
const position = Platform.OS === 'ios' ? 'headerLeft' : 'headerRight';
return { return {
headerTitle: <View style={{ height: 10, width: 200, top: -10, marginLeft }}> headerTitle: <View style={{ height: 10, width: 200, top: -10, marginLeft }}>
<Text style={{ textAlign, fontSize: 16, fontWeight: '600' }}>Channels</Text> <Text style={{ textAlign, fontSize: 16, fontWeight: '600' }}>Channels</Text>
<Text style={{ textAlign, fontSize: 10 }}>{server}</Text> <Text style={{ textAlign, fontSize: 10 }}>{server}</Text>
</View>, </View>,
title: 'Channels' title: 'Channels',
[position]: <Button title='Servers' onPress={() => props.navigation.navigate('ListServerModal', { onSelect: setInitialData })} />
}; };
} }
@ -93,7 +97,7 @@ export default class RoomsListView extends React.Component {
super(props); super(props);
this.state = { this.state = {
dataSource: this.getSubscriptions(), dataSource: [],
searching: false, searching: false,
searchDataSource: [], searchDataSource: [],
searchText: '' searchText: ''
@ -101,19 +105,23 @@ export default class RoomsListView extends React.Component {
} }
componentWillMount() { componentWillMount() {
realm.addListener('change', this.updateState); setInitialData = this.setInitialData;
navigation = this.props.navigation; navigation = this.props.navigation;
if (RocketChat.currentServer) { if (RocketChat.currentServer) {
this.setInitialData();
RocketChat.connect(); RocketChat.connect();
} else { } else {
navigation.navigate('ListServerModal'); navigation.navigate('ListServerModal', {
onSelect: this.setInitialData
});
} }
} }
componentWillUnmount() { componentWillUnmount() {
realm.removeListener('change', this.updateState); this.data.removeListener(this.updateState);
} }
onSearchChangeText = (text) => { onSearchChangeText = (text) => {
@ -161,8 +169,22 @@ export default class RoomsListView extends React.Component {
} }
} }
getSubscriptions = () => realm.objects('subscriptions').filtered('_server.id = $0', RocketChat.currentServer).sorted('name').slice() setInitialData = () => {
.sort((a, b) => { if (this.data) {
this.data.removeListener(this.updateState);
}
this.data = realm.objects('subscriptions').filtered('_server.id = $0', RocketChat.currentServer).sorted('name');
this.data.addListener(this.updateState);
this.setState({
dataSource: this.sort(this.data)
});
}
sort = (data) => {
return data.slice().sort((a, b) => {
if (a.unread < b.unread) { if (a.unread < b.unread) {
return 1; return 1;
} }
@ -173,10 +195,11 @@ export default class RoomsListView extends React.Component {
return 0; return 0;
}); });
}
updateState = () => { updateState = (data) => {
this.setState({ this.setState({
dataSource: this.getSubscriptions() dataSource: this.sort(data)
}); });
} }

View File

@ -88,6 +88,7 @@ export default class ListServerView extends React.Component {
RocketChat.currentServer = item.id; RocketChat.currentServer = item.id;
RocketChat.connect(); RocketChat.connect();
this.props.navigation.state.params.onSelect();
this.props.navigation.dispatch({ type: 'Navigation/BACK' }); this.props.navigation.dispatch({ type: 'Navigation/BACK' });
} }