2019-07-29 15:43:53 +02:00
|
|
|
const React = require('react');
|
2019-07-29 15:58:33 +02:00
|
|
|
|
|
|
|
const { StyleSheet, View, TextInput, FlatList, TouchableHighlight } = require('react-native');
|
2017-11-03 02:09:34 +02:00
|
|
|
const { connect } = require('react-redux');
|
|
|
|
const { ScreenHeader } = require('lib/components/screen-header.js');
|
|
|
|
const Icon = require('react-native-vector-icons/Ionicons').default;
|
|
|
|
const { _ } = require('lib/locale.js');
|
2017-12-14 20:12:14 +02:00
|
|
|
const Note = require('lib/models/Note.js');
|
2017-11-03 02:09:34 +02:00
|
|
|
const { NoteItem } = require('lib/components/note-item.js');
|
|
|
|
const { BaseScreenComponent } = require('lib/components/base-screen.js');
|
|
|
|
const { themeStyle } = require('lib/components/global-style.js');
|
2018-12-16 19:32:42 +02:00
|
|
|
const SearchEngineUtils = require('lib/services/SearchEngineUtils');
|
2017-11-23 20:41:35 +02:00
|
|
|
const DialogBox = require('react-native-dialogbox').default;
|
2017-07-23 00:52:24 +02:00
|
|
|
|
2020-02-09 16:51:12 +02:00
|
|
|
Icon.loadFont();
|
|
|
|
|
2017-07-23 00:52:24 +02:00
|
|
|
class SearchScreenComponent extends BaseScreenComponent {
|
2019-09-13 00:16:42 +02:00
|
|
|
static navigationOptions() {
|
2017-07-23 00:52:24 +02:00
|
|
|
return { header: null };
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
query: '',
|
|
|
|
notes: [],
|
|
|
|
};
|
|
|
|
this.isMounted_ = false;
|
2017-08-01 19:59:01 +02:00
|
|
|
this.styles_ = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
styles() {
|
|
|
|
const theme = themeStyle(this.props.theme);
|
|
|
|
|
|
|
|
if (this.styles_[this.props.theme]) return this.styles_[this.props.theme];
|
|
|
|
this.styles_ = {};
|
|
|
|
|
|
|
|
let styles = {
|
|
|
|
body: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
searchContainer: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
borderWidth: 1,
|
|
|
|
borderColor: theme.dividerColor,
|
2019-07-29 15:43:53 +02:00
|
|
|
},
|
|
|
|
};
|
2017-08-01 19:59:01 +02:00
|
|
|
|
|
|
|
styles.searchTextInput = Object.assign({}, theme.lineInput);
|
|
|
|
styles.searchTextInput.paddingLeft = theme.marginLeft;
|
|
|
|
styles.searchTextInput.flex = 1;
|
|
|
|
styles.searchTextInput.backgroundColor = theme.backgroundColor;
|
|
|
|
styles.searchTextInput.color = theme.color;
|
|
|
|
|
|
|
|
styles.clearIcon = Object.assign({}, theme.icon);
|
|
|
|
styles.clearIcon.color = theme.colorFaded;
|
|
|
|
styles.clearIcon.paddingRight = theme.marginRight;
|
|
|
|
styles.clearIcon.backgroundColor = theme.backgroundColor;
|
|
|
|
|
|
|
|
this.styles_[this.props.theme] = StyleSheet.create(styles);
|
|
|
|
return this.styles_[this.props.theme];
|
2017-07-23 00:52:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.setState({ query: this.props.query });
|
|
|
|
this.refreshSearch(this.props.query);
|
|
|
|
this.isMounted_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.isMounted_ = false;
|
|
|
|
}
|
|
|
|
|
2019-02-03 18:57:29 +02:00
|
|
|
// UNSAFE_componentWillReceiveProps(newProps) {
|
|
|
|
// console.info('UNSAFE_componentWillReceiveProps', newProps);
|
2017-07-23 00:52:24 +02:00
|
|
|
|
2019-02-03 18:57:29 +02:00
|
|
|
// let newState = {};
|
|
|
|
// if ('query' in newProps && !this.state.query) newState.query = newProps.query;
|
|
|
|
|
|
|
|
// if (Object.getOwnPropertyNames(newState).length) {
|
|
|
|
// this.setState(newState);
|
|
|
|
// this.refreshSearch(newState.query);
|
|
|
|
// }
|
|
|
|
// }
|
2017-07-23 00:52:24 +02:00
|
|
|
|
|
|
|
searchTextInput_submit() {
|
|
|
|
const query = this.state.query.trim();
|
|
|
|
if (!query) return;
|
|
|
|
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'SEARCH_QUERY',
|
|
|
|
query: query,
|
|
|
|
});
|
2019-02-03 18:57:29 +02:00
|
|
|
|
|
|
|
this.setState({ query: query });
|
|
|
|
this.refreshSearch(query);
|
2017-07-23 00:52:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
clearButton_press() {
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'SEARCH_QUERY',
|
|
|
|
query: '',
|
|
|
|
});
|
2019-02-03 18:57:29 +02:00
|
|
|
|
|
|
|
this.setState({ query: '' });
|
|
|
|
this.refreshSearch('');
|
2017-07-23 00:52:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async refreshSearch(query = null) {
|
2017-08-01 21:08:38 +02:00
|
|
|
if (!this.props.visible) return;
|
|
|
|
|
2017-07-23 00:52:24 +02:00
|
|
|
query = query === null ? this.state.query.trim : query.trim();
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
let notes = [];
|
2017-07-23 00:52:24 +02:00
|
|
|
|
|
|
|
if (query) {
|
2019-07-29 15:43:53 +02:00
|
|
|
if (this.props.settings['db.ftsEnabled']) {
|
2018-12-28 22:40:29 +02:00
|
|
|
notes = await SearchEngineUtils.notesForQuery(query);
|
2019-07-29 15:43:53 +02:00
|
|
|
} else {
|
2018-12-28 22:40:29 +02:00
|
|
|
let p = query.split(' ');
|
|
|
|
let temp = [];
|
|
|
|
for (let i = 0; i < p.length; i++) {
|
|
|
|
let t = p[i].trim();
|
|
|
|
if (!t) continue;
|
|
|
|
temp.push(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
notes = await Note.previews(null, {
|
2019-09-19 23:51:18 +02:00
|
|
|
anywherePattern: `*${temp.join('*')}*`,
|
2018-12-28 22:40:29 +02:00
|
|
|
});
|
|
|
|
}
|
2017-07-23 00:52:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.isMounted_) return;
|
|
|
|
|
|
|
|
this.setState({ notes: notes });
|
|
|
|
}
|
|
|
|
|
|
|
|
searchTextInput_changeText(text) {
|
|
|
|
this.setState({ query: text });
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-07-25 22:24:30 +02:00
|
|
|
if (!this.isMounted_) return null;
|
|
|
|
|
2017-08-01 21:08:38 +02:00
|
|
|
const theme = themeStyle(this.props.theme);
|
|
|
|
|
|
|
|
let rootStyle = {
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: theme.backgroundColor,
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-08-01 21:08:38 +02:00
|
|
|
|
|
|
|
if (!this.props.visible) {
|
|
|
|
rootStyle.flex = 0.001; // This is a bit of a hack but it seems to work fine - it makes the component invisible but without unmounting it
|
|
|
|
}
|
|
|
|
|
2017-11-23 20:41:35 +02:00
|
|
|
const thisComponent = this;
|
|
|
|
|
2017-07-23 00:52:24 +02:00
|
|
|
return (
|
2017-08-01 21:08:38 +02:00
|
|
|
<View style={rootStyle}>
|
2017-11-23 20:41:35 +02:00
|
|
|
<ScreenHeader
|
|
|
|
title={_('Search')}
|
|
|
|
parentComponent={thisComponent}
|
|
|
|
folderPickerOptions={{
|
|
|
|
enabled: this.props.noteSelectionEnabled,
|
|
|
|
mustSelect: true,
|
|
|
|
}}
|
2019-06-28 01:51:02 +02:00
|
|
|
showSideMenuButton={false}
|
|
|
|
showSearchButton={false}
|
2017-11-23 20:41:35 +02:00
|
|
|
/>
|
2017-08-01 19:59:01 +02:00
|
|
|
<View style={this.styles().body}>
|
|
|
|
<View style={this.styles().searchContainer}>
|
2017-07-23 00:52:24 +02:00
|
|
|
<TextInput
|
2017-08-01 19:59:01 +02:00
|
|
|
style={this.styles().searchTextInput}
|
2017-08-01 21:08:38 +02:00
|
|
|
autoFocus={this.props.visible}
|
2019-07-29 15:43:53 +02:00
|
|
|
underlineColorAndroid="#ffffff00"
|
|
|
|
onSubmitEditing={() => {
|
|
|
|
this.searchTextInput_submit();
|
|
|
|
}}
|
|
|
|
onChangeText={text => this.searchTextInput_changeText(text)}
|
2017-07-23 00:52:24 +02:00
|
|
|
value={this.state.query}
|
2018-04-11 19:25:07 +02:00
|
|
|
selectionColor={theme.textSelectionColor}
|
2017-07-23 00:52:24 +02:00
|
|
|
/>
|
2019-07-29 15:43:53 +02:00
|
|
|
<TouchableHighlight onPress={() => this.clearButton_press()}>
|
|
|
|
<Icon name="md-close-circle" style={this.styles().clearIcon} />
|
2017-07-23 00:52:24 +02:00
|
|
|
</TouchableHighlight>
|
|
|
|
</View>
|
|
|
|
|
2019-09-13 00:16:42 +02:00
|
|
|
<FlatList data={this.state.notes} keyExtractor={(item) => item.id} renderItem={event => <NoteItem note={event.item} />} />
|
2017-07-23 00:52:24 +02:00
|
|
|
</View>
|
2019-07-29 15:43:53 +02:00
|
|
|
<DialogBox
|
|
|
|
ref={dialogbox => {
|
|
|
|
this.dialogbox = dialogbox;
|
|
|
|
}}
|
|
|
|
/>
|
2017-07-23 00:52:24 +02:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
const SearchScreen = connect(state => {
|
|
|
|
return {
|
|
|
|
query: state.searchQuery,
|
|
|
|
theme: state.settings.theme,
|
|
|
|
settings: state.settings,
|
|
|
|
noteSelectionEnabled: state.noteSelectionEnabled,
|
|
|
|
};
|
|
|
|
})(SearchScreenComponent);
|
2017-07-23 00:52:24 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { SearchScreen };
|