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 settingsType from '../constants/settings';
import realm from './realm';
import debounce from '../utils/debounce';
import * as actions from '../actions';
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 = {
createChannel({ name, users, type }) {
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') {
// console.log(ddbMessage);
realm.write(() => {
const data = ddbMessage.fields.args[1];
let key;
if (ddbMessage.fields.eventName && ddbMessage.fields.eventName.indexOf('rooms-changed') > -1) {
this.roomCache[data._id] = data;
key = data._id;
} 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);
data._server = { id: RocketChat.currentServer };
realm.create('subscriptions', data, true);
});
}
});
});
@ -217,7 +178,7 @@ const RocketChat = {
// subscription.value = item.value;
// }
subscription._server = { id: RocketChat.currentServer };
write('subscriptions', subscription);
// write('subscriptions', subscription);
realm.create('subscriptions', subscription, true);
});
});
@ -410,7 +371,7 @@ Meteor.Accounts.onLogin(() => {
return subscription;
});
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(() => {
data.forEach((subscription) => {
// const subscription = {

View File

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