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-07 23:20:05 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-16 20:21:28 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Platform,
|
|
|
|
StatusBar,
|
2016-01-14 01:05:52 +00:00
|
|
|
Text,
|
|
|
|
TouchableOpacity,
|
2016-01-19 23:10:06 +00:00
|
|
|
View,
|
2016-02-15 23:03:16 +00:00
|
|
|
} from 'react-native';
|
|
|
|
|
|
|
|
import TodoItem from './todo-item';
|
|
|
|
import TodoListView from './todo-listview';
|
2017-12-13 16:36:10 +00:00
|
|
|
import TodoListItem from './todo-list-item';
|
|
|
|
import ItemsScreen from './items-screen'
|
2016-02-15 23:03:16 +00:00
|
|
|
import realm from './realm';
|
|
|
|
import styles from './styles';
|
2015-10-07 23:20:05 +00:00
|
|
|
|
2017-12-13 16:36:10 +00:00
|
|
|
// import NavigationExperimental from 'react-native-deprecated-custom-components';
|
|
|
|
import { StackNavigator } from 'react-navigation';
|
|
|
|
import RNExitApp from 'react-native-exit-app-no-history';
|
2017-12-06 12:44:00 +00:00
|
|
|
|
2017-12-13 16:36:10 +00:00
|
|
|
const params = require("./params.json");
|
|
|
|
|
|
|
|
class HomeScreen extends React.Component {
|
|
|
|
static navigationOptions = {
|
|
|
|
title: 'Todo Lists',
|
|
|
|
};
|
2017-12-06 12:44:00 +00:00
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2015-10-07 23:20:05 +00:00
|
|
|
|
2016-10-04 22:02:51 +00:00
|
|
|
// This is a Results object, which will live-update.
|
|
|
|
this.todoLists = realm.objects('TodoList').sorted('creationDate');
|
|
|
|
if (this.todoLists.length < 1) {
|
2015-10-07 23:20:05 +00:00
|
|
|
realm.write(() => {
|
2017-12-13 16:36:10 +00:00
|
|
|
realm.create('TodoList', { name: 'Todo List', creationDate: new Date() });
|
2015-10-07 23:20:05 +00:00
|
|
|
});
|
|
|
|
}
|
2016-10-04 22:02:51 +00:00
|
|
|
this.todoLists.addListener((name, changes) => {
|
|
|
|
console.log("changed: " + JSON.stringify(changes));
|
2017-12-13 16:36:10 +00:00
|
|
|
if (params) {
|
|
|
|
console.error("params.json indicate a test run. Exiting application");
|
|
|
|
RNExitApp.exitApp();
|
|
|
|
}
|
2016-10-04 22:02:51 +00:00
|
|
|
});
|
|
|
|
console.log("registered listener");
|
2015-10-07 23:20:05 +00:00
|
|
|
|
2016-01-14 01:05:52 +00:00
|
|
|
|
|
|
|
// Bind all the methods that we will be passing as props.
|
2017-12-13 16:36:10 +00:00
|
|
|
// this.renderScene = this.renderScene.bind(this);
|
2016-01-14 01:05:52 +00:00
|
|
|
this._addNewTodoList = this._addNewTodoList.bind(this);
|
|
|
|
this._onPressTodoList = this._onPressTodoList.bind(this);
|
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
this.state = {};
|
2015-10-07 23:20:05 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
get currentListView() {
|
|
|
|
let refs = this.refs.nav.refs;
|
|
|
|
return refs.listItemView || refs.listView;
|
|
|
|
}
|
2015-10-07 23:20:05 +00:00
|
|
|
|
2016-01-19 23:10:06 +00:00
|
|
|
componentWillMount() {
|
2016-05-16 20:21:28 +00:00
|
|
|
if (Platform.OS == 'ios') {
|
|
|
|
StatusBar.setBarStyle('light-content');
|
2016-01-19 23:10:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
render() {
|
2016-02-18 05:23:43 +00:00
|
|
|
let objects = realm.objects('Todo');
|
2015-10-27 10:08:18 +00:00
|
|
|
let extraItems = [
|
2017-12-13 16:36:10 +00:00
|
|
|
{ name: 'Complete', items: objects.filtered('done = true') },
|
|
|
|
{ name: 'Incomplete', items: objects.filtered('done = false') },
|
2015-10-27 10:08:18 +00:00
|
|
|
];
|
|
|
|
|
2017-12-13 16:36:10 +00:00
|
|
|
let properties = {
|
|
|
|
ref: 'listView',
|
|
|
|
extraItems: extraItems,
|
|
|
|
onPressItem: this._onPressTodoList,
|
|
|
|
}
|
|
|
|
|
|
|
|
return <TodoListView items={this.todoLists} {...properties} />;
|
2015-10-07 23:20:05 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 01:05:52 +00:00
|
|
|
renderScene(route) {
|
2016-10-04 22:02:51 +00:00
|
|
|
console.log(this.todoLists);
|
|
|
|
return <route.component items={this.todoLists} {...route.passProps} />
|
2016-01-14 01:05:52 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
_addNewTodoItem(list) {
|
2015-10-28 18:23:37 +00:00
|
|
|
let items = list.items;
|
|
|
|
if (!this._shouldAddNewItem(items)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-07 23:20:05 +00:00
|
|
|
realm.write(() => {
|
2017-12-13 16:36:10 +00:00
|
|
|
items.push({ text: '' });
|
2015-10-07 23:20:05 +00:00
|
|
|
});
|
|
|
|
|
2015-10-28 18:23:37 +00:00
|
|
|
this._setEditingRow(items.length - 1);
|
2015-10-27 08:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_addNewTodoList() {
|
2015-10-28 18:23:37 +00:00
|
|
|
let items = this.todoLists;
|
|
|
|
if (!this._shouldAddNewItem(items)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
realm.write(() => {
|
2017-12-13 16:36:10 +00:00
|
|
|
realm.create('TodoList', { name: '', creationDate: new Date() });
|
2015-10-27 08:52:26 +00:00
|
|
|
});
|
|
|
|
|
2015-10-28 18:23:37 +00:00
|
|
|
this._setEditingRow(items.length - 1);
|
2015-10-27 08:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_onPressTodoList(list) {
|
2017-12-13 16:36:10 +00:00
|
|
|
const { navigate } = this.props.navigation;
|
2015-10-27 10:08:18 +00:00
|
|
|
let items = list.items;
|
|
|
|
|
2017-12-13 16:36:10 +00:00
|
|
|
navigate('ItemsScreen', { items: items })
|
2015-10-27 08:52:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 18:23:37 +00:00
|
|
|
_shouldAddNewItem(items) {
|
|
|
|
let editingRow = this.currentListView.state.editingRow;
|
|
|
|
let editingItem = editingRow != null && items[editingRow];
|
|
|
|
|
|
|
|
// Don't allow adding a new item if the one being edited is empty.
|
|
|
|
return !editingItem || !!editingItem.text || !!editingItem.name;
|
|
|
|
}
|
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
_setEditingRow(rowIndex) {
|
2016-01-12 00:01:37 +00:00
|
|
|
let listView = this.currentListView;
|
|
|
|
|
2015-10-27 08:52:26 +00:00
|
|
|
// Update the state on the currently displayed TodoList to edit this new item.
|
2017-12-13 16:36:10 +00:00
|
|
|
listView.setState({ editingRow: rowIndex });
|
2016-01-12 00:01:37 +00:00
|
|
|
listView.updateDataSource();
|
2015-10-07 23:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 16:36:10 +00:00
|
|
|
const SimpleApp = StackNavigator({
|
|
|
|
Home: { screen: HomeScreen },
|
|
|
|
ItemsScreen: { screen: ItemsScreen }
|
|
|
|
});
|
2016-01-14 01:05:52 +00:00
|
|
|
|
2017-12-13 16:36:10 +00:00
|
|
|
export default SimpleApp;
|