2015-10-27 23:19:30 +00:00
|
|
|
/* Copyright 2015 Realm Inc - All Rights Reserved
|
|
|
|
* Proprietary and Confidential
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-10-28 18:23:37 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
let items = this.props.items;
|
2015-10-27 08:52:26 +00:00
|
|
|
let editingRow = this.state.editingRow;
|
|
|
|
|
2015-10-28 18:23:37 +00:00
|
|
|
for (let i = items.length; i--;) {
|
|
|
|
if (i == editingRow) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (this._deleteItemIfEmpty(items[i]) && i < editingRow) {
|
|
|
|
editingRow--;
|
2015-10-27 08:52:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-28 18:23:37 +00:00
|
|
|
|
|
|
|
if (editingRow != this.state.editingRow) {
|
|
|
|
this.setState({editingRow});
|
|
|
|
}
|
2015-10-27 08:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-28 18:23:37 +00:00
|
|
|
return true;
|
2015-10-27 08:52:26 +00:00
|
|
|
}
|
2015-10-28 18:23:37 +00:00
|
|
|
return false;
|
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;
|