2017-05-09 22:46:54 +02:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { connect } from 'react-redux'
|
2017-05-16 21:12:37 +02:00
|
|
|
import { ListView, Text, TouchableHighlight, Switch, View } from 'react-native';
|
2017-06-24 20:06:28 +02:00
|
|
|
import { Log } from 'lib/log.js';
|
|
|
|
import { _ } from 'lib/locale.js';
|
|
|
|
import { Checkbox } from 'lib/components/checkbox.js';
|
|
|
|
import { Note } from 'lib/models/note.js';
|
2017-05-09 22:46:54 +02:00
|
|
|
|
|
|
|
class ItemListComponent extends Component {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2017-05-15 22:50:14 +02:00
|
|
|
const ds = new ListView.DataSource({
|
|
|
|
rowHasChanged: (r1, r2) => { return r1 !== r2; }
|
|
|
|
});
|
|
|
|
this.state = {
|
|
|
|
dataSource: ds,
|
|
|
|
items: [],
|
|
|
|
selectedItemIds: [],
|
|
|
|
};
|
2017-05-09 22:46:54 +02:00
|
|
|
}
|
|
|
|
|
2017-05-16 21:12:37 +02:00
|
|
|
componentWillMount() {
|
|
|
|
const newDataSource = this.state.dataSource.cloneWithRows(this.props.items);
|
|
|
|
this.state = { dataSource: newDataSource };
|
|
|
|
}
|
|
|
|
|
2017-05-09 22:46:54 +02:00
|
|
|
componentWillReceiveProps(newProps) {
|
|
|
|
// https://stackoverflow.com/questions/38186114/react-native-redux-and-listview
|
2017-05-15 22:50:14 +02:00
|
|
|
this.setState({
|
2017-05-24 22:18:09 +02:00
|
|
|
dataSource: this.state.dataSource.cloneWithRows(newProps.items),
|
2017-05-15 22:50:14 +02:00
|
|
|
});
|
|
|
|
}
|
2017-05-15 21:46:34 +02:00
|
|
|
|
2017-07-05 23:29:00 +02:00
|
|
|
async todoCheckbox_change(itemId, checked) {
|
|
|
|
let note = await Note.load(itemId);
|
|
|
|
await Note.save({ id: note.id, todo_completed: checked });
|
2017-05-24 22:51:50 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 21:48:17 +02:00
|
|
|
listView_itemLongPress(itemId) {}
|
2017-06-06 22:01:43 +02:00
|
|
|
listView_itemPress(itemId) {}
|
2017-05-15 22:50:14 +02:00
|
|
|
|
2017-05-09 22:46:54 +02:00
|
|
|
render() {
|
2017-05-15 22:50:14 +02:00
|
|
|
let renderRow = (item) => {
|
2017-05-09 22:46:54 +02:00
|
|
|
let onPress = () => {
|
2017-05-15 22:50:14 +02:00
|
|
|
this.listView_itemPress(item.id);
|
|
|
|
}
|
|
|
|
let onLongPress = () => {
|
|
|
|
this.listView_itemLongPress(item.id);
|
2017-05-09 22:46:54 +02:00
|
|
|
}
|
2017-05-24 22:51:50 +02:00
|
|
|
|
2017-05-09 22:46:54 +02:00
|
|
|
return (
|
2017-05-15 22:50:14 +02:00
|
|
|
<TouchableHighlight onPress={onPress} onLongPress={onLongPress}>
|
2017-07-06 21:48:17 +02:00
|
|
|
<View style={{flexDirection: 'row', paddingLeft: 10, paddingTop:5, paddingBottom:5 }}>
|
|
|
|
{ !!Number(item.is_todo) && <Checkbox checked={!!Number(item.todo_completed)} onChange={(checked) => { this.todoCheckbox_change(item.id, checked) }}/> }<Text>{item.title}</Text>
|
2017-05-16 21:12:37 +02:00
|
|
|
</View>
|
2017-05-09 22:46:54 +02:00
|
|
|
</TouchableHighlight>
|
|
|
|
);
|
|
|
|
}
|
2017-05-12 21:54:06 +02:00
|
|
|
|
|
|
|
// `enableEmptySections` is to fix this warning: https://github.com/FaridSafi/react-native-gifted-listview/issues/39
|
2017-05-09 22:46:54 +02:00
|
|
|
return (
|
|
|
|
<ListView
|
|
|
|
dataSource={this.state.dataSource}
|
|
|
|
renderRow={renderRow}
|
2017-05-12 21:54:06 +02:00
|
|
|
enableEmptySections={true}
|
2017-05-09 22:46:54 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 21:10:00 +02:00
|
|
|
export { ItemListComponent };
|