Fix room search

This commit is contained in:
Diego Sampaio 2017-08-14 15:02:53 -03:00
parent 195a795394
commit ea1172606d
No known key found for this signature in database
GPG Key ID: E060152B30502562
2 changed files with 49 additions and 66 deletions

View File

@ -6,7 +6,6 @@ import { hashPassword } from 'react-native-meteor/lib/utils';
import reduxStore from '../lib/createStore'; import reduxStore from '../lib/createStore';
import settingsType from '../constants/settings'; import settingsType from '../constants/settings';
import realm from './realm'; import realm from './realm';
import debounce from '../utils/debounce';
import * as actions from '../actions'; import * as actions from '../actions';
export { Accounts } from 'react-native-meteor'; export { Accounts } from 'react-native-meteor';
@ -20,25 +19,6 @@ const call = (method, ...params) => new Promise((resolve, reject) => {
}); });
}); });
const write = (() => {
const cache = [];
const run = debounce(() => {
if (!cache.length) {
return;
}
realm.write(() => {
cache.forEach(([name, obj]) => {
realm.create(name, obj, true);
});
});
// cache = [];
}, 1000);
return (name, obj) => {
cache.push([name, obj]);
run();
};
})();
const RocketChat = { const RocketChat = {
createChannel({ name, users, type }) { createChannel({ name, users, type }) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -114,32 +94,13 @@ const RocketChat = {
}); });
} }
this.subCache = this.subCache || {};
this.roomCache = this.roomCache || {};
this.cache = {};
if (ddbMessage.collection === 'stream-notify-user') { if (ddbMessage.collection === 'stream-notify-user') {
const data = ddbMessage.fields.args[1]; // console.log(ddbMessage);
let key; realm.write(() => {
if (ddbMessage.fields.eventName && ddbMessage.fields.eventName.indexOf('rooms-changed') > -1) { const data = ddbMessage.fields.args[1];
this.roomCache[data._id] = data; data._server = { id: RocketChat.currentServer };
key = data._id; realm.create('subscriptions', data, true);
} else { });
this.subCache[data.rid] = data;
key = data.rid;
delete this.subCache[key]._updatedAt;
}
this.cache[key] = this.cache[key] ||
setTimeout(() => {
this.subCache[key] = this.subCache[key] || realm.objects('subscriptions').filtered('rid = $0', key).slice(0, 1)[0];
if (this.roomCache[key]) {
this.subCache[key]._updatedAt = this.roomCache[key]._updatedAt;
}
write('subscriptions', this.subCache[key]);
delete this.subCache[key];
delete this.roomCache[key];
delete this.cache[key];
}, 550);
} }
}); });
}); });
@ -217,7 +178,7 @@ const RocketChat = {
// subscription.value = item.value; // subscription.value = item.value;
// } // }
subscription._server = { id: RocketChat.currentServer }; subscription._server = { id: RocketChat.currentServer };
write('subscriptions', subscription); // write('subscriptions', subscription);
realm.create('subscriptions', subscription, true); realm.create('subscriptions', subscription, true);
}); });
}); });
@ -410,7 +371,7 @@ Meteor.Accounts.onLogin(() => {
return subscription; return subscription;
}); });
Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/subscriptions-changed`, false); Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/subscriptions-changed`, false);
Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/rooms-changed`, false); // Meteor.subscribe('stream-notify-user', `${ Meteor.userId() }/rooms-changed`, false);
realm.write(() => { realm.write(() => {
data.forEach((subscription) => { data.forEach((subscription) => {
// const subscription = { // const subscription = {

View File

@ -98,6 +98,7 @@ class RoomsListItem extends React.PureComponent {
}), dispatch => ({ }), dispatch => ({
actions: bindActionCreators(actions, dispatch) actions: bindActionCreators(actions, dispatch)
})) }))
export default class RoomsListView extends React.Component { export default class RoomsListView extends React.Component {
static propTypes = { static propTypes = {
navigator: PropTypes.object.isRequired, navigator: PropTypes.object.isRequired,
@ -108,14 +109,14 @@ export default class RoomsListView extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.data = realm.objects('subscriptions').filtered('_server.id = $0', this.props.server); // this.data = realm.objects('subscriptions').filtered('_server.id = $0', this.props.server);
this.state = { this.state = {
dataSource: ds.cloneWithRows([]), dataSource: ds.cloneWithRows([]),
searching: false, searching: false,
searchDataSource: [], searchDataSource: [],
searchText: '' searchText: ''
}; };
this.data.addListener(this.updateState);
this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this)); this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
} }
@ -148,10 +149,8 @@ export default class RoomsListView extends React.Component {
} }
} }
componentWillUpdate
componentWillUnmount() { componentWillUnmount() {
this.data.removeListener(this.updateState); this.state.data.removeListener(this.updateState);
} }
onNavigatorEvent = (event) => { onNavigatorEvent = (event) => {
@ -177,9 +176,11 @@ export default class RoomsListView extends React.Component {
}); });
if (searchText !== '') { if (searchText !== '') {
const data = this.state.data.filtered('name CONTAINS[c] $0', searchText).slice();
const dataSource = []; const dataSource = [];
const usernames = []; const usernames = [];
realm.objects('subscriptions').filtered('_server.id = $0 AND name CONTAINS[c] $1', this.props.server, searchText).forEach((sub) => { data.forEach((sub) => {
dataSource.push(sub); dataSource.push(sub);
if (sub.t === 'd') { if (sub.t === 'd') {
@ -187,7 +188,7 @@ export default class RoomsListView extends React.Component {
} }
}); });
if (dataSource.length < 5) { if (dataSource.length < 7) {
RocketChat.spotlight(searchText, usernames) RocketChat.spotlight(searchText, usernames)
.then((results) => { .then((results) => {
results.users.forEach((user) => { results.users.forEach((user) => {
@ -207,10 +208,18 @@ export default class RoomsListView extends React.Component {
}); });
this.setState({ this.setState({
searchDataSource: dataSource dataSource: ds.cloneWithRows(dataSource)
}); });
}); });
} else {
this.setState({
dataSource: ds.cloneWithRows(dataSource)
});
} }
} else {
this.setState({
dataSource: ds.cloneWithRows(this.state.data)
});
} }
} }
@ -228,21 +237,20 @@ export default class RoomsListView extends React.Component {
} }
RocketChat.connect(); RocketChat.connect();
if (this.data) { const data = realm.objects('subscriptions').filtered('_server.id = $0', props.server).sorted('_updatedAt', true);
this.data.removeListener(this.updateState);
}
this.data = realm.objects('subscriptions').filtered('_server.id = $0', props.server).sorted('_updatedAt', true);
this.data.addListener(this.updateState);
this.updateState(); this.setState({
dataSource: ds.cloneWithRows(data),
data
});
data.addListener(this.updateState);
}); });
} }
getSubscriptions = () => this.data.sorted('_updatedAt', true)
updateState = debounce(() => { updateState = debounce(() => {
this.setState({ this.setState({
dataSource: ds.cloneWithRows(this.data.filtered('_server.id = $0', this.props.server).sorted('ls', true)) dataSource: ds.cloneWithRows(this.state.data)
}); });
}, 500); }, 500);
@ -266,8 +274,21 @@ export default class RoomsListView extends React.Component {
if (item.search) { if (item.search) {
if (item.t === 'd') { if (item.t === 'd') {
RocketChat.createDirectMessage(item.username) RocketChat.createDirectMessage(item.username)
.then(room => realm.objects('subscriptions').filtered('_server.id = $0 AND rid = $1', this.props.server, room.rid)) .then(room => new Promise((resolve) => {
.then(subs => navigateToRoom({ sid: subs[0]._id })) const data = realm.objects('subscriptions').filtered('_server.id = $0 AND rid = $1', this.props.server, room.rid);
if (data.length) {
return resolve(data[0]);
}
data.addListener(() => {
if (data.length) {
resolve(data[0]);
data.removeAllListeners();
}
});
}))
.then(sub => navigateToRoom({ sid: sub._id }))
.then(() => clearSearch()); .then(() => clearSearch());
} else { } else {
clearSearch(); clearSearch();
@ -351,6 +372,7 @@ export default class RoomsListView extends React.Component {
renderHeader={this.renderSearchBar} renderHeader={this.renderSearchBar}
contentOffset={{ x: 0, y: 20 }} contentOffset={{ x: 0, y: 20 }}
enableEmptySections enableEmptySections
keyboardShouldPersistTaps='always'
/> />
) )