2016-02-18 19:59:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-10-27 23:19:30 +00:00
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-16 20:21:28 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import {
|
2016-02-15 23:03:16 +00:00
|
|
|
Text,
|
|
|
|
View,
|
|
|
|
} from 'react-native';
|
2015-10-27 08:52:26 +00:00
|
|
|
|
2016-02-15 23:03:16 +00:00
|
|
|
import { ListView } from 'realm/react-native';
|
|
|
|
import TodoListItem from './todo-list-item';
|
|
|
|
import realm from './realm';
|
|
|
|
import styles from './styles';
|
2015-10-27 08:52:26 +00:00
|
|
|
|
2016-02-15 23:03:16 +00:00
|
|
|
export default class TodoListView extends React.Component {
|
2015-10-27 08:52:26 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2016-01-12 00:01:37 +00:00
|
|
|
let dataSource = new ListView.DataSource({
|
|
|
|
rowHasChanged(a, b) {
|
|
|
|
// Always re-render TodoList items.
|
|
|
|
return a.done !== b.done || a.text !== b.text || a.items || b.items;
|
|
|
|
}
|
2015-10-27 08:52:26 +00:00
|
|
|
});
|
|
|
|
|
2016-01-12 00:01:37 +00:00
|
|
|
this.state = {
|
|
|
|
dataSource: this._cloneDataSource(dataSource, props),
|
|
|
|
};
|
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
this.renderRow = this.renderRow.bind(this);
|
|
|
|
}
|
|
|
|
|
2016-01-12 00:01:37 +00:00
|
|
|
componentWillReceiveProps(props) {
|
|
|
|
this.updateDataSource(props);
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
2016-01-12 00:01:37 +00:00
|
|
|
<ListView dataSource={this.state.dataSource} renderRow={this.renderRow} />
|
2015-10-27 08:52:26 +00:00
|
|
|
<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)} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-01-12 00:01:37 +00:00
|
|
|
updateDataSource(props=this.props) {
|
|
|
|
this.setState({
|
|
|
|
dataSource: this._cloneDataSource(this.state.dataSource, props),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_cloneDataSource(dataSource, props) {
|
|
|
|
let items = props.items;
|
|
|
|
let extraItems = props.extraItems;
|
|
|
|
let sections = [items ? items.snapshot() : []];
|
|
|
|
|
|
|
|
if (extraItems && extraItems.length) {
|
|
|
|
sections.push(extraItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataSource.cloneWithRowsAndSections(sections);
|
|
|
|
}
|
|
|
|
|
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);
|
2016-01-12 00:01:37 +00:00
|
|
|
this.updateDataSource();
|
2015-10-27 08:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onEndEditingRow(item, rowIndex) {
|
2016-01-12 00:01:37 +00:00
|
|
|
if (this._deleteItemIfEmpty(item)) {
|
|
|
|
this.updateDataSource();
|
|
|
|
}
|
2015-10-27 08:52:26 +00:00
|
|
|
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}>
|
2016-01-19 23:10:06 +00:00
|
|
|
<View style={styles.listItemCount}>
|
|
|
|
<Text style={styles.listItemCountText}>
|
|
|
|
{this.props.item.items.length}
|
|
|
|
</Text>
|
|
|
|
</View>
|
2015-10-27 10:08:18 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRightSide() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|