2017-05-15 21:10:00 +02:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { connect } from 'react-redux'
|
2017-07-25 19:49:31 +02:00
|
|
|
import { ListView, Text, TouchableHighlight, Switch, View, StyleSheet } from 'react-native';
|
2017-06-24 20:06:28 +02:00
|
|
|
import { Log } from 'lib/log.js';
|
|
|
|
import { _ } from 'lib/locale.js';
|
2017-07-25 19:49:31 +02:00
|
|
|
import { Checkbox } from 'lib/components/checkbox.js';
|
|
|
|
import { NoteItem } from 'lib/components/note-item.js';
|
|
|
|
import { reg } from 'lib/registry.js';
|
|
|
|
import { Note } from 'lib/models/note.js';
|
|
|
|
import { Setting } from 'lib/models/setting.js';
|
|
|
|
import { time } from 'lib/time-utils.js';
|
2017-08-01 20:29:01 +02:00
|
|
|
import { themeStyle } from 'lib/components/global-style.js';
|
2017-07-25 19:49:31 +02:00
|
|
|
|
|
|
|
class NoteListComponent extends Component {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
const ds = new ListView.DataSource({
|
|
|
|
rowHasChanged: (r1, r2) => { return r1 !== r2; }
|
|
|
|
});
|
|
|
|
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_ = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
this.styles_[themeId] = StyleSheet.create(styles);
|
|
|
|
return this.styles_[themeId];
|
2017-07-25 19:49:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
filterNotes(notes) {
|
2017-10-30 02:29:10 +02:00
|
|
|
const todoFilter = 'all'; //Setting.value('todoFilter');
|
2017-07-25 19:49:31 +02:00
|
|
|
if (todoFilter == 'all') return notes;
|
|
|
|
|
|
|
|
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) {
|
2017-08-20 22:11:32 +02:00
|
|
|
if (todoFilter == 'recent' && note.user_updated_time < notRecentTime && !!note.todo_completed) continue;
|
2017-07-25 19:49:31 +02:00
|
|
|
if (todoFilter == 'nonCompleted' && !!note.todo_completed) continue;
|
|
|
|
}
|
|
|
|
output.push(note);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
const newDataSource = this.state.dataSource.cloneWithRows(this.filterNotes(this.props.items));
|
|
|
|
this.state = { dataSource: newDataSource };
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(newProps) {
|
|
|
|
// 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
|
2017-07-30 23:33:54 +02:00
|
|
|
ref={(ref) => this.rootRef_ = ref}
|
2017-07-25 19:49:31 +02:00
|
|
|
dataSource={this.state.dataSource}
|
|
|
|
renderRow={(note) => {
|
2017-07-25 20:36:52 +02:00
|
|
|
return <NoteItem note={note}/>
|
|
|
|
}}
|
2017-07-25 19:49:31 +02:00
|
|
|
enableEmptySections={true}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const noItemMessage = _('There are currently no notes. Create one by clicking on the (+) button.');
|
2017-08-01 20:29:01 +02:00
|
|
|
return <Text style={this.styles().noItemMessage} >{noItemMessage}</Text>;
|
2017-07-25 19:49:31 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-15 21:46:34 +02:00
|
|
|
}
|
2017-05-15 21:10:00 +02:00
|
|
|
|
|
|
|
const NoteList = connect(
|
|
|
|
(state) => {
|
2017-07-30 23:33:54 +02:00
|
|
|
return {
|
|
|
|
items: state.notes,
|
|
|
|
notesSource: state.notesSource,
|
2017-08-01 20:29:01 +02:00
|
|
|
theme: state.settings.theme,
|
2017-07-30 23:33:54 +02:00
|
|
|
};
|
2017-05-15 21:10:00 +02:00
|
|
|
}
|
|
|
|
)(NoteListComponent)
|
|
|
|
|
|
|
|
export { NoteList };
|