-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.android.js
More file actions
121 lines (110 loc) · 3.4 KB
/
Copy pathindex.android.js
File metadata and controls
121 lines (110 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
TextInput,
View,
ListView,
ToolbarAndroid
} from 'react-native';
import * as firebase from 'firebase';
import ListItem from './components/ListItem.js';
import styles from './styles.js'
import FloatingActionButton from 'react-native-action-button';
// Initialize Firebase (unused for now)
var config = {
apiKey: "AIzaSyAPore7FNzkaUf4ayA8aSmQYoBZ3W3r1Zk",
authDomain: "rnlistview.firebaseapp.com",
databaseURL: "https://rnlistview.firebaseio.com",
storageBucket: "rnlistview.appspot.com",
};
const firebaseApp = firebase.initializeApp(config);
class RNFirebaseListView extends Component {
constructor(props) {
super(props);
this.tasksRef = firebaseApp.database().ref();
// Each list must has a dataSource, to set that data for it you must call: cloneWithRows()
// Check out the docs on the React Native List View here:
// https://facebook.github.io/react-native/docs/listview.html
const dataSource = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
});
this.state = {
dataSource: dataSource, // dataSource for our list
newTask: "" // The name of the new task
};
}
componentDidMount() {
// start listening for firebase updates
this.listenForTasks(this.tasksRef);
}
render() {
return (
<View style={styles.container}>
<ToolbarAndroid
style={styles.navbar}
title="Todo List" />
{/*A list view with our dataSource and a method to render each row*/}
{/*Allows lists to be empty, can be removed in future versions of react*/}
<ListView
dataSource={this.state.dataSource}
enableEmptySections={true}
renderRow={this._renderItem.bind(this)}
style={styles.listView}/>
<TextInput
value={this.state.newTask}
style={styles.textEdit}
onChangeText={(text) => this.setState({newTask: text})}
placeholder="New Task"
/>
{/*The library has a bug so I removing the shadow to avoid it*/}
<FloatingActionButton
hideShadow={true}
buttonColor="rgba(231,76,60,1)"
onPress={this._addTask.bind(this)}/>
</View>
);
}
_renderItem(task) {
// a method for building each list item
const onTaskCompletion = () => {
// removes the item from the list
this.tasksRef.child(task._key).remove()
};
return (
<ListItem task={task} onTaskCompletion={onTaskCompletion} />
);
}
_addTask() {
if (this.state.newTask === "") {
return;
}
this.tasksRef.push({ name: this.state.newTask});
this.setState({newTask: ""});
}
listenForTasks(tasksRef) {
// listen for changes to the tasks reference, when it updates we'll get a
// dataSnapshot from firebase
tasksRef.on('value', (dataSnapshot) => {
// transform the children to an array
var tasks = [];
dataSnapshot.forEach((child) => {
tasks.push({
name: child.val().name,
_key: child.key
});
});
// Update the state with the new tasks
this.setState({
dataSource: this.state.dataSource.cloneWithRows(tasks)
});
});
}
}
AppRegistry.registerComponent('RNFirebaseListView', () => RNFirebaseListView);