2015-10-27 08:52:26 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const React = require('react-native');
|
|
|
|
const TodoListItem = require('./todo-list-item');
|
|
|
|
const realm = require('./realm');
|
|
|
|
const styles = require('./styles');
|
|
|
|
|
|
|
|
const { ListView, Text, View } = React;
|
|
|
|
|
|
|
|
class TodoListView extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.dataSource = new ListView.DataSource({
|
2015-10-27 10:08:18 +00:00
|
|
|
sectionHeaderHasChanged: () => false,
|
2015-10-27 08:52:26 +00:00
|
|
|
rowHasChanged: (row1, row2) => row1 !== row2
|
|
|
|
});
|
|
|
|
|
|
|
|
this.state = {};
|
|
|
|
this.renderRow = this.renderRow.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUpdate(nextProps, nextState) {
|
|
|
|
let editingRow = this.state.editingRow;
|
|
|
|
|
|
|
|
if (editingRow != null && editingRow != nextState.editingRow) {
|
|
|
|
let item = this.props.items[editingRow];
|
|
|
|
|
|
|
|
// The item may have already been deleted.
|
|
|
|
if (item) {
|
|
|
|
this._deleteItemIfEmpty(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
// Clone the items into a new Array to prevent unexpected errors from changes in length.
|
2015-10-27 10:08:18 +00:00
|
|
|
let sections = [Array.from(this.props.items)];
|
|
|
|
let extraItems = this.props.extraItems;
|
|
|
|
|
|
|
|
if (extraItems && extraItems.length) {
|
|
|
|
sections.push(extraItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
let dataSource = this.dataSource.cloneWithRowsAndSections(sections);
|
2015-10-27 08:52:26 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<ListView style={styles.listView} dataSource={dataSource} renderRow={this.renderRow} />
|
|
|
|
<Text style={styles.instructions}>
|
|
|
|
Press Cmd+R to reload,{'\n'}
|
|
|
|
Cmd+D for dev menu
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRow(item, sectionIndex, rowIndex) {
|
2015-10-27 10:08:18 +00:00
|
|
|
let RowClass;
|
|
|
|
let editing = false;
|
|
|
|
|
|
|
|
if (sectionIndex == 0) {
|
|
|
|
RowClass = this.props.rowClass || TodoListItem;
|
|
|
|
editing = this.state.editingRow == rowIndex;
|
|
|
|
} else if (sectionIndex == 1) {
|
|
|
|
RowClass = TodoListExtraItem;
|
|
|
|
}
|
2015-10-27 08:52:26 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<RowClass
|
|
|
|
item={item}
|
2015-10-27 10:08:18 +00:00
|
|
|
editing={editing}
|
|
|
|
onPress={() => this._onPressRow(item, sectionIndex, rowIndex)}
|
|
|
|
onPressDelete={() => this._onPressDeleteRow(item)}
|
2015-10-27 08:52:26 +00:00
|
|
|
onEndEditing={() => this._onEndEditingRow(item, rowIndex)} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-27 10:08:18 +00:00
|
|
|
_onPressRow(item, sectionIndex, rowIndex) {
|
2015-10-27 08:52:26 +00:00
|
|
|
let onPressItem = this.props.onPressItem;
|
|
|
|
if (onPressItem) {
|
2015-10-27 10:08:18 +00:00
|
|
|
onPressItem(item);
|
2015-10-27 08:52:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no handler was provided, then default to editing the row.
|
2015-10-27 10:08:18 +00:00
|
|
|
if (sectionIndex == 0) {
|
|
|
|
this.setState({editingRow: rowIndex});
|
|
|
|
}
|
2015-10-27 08:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onPressDeleteRow(item) {
|
2015-10-27 10:08:18 +00:00
|
|
|
this._deleteItem(item);
|
2015-10-27 08:52:26 +00:00
|
|
|
this.forceUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onEndEditingRow(item, rowIndex) {
|
|
|
|
this._deleteItemIfEmpty(item);
|
|
|
|
|
|
|
|
if (this.state.editingRow == rowIndex) {
|
|
|
|
this.setState({editingRow: null});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-27 10:08:18 +00:00
|
|
|
_deleteItem(item) {
|
|
|
|
let items = item.items;
|
|
|
|
|
|
|
|
realm.write(() => {
|
|
|
|
// If the item is a TodoList, then delete all of its items.
|
|
|
|
if (items && items.length) {
|
|
|
|
realm.delete(items);
|
|
|
|
}
|
|
|
|
|
|
|
|
realm.delete(item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
_deleteItemIfEmpty(item) {
|
|
|
|
// The item could be a TodoList or a Todo.
|
|
|
|
if (!item.name && !item.text) {
|
2015-10-27 10:08:18 +00:00
|
|
|
this._deleteItem(item);
|
2015-10-27 08:52:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-27 10:08:18 +00:00
|
|
|
class TodoListExtraItem extends TodoListItem {
|
|
|
|
renderText() {
|
|
|
|
return super.renderText(styles.listItemTextSpecial);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderLeftSide() {
|
|
|
|
return (
|
|
|
|
<View style={styles.listItemLeftSide}>
|
2015-10-27 21:23:17 +00:00
|
|
|
<Text style={styles.listItemCount}>
|
|
|
|
{this.props.item.items.length}
|
|
|
|
</Text>
|
2015-10-27 10:08:18 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRightSide() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
module.exports = TodoListView;
|