-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
118 lines (101 loc) · 2.71 KB
/
index.android.js
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
ListView,
Text,
View
} from 'react-native';
var DDPClient = require('ddp-client');
var _ = require('lodash');
if (typeof process === 'undefined') process = {};
process.nextTick = setImmediate;
module.exports = process;
class reactExample extends Component {
constructor(){
super();
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => !_.isEqual(row1, row2),
}),
loaded: false};
}
componentDidMount() {
var ddpClient = new DDPClient({ url: 'ws://192.168.1.8:3000/websocket' });
ddpClient.connect(() => { ddpClient.subscribe('friends') });
// observe the lists collection
var observer = ddpClient.observe('friends');
observer.added = () => {
this.updateRows(_.cloneDeep(_.values(ddpClient.collections.friends)));
}
observer.changed = () => {
this.updateRows(_.cloneDeep(_.values(ddpClient.collections.friends)));
}
observer.removed = () => {
this.updateRows(_.cloneDeep(_.values(ddpClient.collections.friends)));
}
}
updateRows(rows) {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(rows),
loaded: true,
});
}
render() {
var tag
if(!this.state.loaded){
this.componentDidMount();
}else{
}
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderFriend}
style={styles.listView}
/>
);
}
renderLoadingView() {
return (
<View style={styles.container}>
<Text>
Loading lists...
</Text>
</View>
);
}
renderFriend(friend) {
return (
<View style={styles.container}>
<Text style={styles.firstName}>{friend.firstName}</Text>
<Text style={styles.lastName}>{friend.lastName}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
backgroundColor: 'white',
padding: 10,
},
firstName: {
flex: 5,
fontSize: 18,
fontWeight: 'bold',
},
lastName: {
flex: 5,
fontSize: 18,
},
listView: {
paddingTop: 20,
backgroundColor: 'white',
},
});
AppRegistry.registerComponent('reactExample', () => reactExample);