2019-07-29 15:43:53 +02:00
|
|
|
const React = require('react');
|
|
|
|
const Component = React.Component;
|
2018-03-09 22:59:12 +02:00
|
|
|
const { connect } = require('react-redux');
|
2019-07-30 11:36:56 +02:00
|
|
|
const { ListView, Text, StyleSheet, Button, View } = require('react-native');
|
2018-03-09 22:59:12 +02:00
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const { NoteItem } = require('lib/components/note-item.js');
|
|
|
|
const { time } = require('lib/time-utils.js');
|
|
|
|
const { themeStyle } = require('lib/components/global-style.js');
|
2017-07-25 19:49:31 +02:00
|
|
|
|
|
|
|
class NoteListComponent extends Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
const ds = new ListView.DataSource({
|
2019-07-29 15:43:53 +02:00
|
|
|
rowHasChanged: (r1, r2) => {
|
|
|
|
return r1 !== r2;
|
|
|
|
},
|
2017-07-25 19:49:31 +02:00
|
|
|
});
|
|
|
|
this.state = {
|
|
|
|
dataSource: ds,
|
|
|
|
items: [],
|
|
|
|
selectedItemIds: [],
|
|
|
|
};
|
2017-07-30 23:33:54 +02:00
|
|
|
this.rootRef_ = null;
|
2017-08-01 20:29:01 +02:00
|
|
|
this.styles_ = {};
|
2019-07-30 11:36:56 +02:00
|
|
|
|
|
|
|
this.createNotebookButton_click = this.createNotebookButton_click.bind(this);
|
2017-08-01 20:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
styles() {
|
|
|
|
const themeId = this.props.theme;
|
|
|
|
const theme = themeStyle(themeId);
|
|
|
|
|
|
|
|
if (this.styles_[themeId]) return this.styles_[themeId];
|
|
|
|
this.styles_ = {};
|
|
|
|
|
|
|
|
let styles = {
|
|
|
|
noItemMessage: {
|
|
|
|
paddingLeft: theme.marginLeft,
|
|
|
|
paddingRight: theme.marginRight,
|
|
|
|
paddingTop: theme.marginTop,
|
|
|
|
paddingBottom: theme.marginBottom,
|
|
|
|
fontSize: theme.fontSize,
|
|
|
|
color: theme.color,
|
2019-07-30 11:36:56 +02:00
|
|
|
textAlign: 'center',
|
|
|
|
},
|
|
|
|
noNotebookView: {
|
|
|
|
|
2017-08-01 20:29:01 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
this.styles_[themeId] = StyleSheet.create(styles);
|
|
|
|
return this.styles_[themeId];
|
2017-07-25 19:49:31 +02:00
|
|
|
}
|
|
|
|
|
2019-07-30 11:36:56 +02:00
|
|
|
createNotebookButton_click() {
|
|
|
|
this.props.dispatch({
|
|
|
|
type: 'NAV_GO',
|
|
|
|
routeName: 'Folder',
|
|
|
|
folderId: null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:49:31 +02:00
|
|
|
filterNotes(notes) {
|
2018-03-09 22:59:12 +02:00
|
|
|
const todoFilter = 'all'; //Setting.value('todoFilter');
|
|
|
|
if (todoFilter == 'all') return notes;
|
2017-07-25 19:49:31 +02:00
|
|
|
|
|
|
|
const now = time.unixMs();
|
|
|
|
const maxInterval = 1000 * 60 * 60 * 24;
|
|
|
|
const notRecentTime = now - maxInterval;
|
|
|
|
|
|
|
|
let output = [];
|
|
|
|
for (let i = 0; i < notes.length; i++) {
|
|
|
|
const note = notes[i];
|
|
|
|
if (note.is_todo) {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (todoFilter == 'recent' && note.user_updated_time < notRecentTime && !!note.todo_completed) continue;
|
|
|
|
if (todoFilter == 'nonCompleted' && !!note.todo_completed) continue;
|
2017-07-25 19:49:31 +02:00
|
|
|
}
|
|
|
|
output.push(note);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:38:19 +02:00
|
|
|
UNSAFE_componentWillMount() {
|
2017-07-25 19:49:31 +02:00
|
|
|
const newDataSource = this.state.dataSource.cloneWithRows(this.filterNotes(this.props.items));
|
2017-10-30 20:17:01 +02:00
|
|
|
this.setState({ dataSource: newDataSource });
|
2017-07-25 19:49:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:38:19 +02:00
|
|
|
UNSAFE_componentWillReceiveProps(newProps) {
|
2017-07-25 19:49:31 +02:00
|
|
|
// https://stackoverflow.com/questions/38186114/react-native-redux-and-listview
|
|
|
|
this.setState({
|
|
|
|
dataSource: this.state.dataSource.cloneWithRows(this.filterNotes(newProps.items)),
|
|
|
|
});
|
2017-07-30 23:33:54 +02:00
|
|
|
|
|
|
|
// Make sure scroll position is reset when switching from one folder to another or to a tag list.
|
|
|
|
if (this.rootRef_ && newProps.notesSource != this.props.notesSource) {
|
|
|
|
this.rootRef_.scrollTo({ x: 0, y: 0, animated: false });
|
|
|
|
}
|
2017-07-25 19:49:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
// `enableEmptySections` is to fix this warning: https://github.com/FaridSafi/react-native-gifted-listview/issues/39
|
|
|
|
|
|
|
|
if (this.state.dataSource.getRowCount()) {
|
|
|
|
return (
|
|
|
|
<ListView
|
2019-07-29 15:43:53 +02:00
|
|
|
ref={ref => (this.rootRef_ = ref)}
|
2017-07-25 19:49:31 +02:00
|
|
|
dataSource={this.state.dataSource}
|
2019-07-29 15:43:53 +02:00
|
|
|
renderRow={note => {
|
|
|
|
return <NoteItem note={note} />;
|
2017-07-25 20:36:52 +02:00
|
|
|
}}
|
2017-07-25 19:49:31 +02:00
|
|
|
enableEmptySections={true}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else {
|
2019-07-30 11:36:56 +02:00
|
|
|
if (!this.props.folders.length) {
|
|
|
|
const noItemMessage = _('You currently have no notebooks.');
|
|
|
|
return (
|
|
|
|
<View style={this.styles().noNotebookView}>
|
|
|
|
<Text style={this.styles().noItemMessage}>{noItemMessage}</Text>
|
|
|
|
<Button title={_('Create a notebook')} onPress={this.createNotebookButton_click} />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const noItemMessage = _('There are currently no notes. Create one by clicking on the (+) button.');
|
|
|
|
return <Text style={this.styles().noItemMessage}>{noItemMessage}</Text>;
|
2019-07-30 11:37:52 +02:00
|
|
|
}
|
2017-07-25 19:49:31 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-15 21:46:34 +02:00
|
|
|
}
|
2017-05-15 21:10:00 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
const NoteList = connect(state => {
|
|
|
|
return {
|
|
|
|
items: state.notes,
|
2019-07-30 11:36:56 +02:00
|
|
|
folders: state.folders,
|
2019-07-29 15:43:53 +02:00
|
|
|
notesSource: state.notesSource,
|
|
|
|
theme: state.settings.theme,
|
|
|
|
noteSelectionEnabled: state.noteSelectionEnabled,
|
|
|
|
};
|
|
|
|
})(NoteListComponent);
|
2017-05-15 21:10:00 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { NoteList };
|